Geeks With Blogs

News


Dylan Smith ALM / Architecture / TFS

I've been thinking about what makes one chunk of code "better" than another. And in general what qualities do I look for and focus on when writing code.

Personally I believe that the 2 most important qualities of good code (in order of priority) are:

  1. Meets customer requirements
  2. Maintainability

Now those are pretty broad qualities, but I think I can break it down a little further. How do I focus on meeting customer requirements? Well I believe there are 2 major facets to this, in order of priority:

  1. Verifiable Requirements - This usually means executable requirements in the form of acceptance tests. This provides us with a verifiable way to determine if our implementation meets the requirements as stated, whether they be functional or non-functional requirements. However, it doesn't address the issue of inaccurate requirements, thats the purpose of the next area of focus.
  2. Short Feedback Cycle - When implementing an application there should be a strong focus on shortening the feedback cycle. This means frequent releases, and high-quality releases. This enables you to identify inaccurate requirements and complements the Verifiable Requirements focus. A large part of accomplishing this lies in maintaining a high-level of quality such that it is always as close as possible to release-ready. This typically involves techniques such as continuous integration, aggressive refactoring, and comprehensive unit tests.

How do I define maintainability? I see it as the combination of 2 sub-qualities, in order of priority:

  1. Testability - I believe that testability is the biggest factor in determining whether code is maintainable. Whether you have an existing test suite or not is a separate issue, but when evaluating the implementation code itself testability is key. This means using principles such as separation of concerns, dependency injection, the law of Demeter, loose coupling, etc.
  2. Simplicity - After testability the next most important factor in determining maintainability is simplicity. Assuming you have multiple choices for how to implement a feature, and they are all equally testable, the simplest solution wins.

Obviously just stating that the simplest solution is best is a little vague. So lets break down what I mean when I say "simple". I think that simplicity is determined by 3 factors, in order of priority:

  1. Separation of Concerns - Each piece of code should have one and only one well-defined responsibility. This allows you to easily isolate pieces of logic for testing. It also helps lead to a set of classes that has high-cohesion and loose-coupling, an important set of design principles.
  2. Minimize Duplication - Also commonly called DRY (Don't Repeat Yourself), this demands that you employ aggressive refactoring to drive out duplication in your implementation. Duplication occurs in many forms, and spotting all forms of duplication is a skill acquired with experience. Nonetheless, minimizing duplication will result in a simpler solution that is ultimately more maintainable.
  3. Minimize # of classes/methods - Assuming you've first focused on separating the concerns and minimizing duplication, the last factor you should consider is minimizing the # of classes/methods while still retaining the previous 2 qualities. This also falls under the YAGNI (You Ain't Gonna Need It) principle, since if classes/methods don't help to separate concerns or reduce duplication, and the current customer requirements can be met with less classes/methods, then you should use the solution with the fewest moving parts.

 

So to summarize, I believe that the hierarchy of qualities that are important for code (in order of priority) is:

  • Meets Customer Requirements
    • Verifiable Requirements
    • Short Feedback Cycle
  • Maintainability
    • Testability
    • Simplicity
      • Separation of Concerns
      • Minimize Duplication
      • Minimize # of classes/methods

 

How would you define "good" code?

Posted on Tuesday, May 1, 2007 5:44 PM | Back to top


Comments on this post: What Makes Code "Good"?

# re: What Makes Code "Good"?
Requesting Gravatar...
Very nice, concise, "good" post.
Left by MattH on May 03, 2007 2:38 PM

Your comment:
 (will show your gravatar)


Copyright © Dylan Smith | Powered by: GeeksWithBlogs.net