Indentation styles for namespaces?

Discussion in 'C++' started by Jorgen Grahn, Nov 13, 2011.

  1. Jorgen Grahn

    Jorgen Grahn Guest

    I hope this doesn't turn into an argument. I had these header files
    at work, where I had most of the contents in a namespace:

    /* foo.h */
    namespace foo {
    stuff;
    pages_of_stuff;
    ...
    }

    Quite a few pages of that, and everything inside the namespace
    indented. Then others took over the code, and when I revisited it last
    week I noticed it had been reindented so the namespace content wasn't
    indented:

    /* foo.h */
    namespace foo {
    stuff;
    pages_of_stuff;
    ....
    }

    My questions are simply:
    - Do you people tend to indent in such cases?
    - Is there some popular editor or IDE which prefers not to?

    I can certainly understand that some feel the indent is a waste of
    horizontal space, but it's really the way I (and my editor, Emacs)
    prefer it.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Nov 13, 2011
    #1
    1. Advertising

  2. On 13.11.2011 22:29, Jorgen Grahn wrote:
    > I hope this doesn't turn into an argument. I had these header files
    > at work, where I had most of the contents in a namespace:
    >
    > /* foo.h */
    > namespace foo {
    > stuff;
    > pages_of_stuff;
    > ...
    > }
    >
    > Quite a few pages of that, and everything inside the namespace
    > indented. Then others took over the code, and when I revisited it last
    > week I noticed it had been reindented so the namespace content wasn't
    > indented:
    >
    > /* foo.h */
    > namespace foo {
    > stuff;
    > pages_of_stuff;
    > ...
    > }
    >
    > My questions are simply:
    > - Do you people tend to indent in such cases?


    Of course.

    Perhaps the programmer in question had background from HTML?



    > - Is there some popular editor or IDE which prefers not to?


    No.


    > I can certainly understand that some feel the indent is a waste of
    > horizontal space, but it's really the way I (and my editor, Emacs)
    > prefer it.


    There is one thing though, don't let the parsing level command the
    indentation level. For example, IMO, write

    namespace a { namespace b { namespace c {

    // blah

    } } }

    instead of

    namespace a
    {
    namespace b
    {
    namespace c
    {
    // blah
    }
    }
    }

    It is only be similarity with conditionals that the latter may appear
    more clear. But that similarity has only to do with outer form (namely
    braces), not meaning. For namespaces, parsing-level indentation is just
    impractical eye candy -- pretty to look at, until you run out of line.


    Cheers,

    - Alf
     
    Alf P. Steinbach, Nov 13, 2011
    #2
    1. Advertising

  3. On 13.11.2011 22:29, Jorgen Grahn wrote:
    > My questions are simply:
    > - Do you people tend to indent in such cases?


    I prefer not to indent the content of public namespaces.
    The reason is simple: there is not benefit of doing so, except for the
    waste screen space, of course. One file should not contain more the one
    public namespace, so the indentation won't bring any further structure
    information. The package syntax of Java takes care of that.

    Things are a bit different with private implementation namespaces. They
    may only cover parts of a file, in which case indentation makes sense.


    > I can certainly understand that some feel the indent is a waste of
    > horizontal space, but it's really the way I (and my editor, Emacs)
    > prefer it.


    Who cares, what /your/ editor prefers?

    In fact, if you are working in a team, no one must format entire
    documents. If you do so, the you need to agree to the same formatting
    rules up to the last blank. This implies that all of you use exactly the
    same tools with the same settings.
    Otherwise you will get tons of merge conflicts, which is most likely not
    what you intended.

    On the other side, if your tools keep existing formatting of unmodified
    lines, and if you use tabs only in the front of a line, then you might
    use rather different settings without conflicts, including the tab size.

    You might configure your version control software to reject changes of
    existing source files where almost all lines are modified with a
    meaningful message. So the people that can't control their fingers or
    editors get their own problem.


    Marcel
     
    Marcel Müller, Nov 14, 2011
    #3
  4. Jorgen Grahn

    Miles Bader Guest

    Marcel Müller <> writes:
    >> I can certainly understand that some feel the indent is a waste of
    >> horizontal space, but it's really the way I (and my editor, Emacs)
    >> prefer it.

    >
    > Who cares, what /your/ editor prefers?


    You can tell Emacs not to indent namespaces by putting:

    (add-to-list 'c++-mode-hook (lambda () (c-set-offset 'innamespace 0)))

    in your ~/.emacs file.

    More clever would be to add a hook that looks at the "namespace" line,
    and bases its decision to indent or not on some sort of context, e.g.,
    if there's a comment saying "// noindent", it wouldn't indent (or the
    opposite if not indenting is more common). [Basically you write a
    lisp function, and use it's name in place of the "0" above; see the
    info node "(ccmode) c-offsets-alist" for details.]

    As such conventions often vary among _projects_, an altnerative would
    be to place an appropriate ".dir-locals.el" file in the root of source
    trees; see the info node "(emacs) Directory Variables" for details.

    -Miles

    --
    Any man who is a triangle, has thee right, when in Cartesian Space,
    to have angles, which when summed, come to know more, nor no less,
    than nine score degrees, should he so wish. [TEMPLE OV THEE LEMUR]
     
    Miles Bader, Nov 16, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SJ

    DataList Indentation

    SJ, Nov 3, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    2,060
    Patrick.O.Ige
    Nov 4, 2005
  2. jacov

    [Fwd: Indentation]

    jacov, Jul 22, 2004, in forum: Java
    Replies:
    0
    Views:
    333
    jacov
    Jul 22, 2004
  3. Daan

    Unwanted indentation

    Daan, Dec 9, 2003, in forum: HTML
    Replies:
    2
    Views:
    451
  4. Jesse B.
    Replies:
    2
    Views:
    192
    Josh Cheek
    Mar 27, 2010
  5. Stefan Ram

    Indentation styles

    Stefan Ram, May 7, 2013, in forum: Java
    Replies:
    27
    Views:
    535
    Stanimir Stamenkov
    May 26, 2013
Loading...

Share This Page