5 ms·
only tangentially related, but one of the things I miss so much about Perl is that, as in English, you can add an "if" or "unless" clause as an afterthought. $
by logicallee 7y ago
only tangentially related, but one of the things I miss so much about Perl is that, as in English, you can add an "if" or "unless" clause as an afterthought.
$x++ if (!condition);
like if you start to increment $x++ and then you realize wait a second I only mean if....
"unless" likewise functions as an "if not".
This is such a "natural" way to write.
Let's compare what happens if you have the thought to add a "statement modifier" in any other programmer language.
you're written your statement, and now you have to go back to the beginning of your line, write your condition, open a brace, go back to the end of the line, and close your brace. Or maybe begin an entire code block, since you can't have it on one line anymore.
it is so much less natural. why can't other programming languages have that?
- marcinzm 7y agoThe greatest difficulty with code is not in writing it but in reading it.
- dragonwriter 7y ago<result> if/unless <condition> is pretty common in natural language for good reason. I find it far more readable than prefix, block-form if for the simple case of one-line results with no alternative branch.,
- matthewmacleod 7y agoAgreed. Particularly useful for for constructs like: def do_stuff() return unless stuff_enabled? ... end
- pmontra 7y agoRuby has that. x += 1 if !condition Better as x += 1 unless condition I use them not as an afterthought but by design. This is the Ruby Style Guide about it https://rubystyle.guide/#if-as-a-modifier https://rubystyle.guide/#if-as-a-modifier
- bhaak 7y agoRuby has that. But you have to be careful that your code stays readable. I know that's not a concern for Perl. :-) Although the "unless" case very often trips me. Somehow my brain can't parse that correctly and I've found that surprisingly many work colleagues have the same problem.
- astrobe_ 7y agoForth love if honk then Be careful what you wish for...