6 ms·
So, Ruby has: consequent-expr if antecedent-expr ...which has all the same problems as the PEP308 example, as far as I can tell. Its (implicit) else-claus
by derefr 10y ago
So, Ruby has:
consequent-expr if antecedent-expr
...which has all the same problems as the PEP308 example, as far as I can tell. Its (implicit) else-clause is just always nil.
I can't tell from the author's argument whether this condemns Ruby as well, or if something about the PEP308 style was uniquely bad.
I could see something definitely worse about the readability of this code:
{long expr} if x else {another long expr}
but the author didn't discuss that case.
- Lazare 10y agoThe difference between Ruby and Python here is that Ruby has a perfectly functional ternary operator that works as one would expect. It also allows some other crazy syntax which can be appropriate in some cases, but it doesn't force you to use it in all cases. Python does force you to use their awkward conditional expressions; there is no normal ternary operator. There's a big difference between a language which lets you write hideous code, and a language which forces you to write hideous code.
- TheDong 10y agoPython does not force you to. In any case where you can use PEP 308, you can use a boring traditional "if else".
- jolux 10y agoBut you can't use a normal ternary operator...
- Lazare 10y agoIs that intended as a defense of Python's ability to enable clear, concise, expressive code? :) More seriously: Yes, you are obviously correct. But while you can use if/else, you can't use a ternary, and you can in Ruby. That's why people complain about the lack in Python but don't wrote the same article about Ruby.
- luord 10y ago"Clear, concise, expressive code" isn't something that applies to ternary operators. They were always meant as shortcuts and, as such, will never be as clear as if else statements, practically by design. Though I don't really dislike Python's approach so what do I know.
- jolux 10y agoYou can definitely use too many ternary operators, but I think when used sparingly they can be clear and tasteful. As long as you don't start nesting them or repeating them one after the other you should be fine.
- tasty_freeze 10y agoPerl has the same type of syntax: print "true" if (a < b); I recall seeing dialects of BASIC back in the 70s which supported it as well. Who knows, maybe this is where Perl got it from. I use the post conditional most often in perl in this way: foreach my $foo (@collection) { next if (I-dont-care-about-this-$foo); next if (another-reason-for-ignoring-$foo); do_something($foo); }
- Mordak 10y agoI use ruby's trailing conditionals exactly this way all the time - it makes me happy to see someone else using the same pattern. I also use it at the start of functions in order to sanity check arguments: def do_something(arg1, arg2) return false if arg1.is_invalid return false if arg2 < sane ... end The trailing `unless` is also handy, and I find it a bit nicer to read compared to a negated `if`.
- skywhopper 10y agoThe trailing if can sometimes be confusing in Ruby but the else clause is the thing that's just blatantly anti-intuitive about the Python syntax at issue. Ruby doesn't allow else there because that would be insane. Python chose the insane convoluted and confusing syntax over a well understood and accepted ternary operator.