"1 * 3 + 4 / 20 && foo" is unreadable. No one wants to memorize the order of operations for every language they use and have to figure it out in their head. Make life easier for the people who have to maintain that line of code you just wrote.
Maybe I'll eat my foot some day, but
(a == 1) && (b == 2)
or (2 * 3) + 1
is nothing but code smell to me.That is, there's a reason for whitespace around operators, after commas, extraneous newlines, etc. It makes the code easier to read and maintain. Now while I'm willing to admit that this whole area is largely aesthetic and personal, if you submitted a line of code to me that was just this without the parentheses:
> 2 * 3 + 1
I would reject the code review in a heartbeat. Part of making your code readable to others and less error-prone during maintenance is adding extraneous stuff to it that isn't necessary for the code to parse/compile/interpret.
- Tim Bray
[1] http://www.tbray.org/ongoing/When/201x/2010/06/29/No-Default...
[0] http://stackoverflow.com/questions/1426826/difference-betwee...
If `foo == bar` evaluates to true, the `or` is short-circuited, else it calls the built-in `die` function which kills the process.
@current_user.logged_in? or redirect_to login_path
@current_user.can? :do_this or render :status => 403
shirt.blue? and return bar
put_suit_on or put_pants_on shirt.blue? and return bar
would be equivalent to return bar if shirt.blue?
You can do the same with or and unless.if first_user = User.find(4) && second_user = User.find(6) ... end
It looks like it would evaluate as: first_user = (User.find(4) and second_user = User.find(6))
That is to say, the assignment comes after the boolean operation, which is unexpected.
first_user = User.find(4) && second_user = User.find(6)
to be literally equivalent to second_user = User.find(6)
first_user = User.find(4) && second_user
I honestly can't see why you might want it to mean something else...Edit: I see. if you do something like
if (first_user = User.find(4) && second_user = User.find(6)) {
..
}
it might bite you. You might expect it to be equivalent to first_user = User.find(4)
second_user = User.find(6)
if (first_user && second_user) {
..
}
which I personally think is a very, very bad practice. Parenthesis should always be used when there's even the slightest possibility that you or another maintainer/contributor might be confused about.Having an assignment that could be skipped by short-circuiting also seems like bad practice, but I realize it was designed to be a toy example
first_user = User.find(4) if first_user second_user = User.find(6) if second_user ... end end
Yes, but you can make the same point in favor of using "and" and "or" (and deprecating &&/||).
As Avdi Grimm says: and and or, despite an apparent similarity to && and ||, have very different roles.
It's worth using "and" and "or", especially considering that && and || bind too tightly for some purposes. I try to avoid && and || unless I'm doing a specific boolean operation (e.g. x = foo || bar). Not for flow control, though.
If you really can't grok the logic, use parens.