Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Namespace indentation

94 views
Skip to first unread message

Adam Wysocki

unread,
Jul 17, 2018, 5:00:07 PM7/17/18
to
Hi,

What is your preference towards indenting contents of namespaces?

Although I find indented blocks easier to read (easier to see that this is
in fact part of the namespace), I know and agree with arguments against
indentation and in simple cases I don't feel it affects readability, but
in more complicated ones, where there are many nested namespaces, it
easily becomes a mess...

I know it's a matter of personal preference (or rules agreed by the team),
but I'm curious what your preference is, and why.

--
[ Adam Wysocki :: Warsaw, Poland ]
[ Email: a@b a=grp b=chmurka.net ]
[ Web: http://www.chmurka.net/ ]

Mr Flibble

unread,
Jul 17, 2018, 6:02:06 PM7/17/18
to
On 17/07/2018 21:59, Adam Wysocki wrote:
> Hi,
>
> What is your preference towards indenting contents of namespaces?
>
> Although I find indented blocks easier to read (easier to see that this is
> in fact part of the namespace), I know and agree with arguments against
> indentation and in simple cases I don't feel it affects readability, but
> in more complicated ones, where there are many nested namespaces, it
> easily becomes a mess...
>
> I know it's a matter of personal preference (or rules agreed by the team),
> but I'm curious what your preference is, and why.
>

namespace foo::bar::baz
{
}

/Flibble

--
"Suppose it’s all true, and you walk up to the pearly gates, and are
confronted by God," Bryne asked on his show The Meaning of Life. "What
will Stephen Fry say to him, her, or it?"
"I’d say, bone cancer in children? What’s that about?" Fry replied.
"How dare you? How dare you create a world to which there is such misery
that is not our fault. It’s not right, it’s utterly, utterly evil."
"Why should I respect a capricious, mean-minded, stupid God who creates a
world that is so full of injustice and pain. That’s what I would say."

Vir Campestris

unread,
Jul 17, 2018, 6:19:23 PM7/17/18
to
On 17/07/2018 23:01, Mr Flibble wrote:
> On 17/07/2018 21:59, Adam Wysocki wrote:
>> Hi,
>>
>> What is your preference towards indenting contents of namespaces?
>>
>> Although I find indented blocks easier to read (easier to see that
>> this is
>> in fact part of the namespace), I know and agree with arguments against
>> indentation and in simple cases I don't feel it affects readability, but
>> in more complicated ones, where there are many nested namespaces, it
>> easily becomes a mess...
>>
>> I know it's a matter of personal preference (or rules agreed by the
>> team),
>> but I'm curious what your preference is, and why.
>>
>
> namespace foo::bar::baz
> {
> }
>
I indent them all
so
namespace foo
{
class bar
{
}
}

I seem to be in a minority.

Andy

Chris M. Thomasson

unread,
Jul 18, 2018, 1:06:17 AM7/18/18
to
I have indented them all before. It does seem to have potential scaling
issues wrt deeply nested namespaces.

Ian Collins

unread,
Jul 18, 2018, 2:34:24 AM7/18/18
to
Mitigate by sticking to two space indentation, or use nested namespace
as Flibble showed.

--
Ian

Juha Nieminen

unread,
Jul 18, 2018, 4:04:12 AM7/18/18
to
Adam Wysocki <g...@somewhere.invalid> wrote:
> Although I find indented blocks easier to read (easier to see that this is
> in fact part of the namespace), I know and agree with arguments against
> indentation and in simple cases I don't feel it affects readability, but
> in more complicated ones, where there are many nested namespaces, it
> easily becomes a mess...

C++17 added support for the syntax

namespace A::B::C
{
...
}

which was quite welcome, for me at least.

I also often use another syntactical trick (already supported by C++98) to
minimize the amount of indentation:

namespace A { class B; }

class A::B
{
...
};

Tim Rentsch

unread,
Jul 18, 2018, 2:16:48 PM7/18/18
to
g...@somewhere.invalid (Adam Wysocki) writes:

