Tuesday, February 18, 2014

Writing clean code and following the standards

What does "writing clean code" and "following the standards" mean?

You might wonder what does "writing clean code" mean, well it means that as a programmer you write code that can be easy to read, understand, modify and test. Also what I mean by "following the standards" is that we should learn and apply the rules and conventions of a programming language, team and community.

For example Oracle offers the coding conventions for Java or Microsoft offers the coding conventions for C#. Another example would be that if you would like to contribute for developing a framework like Symfony2 you must also follow its conding standards.

Why should we write clean code and follow the standards?

In our days most of the real world applications need continuous development and even small programs require to be changed or improved after a while. Sometimes we often share pieces of code or entire applications to help others. Also from time to time we need to update or fix code we wrote a long time ago. As a result of all this we often spend our time reading code, understand what does it do and we will be required to modify it. That is why writing clean code will help us to deliver quality code which others can understand and can update it to their needs, we will be able to fix bugs and add new features or update the code faster.

Examples of why it's important to write clean code


Nowadays applications get bigger and are used by a lot of users everyday, many of them offer a lot of features and rarely you are the only programmer working on an application, often you will work in a team or even sometimes several teams work on an application, each working at one of its module. You will often need to read and fix a piece of code wrote by one of the other team members which is in vacation, imagine how hard it would be if the code would have been written in chaotically? Maybe you would think that it would have been faster to rewrite the whole thing again? But no investor is willing to pay you to rewrite a piece of functionality again, maybe you would not write it better than the previous team member. Now imagine how easy it would be for you to change the code if it was written clean and easy to understand, even more it was written by following the coding style and standards on which the team agreed at the beginning of the project? It would be a lot easier and you would not need to suffer just because you need to do a bug fix.

Another example of why we should write clean code and follow the standards which I've experienced was that I was assigned a job to update a relative small PHP script designed for interacting with twitter, even if I am experienced with PHP I was unable to understand what did the code do because the code was written chaotically, half of its naming was in the native language of the previous programmer, it was not in English which is the common language used for naming variables, methods, classes, folders etc. The files had between five hundred and one thousand lines of code, variables were not suggestive(at least ones which were named in English). It would have been easy for me to update the script if it was written clean, had meaningful names, if it had been divided logically into modules.

Resources

There are a lot of resources out there which will help you start writing clean code, one of my favorites is a book called Clean Code: A Handbook of Agile Software Craftsmanship written by Robert C. Martin in who is a promoter of writing clean code. 

For following the standards of a programming language in which you write code you should consult the official coding conventions, also if you use a framework you should check out what is the standard coding style on top of which it was built, in general it should be the same of it's related programming language. Besides that a framework often has a standard way to organize classes and other resources, this is one of the many advantages of using a framework, that other programmers can understand the code easier.

No comments:

Post a Comment