Indentation styles for namespaces?

J

Jorgen Grahn

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
 
A

Alf P. Steinbach

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
 
M

Marcel Müller

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
 
M

Miles Bader

Marcel Müller said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top