Thursday, February 23, 2012

C++ Programming Anti-Patterns and Annoyances

  1. #include "file.cpp"
  2. Declaring fields as protected with a comment stating that a friend class is the only one who should access them.
  3. Allocating an object using new instead of on the stack because it must be passed by pointer to a function and the programmer doesn't know that they should use the address-of (&) operator.
  4. Having two functions foo(A*) and foo(A&) that are copy-and-paste identical save for using -> instead of . because the programmer didn't realize how to convert references to pointers and vice versa.
  5. Making fields public via by-reference getter and setter.
  6. Thinking copy-and-paste == code reuse.
  7. Using "string const &" instead of "const string&"
  8. Implementation in header files
  9. if (p) delete p;
  10. if (b == true), if (b == false)
  11. if (v.size() == 0)
  12. "using namespace" in header files
  13. Using vector to represent a rectangle
  14. v[15].first.second.first[5].WhatTheHeckAmIAnyway()
  15. Passing user-defined objects by value
  16. for(int i = 0; i < n; ++i) { if (i == 5) a[i] = 0; }