A Critical View of C++ Practices

http://www.accu-usa.org/Slides/ACriticalViewOfCppPractices.pdf - very interesting paper about some modern C++ practices. I really enjoyed reading it, because I could find relations to my own “coding biography” there.

When I first found out about all those nice design patterns , I started to implement them everywhere I could (if all you have is hammer…). I replaced all my global variables with Singletons and so on. Now, after few years I’m almost at the same point where I started. “Almost”, because I sometimes do use some fancy pattern, but only if it really fits there. I noticed that they tend to make coders lazy. I didnt think much about the problem. Dont know how to solve object ownership? Bah, we have reference-counting and smart pointers for that. Sure, it may work, but perhaps analyzing situation for a while can result in simpler and cleaner solution.

For example, I try to avoid singletons completely now. I actually analyze the advantages and disadvantages connected with them. One of the most common pros of singletons is: “if you use raw global variables someone may modify/delete it”. Yes, well, it’s a valid argument, but come on… How often does it actually happen that someone deletes your g_pRenderer in the middle of game? If someone on my team did this I’d seriously question his sanity. Sure, additional safety net is a good thing, but some of the quoted Singleton advantages are very theoretical. It seems to me like it’s more a patch than elegant solution. In my home project I try to avoid singletons and global variables at all. I definitelly do not believe that this decision should be taken on the class level. I try to initialize subsystems with objects they need. Then, inside subsystems there can be wrapper in singleton-like manner, but other subsystems dont care. I really like how it works out so far.

Another funny thing about all those fancy C++ techniques is that they dont mix that well with modern consoles. If you dont consider it when starting with your engine, you can find yourself in deep trouble later. So, think twice before writing another cool line of code that does 5 things at once, reallocates memory 10 times and introduces 7 temporary objects.

Hmm, I wanted to write also about _std::foreach and it’s limited usability, but rant is rather long as it is, so I’ll do it another time, stay tuned :).

Old comments