by jimbokun · 17y ago · 0 comments
A lot of time it just kills me that "if" statements don't default to returning the true branch or false branch. You need the ternary conditional operator in Java and other C-derivatives, and in Python you need to resort to some hack with and/or precedence.
I'm also wondering if I should declare all variables final in Java, and only change that default if I really, really have to.
The non-working python if statement/assignment example from the article works perfectly fine in Ruby. (No python/ruby flamewar please, that's really not the point)
In all seriousness though, I've recently moved to Clojure from Ruby. I'm not looking back. Clojure is significantly cleaner, faster, and has better libraries than Ruby.
(x if cond else y) which is essentially (cond ? x : y)
a = ("False", "True")[condition]
It's a little fugly -- but if you know the basic semantics of Python, it's quite readable. It is nice that we have a proper ternary now, though.
a = cond and x or y
which had the downside that if the second part (in this case x) evaluates to False then a ends up equal to y even through that's not what you were trying to do. Sometimes this is a problem and sometimes not, but it's been irrelevant for the past few years.