How Do You Define “Good Code” ?
Tags: programming, softwaredevelopment
Published: 2024-01-29 21:57:33

---
title: How Do You Define “Good Code” ?
published: true
date: 2016-12-23 01:07:17 UTC
tags: programming,softwaredevelopment
canonical_url: https://developerzen.com/how-do-you-define-good-code-c8a383c207a4
---

![](https://cdn-images-1.medium.com/max/1024/1*1okxJmZ6yUyxeVhhZduR2A.jpeg)

I was on a phone interview the other day where I was asked for my definition of “Good Code”.

The first thought that came to mind was _maintainability_ — if it can’t be understood, maintained and extended by other developers than its definitely not good.  
 Then, other things came to mind: _efficiency_, _elegance_ (simple, proper use of language constructs and environment capabilities), _modularity_, proper _object-oriented design_, …  
 Of course, and we tend to take that for granted, it also has to work… without errors, security holes, etc.

In his book, [Code Complete](http://amzn.to/2hYoeJE), Steve McConnel supports my definition of good code as maintainable code:

> _Another theme that runs throughout this book is an emphasis on code readability. Communication with other people is the motivation behind the quest for the Holy Grail of self-documenting code._

> _The computer doesn’t care whether your code is readable. It’s better at reading binary machine instructions than it is at reading high-level-language statements. You write readable code because it helps other people to read your code. Readability has a positive effect on all these aspects of a program:_

> Comprehensibility  
> Reviewability  
> Error rate  
> Debugging  
> Modifiability  
> Development time — a consequence of all of the above  
> External quality — a consequence of all of the above

> _Readable code doesn’t take any longer to write than confusing code does, at least not in the long run. It’s easier to be sure your code works if you can easily read what you wrote. That should be a sufficient reason to write readable code. But code is also read during reviews. It’s read when you or someone else fixes an error. It’s read when the code is modified. It’s read when someone tries to use part of your code in a similar program._

> _…_

> _Making code readable is not an optional part of the development process, and favoring write-time convenience over read-time convenience is a false economy. You should go to the effort of writing_ **_good code_** _, which you can do once, rather than the effort of reading bad code, which you’d have to do again and again._

On the other hand, Paul DiLascia, from MSDN’s _{END BRACKET}_ column, [provides a list of traits](http://msdn.microsoft.com/en-us/magazine/cc163962.aspx) that good code should have:

> _Whether you code in C/C++, C#, Java, Basic, Perl, COBOL, or ASM, all good programming exhibits the same time-honored qualities: simplicity, readability, modularity, layering, design, efficiency, elegance, and clarity._

> **_Simplicity_** _means you don’t do in ten lines what you can do in five. It means you make extra effort to be concise, but not to the point of obfuscation. It means you abhor open coding and functions that span pages. Simplicity — of organization, implementation, design — makes your code more reliable and bug free. There’s less to go wrong._

> **_Readability_** _means what it says: that others can read your code. Readability means you bother to write comments, to follow conventions, and pause to name your variables wisely. Like choosing “taxrate” instead of “tr”._

> **_Modularity_** _means your program is built like the universe. The world is made of molecules, which are made of atoms, electrons, nucleons, quarks, and (if you believe in them) strings. Likewise, good programs erect large systems from smaller ones, which are built from even smaller building blocks. You can write a text editor with three primitives: move, insert, and delete. And just as atoms combine in novel ways, software components should be reusable._

> **_Layering_** _means that internally, your program resembles a layer cake. The app sits on the framework sits on the OS sits on the hardware. Even within your app, you need layers, like file-document-view-frame. Higher layers call ones below, which raise events back up. (Calls go down; events go up.) Lower layers should never know what higher ones are up to. The essence of an event/callback is to provide blind upward notification. If your doc calls the frame directly, something stinks. Modules and layers are defined by APIs, which delineate their boundaries. Thus, design is critical._

> **_Design_** _means you take time to plan your program before you build it. Thoughts are cheaper than debugging. A good rule of thumb is to spend half your time on design. You need a functional spec (what the programs does) and an internal blueprint. APIs should be codified in writing._

> **_Efficiency_** _means your program is fast and economical. It doesn’t hog files, data connections, or anything else. It does what it should, but no more. It loads and departs without fuss. At the function level, you can always optimize later, during testing. But at high levels, you must plan for performance. If the design requires a million trips to the server, expect a dog._

> **_Elegance_** _is like beauty: hard to describe but easy to recognize. Elegance combines simplicity, efficiency, and brilliance, and produces a feeling of pride. Elegance is when you replace a procedure with a table, or realize that you can use recursion — which is almost always elegant:_

```
_int factorial(int n) {   
  return n==0 ? 1 : n \* factorial(n-1);   
}_   
```

> **_Clarity_** _is the granddaddy of good programming, the platinum quality all the others serve. Computers make it possible to create systems that are vastly more complex than physical machines.  
> The fundamental challenge of programming is managing complexity. Simplicity, readability, modularity, layering, design, efficiency, and elegance are all time-honored ways to achieve clarity, which is the antidote to complexity._

> _Clarity of code. Clarity of design. Clarity of purpose. You must understand — really understand — what you’re doing at every level. Otherwise you’re lost. Bad programs are less often a failure of coding skill than of having a clear goal. That’s why design is key. It keeps you honest. If you can’t write it down, if you can’t explain it to others, you don’t really know what you’re doing._

So what are the most important trait for “Good Code” ?  
 Later on, it struck me — like anything when it comes to engineering, its about **balance**. When we write code we strive to find balance between complexity and simplicity by constantly evaluating the different tradeoffs we have to choose in order to get there.

Therefore, _good code_ is code that _strikes the right balance balance_ between all of the qualities mentioned above.

Think about it the next time you’re writing code or reading someone else’s…

_Originally published at_ [_www.developerzen.com_](http://www.developerzen.com/2008/06/26/how-do-you-define-quotgood-codequot/) _on June 26, 2008._

* * *