My five most common programming mistakes in C++

I’m a junior programmer. Junior means I don’t have industrial experience, sad, but coding at home and working at a company are entirely different things. Strict deadlines, teamwork with a lots of smart guys, really big source codes and the experience that the small and smart modifications (in other words, the smartness and thinking) can be more valuable than bare coding and reinventing the wheel – I guarantee you won’t experience these things if you are just sitting at home. “Being an amateur at means I can be comfortable. It’s safe. There is no fear of success or failure … Being an amateur is the opposite of going pro.” (I quoted from here.) I’m a junior programmer and junior means I started to acquire the skills that distinguish an amateur and a professional programmer.

But these skills are not about programming basically. If you will be a professional programmer, you will be a more effective programmer but you won’t be a smarter programmer. You will learn to use the things that you know and that you own in a more effective way but you won’t know more about the programming. Likely you cannot practice things you don’t know at all so learning is inevitable. Reading, thinking, talking, debating – these things come always before the practice.

I’ve read an article about common C++ mistakes (you’ll find this article here), and I was a bit surprised, because I notice the most of the mistakes of the article in the most of cases and I don’t notice many other things. For example, it is pretty hard to forget the initialization of variables after some Java coding (Java does not have uninitialized variables), by the way, initialization lists in C++ are lot more fun. I like them because they make possible to perform the base class initialization and variable initialization in the same way and it is so beautiful. (The compulsory definitions of the declared static class variables make the initialization more unforgottable.) Integer division, the operator ==, the myFunction(x, x++, x--) bug (read here about it), lazy evaluation and breaks in switch-case statements aren’t C++ issues, they can be problematic in pure C too, so if you aren’t sure how to use them, all you need is some C practice. And then, when you are coding a C-like function, you just have to use your C knowledges.

No, my C++ mistakes are more typical and more painful. When I get a weird C++ compile-time error, I follow the checklist below to see whether it is a trivial error (oops, I did it again), or not.

  1. Using g++. I’m not very proud of it, but I frequently try to compile my C++ programs with gcc. I don’t know, why. I like to think that I’m absent-minded, evil people like to think that I’m an idiot, only the god knows the truth. Interesting, but it isn’t easy to see in the dump of the gcc that the problem is the wrong compiler, so in one hand, when I have an incomprehensible dumb, then it is my first idea to fix the error. In other hand, that’s why I always create a Makefile (even to the simplest projects), and it is a good programming practice. Writing a Makefile doesn’t take much effort, it makes the whole build trivial and it helps to avoid the first mistake of my checklist. (Of course if you are using Visual C++, it is not an issue. But why are you using Visual C++?)
  2. The semicolon at the end of the class definition. I forget it frequently. Then the g++ inserts my header files into other files and it process the start of the other file as the declarator part of the wrong class definition. (I wrote an article about declarators, do you remember?) Short and sweet, the compiler will show a non-existing error in an irrelevant file and it won’t show the real error in the relevant header file. It is very annoying, and I check the semicolons at the end of my class definitions twice because it makes me always angry.
  3. Public and private inheritance. Likely it is the side effect of  Java and ActionScript 3.0 programming, but I forget the the keyword public and private after the colon again and again. I don’t like this mistake, because it makes me look like a world-class idiot. Why? Because thinking people always know, what they want, ISA relationship (== public inheritance) or the reusing codes of other classes without ISA restriction (== private inheritance). If the ISA issue doesn’t matter you at all, it shows that you aren’t thinking at all and it always causes problems.
  4. The public and private keywords in class definitions. When I’m coding in Java or in ActionScript 3.0, I always write explicitly whether a member is private or public. In C++, it is not possible, so I have to give some attention to the structure of the class definition and to the placing of the private: and public: separators. It is good, because it forces me to order my class members (public and private members won’t be mixed) and my class definitions will be more manageable. And it is bad, because sometimes I forget these separators. It is an other very stupid mistake (a noob error), that’s why it is so painful.
  5. The MyClass class(); misdeclaration and similar misdeclarations. I give much attention to avoid this mistake, but once I did frequently. It is a very famous C++ design issue, variable declaration and function declaration are so similar in C++, that it can cause disambiguation. Of course not to the compiler (it compiles the code always in one way, in the way of the language specification or in its own way), but to the programmer. MyClass myName(); seems like a variable declaration but it is the forward declaration of a function and that’s what I call disambiguation. In the most simple cases, it is easy to avoid this mistake. But there are some rare complicated cases, so alertness is encouraged. I recommend you the book More effective STL, especially the Item 6 (because it deals this mistake).

So, these are my mistakes. I’m not perfect and bulletproof, gentle reader, and those articles like this help me to face my mistakes and to learn from my mistakes. And I think it is a good way to show what kind of people I am. I find it a bit funny when people write a personal blog about their favourite films and foods to show themself – these things seem so irrelevant to me. Just the things that you do (well or badly) show your true colors.

About vrichard86

I'm Richard and I'm an enthusiastic coder. I want to be better and I think a professional journal is a great self improvement tool. Welcome to my blog, to my hq, to my home on the web!
This entry was posted in C++ and tagged , , , , . Bookmark the permalink.