Protect the user:
Make assumptions and expectations explicit.
Produce errors when expectations are not met.
Consider error options, and perform error management:
Protect the developer (you!)
Test the expected behavior of your functions:
Capture unexpected errors to identify further options for error management
You can automate running tests when pushing to Github using Continuous Integration
Tests are definitely worth learning when your project increases in size!
More on tests later…
. . .
Why not simply adjust the function output?
. . .
Because it is unclear if None
is expected behavior or indicative of a problem.
An error breaks code execution
. . .
Capture the error but release a warning
If you do not want to interrupt your script when an error is raised: use try/catch (‘except’ in Python). NB: Note that Python allows you to distinguish by error type!
Compare:
and
Consider early statements in the script to validate (data) input.
With if/else (Python):
With try/catch (R):
Expect the worst:
Identify assumptions in your code
Make the input/data assumptions explicit
Test the input for a function
Workshop Computational Reproducibility