I don't want to see "using namespace xxx;" in any header file anymore.

Source: Internet
Author: User

Original article: I don't want to see another "Using namespace xxx;" in a header file ever again

Here, I did not shy away from this sentence.

As a developer/Team Leader, I often recruit new project members and sometimes help people in other groups to interview candidates. As one of the application processes, I often asked the applicant to write some code, so I checked a lot of code. In the recently submitted C ++ code, I noticed a trend. In any header file, I can always see the following code:

using namespace std;

If I use our code check system (I highly recommend this system in practice) to check the code, the above line of code will often comment "Timo won't write this way ". They are right. I really won't write that.

So why did I persuade many c ++ textbooks (maybe not a good thing) to think that using the above Code is a very bad way? Let's take a look at what the above Code has done. In general, it introduces all the content within the namespace "STD" (or other namespaces called by the author using) to the current namespace without exception. Note that the "all content" I am talking about is not one or two Classes \ Type \ templates you want to use. The reason why namespaces are introduced at the beginning of a piece of code is to enhance program modularization and reduce naming conflicts. In general, it allows you to write code similar to the following section and ensures that the compiler can select the correct implementation:

std::vector<std::string> names;    my_cool_reimplementation::vector<our_internal_stuff::string> othernames;

Now, let's assume that we are trying to reduce the code input and use the using declaration in the above Code (or worse, both namespaces are declared), follow the code below:

vector<string> names;    vector<our_internal_stuff::string> othernames;

If the author of this Code is lucky, the compiler will choose the correct implementation of vector, or at least at the initial stage. However, after a while, you may encounter some strange compiler errors. Fortunately, you can find the cause of these errors-I have encountered similar problems and it takes me several days to find the cause of these problems. Damn it, they will waste a lot of time, just because you want to play 5 Characters less code.

In addition, if you use the using declaration in the header file, you will make this kind of problem worse, because the naming conflict problem will occur in a module with a very distant call relationship sooner or later, however, you may need to check the layer-3 call to find the cause. One header file contains another header file that directly uses the using declaration, which can cause the namespace to be immediately contaminated, any file using a namespace that uses the STD namespace will cause such problems.

So why can you see in many textbooks that they use using namespace STD? My theory is that it does help improve the layout of the entire book and reduce visual confusion. In a paper book, you only have a limited amount of space to write text, so you must use it to the maximum extent. In addition, the code examples in the book are usually very simple. On the other hand, different namespaces may cause a lot of visual confusion, which makes it difficult for readers to judge the author's intention from the context. When you want to write some efficient code in this era, the above two points are not completely correct. Currently, most compilers can process 60-80 words per line (you can try it, ). Therefore, do not introduce namespaces in disorder.

If you want to use the using declaration in a header file, what should you do? We have other ways to reduce the situation where we have to use the using statement-you can use one of the following, or a combination of multiple methods.

First, you only need to use typedef. I recommend that you use this method. Even if I do not always follow my own suggestions, I think it is a good method in practical application. Actually, using typedef has two advantages: it increases the readability of A type name. If you choose a good name, it can make the author's intention more obvious. Compare the following declaration methods:

std::map<std::string, long> clientLocations;    typedef std::map<std::string, long> ClientNameToZip;    ClientNameToZip clientLocations;

The second declaration-even if it is expanded into two rows-is more intuitive than the first declaration, and it also avoids the blurring of the namespace.

Another option is to use two methods to restrict the scope of the using Declaration-just the "using" symbol you want to use, for example:

using std::string;

However, throwing this declaration into the header file is almost as bad as using "using namespace". Therefore, you should use the scope to limit its visibility, to ensure that your using declaration is only valid in the first place where the using declaration is made. For example, you can use the following method to restrict the class declaration scope:

namespace bar{  struct zzz  {    …  };}class foo{  using namespace bar;  zzz m_snooze; // Pulls in bar::zzz  };

Alternatively, you can directly restrict the using scope to a function. For example:

void temp (){  using namespace std;  string test = "fooBar";}

Regardless of the method, you can restrict the using scope to the code that requires it, rather than put it in the public space of the Code. The larger your project, the more important it is to ensure modularity and minimize unexpected negative effects.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.