[gdal-dev] emacs configuration for correct code indentation?

Even Rouault even.rouault at mines-paris.org
Fri Sep 2 15:25:16 EDT 2011


Le vendredi 02 septembre 2011 21:06:16, Etienne Tourigny a écrit :
> Frank, please do send me your emacs macros.   With your permission I will
> add that information to the wiki as suggested by Even.
> 
> I agree with your policy of not being too strict, but a minimal set of
> standards is a good thing IMHO.
> It could help in elaborating a suggested standard in the wiki page (just my
> 2 cents).
> 
> Two issues that come to mind are
> 
> 1) where to put opening braces in if/else/for blocks  It can get a little
> messy, as different flavors are used by different people
> 
> if ( 1 ) {
> ...
> }
> vs
> if ( 1 )
> {
> ...
> }


if ( 1 )
{
    ...
}

seems to be the most used convention in GDAL source code

> 
> 2) one-liner if/else statements without braces.  Some projects like qgis
> discourage their use, but personally I like it more compact

I'm just looking at a file where I can see

    if( pszSrcEncoding[0] == '\0' )
        pszSrcEncoding = CPL_ENC_ISO8859_1;

    if( pszDstEncoding[0] == '\0' )
        pszDstEncoding = CPL_ENC_ISO8859_1;

So their use isn't discouraged. But in the case of a "if() {} else if() ..."  
where some of the if or else if already use braces, braces are generally used. 
For example :

    else if( chNext == '<' && !psContext->bInElement )
    {
        psContext->eTokenType = TOpen;
        psContext->bInElement = TRUE;
    }
    else if( chNext == '>' && psContext->bInElement )
    {
        psContext->eTokenType = TClose;
        psContext->bInElement = FALSE;
    }
    else if( chNext == '=' && psContext->bInElement )
    {
        psContext->eTokenType = TEqual;
    }
    else if( chNext == '\0' )
    {
        psContext->eTokenType = TNone;
    }

That seems to be "natural" in that case.

As Frank said, we tend not to engage in style wars, provided that the code is  
readable.

> 
> 
> thanks, Etienne
> 
> On Fri, Sep 2, 2011 at 3:45 PM, Frank Warmerdam <warmerdam at pobox.com> wrote:
> > On Fri, Sep 2, 2011 at 11:29 AM, Even Rouault
> > 
> > <even.rouault at mines-paris.org> wrote:
> > >> (setq-default c-basic-offset 4
> > >> 
> > >>           tab-width 4
> > >>           indent-tabs-mode nil)
> > >> 
> > >> (c-set-offset 'case-label '+)
> > > 
> > > Not sure if it is RFC material, but you can definitely share it in
> > > http://trac.osgeo.org/gdal/wiki/HowToContribute
> > > 
> > > You have summed up perfectly the most important coding conventions.
> > 
> > Etienne,
> > 
> > Agreed, 4 character indent using spaces not
> > tabs is the key thing to avoid messiness for others.
> > 
> > > You have also probably noticed the nice visual ascii art that is
> > 
> > generally
> > 
> > > prepended before each function/method :
> > /************************************************************************
> > /
> > 
> > > /*                               foo()
> >  
> >  */
> > 
> > /************************************************************************
> > /
> > 
> > I actually have emacs macros for editing these header blocks.
> > I could share them if you are interested, but they are not that
> > critical.
> > 
> > I deliberately have avoided trying to be really strict about code
> > formatting in GDAL/OGR.  I think over-strictness can be offputting
> > to new contributors and lead to disputes over details.  But do
> > make an effort to fit into the modules you work on and feel
> > free to take more liberties on drivers that you are writing from
> > scratch.
> > 
> > > And if you really want to confuse FrankW and make him believe he has
> > 
> > written
> > 
> > > the code himself, you'd write if( foo ) rather than if (foo) ;-)
> > 
> > This is one of the details that seems fairly idiosyncratic
> > to me, but it is contrary to Google code style so perhaps
> > I will adapt to "if (foo)" now.
> > 
> > Best regards,
> > --
> > 
> > ---------------------------------------+---------------------------------
> > ----- I set the clouds in motion - turn up   | Frank Warmerdam,
> > warmerdam at pobox.com
> > light and sound - activate the windows | http://pobox.com/~warmerdam
> > and watch the world go round - Rush    | Geospatial Software Developer
> > _______________________________________________
> > gdal-dev mailing list
> > gdal-dev at lists.osgeo.org
> > http://lists.osgeo.org/mailman/listinfo/gdal-dev


More information about the gdal-dev mailing list