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; }

Friday, March 18, 2011

Makefile Optimization: Disable Implicit Rules

make -d shows that make spends a lot of time looking for files that don't exist and evaluating obsolete implicit rules. You can use make -r to disable these implicit rules and potentially speed up your build. However, if you're like me and you can't rely on developers using the -r flag, you can add this to your makefiles:

# Disable implicit rules to speedup build
.SUFFIXES:
SUFFIXES :=
%.out:
%.a:
%.ln:
%.o:
%: %.o
%.c:
%: %.c
%.ln: %.c
%.o: %.c
%.cc:
%: %.cc
%.o: %.cc
%.C:
%: %.C
%.o: %.C
%.cpp:
%: %.cpp
%.o: %.cpp
%.p:
%: %.p
%.o: %.p
%.f:
%: %.f
%.o: %.f
%.F:
%: %.F
%.o: %.F
%.f: %.F
%.r:
%: %.r
%.o: %.r
%.f: %.r
%.y:
%.ln: %.y
%.c: %.y
%.l:
%.ln: %.l
%.c: %.l
%.r: %.l
%.s:
%: %.s
%.o: %.s
%.S:
%: %.S
%.o: %.S
%.s: %.S
%.mod:
%: %.mod
%.o: %.mod
%.sym:
%.def:
%.sym: %.def
%.h:
%.info:
%.dvi:
%.tex:
%.dvi: %.tex
%.texinfo:
%.info: %.texinfo
%.dvi: %.texinfo
%.texi:
%.info: %.texi
%.dvi: %.texi
%.txinfo:
%.info: %.txinfo
%.dvi: %.txinfo
%.w:
%.c: %.w
%.tex: %.w
%.ch:
%.web:
%.p: %.web
%.tex: %.web
%.sh:
%: %.sh
%.elc:
%.el:
(%): %
%.out: %
%.c: %.w %.ch
%.tex: %.w %.ch
%: %,v
%: RCS/%,v
%: RCS/%
%: s.%
%: SCCS/s.%
.web.p:
.l.r:
.dvi:
.F.o:
.l:
.y.ln:
.o:
.y:
.def.sym:
.p.o:
.p:
.txinfo.dvi:
.a:
.l.ln:
.w.c:
.texi.dvi:
.sh:
.cc:
.cc.o:
.def:
.c.o:
.r.o:
.r:
.info:
.elc:
.l.c:
.out:
.C:
.r.f:
.S:
.texinfo.info:
.c:
.w.tex:
.c.ln:
.s.o:
.s:
.texinfo.dvi:
.el:
.texinfo:
.y.c:
.web.tex:
.texi.info:
.DEFAULT:
.h:
.tex.dvi:
.cpp.o:
.cpp:
.C.o:
.ln:
.texi:
.txinfo:
.tex:
.txinfo.info:
.ch:
.S.s:
.mod:
.mod.o:
.F.f:
.w:
.S.o:
.F:
.web:
.sym:
.f:
.f.o: