Created
March 28, 2012 17:16
-
-
Save cmer/2228355 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def foo | |
# do something here | |
end | |
# Is it considered bad practice to assign a variable in a conditional statement? | |
# Ruby gives me a warning: "warning: found = in conditional, should be ==" | |
if x = foo | |
puts x.to_s | |
end | |
# Or is this how it should be done: | |
x = foo | |
if x | |
puts x.to_s | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you put an assignment in a conditional like that, then you're doing two things at once. I like to have each line to do one thing at a time. This make my code a lot more readable and clear.
On top of that, the assignment operator looks very similar to the equality comparison operator, making it all even more confusing.