views:

975

answers:

12

Does anyone have a reasonable definition of Code Quality? I don't think there is a good definition, and I see plenty of bad definitions. There are many definitions for Correctness, but I would expect correctness to be a given, and quality is meaningless if the code is broken.

I suspect quality is something like:

correctness               AND spec-conformant AND
clever                    AND not-too-clever  AND
simple                    AND not-too-simple  AND
uses-sensible-algorithms  AND
does-not-reinvent-wheel   AND
uses-appropriate-language AND
lacks-bad-smells          AND
concise                   AND
clear                     AND
well-documented           AND
with-good-performance     AND
elegant                   AND not-baroque

but that's just hopeless. Does anyone have any better definitions?

+23  A: 
Dan Herbert
That was hilarious, thanks for sharing
Otávio Décio
Very good. I agree - rating it would be impossible. But I like to think about it.
Paul Beckingham
+4  A: 

You can punt and say "high quality" == "meets requirements".

Then begin to define what the requirements are: e.g. functional, cheap, reliable, accessible, ...

Doing this would tie "quality" to the requirements, and to the context: to the properties which are seen by, measured by, and important to the stake-holders (which may include but is certainly not limited to the programmers).


Note that "cheap", as a quality, correlates with other measures such as "does-not-reinvent-wheel" (in the OP), and, with "maintainable" and with "elegant": "inexpensive" I think is a consequence of quality in the design, and is a (measurable) quality in its own right.

Similarly, "fast" is also a measure of quality.

Some people might say that "fast" and "cheap" are not measures of software quality: for example http://en.wikipedia.org/wiki/Project_triangle#Example suggests that they are antithetical to quality ... but my idea of "quality" is informed by http://en.wikipedia.org/wiki/Zen_and_the_Art_of_Motorcycle_Maintenance so I see "quality" as more central, as something which informs people's perceptions of every aspect of the thing.

ChrisW
True, but a total cop-out. I'm not sure cheap has anything to do with quality. It may be desirable, but isn't it orthogonal?
Paul Beckingham
It's not a cop-out (it's quality as measured by the stake-holders: customers, users, etc.). Also, "cheap" correlates with "does-not-reinvent-wheel" in the OP, and with "maintainable", and with "elegant" ... it's a consequence of quality in the design, and a (measurable) quality in its own right.
ChrisW
+1  A: 

What makes a movie good? What you like and what I like may differ vastly, but that doesn't mean that even if I don't like the movie isn't a movie of quality. To determine if a movie is good or not, we often turn to a trusted critic to give us advice. Roger Ebert comes to mind.

To determine if code is good or not, we do the same thing. I, and many other programmers, turn to Steve McConnell. In Code Complete 2, he has an entire chapter on software quality. His words there exceed the quality and completeness of any answer you'll get here!

Get Code Complete to a) understand the metrics of software quality and b) for a thousand other reasons!

