http://qs321.pair.com?node_id=11132825


in reply to Re: Code style question -- code review
in thread Code style question

"I prefere the and put at the end of the line and all and aligned: the same for or and for thernary like ? and : because somenting at the end of line is easier to spot."

I emphatically disagree! The easiest stuff to see is at the start of the line. I frequently break complicated expressions over lines so that it is easier to understand them. For example, the following test from a C++ project I'm working on right now could be laid out several ways:

if (currMaj < bestMajVer || (currMaj == bestMajVer && currMin <= bestM +inVer)) // Current image is no better than the best so far if (currMaj < bestMajVer || (currMaj == bestMajVer && currMin <= bestMinVer) ) // Current image is no better than the best so far if ( currMaj < bestMajVer || (currMaj == bestMajVer && currMin <= bestMinVer) ) // Current image is no better than the best so far

To my eye the last version is much easier to understand than the previous two.

One of Perl's features that I really like is the provision of statement modifiers because, for short statements at least, I can get compact expressive code with the important bits in the right order:
<do this interesting thing> <while/for/if> <something vaguely interesting>.

For the same reason the default increment operator should be pre-increment (++$thing) instead of the cargo culted post-increment operator ($thing--). There are places where post-increment must be used, but they are rare and the default should always be pre-increment just because the operator is easier to see and the operation is less surprising.

Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

Replies are listed 'Best First'.
Re^3: Code style question -- code review
by Discipulus (Canon) on May 21, 2021 at 06:55 UTC
    Hello GrandFather

    > I emphatically disagree!

    :) as I told, I suppose is a matter of taste. I tend to use something like:

    # complex clause if ( defined $thing and $thing > 3 and $thing <= 42 and 0 == $thing % 2 ) # ternary print (defined $thing and $thing > 0) ? $thing : 'not';

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.