Supun Nakandala: Clean Code: Writing Code for Humans

_"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." - _Martin Fowler

Recently during my internship period at 99XTechnology I came across some leanings about clean coding practices. I think it is worth sharing some important facts about clean coding with you.

Three Principles of Clean Code

  • Right tool for the job
    • As software developers we have so many tools at our disposal. Selecting right tool for the job is very important. We have to recognize that every technology has it's place. 
  • High signal to noise ratio
    • In science and engineering SNR refers to the level of desired signal to level of background noise.  Clean code optimizes the signal and reduces the noise so that the reader can easily read the logic and understand the intent. 
  • Self-documenting
    • The main contributor to the code level documentation is not comment but good programming style. 

The right tool for the job.

Every developer has his/her favorite technologies. There is nothing wrong in  specializing and striving for expertise of a given tool. But a developer should not get too fixated to a tool, pattern or paradigm

When picking tools the risk of selecting wrong tool for the job exists in the boundaries. Consider the bellow set of technologies used for web applications. Each technology is used for a specific purpose

  • HTML - Semantic markup for the content.
  • CSS - Separates styling from the markups.
  • Javascript - Provides behavior.
  • C#, PHP, Python - Used for implement business logic.
  • SQL - Data storage, access and manipulation.

The boundaries between these technologies let developers in trouble.

In such a scenario some frequent bad coding practices that developers do as follows

  • Embed Javascript in html or generate HTML from javascript without having separate Js and HTML files.
  • Generate Javascript from server side programming language such as C#, PHP.
  • Storing HTML in SQL databases. Databases should contain only the row data.
  • Putting inline styles in HTML instead of a separate CSS file.
  • Generating dynamic SQL in server side programming language instead of using an ORM or stored procedure.

The bottom line is avoid using one language to write another language/format via strings. Eg Using strings in C#, Java, PHP etc. to create

  • JavaScript
  • XML
  • HTML
  • JSON
  • CSS

This ideas also called as "Staying Native". A good rule of thumb is to stick for a single language for a file. So if you have more than one language in a single file think about more different and elegant way of solving the problem.

link

Posted by SourceForge Robot 2013-12-03