http://www.perlmonks.org?node_id=1074125


in reply to Code blocks with ternary operator or trailing conditionals

I’ll agree rather strongly with kcott and the others on this one:   when the language fairly-obliges you to use a few “extra” keywords, do so.   Why?   Maybe because these “extra few” symbols serve to bypass ambiguities in the underlying grammar, such that there now becomes, and in the general(!) case, only one way that your intentions might be evaluated by the parser and so on.   Every language has characteristics like this, somewhere in the any every language.   There will be a “right way” that might superficially seem odd or inconvenient.   Why did they require that extra stuff?   Maybe just because it made the grammar necessarily-stronger in a place where it truly needed to be stronger.

Also, think carefully about long-life maintainability of the code ... because computer software does have a very long life-span during which it will pass through many hands besides your own.   It could well be that a future change to your code, if written in this way, could have unintended consequences that do not result in a syntax-error.   (And this whether you were the one making the change, or not.)     “Two paths diverged in a snowy wood,” and both were equally valid, and [Perl...] took the other one and raised no yellow flags.   Maybe they affect the direct code-line that you are modifying at that time, but maybe they do not ... and does your project really have an all-encompassing test suite that is constantly maintained along with the code?   Didn’t think so.   So, this is just something that you should avoid as a matter of course, because hair-follicles are a precious thing . . .

Replies are listed 'Best First'.
Re^2: Code blocks with ternary operator or trailing conditionals
by puterboy (Scribe) on Feb 09, 2014 at 15:26 UTC
    Thanks for the good lesson. I don't disagree with you (or the others who have answered similarlY) at all.

    My reason for asking was not out of coding "laziness" but rather to seek new grammatically *valid* tricks.

    The cool thing about Perl is that I find more often than not that there are cleaner/better ways of doing things than are available in other C-like languages. Hence, my reason for asking - to learn. Thanks!!!