> What is your preference towards indenting contents of namespaces?
>
> Although I find indented blocks easier to read (easier to see that this is
> in fact part of the namespace), I know and agree with arguments against
> indentation and in simple cases I don't feel it affects readability, but
> in more complicated ones, where there are many nested namespaces, it
> easily becomes a mess...
>
> I know it's a matter of personal preference (or rules agreed by the team),
> but I'm curious what your preference is, and why.

I don't have any hard-and-fast rule. Generally, though, I find
that indenting nested constructs becomes counterproductive when
what is being nested exceeds some length. I don't know what
that length is exactly but for concreteness let me call it 100
lines. Below threshold, especially when what is being nested
fits on a single 50- or 60- line page, indenting is okay. Above
threshold, indenting hurts more than it helps, and it's better
just not to indent at all.

Related comment: any nesting construction where what is being
nested exceeds some relatively small amount (50 lines, say)
deserves a comment on its end marker indicating what is being
closed. I recommend this practice whether or not the nested
portion is indented.

Vir Campestris

unread,
Jul 19, 2018, 4:55:18 PM7/19/18
to
On 18/07/2018 06:06, Chris M. Thomasson wrote:
>
> I have indented them all before. It does seem to have potential scaling
> issues wrt deeply nested namespaces.

You are Linus and ICMFP.

The Linux Kernel standards have an implied limit on nesting by making
sure that all tabs are 8, and the code must fit on a VT52.

Nowadays we have bigger screens. Nesting deeply is not the issue it used
to be. I have one of the narrowest screens on the department - 1200
pixels (1) - and that doesn't seem to be narrow enough that nesting is a
a limit. It's print statements where I need to wrap.

Andy
--
2x1920x1200 in portrait mode :)

Thiago Adams

unread,
Jul 19, 2018, 5:13:02 PM7/19/18
to
On Tuesday, July 17, 2018 at 6:00:07 PM UTC-3, Adam Wysocki wrote:
> Hi,
>
> What is your preference towards indenting contents of namespaces?

My preference is not to use namespaces.

Unless I have to do some library for other people
(other companies) to use. In this case I will have just
one level like namespace MyLib;
In this case I would use the indentation that the IDE does
by default.

The reason to avoid namespaces is that I don't want to
add more text and logic (like the reasoning about
the namespace names) to the code to solve one
problem (name crash) that I don't have and from my
experience I will never had or it will be just
one function that could(should) be static.

Also I don't want o manage "sets" of better logic for
the position of some function and creates a debate
about this.

If the code looks like many layers of abstraction
and the layers are represented by namespace this is
a bad sign for me. Just like class hierarchies.

Generally I use folders to represent source
that will always be used together because they depend on
each other to build and they can be reused in other
projects just coping the folder.



Chris M. Thomasson

unread,
Jul 19, 2018, 5:30:14 PM7/19/18
to
Fwiw, I remember back in the day using something like:
______________________
namespace foo {
namespace bar {
namespace baz {

struct hello_there
{
int a;
};

}}} // foo::bar::baz
______________________

Love the new syntax wrt:
______________________

namespace foo::bar::baz
{
struct hello_there
{
int a;
};
}
______________________

Nice. :^)

Mr Flibble

unread,
Jul 19, 2018, 5:36:33 PM7/19/18
to
Is this a deliberate troll? I sincerely hope so as the alternative is too
depressing to think about.

Ian Collins

unread,
Jul 20, 2018, 4:17:42 AM7/20/18
to
On 20/07/18 08:55, Vir Campestris wrote:
> On 18/07/2018 06:06, Chris M. Thomasson wrote:
>>
>> I have indented them all before. It does seem to have potential scaling
>> issues wrt deeply nested namespaces.
>
> You are Linus and ICMFP.
>
> The Linux Kernel standards have an implied limit on nesting by making
> sure that all tabs are 8, and the code must fit on a VT52.
>
> Nowadays we have bigger screens. Nesting deeply is not the issue it used
> to be. <snip>

It's not screen size that matters, it is text window size..

--
Ian.

Thiago Adams

unread,
Jul 20, 2018, 8:41:30 AM7/20/18
to
Try to say something useful about the subject instead
of think about depressing stuffs.

