Code Quality (part 1)

Code Quality (part 1)

[dropcaps round=”no”]T[/dropcaps]his is our first post of a series about software Code Quality and a practical method to measure it.  While the quality of code may not determine your project’s success, it will certainly impact the cost of maintenance, reliability, supportability and updatability of the project.  View the second post to learn how to measure Defectiveness and Maintainability.  The third post talks about Supportability and Performance.

Poor quality code can leave your customers, developers and executives feeling like this;

One of the largest problems in software development is a lack of consistent quality, and no practical way to evaluate code quality.  We have all used the web or mobile application that just doesn’t work in numerous ways, not simply UI design issues, but error messages, slowness… crappy code.  How does one measure the quality of the code that has been developed?  There are many factors; Does it work?  Can the user get their results before they have to go to sleep? Is it a good architecture? Is the right design pattern used? Is the code readable?  And more…

There are a number of different facets to “Quality Code” and many books/blogs talk about various aspects of how to build quality code.  However, there are few reasonable methods of measuring code quality.  ISO has the ISO 9126/25010 standards, but like many ISO standards it requires a significant investment to implement, and is unreasonably complex for all but the most cash-rich teams (BTW, if you are on one of those, give me a call/IM/Text).

In this and future blogs I will attempt to describe a fairly comprehensive, yet practical definition of code quality from an overall project perspective.  I also propose a reasonably empirical method for measuring code quality that can be undertaken by most CMMI level 2+ organizations or SCRUM masters.  The main requirement is a decent QA team and defect tracking.  If you do not have both, stop now because you have much bigger problems to solve.

codequality

What this blog is;

It is a proposal on what defines ‘Quality Code’ and how to measure relative code quality.  It covers the most important aspects of measuring software quality, and from my experience should do a fairly reasonable job.  It is based on my 17 years of experience as a software development consultant, software architect and CTO.  It is a set of rules that require some subjectivity, but outlines a process to reasonably rate and compare code quality.

If you are looking for a complete breakdown of code quality into umpteen categories and sub-categories (ok, 37, but who’s counting), then I suggest reading ISO 25010.

What this blog is not;
equation

It is not a peer-reviewed scientifically researched formula or method.  It is not a discussion of architectural/design patterns and choices.  Nor do I delve into various static code analysis techniques, the validity of different code rules or comparative performance of developers.  It is not meant for government or mission-critical applications where people may die or 12 levels of reviewers need to justify their job with reams of paperwork/checklists.  It is not a set of absolute rules without any subjectivity, for even Einstein’s theory of relativity is under constant attack and questioning, so what chance do us mortals have?  While I would love to have a COCOMO II-type calculator or method to get an absolute rating of code quality, that would be overly complex.

Prelude

Let us take an abstract example of the most common code today; a responsive web page.

A responsive web page is one which knows your mood and responds appropriately… ooops, no, that’s not it.  It is a web page that ‘automagically’ adjusts to the device being used. Most responsive web sites are built such that there is no separate mobile/tablet site, the code adjusts on the fly… Well at least that is the theory. See GotGuru or Level9SEO for examples of performant, quality responsive websites.

It generally includes HTML5 + Javascript + CSS, and usually some kind of backend processing such as PHP/Perl/.NET/Java.  One of the more common problems I come across is the website that takes 10s+ to load, maybe it has 50+ resource files to load, large CSS files, etc.  In this case the page does not meet the quality guidelines for performance, according to a 2009 Forrester research study the average user will wait less than 3s for a web page to load.  Most likely it does not meet requirements for maintainability or supportability as they tend to be very fragile with a mixture of javascript, css and html combining to create a responsive design.  This is not to say a responsive website cannot be of high quality, but that many are not.

From a supportability perspective, debugging through 1000s of lines of javascript with today’s tools could be difficult, especially if the library is not structured well.  With events, anonymous methods, closures and so forth in javascript, it is easy to make highly complex, but hard to support libraries.  Add to that conditional CSS, and the tendency is to make complex websites that are difficult to support even for the most advanced developer.

There are similar issues from a maintenance perspective; the two are mostly correlated, but not always.  How many websites do you see that haven’t been updated in months/years?  You might have even been involved with the dreaded fragile website, nobody wants to update it because of fear it might break.  Even worse, it might break only for a specific browser or resolution/orientation.

By their nature, HTML+Javascript+CSS websites are quite complicated from a development, support and maintenance perspective.  Chrome, IE, Firefox, Safari on Win/Mac/iOS/Android across 100+ resolutions/orientations makes for a very complex development/testing/support environment.  If you are building a REST web service your testing matrix is tiny compared to a responsive modern web page, and for the most part the tools are much more advanced.

What is ‘Quality Code’?

I propose that code quality can be determined by a combination of the following attributes;

  1. Defectiveness
  2. Maintainability
  3. Supportability
  4. Performance
  5. Scalability

There are tools that try to do static code analysis and other tried and true analysis, but none of them really measure the entirety of the code.  After all, beautiful code that does not do what is needed or intended is not high-quality code.  Likewise, even if you spend the effort to get 100% code coverage in you TDD, it doesn’t guarantee that the code or the tests are actually correct.

Some of these things are often measured, others are not.  I will recommend the kind of metrics and processes required to gather the required data.  We will also discuss how to take the metrics and assign an overall rating of software quality, and compare quality between projects.

…Coming Up Next… What is “Defectiveness” and how to measure it