Few steps to improve the quality and the readability of your code
Writing a clear code can be a challenge for many developers. What mistakes make our code incomprehensible and what we can do to avoid them? Let me share with you a couple of tips which helped me to improve my coding skills.
1. First think, then write
It’s common to start coding the moment the first idea comes to your mind. You start writing, before thinking more globally, widely about your code, before taking in consideration other parts of your application. There is nothing wrong with this approach as long as you follow rules of keeping your code clean and you don’t end up by rewriting the whole logic.
My advice is simple. The moment you know what you want to create, stop for a moment and think. Some idea came to your mind suddenly? That’s great! Now try to find gaps in your first concept or to come up with a better solution. It will save your time later if you rethink everything before starting to code.
2. Think about others – syntax and comments
Remember that not everything which is clear for you is also understandable for other people. That’s why the syntax of your code is very important. Try to give your variables, functions, and methods more meaningful names, so not only you but any other developer reading the code understands what a function does before checking its content, just by looking at its name.
Sometimes writing meaningful names is not enough and our code is still not as comprehensible as it could be. In this case, it would be great to add comments with some explanation. Just remember, you don’t write a story! Try to keep them short and understandable. Add comments to parts which are not clear at the first glance and need further explanation.
3. Review your code and try to take the most from code reviews
There are many ways to learn, but is there a better way than learning from others? Code reviews give you a chance for that in particular.
Pay attention to remarks of other colleagues and remember that all code reviews are there to make you become a better developer, not to criticize you. Listen carefully to what they have to say, agree or disagree and discuss. It will bring you many benefits!
It’s also really important to review a code by yourself. After writing the last semicolon, check everything again and remember that there will be always something you can improve.
Probably everybody heard about DRY. For those who don’t remember or don’t know what it means: DRY it is a software development principle which stands for “Don’t Repeat Yourself”. It’s a good practice to reduce repetition in your code and create a reusable section. For example, if you have the same code in 3 different places, you should abstract it into one common method, variable etc., so that it can be used in other parts of your app.
5. Single responsibility
What can create a real mess? A function which handles many things. Why? Because in these functions anything can happen.
First of all, they are hard to name, secondly, you will probably end up by writing comments to keep track what the function is responsible for, not mentioning a debugging process of that unpredictable filed. My advice: if you find out that it’s hard to name your function properly or you end up by writing various instructions, split the logic. Let one function perform one task.
6. Test your code
Testing is as important as each feature in your application. Writing good tests shows if you understand your code, what it does or how an application changes with different user’s interactions. Tests can really save your time, even if you have to spend it to write them.
How many times did you say that you wasted all your day looking for that tiny bug in your well-refactored code? Bugs happen and tests can help to find them.
7. Learn from your mistakes
Personally, I believe this is how we really learn and remember things in general. Once you made a mistake, it’s hardly possible that you will make the same one again. Don’t treat them like defeats, more like important moments thanks to which you become better!
8. Relax and take your time
Time works on your benefit. The more you code, the better you become. It works the same with everything else. If you learn a new language, participate in dance classes or try to improve your cooking skills. If you practice, then every day you become better and better. Although remember, give yourself time (break)! Don’t try to read 10 articles, books or code days and nights. Your brain needs to rest, to have those 8 hours to organize and memorize what you have learned. Please, remember that.
To sum up, try not to stress too much. After all, you reached this article, right? It means you are on a good way to put your coding skills on a higher level. When you write your code always remember that other people will work with it or that one day, maybe in a couple of months, you will come back and read it. That’s why it’s so important to give your variables, functions, and methods good names, to not duplicate chunks of code, but to write reusable ones and to learn from others. Therefore, take your time to learn and make mistakes!
Sources of images: