19 April 2022

Beautiful Code

So a lot is written about how to improve your code and what makes it "good" code and it gets me thinking about beautiful code. I often listen to great music while coding and think about the artists working for years to become great singers/guitarists/drummers/performers to bring together all they have learned to create masterpieces that are available to us at our fingertips. Songs and videos that inspire our soul and demonstrate the ability to connect with people through a beautiful expression of music created through years of practice. Any skill can be equally inspiring and powerful, especially to the right audience. So the inevitable questions comes up: what makes a coder/developer/engineer/architect an expert and their coding as beautiful as the art that inspires us?

Here are my thoughts from many years of using a variety of languages, platforms, and technologies over 20+ years of coding about what is beautiful code:

Simple yet Powerful

When developers first start programming, they often write very complex code because they either don't fully understand the framework and try to force it to do things it wasn't designed to do, or they are trying to impress their fellow programmers with their cleverness. But great code is rarely complex and its often considered "boring" to the inexperienced developers. You probably overlook great code because it is plain and just "works." Code that plainly executes in a straighforward way, with no surprises and does its task plainly is the foundation to much of the applications/networks we rely on today. Despite the simplicity, beautiful code can perform tasks that are used by billions of people all over the world to make their lives easier through applications/media devices/networks constantly. And as a developer, you should understand the edges of the code clearly and the states you can get into from your code. Simple code leads to simple interactions with other code components - and these should be easily followed in your head or in your design. Conversely, complex code leads to complex interactions. As soon as code operates in a complex way, hundreds/thousands of variations sneak in that are unanticipated - which leads to buggy, brittle code.

Intuitive in Design

Beautiful code doesn't needlessly obscure its purpose or to abstract itself to the point of confusion. If you can look at a library and you just "know" where a certain function is in the namespace, or you anticipate the functions of a namespace - you are looking at great code. The heirarchy of functions/classes/namespaces is often the foundation of beautiful code that is a joy to use. The various programming language popularity rankings try to get a sense of this by asking developers which language they enjoy working with to get a sense of this metric. And beautiful code will often have an overall architecture that is obvious to those who need it.

Efficient in Syntax and Operation

Beautiful code is just concise enough to fulfill its purpose which makes its both simple, intuitive, and efficient to execute. There are times I realize I am doing something wrong because I am putting too much work into a section of code - which generally means the code is not efficient, not clean, and not properly designed. Efficient code can often be more complex to write because you may have to do something in a way that a computer can perform quickly, as opposed to peoples brains. But beautiful code can combine the two and produce masterpieces that are both efficient to operate and efficient to understand. Simple in appearance and simple in operation, but perform the task at hand.

Self Describing

Great code needs little documentation and few comments. That is not to say you shouldn't comment or document, because its hard to write great code and there are always less experienced developers that benefit from comments to understand the code - but if its truely great code, the syntax alone describes what its doing. This means the class/variable/constant/function names express what it's doing so clearly that its difficult to mis-interpret what is happening. Many of us know that this is what leads to "naming" taking a huge chunk of the time during coding. A good name of a variable can save hours of debugging later. Great code can describe itself in content alone.

Wrapup

I don't want to mention any specific frameworks, because like music, different styles have different "greats". But I try to recognize these attributes in code I use, and respect the developers who craft beautiful software. I also strive everyday to make my own code more beautiful, even if most days I am practicing my scales and just learning. And as a technical leader, I also try to push for others to see and create beautiful code - and hopefull they will become one of the next great creators of software you will one day admire!