Our unanimous winner is Jan Van Ryswyck of Belgium. Jan can look forward to his copy of Beautiful Code in the mail in the next few weeks as it will be going book rate Sorry, man. Low budget over here.
Elegant Code by Jan Van Ryswyck
What is elegant code? It might depend on who you ask. It’s kind of a subjective matter. Depending on the type of programmer, you get all kinds of answers. This short essay is my take on the subject. It’s a topic that is very close to my heart, although I believe I’m not able to produce very elegant code (yet).
In my view, elegant code has many characteristics. Here are some of these characteristics that I believe are important.
Elegant code adheres to object-oriented principles. We’ve all seen what a procedural approach does to a code base and its maintainability. Heck, there are even large tool vendors out there today that put out OO languages and preach software design that leans to typical Cobol. Good old plain classes: data and behavior that acts on that data. Code that reflects the Principles of Object Oriented Design, like the Single Responsibility Principle (SRP) and Separation of Concerns (SoC) to name two of them.
Elegant code reads like a best-selling novel. Remember that last book you’ve read that you just couldn’t stop reading? The same goes for elegant code. You start at the beginning of the code base and you can’t wait to see the rest. Code that invites other members of your team to read and review it. Code that does not have a need for comments, as it documents itself.
Elegant code is loosely coupled with a high cohesion. Orthogonal code is a very important topic. When you change one part of a system, it should not affect another part of that system. This can only be achieved through loosely coupled code. You gain productivity, reduce risk and increase maintainability.
Elegant code is therefore open to change and easy to extend whenever necessary. It should be very easy to make some fundamental changes to the code base, not only by its creators but by every other member of the team or even another team for that matter. This also involves some aspects like unit tests and continuous integration for example. These practices give any member of the team the ability to give immediate feedback about the condition of the code.
Elegant code clearly reflects the concepts of the domain for which it tries to solve problems and make life easier for its users. The model of the code should be understandable to both the developers as the domain experts.
Elegant code doesn’t try to reinvent the wheel. It is developed with the right tools for the right job without falling into the trap of the new technology du jour.
Last but certainly not least, elegant code does what it’s supposed to do. I’ve put this last, not because I think it’s not important. I believe that you automatically get working code when you apply every other characteristic I described earlier.
Notice that I mentioned the word team a lot. I’m a big believer of self-organizing teams that take matters into their own hands and pick up their responsibility. Elegant code is something that can be created as a team and most likely not as a sole individual. Although there are lots of very smart individuals out there, they are nothing compared to a team of open-minded, disciplined people who are set to meet their goal.