C++Guns – RoboBlog


Good C++ Code

Filed under: — Thomas @ 19:04

My tips for a more readable, safer and reuseable code!
The tips are very very simple. Everyone can use them in daily programming!

Five Myths about C++ BUSTED!
integer overflow debugger trap
Passing random generators around (functor)
Calling multiple random generator in parallel c++11
Polymorphie virtual abstract pure deconstrutor WTF?
rosettacode - Simple moving variance
rosettacode - Simple moving average
raw speed of C using OO with C++

Common editing

  • Intend your code. Usually use 4 whitespaces.
  • Never use tabs to intend, only spaces!
  • Structure the code in logical blocks. Use one newline between the blocks.
  • Use brackets! Use { }! Use ()! Its much more safer and readable! Use {} after every for if while! Don't be lazy. Use auto completion if you are lazy. One exception for this rule: for throwing exceptions. e.g. if(false) throw "Shit"

    C and C++ mixing

  • Never mix C and C++
  • Don't use the old C include files with prefix ".h" e.g. math.h Instead use cmath
  • Always write the full standard namespace when using standard functions e.g. std::abs
  • Write "using namespace myspace" only for non-standard namespaces.
  • Never write "using namespace" in header files. Only in cpp files.
  • Loops

  • The first running variable in a loop is "i", then "j" and "k".
    Never use something like "ii", "iii". Don't use "l" (lower L) eiher, it looks too much like "i"
  • Prefer for() loops for while() loops. They have a clearer syntax how log the loop is running. And you prevent endloss loops.
  • Every while() loop can be written as for() loop()
  • common function calls

  • Pass non derived POD object as value. Like int float char
  • Pass non-POD objects as const reference. This performs no deep-copy and the caller can be sure that his object is not modified.
  • Pass by pointer only if you must modify the object! The caller knows throw the pointer notation that the object may be altered
  • Pass functions objects by value???
  • get functions

  • Functions which returns a variable and not change the object must be marked with "const"
  • No Comments

    No comments yet.

    RSS feed for comments on this post.

    Sorry, the comment form is closed at this time.

    Powered by WordPress