Newbie Programming Mistakes

From David's Wiki
\( \newcommand{\P}[]{\unicode{xB6}} \newcommand{\AA}[]{\unicode{x212B}} \newcommand{\empty}[]{\emptyset} \newcommand{\O}[]{\emptyset} \newcommand{\Alpha}[]{Α} \newcommand{\Beta}[]{Β} \newcommand{\Epsilon}[]{Ε} \newcommand{\Iota}[]{Ι} \newcommand{\Kappa}[]{Κ} \newcommand{\Rho}[]{Ρ} \newcommand{\Tau}[]{Τ} \newcommand{\Zeta}[]{Ζ} \newcommand{\Mu}[]{\unicode{x039C}} \newcommand{\Chi}[]{Χ} \newcommand{\Eta}[]{\unicode{x0397}} \newcommand{\Nu}[]{\unicode{x039D}} \newcommand{\Omicron}[]{\unicode{x039F}} \DeclareMathOperator{\sgn}{sgn} \def\oiint{\mathop{\vcenter{\mathchoice{\huge\unicode{x222F}\,}{\unicode{x222F}}{\unicode{x222F}}{\unicode{x222F}}}\,}\nolimits} \def\oiiint{\mathop{\vcenter{\mathchoice{\huge\unicode{x2230}\,}{\unicode{x2230}}{\unicode{x2230}}{\unicode{x2230}}}\,}\nolimits} \)

Weird Syntax

comparison to true or false

If you have a bool, you can just use it instead of comparing to true or false:

# some code which produces bool b
b = a > 5
# return b == false
return not b

Similarly, you don't need to do:

# Unnecessary
if not b:
  return true
else:
  return false

Not using vectorization

If you're using a language like Python or R, you can take advantage of faster SIMD instructions by using vectorized code.
Instead of writing:

a = np.arange(5)
b = np.arange(5)
# Bad
c = np.zeros(5)
for i in range(len(a)):
  c[i] = a[i] + 5*b[i]
# Good
c = a + 5*b