Making Good Software

A blog by Alberto G (Alberto Gutierrez)

Written by Alberto Gutierrez

March 2nd, 2010 at 5:22 pm

Robustness, the forgotten code quality.

with 4 comments

From among all the qualities that make good code, robustness is one of my favourites. Robustness can be explained as a measure of how hard is to break someone’s code. The opposite of robust code is fragile code; an example of fragile code would be having a class with all its member variables marked as public.

My concern with robustness is that is usually overlooked, is not placed as a priority for some developers, which in my opinion is a mistake. The main problem with fragile code, is that for its correct behaviour, it depends on how the code is being used, and that, in my experience, is synonym of bugs.

The benefits of having robust code are many, some of the most important ones are:

  1. Easy to change. Robust code is easier to change because external factors don’t affect its behaviour .
  2. Less bugs. The more robust the code is, the less combinations that cause a bug in the code.
  3. Better integration. Robust code is predictable, so from an integrator point view, is always going to have the same behavior.
  4. Consistency. Repeat this with me “Robust code, works fine no matter who, when or how it gets called”.

To help spot fragile code here there is a list of common fragile code patterns.

  1. Public getters / setters / fields.
  2. Bad equals / hashCode / compareTo methods.
  3. Code duplication.
  4. Use of plain constants when an enum should be used.
  5. Not providing with any factory or build pattern for complex objects.
  6. Dependency on global variables.
  7. Coding against implementations and not interfaces.

One of the things that upsets me from modern frameworks is that they push you to create non robust code, they sometimes force you to have default constructors, getters and setters for all instance variables… but there’s no much we can do about it, and sometimes the advantage of using them is worth the hacking, but, from my point of view, fragile code shouldn’t be accepted in any other scenario.

Test code deserves a special mention. Test code is usually seen as not as important as production code, and this causes lots of developers to write very fragile tests. I am sure that anyone who has work in a large project using automated tests will absolutely agree with me that there is nothing more tedious than changing 10 lines of test code for each line of production code that changes, and that can be prevented with robust test code. So when writing test codes, please keep in mind that you have to care for it just as much as if was production code!!!

Update

Find in this article the 10 characteristics of a robust application/service.

4 Responses to 'Robustness, the forgotten code quality.'

Subscribe to comments with RSS or TrackBack to 'Robustness, the forgotten code quality.'.

  1. I added a preprocessor directive called #assert to my compiler. In school they had such a thing for asm.

    #assert is an excellent way to make robust code. It prints a warning if something is not true.

    My source code has active links, like html. I put links to other locations which must be updated if something is changed.

    Out-of-date comments and documentation are a nightmare–you should try to solve that for robust code.

    Terry A. Davis

    4 Mar 10 at 4:11 am

  2. [...] uma arquitetura mais adquada para sites sem usar EJB e aumentando as capacidades de teste e robustez das aplicações. Algumas pessoas pediram um exemplo mais concreto desta arquitura. Porque isto [...]

    Anonymous

    25 Apr 10 at 6:11 pm

  3. You need to take part in a contest for among the finest blogs on the web. I will advocate this website! J”OF( diet pills that perform n’#zY

    kingninggand76

    26 May 11 at 5:59 pm

  4. Excellent article. Thanks for expose your point of view about Robust and Fragile Code.

    John Ortiz

    3 Feb 12 at 6:13 am

Leave a Reply