c++ programming

c++ is an extremely powerful language. It covers a large range in abstraction – from bit manipulation to template metaprogramming.

Here are some guidelines which are used by Dreik Ingenjörskonst when programming c++.

  1. Correctness. The program should be correct, even if performance has to be sacrified. Once it is correct, one can start thinking about performance.
  2. Defensive programming. By using methods like assert(), checking return codes and using unit tests it is possible to catch errors at an early stage. This saves a lot of time because hard to find bugs are easier to find at an early stage.
  3. Standards conformance. By using c++ standard and no extensions, the code will be easier to move and update.
  4. Use of the STL (standard template library). By using STL, a lot less coding is generally used. Other programmers will also recognize the code, meaning it will be easier to maintain the code.
  5. In-code documentation. Using a tool like doxygen to automatically extract documentation text, it is easy to keep documentation up to date and easy to read.
  6. Use boost libraries when STL is not sufficient. The boost libraries are generally of high quality and widely spread. It is allowed to use in commercial projects.
  7. Hide platform dependent code by using boost to the largest extent possible.
  8. Avoid manual memory management. Juggling pointers to memory allocated is error prone. Smart pointers are preferred, which are exception safe and reduces the risk of programming error. Also, the difficulty to maintain the code is reduced.

With that said, one can start considering performance. Many applications written by Dreik Ingenjörskonst need to be as fast as possible, either because there is a realtime limit, the amount of data is vast or there are other processes needing the cpu capacity. It is important to not start optimizing the code to early, as that usually leads to complicating things without any performance gain. There are several methods to increase performance:

  1. Find hot spots in the code and try rewrite them. If there is a working base, it is easy to enhance the code because there is a reference implementation to compare the results to.
  2. Use several threads to utilize several cores simultaneously. This requires the program to be rewritten. Careful use of mutexes along with division of work tasks is used to rewrite the program to allow it to execute in parallel.
  3. Use of parallel programming methods like OpenCL. This allow a graphics processor or even the normal processor carry out work. If a loss in precision can be accepted, it is possible to reach a tremendous gain in execution speed.

Techniques for development

A great technique is called “tracer bullet”, a term found in the book Pragmatic programmer. It means that one first reach to the goal with a prototype program, sacrificing implementing details and features. Once the goal is reached, it is easy to see if something needs to be changed or remade in order to fulfill all requirements. The final program is then written, using the experience from the prototype. In practice, this can mean faking user input, avoiding implementing a user interface or insert fake data. This reduces the total development time and detects design errors at an early stage. It is also easier for the client to see where the development is going, so there is a chance to influence the program at an early stage.

See also the separate page on c++ resources.