Algorithm Design Flaws
Here is a list of common flaws in algorithm designs:
- Method too long. In general a method should not exceed 50 lines.
- 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.
- Don't use static methods,
unless it really must not be instantiated.
This 3 minute video explains why.
- No static keyword on
instance variables.
- Variables that
should be local to a particular method must be
declared in that method, not as instance variables
visible to all the methods.
- Don't use bizarre sequences
of decisions to assign control variables.
- No break or return in a
loop or decision.
- No return from void methods.
- No empty then or else
clauses (if (condition) { } else { something.. }
- No empty catch blocks. 1
- No == true in a
conditional
- No if (something) return
true else return false
- No if x.compareTo > 0
return 1 ... just return x.compareTo
- No use of for statement to
implement a conditional loop (should be while)
- No use of while statement
to implement a counting loop (should be a for)
- No while (true) loops.
- No for statement
to iterate over a Collection; should be for-each.
- Don't implement a sort
algorithm. Use Collections.sort().
- No recursion (unless specifically permitted).
- Don't be a brute.
1See
section 4.1 of
this article.
JD 10/2/2008