DEV Community

Discussion on: A trick with Ruby array literals

Collapse
 
bizzibody profile image
Ian bradbury

I do this...

if user = User.find(params[:id])
puts "User is #{user.name}"
end

All the time!

Does this make me a bad person?

Collapse
 
nflamel profile image
Fran C.

xD not at all.

The reason why I prefer to keep declaration and conditionals separated is because it helps a bit with reading the code and I've found more than once that it is not always understood!

I've seen teammates trying to "fix" it as if user == User.find(params[:id]) because they weren't understanding I was just assigning something! Besides, Ruby emits a warning in this case... so it is not clear at all even when you can do it.

The difference with the case of the constants is that the constants declaration doesn't usually involve your applications's logic, so it is easier to reason about and less "dangerous".