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

Barry Smith bsmith at mcs.anl.gov
Mon Jan 21 18:14:05 CST 2013


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

> Hi,
> 
> as some of you already noticed, there's progress on the PETSc style front. I'm now mostly done with adding simple checks not requiring a full-fledged compiler for it. Instead, they are simple find/grep/python calls and fairly accurate. A daily HTML report is currently available here (click on numbers for details):
>  http://krupp.iue.tuwien.ac.at/petsc-style/
> 
> One thing is fairly fuzzy, though. The style guide requests
>  if (...) single_line_here
> instead of
>  if (...)
>    single_line_here
> So, in the source tree we find various one-liner versions:
>  if (...) first_statement;
>  if (...) { first_statement; }
>  if (...) {first_statement;}
>  if (...) { one_statement; second_statement; }
>  if (...) {one_statement; second_statement;}
>  if (...) {one_statement;second_statement;}
> with the latter often being used with CHKERRQ. On the other hand, the style guide says
> 'The closing bracket } should always be on its own line.'
> which contradicts all uses except
>  if (...) first_statement;
> 
> @all, particularly Barry:
> What should be the unified guideline(s) in such cases (if/for/while)?

   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.

    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. 

   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.

   Barry



> 
> Best regards,
> Karli




More information about the petsc-dev mailing list