3 ms·
Well, I expect first_user = User.find(4) && second_user = User.find(6) to be literally equivalent to second_user = User.find(6) first_user = User
by pooriaazimi 14y ago
Well, I expect
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.
- alanctgardner2 14y agoPersonally, I'm almost painfully verbose with my parens to avoid exactly this scenario. Better to be explicit about my intent to the interpreter and other coders. 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
- gurkendoktor 14y agoSkipping the second assignment if I don't need it is more of an optimisation. The "long form" would be, first_user = User.find(4) if first_user second_user = User.find(6) if second_user ... end end
- gurkendoktor 14y ago> Parenthesis should always be used when there's even the slightest possibility that you or another maintainer/contributor might be confused about. Yes, but you can make the same point in favor of using "and" and "or" (and deprecating &&/||).