C++ Programming guidelines


This article contains some major guidelines which you can call a collection of Tips and suggestions for C++ programming.

  • First think, draw it on paper, create designs how your program will work, then code.
  • Even a well written program can encounter bugs but If you belive in the first step then it will be easier to understand, maintain & debug your program.
  • Remember the “divide & conquer” principle. If the problem you’re looking at is too confusing or too big, try to imagine the basic operation or the requirement of the problem you are going to solve.
  • This is one of the most important point, do not code the entire program at one go. If your program contains a large body of code that needs changing, select that code that will not be modified. Once you succeeded, and then focus on the code that will be modified.
  • When declaring a class, select a clear and meaningful name for that class, and it goes for variables also, so that once you look at the class name or variable name, you get the idea what it is about.
  • Avoid too many comments because it can clutter you program. Your goal should be to make it conceptually simple. This is possible only with the appropriate names.
  • In a class declaration, keep everything as private as possible, and make only the class interface public, always using functions rather than data. In some cases it is hard to avoid data from being declared as public but try to avoid them from this control access specifier as much as possible.
  • If a portion of your class is to be exposed to inheritors as protected, provide a function interface rather than expose the actual data. In this way, implementation changes will have a little effect on the derived classes.
  • Some programmers do involve in the first step that is the analysis state. There are things which you won’t be able to learn or start thinking over it until you start coding and get some kind of system working.
  • The analysis and design must produce a better look of the classes, their interfaces and relationships to other classes, especially base classes.
  • Now its time to write the test code first before you write the class, and keep it it with the class. Make a test file to see the result and it all depends on available tool. With this great opportunity, any changes can be verified by seeing the test code. If any error, you’ll immediately discover them.
  • Each and every class must be crystal clear that is as atomic as possible. If your design grows too complicated, make it simpler as much as you can. Do not include too much stuff inside the class.
  • Look for the definition of a member function. A function that is long it would be good to break it or if not possible, at least move it outside the class.
  • You must add valuable functionality to you interface. You can do it if you move the repetition of code to a base class and call it from the derived class.
  • Watch for switch statements or the dangling else problem if any. This is typical and indicator of type-check coding, which means you are choosing what code to execute based on some kind of type information. This can be easily replaced by a code with inheritance and polymorphism.
  • Avoid multiple inheritance.
  • The control access specifier private is part of the language but try to avoid private inheritance because it introduces ambiguities when combined with run-time type identification.
  • As we know that operator overloading is different way to make a function call. If it doesn’t provide clarity of the class interface then don’t use it.
  • Avoid the preprocessor and macros. C++ uses const and inline function
  • Make the lifetime and visibility of your object as small as possible.
  • Avoid global variables as much as you can.
  • If you do not want to modify your arguments, always pass them by const.
  • If class template is available in standard library of C++ programming language, then do not use your own. This will increase your productivity.
  • Avoid the use of ‘C’ header files such as stdio.h, always use iostreams instead.
  • Use c++ type casting instead of ‘C’ if your compiler supports them.

Leave a Comment