Rakesh's Blog

Namespaces

I have heard this a lot of times… 75-80% of the C++ code out there doesn’t use namespaces. A lot of C++ developers do not use namespaces.

 

Is this a valid assumption?

 

I would appreciate if you could share your opinion about this.

 

If we were to believe that most C++ code or C++ developers don’t use namespaces, it could influence how we triage/prioritize some bugs and how content we are with some fixes in Beta2.

Published Saturday, November 13, 2004 4:33 PM by rakeshna

Comments

 

Adi Oltean said:

My take on this: namespaces are usually defined by those developing reusable code (libraries or templates, like STL, ATL, Boost, Loki, etc). This class of developers would be pretty small, probably less than 1%?

Most developers will probably fall in the "namespace consumers" category where all you want is a reliable way to access stuff in an existing namespace.
November 13, 2004 4:58 PM
 

Brian Kretzler said:

I've just made an attempt internally to curtail a bit the proliferation of namespaces in our codebase. Seems like we were approaching 1 namespace per file. I'm steering toward the use of namespaces such that they can provide an extra organizational layer for use in code navigation. We have a policy that all code must reside inside a namespace. There is some resistance though to nesting namespaces (with the exception of "detail" style or anonymous ones), so we have a flat model.

Note though, this is in a relatively new (2 year old) and relatively small (1500 files) code base. Our older codebase had zero namespaces.

The more extensible your code is, the more likely you need namespaces to help avoid conflicts.
November 13, 2004 9:05 PM
 

Uwe Keim said:

We do use namespaces in our projects too.

The disatvantages, compared to C# namespaces is that you not only have to use the "using" statement but also (of course) need an "#include" statement.

So in summary you need "2 instructions" in C++ where you need only "1 instruction" in C#. Maybe this is an additional reason why C++ code fewly uses namespaces.
November 13, 2004 11:58 PM
 

James Slaughter said:

I think there are few people who avoid having any interaction with namespaces. The standard library, Boost, even various bits of MFC and ATL live in namespaces. At some level, they're used by a lot of your customers' code.

Do people write code using namespaces? Sure: Their proliferation is increasing for silly reasons like the fact that the C# project wizards use them. "Hey, I can do something similar in C++..." and off they go. Unnamed namespaces are also very popular with the <functional> loving crowd (though not as popular as a compiler that gave local classes linkage would be).

Do people 'understand' namespaces? Do they know the difference between using declarations and using directives (the latter not being just a 'bulk version' of the former) or the subtleties of ADL, the implications of extern "C" in namespaces, etc? Most of them don't.

Of course, those who are going to be annoyed by deficiencies fall into two categories: those porting code who encounter compiler differences, and those expert-level developers who spend a good twenty percent of their time working around compiler problems as they try to achieve something unnecessary. Both sets of developers are quite vocal in setting a reputation for the product and affecting whether people switch compilers, upgrade to the latest version, etc. Many other sweatshops will take what they're given. And they'll like it!
November 14, 2004 7:54 AM
 

Tonetheman said:

I use namespaces but I think that for the most part they are a convenience feature and not something I could live without in C++. But if it would somehow mean that STL or other large libraries "break" or leak then I think they are very important.
November 14, 2004 12:30 PM
 

Dean Harding said:

The number of times I've seen "using namespace std" inside of StdAfx.h is scary.

I think the 75-80% figure is probably fairly accurate. Even myself, if I'm just writing a quick little project to test something out, I won't bother with namespaces. For larger projects, I try to be more organised, but - and this is especially true for older projects - a lot of the "professional" code I've worked on did not make any use of namespaces at all.
November 14, 2004 5:47 PM
 

Harish Nataraj said:

Typically in large projects, (greater than 15 developers) namespaces become mandatory. Otherwise there are several conflicts that arise, as the code base grows.

So It depends, who your consumers are? What % of your consumer base can be categorized as large projects?

What % of revenue is being receveid from these consumers? Are these consumers contributing 80% of the revenue?

First Answer these questions, and you will have an answer to your original question.


You may have to ask your stupid marketing/sales department for this information :-)
November 15, 2004 3:26 PM
 

Harish Nataraj said:

Developer's such as you need beter visibility on your target customers.

I suggest you write to Steve Ballmer and tell him, that if they need to business at the speed of though, they need to make all this info about current customers available to developers via the company intranet.

That way, u have hard numbers and can make good decisons, without relying on random input from blogs.

$0.02
November 15, 2004 3:40 PM
Anonymous comments are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker