Best practices

Familiarise yourself with the following best practices for coding.

The more code you write, the more experienced you will become and the easier it will be for you to avoid mistakes. After a while you will notice that certain patterns emerge that help you to tackle a certain class of problems. However, there are some best practices that you should adopt right form the start to help you write clean code and avoid bugs.

Comment your code

Commenting/describing your code helps you to keep track of what you have already coded. It also helps you and other people understand why you wrote certain lines of codes when you look at your code after some time has passed or when somebody else tries to replicated your results.

Give clear object names

Give your variables and functions names that describe well what the variable contains or what the function achieves.

Keep code easy and readable

"Think before you type". Take some time to properly think about what is the easiest way to solve the problem you are facing. Evaluate different options and pick the one that is the simplest solution. This will make your code much easier to understand.

Start simple

When writing complex programs it is important to break the problem down into easier problems that you can solve quickly. Write the code for the simple and easy version of the problem, test it extensively, then tackle the more complicated problem.

Make it specific

Write a separate script for tackling different problems. To avoid repetitions, write functions and store them in a location that can be accessed by all your programs.

Make it easy to replicate

Make sure that your code can be understood and run by other people. This involves keeping all files and resources that belong to a project in one common folder (potentially with subfolders) and testing whether you can run your code on a different computer or when it is saved in a different location. This is especially important when handing in coding assignments for your courses!

DRY: Don't repeat yourself

Avoid repetition of code statements with minor changes. Use loops and functions to automate repetitions.

Last updated