Skip to content

Good Programming Practices

NickG-Math edited this page Sep 16, 2019 · 5 revisions

If you plan to contribute, always keep in mind the following practices (ordered semi-randomly)

  • Run the test functions for regression checking.
  • To see bottlenecks and quantify performance, use the MATLAB profiler (for parfor loops use timeit instead).
  • Leave comments explaining what the code does.
  • Avoid loops and instead vectorize code.
  • Use bsxfun for more advanced vectorization.
  • Write functions, not scripts (OK scripts are fine for quick checking/debugging).
  • cellfun, arrayfun are not vectorized and are sometimes slower than loops; use with caution.
  • Don't use global variables (it's OK to temporarily use them to gather data for speed experiments)
  • Careful when using functions like diag that operate differently on arrays vs matrices. They might have unintended effects in edge cases.
  • Debug extensively using breakpoints. Use the function call stack to debug the callers of the function throwing an error.
  • Exception handling incurs speed penalties. It's best to handle errors using if statements.
Clone this wiki locally