Tom Ritchford
2 min readApr 8, 2023

--

using namespace std;

Aw, man, why do you do this?! You turn me off on literally your first line of code!

How many symbols have you just brought into your global namespace, and what are they? The answer depends on how many includes appear in the file and what version of C++ and the STL you are in, but it could easily be thousands, and it will change, perhaps between compilations, due to changes in other, distant include files.

Symbols shadowing symbols so you get the wrong symbol is a real thing. I have encountered it in my own code. Worse, I had this interaction between two separate libraries I was using so I eventually had to divide my code into separate siloed compilation units (though it didn’t involve std::.)

Most of the time, there will be no surprises. Most of the time. I like zero surprises.

Just as important, as Howard Hinnant explained to me, if you're skimming code and see std::string you immediately know exactly what you are getting.

But if you see string you do not know for certain what symbol that refers to - the correct answer depends on the entire file and all its includes. It's both a possibility for failure, and a minor cognitive load on you.

I read a huge amount of code. I want code to be boring and simple and predictable, so this argument had a lot of weight for me.

I used to do this. I was wrong. C++ is a difficult enough language without exposing yourself to unnecessary risks.

Rant mode off! Now let me read the rest of the article.

--

--

No responses yet