rp
I have Code Complete. In fact, I bought it twice because one was stolen (I hear that's quite common). I want to know what the SO community thinks, in addition to what S.McC thinks.
Paul Beckingham
+13  A: 

My definition of code quality is LTFCE, based on the notion that code is literature. Over its lifetime (maintenance, reuse, etc.) the code will be read many more times than written. So, good code is (in order)...

  1. Legible - The code (the code itself, not comments) should clearly state the intent. If the reader can't make sense of the code, than all other efforts are doomed to frustration if not outright failure.

  2. Testable - The code should be organized in a way that facilitates unit testing. That supports all subsequent efforts (refactoring for modification, correction of defects, revision due to changed specs, etc.)

  3. Flexible - Dependencies, both on other code in the code base and arbitrary implementation choices, should be minimized. Hard-coded assumptions about data size, concrete classes or data structures, etc. make the code more brittle, and therefore harder to reuse or adapt.

  4. Compliant - The code should comply with its requirements, functional and otherwise. (I don't state this as "correct" because the discussion about whether the requirements themselves were the "right" requirements is about the process or the environment, not about the code.)

  5. Economical - The code should make reasonable use of system resources: memory, CPU, etc. (I don't state this as "efficient" because that word is too often misused, by limiting it to a single aspect, such as speed. Economy is simply about return on investment, and requires thought about all the resources being invested and all the measures of return.)

The point of insisting that these be considered in order is that each property supports the ones that follow. For example, defects (failure to comply with specs) in code which is legible, testable, and flexible can probably be corrected with reasonable effort. On the other hand, I suspect that every programmer has had experience with code which was micro-optimized for performance to the point that it was too brittle for reasonable maintenance.

joel.neely
Hm LTFCE is a bad acronym. Try CLEFT or FLECT, you can at least pronounce them.
Gamecat
Minor point: LTFCE is an abbreviation, not an acronym. CLEFT is an acronym.
Paul Beckingham
@Gamecat: LTFCE emphasizes the order, which is significant per the explanation. Pronunciation is not required of other common abbreviations (e.g. CPU, DBMS, HTML, etc.)
joel.neely
@paulbeckingham: Per both Wikipedia and Webster's (http://en.wikipedia.org/wiki/Acronym_and_initialism#cite_note-WDEU-1) the initialism/acronym distinction is not common to everyday usage.
joel.neely
+2  A: 

high quality code:

  • Easy to understand for fellow programmers
  • Easy to maintain for fellow programmers
  • Easy to use for target users
  • Works right
Jiaaro
A: 

Measuring code quality might be like measuring IQ. You can not really tell why someone is intelligent, but you somehow now it. For IQ, psychologists have made tests that more-or-less can tell your intelligence, but we still do not really know what intelligence is. Code quality is something similar to this (except we don't have tests), you can not really define good code, you know it when you see it.

bandi
+4  A: 

Good code makes you feel good when you read it. In other words, code quality is related to something that happens behind a programmer's eyes, not some external metric. It is subjective because different programmers have different expectations based both on the amount of experience they have and the tradition in which they were trained.

I wouldn't overemphasize correctness. Great code with 5 bugs in it is a lot better than crappy code that passes all known tests. Which one would you want to maintain?

The individual measures you cite are useful primarily as testable ways for improving code, not for evaluating it. For example,

  • If I simplify this code while simultaneously making it 20% faster, I will feel much better about it.

One of the benefits of code reviews is that you can start to develop group standards about what you feel good (and hence what you want to maintain).

Norman Ramsey
Not sure - if I looked at the source for two video drivers (ie something outside my sphere), I'm not sure my <i>feelings</i> would correlate with quality.
Paul Beckingham
I'm sure I wouldn't want my boss to evaluate the quality of my code based on his or her feelings. And really, correctness is the sine qua non of code quality; if it's complete enough to evaluate, it should be tested and debugged.
JasonFruit
@Jason: I read a lot of code written by students, and I stick to my guns. I've seen code that didn't work that I could fix in 5 minutes and happily maintain indefinitely. And I've seen code that worked but stunk to high heaven. Correct behavior is overrated; tomorrow, the spec will be different.
Norman Ramsey
@Paul: If you're not qualified to read the code, you're probably also not qualified to judge its quality. @Jason: same goes for your boss.
Norman Ramsey
You said, "Great code with 5 bugs in it is a lot better than crappy code that passes all known tests. Which one would you want to maintain?" But OTOH, which one would you want to run? Your POV seems to be centred on the code's developers and maintainers, ignoring its customers and users.
ChrisW
@Chris: Yup. Customers and users don't know what code quality is and won't pay for it. Only developers care about quality. Rant at http://stackoverflow.com/questions/347584/why-is-software-quality-so-problematic#348563
Norman Ramsey
+2  A: 

Quality code for me ==

  1. Meets or exceeds requirements.
  2. Does not anger the customer.
  3. Does not anger their customers (if they happen to be the end-user).
  4. Does not anger future developers (including myself) who have to go in and maintain my code (or at least minimizes the anger to a few unkind words).
Andy Webb
+1  A: 

I like the definition/explanation which appears in the first chapter of Object Oriented Software Construction, 2nd Edition (Bertrand Meyer; Prentice Hall, 1997).

The real interest is in the long story, but just for the sake of summarizing, quality is composed of several factors:

  • Correctness
  • Robustness
  • Extendibility
  • Reusability
  • Compatibility
  • Efficiency
  • Portability
  • Ease of use
  • Functionality
  • Timeliness
Daniel Daranas
A: 

In most businesses, code quality is defined in dollars.

Max
-1: Too vague. Please explain.
Jim G.
+1  A: 

Code quality begins with the language in use. Languages are tools and have strengths and weaknesses. So any definition for code quality must consider that. In particular, quality code exploits the strength of the language while exhibiting minimal dependence on its weaknesses.

Having established that, we then can see that the skill of the coder is really what we are looking for in this metric.

This means that money has no place in this metric. Some of the best code I’ve ever seen never made a dime. It also means that the number of bugs in the code is not a good factor either. Consider that code may work exactly as the requirements dictate thus making the bug an aspect of the requirements and not the code. Conversely, code that flawlessly implements perfectly correct requirements can still be of low quality, so correctness or lack of bugs can only be part of the definition.

This leaves two other things to consider in making our judgment, ease of maintenance and performance when executing.

Ease of maintenance is harder to judge, but it has to do with the difficulty in reading and understanding the code (that, of course brings in another variable, the skill of the reader) that might best be measured in time. It also has to do with the difficulty in changing the code. After all, code that is engineered well anticipates extensions to some degree. So forethought in design that enables changes without whole scale restructuring makes for higher quality. We all have faced the prospect of altering code significantly only to find that we are better off with a complete rewrite.

Performance in execution is more than just speed. It means writing code that is not redundant, as in setting values that remain unchanged within loops rather than outside the loop. The comment above about efficient use of resources is very good as well.

yetanotherdave
A: 

NDepend documentation comes with some pretty cool and advanced online blog posts, articles and white books concerning code quality of .NET code:

Fighting Fabricated Complexity

Layering, the Level metric and the Discourse of Method

82 code metrics definition

Code metrics on Coupling, Dead Code, Design flaws and Re-engineering

Patrick Smacchia - NDepend dev