Thiago Adams

unread,
Jul 20, 2018, 4:04:44 PM7/20/18
to
On Thursday, July 19, 2018 at 6:13:02 PM UTC-3, Thiago Adams wrote:
...

> Also I don't want o manage "sets" of better logic for
> the position of some function and creates a debate
> about this.
>
> If the code looks like many layers of abstraction
> and the layers are represented by namespace this is
> a bad sign for me. Just like class hierarchies.
>

I just found this video from Jon Blow:
I agree on this part about layers.

https://youtu.be/uZgbKrDEzAs?list=PL_LX30v0VQqEMTTX3uG93_tUnTUPMG6X6&t=351



Jorgen Grahn

unread,
Jul 30, 2018, 6:07:52 AM7/30/18
to
On Tue, 2018-07-17, Adam Wysocki wrote:
> Hi,
>
> What is your preference towards indenting contents of namespaces?
>
> Although I find indented blocks easier to read (easier to see that this is
> in fact part of the namespace),

Me too. "Don't indent namespaces" has been the rule at work (2/2
places which used modern C++) but I can't get used to it. It's a
block of related stuff; I want the indentation to reflect that.

For example, I can't get used to seeing:

};
}

I remember not indenting them when I first started using them, in 2002
or so. My books indented namespaces; my editor defaulted to indenting
namespaces ... it seemed like a dumb idea to, as a newbie, start by
breaking existing conventions.

I don't see any negative effects of indenting. For me, it mostly
affects header files and anonumous namespaces, and I never nest them
very deeply.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

bol...@cylonhq.com

unread,
Jul 30, 2018, 7:24:37 AM7/30/18
to
On 30 Jul 2018 10:07:40 GMT
Jorgen Grahn <grahn...@snipabacken.se> wrote:
>On Tue, 2018-07-17, Adam Wysocki wrote:
>> Hi,
>>
>> What is your preference towards indenting contents of namespaces?
>>
>> Although I find indented blocks easier to read (easier to see that this is
>> in fact part of the namespace),
>
>Me too. "Don't indent namespaces" has been the rule at work (2/2
>places which used modern C++) but I can't get used to it. It's a
>block of related stuff; I want the indentation to reflect that.

The problem is that namespaces usually extend over 1 or more files so if you
indent everything in a namespace you often indent almost every line in a file
which IMO is rather pointless and leads to reduced readability.

Vir Campestris

unread,
Aug 2, 2018, 4:45:41 PM8/2/18
to
IMHO _not_ indenting impairs readability. That indent tells you it's not
a global scope, it's somewhere else. Just the same as you do with local
variable inside a function.

Andy

bol...@cylonhq.com

unread,
Aug 3, 2018, 4:34:11 AM8/3/18
to
I suppose at the end of the day its all subjective. Personally I don't see
the point in indenting almost every line in a file but if it works for you
then fair enough.


Vir Campestris

unread,
Aug 3, 2018, 5:55:29 PM8/3/18
to
On 03/08/2018 09:34, bol...@cylonHQ.com wrote:
> I suppose at the end of the day its all subjective. Personally I don't see
> the point in indenting almost every line in a file but if it works for you
> then fair enough.

IMHO consistency matters more than the exact standard.

I like my curly brackets there... but there are other ways.

Andy

Alf P. Steinbach

unread,
Aug 4, 2018, 3:50:49 AM8/4/18
to
Before C++17 I always reduced namespace indentation to one level:

namespace foo{ namespace bar{

namespace impl{
void gah(){}
} // namespace impl

void hello(){ impl::gah(); }

class Baluba
{
};

}} // namespace foo::bar

That turned out to be consistent with C++17:

namespace foo::bar{

namespace impl{
void gah(){}
} // namespace impl

void hello(){ impl::gah(); }

class Baluba
{
};

}} // namespace foo::bar

I recall distinctly that there is a problem with a nested `impl` or
`details` namespace, namely that when it occurs in two or more distinct
parent namespaces, it can be ambiguous. However, I'm unable to reproduce
that problem. Could someone shed some light on that?

Cheers!,

- Alf
0 new messages