-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
assignment in conditional ternary #704
Comments
Conditional ternary should be used when the operations after the if or else statements are simple enough to be put into one line. The assignment operation is one of the simplest operations so it seems natural to use the simplified conditional in Ruby instead of a classic if/else. |
Interesting question. I think @khmariem is right in principle, but in practice I don't think I've seen many assignments in ternary ops. I'd love to hear what others think about this or get some data from real-life usages in projects. |
As someone who recently encountered basically this exact example, my opinion is that assignments should stay out of ternary, mainly because of humans difficulty in parsing statements effectively - I feel there are too many chances to misunderstand what the statement is trying to communicate. |
There is this https://github.com/rubocop-hq/ruby-style-guide#nested-ternary-operators
Is it sufficient to clarify that an "expression" should not have side effects? (see https://en.wikipedia.org/wiki/Expression_(computer_science)#Side_effects_and_elimination) Frankly, I'm puzzled to tell if |
Assignment in ternaries should definitely be avoided if they are assigning to the same variable, eg
vs
|
Should assignment in conditional ternary be avoided ?
something like this
it's dealing with operator precedence and it is less clear than
that we are modifying some state
The text was updated successfully, but these errors were encountered: