Archive for the ‘design’ Category

Smells to refactoring

Although “smells to Refactoring” is hosted on java.net, the suggested practices within this document are to be applied with any Object Oriented Programming (PHP, Java, Ruby, .Net, …). Every programmer must read it.

Long method is probably one of the most common code smell. Below a list of the troubles a 200 lines method can create :

  • it is hard to understand and hard to maintain. Mostly when the only comment can be found at the 130th line and looks like : “POST section”. Thank you very much.
  • it is so specific, it is totally impossible to use it in another context. Furthermore, it quickly becomes a copy/paste repository as some code blocks are closed to be generic snippets.
  • It is probably doing too much things. This statement becomes obvious as soon as you need to modify some behavior.
  • it is an open door to security issues. For two reasons: first, a large piece of code often comes with a high complexity, which is suitable for hard to find vulnerabilities. Then, it is easy to write a clean and secured method with only 15-20 lines, although the same care cannot be applied to a 200 lines method.

This kind of mistake can be easily avoided with a good conception prior to the coding process. Read the entire document, and you will probably conclude by yourself that most of the listed code smells are the aftermaths of a poor design.