Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Depends on what you call "this kind of code". Operators like "and" and "or" facilitate some very useful and expressive idioms, so having that kind syntactic constructs is rather useful.

However, I think it's a big mistake to make those constructs use "and" and "or", because it overloads the semantics normally associated with those keywords. Yes, it's true that it's precisely the same logic under the hood, but you have to fight the "oh, boy, that's so neat!" reflex and think about all that could go wrong.

There's a similar gotcha in Python, where instead of ternary operator (a ? b : c) you use a combination of "and" and "or" (a and b or c). The gotcha is that it backfires if "b" evaluates to false.

All in all, it's probably not "worth it" to have nasty surprises like that, but that could be avoided by using different keywords, not "and" and "or".



In Python, the side effects were considered so nasty that they finally added ternary operators in 2.6. The syntax goes like:

    x = 'foo' if foo else bar
which is at least more readable than the C/PHP/Ruby equivalent.


You can do

     x = if foo then 'foo' else 'bar' end
in Ruby, if you'd like.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: