← Back to team overview

yade-dev team mailing list archive

Re: [Branch ~yade-pkg/yade/git-trunk] Rev 4087: Fix compilation. (for latter reference regarding coding style)

 

On 17/07/14 10:32, Anton Gladky wrote:
> That is not a big deal. Our brace syntax convention is not very strict,
> but in most cases it is :
>
> if () {
>
> }
>
> or
> if ()
> {
> }
>
> Both of them are "readable".

Not strict indeed, but overall we can try to minimize the number of
lines, so we see more of them on one screen.
Some more suggestions (again, this is somehow flexible, just it is
painfull to have 2000 lines in a file):

*If a block has a single statement, no braces:
if () doSomething;
else somethingElse;

*if multiple but very short statements:
if () {a=1; b=2; c=3;}

*for more complex blocks, the first form from Anton is better

*for extremely long blocks, the last version can make the structure more
visible, for one-liners it is a waste.

A practical example, 14 vs. 5 lines:
<<<
if ( valPropres[0] > std::max(valPropres[1],valPropres[2]) )
{
    whereSigI = 0;
    if (valPropres[1]>valPropres[2])
    {
        whereSigII=1;
        whereSigIII=2;
    }
    else //valPropres[0] > valPropres[2] >= valPropres[1]
    {
        whereSigII = 2;
        whereSigIII=1;
    }
}
=====
if ( valPropres[0] > std::max(valPropres[1],valPropres[2]) ) {
    whereSigI = 0;
    if (valPropres[1]>valPropres[2]) {whereSigII=1; whereSigIII=2;}
    else {whereSigII = 2; whereSigIII=1;}
}
>>>

Or even (to an extreme), in one line:
if ( valPropres[0] > std::max(valPropres[1],valPropres[2]) )
{whereSigI=0; whereSigII=valPropres[1]>valPropres[2]? 1 : 2; whereSigII=
valPropres[1]>valPropres[2]? 2 : 1}

(and no it is not the same test repeated twice, the compiler will fix that).

Cheers.

Bruno




Follow ups

References