[petsc-dev] Style Guide: How to format single-line if/for/while-blocks?

Barry Smith bsmith at mcs.anl.gov
Mon Jan 21 21:41:30 CST 2013


On Jan 21, 2013, at 7:05 PM, Karl Rupp <rupp at mcs.anl.gov> wrote:

> Hi,
> 
>>    As you say, technically only the first one is correct. So for example
>> 
>>    if (y < 12)  y += 93;
>>    if (y < 12) SETERRQ();             note that if (y < 12) {SETERRQ();} is wrong since the {} are not needed.
>> 
>>    Things like
>> 
>>     if (y < 12) ierr = Something(); CHKERRQ(ierr);
>> 
>> are just wrong source code (there should not be many of these, I fixed all the ones I found.
> 
> Fortunately I haven't come across any of these misuses (yet)...
> 
>> 
>>     Things like
>> 
>>     if (y < 12) {ierr = Something(); CHKERRQ(ierr);}
>> 
>> don't match the standard but …. am I being too picky? I believe the PETSc make uncrustify rule would move that to separate lines.
> 
> I expect that it's a lot easier to define a rule where '{' opens a new block on the next line rather than having a 'sometimes it's a single line'-type of exception.

   uncrustify (with my configuration) leaves 
 
     if (y < 12) Something(); 

alone, I found a setting so that it leaves 

>> if (y < 12) {ierr = Something(); CHKERRQ(ierr);}

alone :-) @all except :-( @Sean.   Unfotunately it will not take 

>> if (y < 12) {

>>   ierr = Something(); CHKERRQ(ierr);
>> }

and turn it into 

>> if (y < 12) {ierr = Something(); CHKERRQ(ierr);}


   though I guess in theory it could be hacked to do that.


   So it may be that the filter would run uncrustify and then run a series of style checks that find style bugs like CHKERRQ() on their own line and bitch about them etc. Thus we impose some style  on the code developer but trivial style like if(godthisisugly) is up to each person and handled automatically? Maybe as time goes by more stuff can be handled by an improved uncrustify.

   Barry



> 
> 
>>    Karl,
>> 
>>      Since you are exploring this why not run the make uncrustify rule after your cleanups and see what it formats differently in all the source code. It would be good to converge to an "automatic" format quickly so people can go back to using their own styles when typing in code if they like.
> 
> Yes, I considered uncrustify as the third stage in my initial email. I'll try to achieve consistency asap and then fire a suitably configured uncrustify on it.
> 
> Best regards,
> Karli
> 




More information about the petsc-dev mailing list