By Phillip Brooker, an interdisciplinary researcher in the field of social media analytics and course instructor on Introduction to Python for Social Scientists.
When writing code you’ll probably, at some point, want to reuse that code and maybe have someone else be able to read it and use it. So it is important to make sure that the code is readable for both yourself and others. You can achieve this by bearing in mind the following 3 things:
1. Adding comments and ensuring readability
We can add comments so that features of your code can be explained, however, it is important not to add too many. Assume that the person reading the code has a basic knowledge of the program. The main thing is that comments should be informative and useful to a reader, without overly getting in the way of the code itself.
If you have repeated lines of code then you may want to think about having functions. This will mean you won’t have to keep on writing the same thing over and over again.
If you’re building tools which you want to re-use or share with others, then using classes can help you do that. Classes can be used to set out ‘blueprints’, which can help you do things like standardize and create some consistencies around datasets that you intend to use across multiple purposes. It might be tricky to learn how to work with classes, but it definitely helps to have that knowledge as you get into increasingly complex computational tasks.
There are many different things to consider when writing programming or code, however, it is most important to make sure that it is both readable and usable. It is also important to make the code as streamlined as possible while still being accessible to anyone who is going to read it.
If you're interested in learning more about Python take a look at Introduction to Python for Social Scientists.
Do you want to read more posts by Phil, or to find out more about Python? You might like these: