Algorithm Design Flaws


Here is a list of common flaws in algorithm designs:
  1. Method too long. In general a method should not exceed 50 lines.
  2. There should be no redundant code.  Code that is similar to code in another place must be combined so the code is written only once, in a procedure or function. Use parameters to accomodate differences in similar code segments. See Once and Only Once.
  3. Don't use static methods, unless it really must not be instantiated. This 3 minute video explains why.
  4. No static keyword on instance variables.
  5. Variables that should be local to a particular method must be declared in that method, not as instance variables visible to all the methods.
  6. Don't use bizarre sequences of decisions to assign control variables.
  7. No break or return in a loop or decision.
  8. No return from void methods.
  9. No empty then or else clauses (if (condition) { } else { something.. }
  10. No empty catch blocks. 1
  11. No  == true in a conditional
  12. No if (something) return true else return false
  13. No if x.compareTo > 0 return 1 ...   just return x.compareTo
  14. No use of for statement to implement a conditional loop (should be while)
  15. No use of while statement to implement a counting loop (should be a for)
  16. No while (true) loops.
  17. No for statement to iterate over a Collection; should be for-each.
  18. Don't implement a sort algorithm. Use Collections.sort().
  19. No recursion (unless specifically permitted).
  20. Don't be a brute.

1See section 4.1 of this article.

JD  10/2/2008