Skip to content

Instantly share code, notes, and snippets.

@cmer
Created March 28, 2012 17:16
Show Gist options
  • Save cmer/2228355 to your computer and use it in GitHub Desktop.
Save cmer/2228355 to your computer and use it in GitHub Desktop.
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
@JonAbrams
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment