Coding best practices
There are code standards and conventions available depending on the language you are using and sometimes also conventions adopted for specific collaborative projects. These can be quite complex and out of scope if you are writing a code for your analysis, however there are a few things you can do to make your code much more readable and safer from bugs which are quite simple. In the video linked below, kindly provided by DataTAS, the presenter gives some useful tips which can be applied to any language:
It is worth watching the video (the actual presentation is about half of the video ~35 minutes) to understand fully how valuable these tips are and also to get a perspective from someone who went from a science background to a commercial software engineering position.
Below is a list of best practices discussed in the video.
- Use descriptive names for variables and functions
- Use consistent naming across the code
- Avoid hard-coding values
- Initialising variables
- Use functions to organise your code
- Don't Repeat Yourself (DRY) code
- One statement per line
- Write explicit code
- Keep your files a reasonable length
- Clear flow: try to have only one exit point in a function
- Test important parts of your code
Writing Reusable Code
There are many definitions of reusable code, and the details often depend on the use to which the code is being put. From wikipedia
The key idea in reuse is that parts of a computer program written at one time can be or should be used in the construction of other programs written at a later time.
Why write reusable code? This excellent article articulates what it takes to transform code into a fully fledged scientific contribution, and reusability is a key component:
Making your program reusable means it can be easily used, and modified, by you and other people, inside and outside your lab
The process of making your code reusable will also make it better, less error prone, saving time and increasing productivity.
How do you write reusable code? There are levels of reusability, from re-using your own code to a fully published library/module for others to use. Start with the basics and with experience add more reproducible practices. If you're already doing the basics, try some of the intermediate or advanced ideas.
The basics of code reusability involve easily readable code and DRY (don't repeat yourself) principles, so using functions/subroutines/procedures to avoid copying blocks of code and modifying each block. The tips in the introduction can all help with code reusability as well as those listed below.
Ten tips for writing readable code (based on PHP but principles are universal):
A good (python specific) section of a course from Software Carpentry about writing functions:
For the beginner python programmer it can be difficult to know exactly how to go about reusing code, how to organise it and import it into your notebooks or programs. This is a short, clear article about the python specific details on reusing your code:
In a similar vein (and also python specific), how and when to use a main function in python:
Once code style, readability and DRY principles hace been mastered the next step is improving what you're already doing and using the more advanced language features.
This is a really nice and clear (FORTRAN specific) explanation of how to move from a purely procedural approach, explaining progressively more advanced features of FORTRAN functions and subroutines, and finishing with a real-world scientific example program:
Unfortunately the above link is to a book (Modern Fortran) only part of which is freely viewable. Ideally the book may be available institutionally, but if there is an equivalent freely available link let us know.
To see the advice above put into practice, you can watch this video. It is a bit long (18min). It is under the Advanced resources because the code it is using is written in Rails. Not many of you will have experience in Rails but the purpose of the video is to talk about the principles and not the specificity of the code. It also involves some more advanced concepts such as classes and modules.
Documentation plays a critical role for code reusability. Any effort to document code is worthwhile and will improve reusability, but it is likely a large effort in code documentation will only be made in advanced code reuse scenarios, like a published module or library. In that scenario this is an excellent introduction primarily about taking into account the audiences for different aspects of code documentation:
- pep8 Python Enhancement Proposal
- Python reserved keywords