Less code written, more work done
Writing a tool while doing your analysis
Stop reinventing the wheel!
Separate code and data: data is specific, code need not be
gravity = 9.80665
, once.. . .
Do One Thing (and do it well)
. . .
Don’t Repeat Yourself: use functions
Write routines in functions, i.e., code you reuse often
Identify potential functions by action: functions perform tasks (e.g. sorting, plotting, saving a file, transform data…)
Functions are smaller code units reponsible of one task.
Functions are meant to be reused
Functions accept arguments (though they may also be empty!)
What arguments a function accept is defined by its parameters
Functions do not necessarily make code shorter (at first)!
Small, cohesive units are much better than…
… a customized behemoth!
Choose:
The objective is for you to ‘see’ your code!
Yellow denotes scripted, unstructured code (basic, sequential lines of instructions)
Purple denotes functions or other structured code (e.g. for-loops, conditionals, etc.)
Green denotes comments (or comment blocks) (consider combining this with yellow for heavily commented code)
Again, make notes in your code (#TODO
!) if you see:
What can you learn from your colleagues today?
You have visualized your code. Use your findings to improve it!
Preferably: take scripted code and turn it into a function, or split an existing function into two or more functions.
If there is no function to work on: try and address the readability of your code.
However: for future exercises you will need at least one function, preferably with parameters, in your code! For example:
Workshop Computational Reproducibility