Thursday, July 22, 2010

Please don't scribble.... Its all about Keeping the code clean

I was travelling to the past... don't mistake me... I really don't have a time machine... it was only with the help of my memories....

Just went to 1992.... Should be 1992.. I remember my mom was waking me at morning 5:30 for an exam.... Hmmm.... Thats too early.... I still remember how sleepy I felt... She was asking me to write something and I was scribbling.

Mom: Don't scribble (Bang..... a nice hit on my head).
Me: zzzzzzzzzzz........................zzzzzzzzzzz

Mom: Wake up.... Don't sleep....... you have to score first in the class......
Me: I feel sleepy.... What do I want to do now......
2000.... At the school...
Me: Teacher... All my answers are right... but why have you reduced 1 mark per question?????
Teacher: Don't scribble.... Just present it well to get full marks....

Me: Why do I want to present well, the test is conducted to know that I have learnt it or not.....
Teacher: Ofcourse you are right... but, how can I know that, if I cant read what you have written???

Me: ??????
Teacher: Writing neat is a good practice.. it helps you to read your answer paper at any age. May be now you can read your handwriting but when you grow you may not be able to read the same. So it is better to write neat that you can read it for ever in the lifetime.

2002.... I am not sure about the date..... At NIIT , Coimbatore
Friend: Sujai...... Could you please help me in debugging this problem.
Me: Yes... Let me look in to the code....

After looking at the code....
Me: ??#?#%$#^$%^$^$^&%&%&
Me: Friend, I think you need to write the code better..... because I can't read it. Programing is not just making things work for now... but writing something that works for ever / maintainable for ever.
2008... Started my career as Software Engg.

Kari: Sujai.... write the code cleaner. It is always now or it will never happen again.
Sujai: mmmm.....

Kari: Give a space... here.. Why are the variables not understandable......

Yes, I started to learn it better, Still learning to write a neat code, This will evolve for ever...
Through out the career, I have observed more scribbled code, than neat code. It is because, most of us are not evolving further, after we start our career. We think of making things work on a rough go and finally we scribble to write faster code. Writing a clean code needs time in the early stages to practice but later you are in the same speed, like the rest.

Anonymous: Hey.... Stop saying about clean code.... What do you say it clean or neat..
Me: Basically, a clean code is a code without bug... but am not mentioning that.. I am saying how to write a code that looks beautiful / readable for anyone.

The industry comes up with too many standards to write a good code. I just want the PHP developers to follow one among them rather than scribbling.

Few known standards

We have too many standards... Ofcourse we have our own standard. But it is better that our standard matches the existing standards...

I would recommend to use Zend Coding Style, it explains better than others. I have my own style, which mostly meets to all the above standards with very few changes..

Question: Hey I have a team, How can I insist this to everyone, Things will run out of control. We can't have someone dedicated to look into the code of everyone to keep it better...
Answer: Use phpcs (php code sniffer). Set a standard on your own or derive from industry standards, finally ask everyone to pass the phpcs validation, before they deliver the code.

The coding style is like a handwriting for a person, which need to be readable and understandable by everyone. Don't try to make your coding style unique... try to to match to the standards so it would be easy for everyone to follow when they need to continue your work.....

Hope, This post explains, why and how(refer to the links above) to write a clean code...

Soon, An article on how to use our own phpcs standards and use it in the team will be posted, keep reading.


  1. Well said.

    I think developers scribble and do not want their handwriting to look good because there is a saying, "if the handwriting is good, your fate is not good" ;-).

    I could not agree more with Sujai on this point. Since, clean coding is not practiced as a habit these days. We have come across many examples to prove this, but our client won't happy if we refer the same in the forum. So, the developers must chant a mantra "Keep the quality bar green, Keep the code clean". This must become a practice in every developer, and going away from this practice should make the developers feel uncomfortable.

  2. What happened sir, we were waiting for you to undress the PHP more so that we can become horny by seeing half naked PHP, what is going on?

  3. Sorry Kari, Not able to reply.... for a long time.

    I agree that going away from neat code should make the developer feel bad.

    But Clean code is not always away for quality it is part of quality.

    Keep the Quality bar green - Clean code is part of it. Quality is not just in output. It is also how we do it. Just a well painted building is not a quality building. How each brick is placed plays a major role. Clean code is one such thing. If we write clean, we find the bugs faster, we transfer the knowledge faster. The application will be maintained easily. Easy maintenance is part of Quality too.

    If quality bar in a continuous integration is available. It is surrounded by several validations.
    1. Syntax (No Compilation Errors)
    2. Code Sniffers (Optional for many / Mandatory for a good product)
    3. Unit Tests

    Quality is not complete, without clean code.