100DaysOfGameDev: Day 14

Today was rather swift, but alas boring. It was about the namespace in C++. Since the book was written a while ago, it is so funny to me, that he introduces the namespaces as a facility to divide code into logical units as with modules. He says that by combining the namespaces with other facilities of the language we can achieve clean code as with modules. What's so funny, is that they have added modules in C++20. Pffff, it only took them three standards to add modules, showing what of a trash pile C++ is. Just do it properly and do it once dammit! I am kind of developing a pleasant reaction every time the author mentions the horrible C++ codebases he faced throughout his career. Makes me think, that he deserves it since if he did everything properly from the start, he wouldn't see that crap. Another fun fact, if you read my blog post and I didn't mention an error in a code example from the book, then assume that I did not type it out myself and took it for granted. Because, with a 100% rate, every time rewrite a book code example it fails to compile with the stupidest mistakes you can imagine. Gives you an idea of what we deal with. Finally, a tiny, code snippet ( from the book ) to help me get my point through. Bjarne has this proud tone every time he talks about a feature, like how easy it made the life of developers. However, he completely misses the fact that the illusion of “convenience” hides a dozen implicit rules, that programmers have to memorize and are easy to mess up. Quoting from the book:

namespace Chrono {
    class Date { /* ... */ }

    bool operator==(const Date&, const std::string&);

    std::string format(const Date&);
    // ...
}

void f(Chrono::Date d, std::string s)
{
    if (d == s) {
        // ...
    }
    else if (d == "August 4, 1914") {
        // ...
    }
}

We look for the function in the scope of the call ( as ever) and in the namespaces of every argument (including each argument's class and base classes) and do the usual overload resolution. So, such an innocent “==” operator has this grand set of rules working together, so you will not have an extra line or two, so fucking novel!!! Anyway, that's it for today. I should keep my cool, or else can't survive in the world of C++. Thank you, see you tomorrow!