Banner Default Image
Back-to-blogs

Beautiful Code

Posted-on January 2020 By Semi Coen

Blog Img

Beauty has many sources.

Coding style is an integral part of the code's elegance and should not be underestimated. Let’s explore in more detail the concept of beauty in code and what makes programming an art...

​'Beauty has many sources. When it comes to software development, beauty is conferred by simplicity and architectural coherence, which go hand in hand with the functional precision as well as the creativity applied to solve the problem in question. Likewise, the coding style is an integral part of the code’s elegance and should not be underestimated when it comes to improving the development team’s communication.

Seek beauty through simplicity

I recently read the Beautiful Code anthology, and the essay that resonated the most with me was The Most Beautiful Code I Never Wrote by Jon Bentley, which is based on the overarching principle that ‘less is more’ i.e. all the refactoring effort a few star developers make, time and again, to refine their code and eliminate redundancy, so it is as concise, comprehensible and succinct as possible. Some aphorisms that express this skill, as selected from the book, are:

  • A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away. Saint-Exupéry

  • The cheapest, fastest, and most reliable components of a computer system are those that aren’t there. Bell

  • If I had more time, I would have written you a shorter letter. Pascal

  • Simplicity does not precede complexity, but follows it. Perlis

In other words, this is a call for simplicity.

Every single line of code is code that has to be debugged, read and understood, and later on supported. This holds an inherent cost! So reducing the code while preserving the functionality is a noble effort. If a method has 10 statements, but we then minimise it to 5, the likelihood of issues has halved as the places it can be wrong, overly complex, or problematic has gone down. Source code is a liability, not an asset! Think about this next time you use LOC (Lines of Code) as a productivity measurement!

Three related philosophies are YAGNI, DRY and KISS. You Aren’t Gonna Need It suggests that code needs a reason to exist, otherwise it has to be refactored or deleted. Don’t Repeat Yourself proposes that every piece of code must have a single, unambiguous purpose and representation within the system. And finally, Keep It Simple Stupid, is pretty descriptive: just keep things simple!

Simplicity is, ironically, a complex subject!

"In software, the most beautiful code, the most beautiful functions, and the most beautiful programs are sometimes not there at all."

Quality design is the soul of a program

Just listening to the word ‘bug’ makes every developer cringe. It is commonly believed that good programmers are those who write bug-free code. Although a bug-free program is obligatory, bug-free code-writing is not. What I mean by this is that some developers strive to write bug-free code before even the program is compiled or executed, focusing more on functionality than design.

Neglecting maintainability during the development process and ignoring patterns/standards for the sake of functionality, is a recipe for disaster (an expensive one too). Another problem is that programmers can spend far too much time worrying about efficiency in the wrong places and at the wrong times; premature optimisation is the equivalent of opening Pandora’s box in engineering.

"Good design is all about making other designers feel like idiots because the idea wasn’t theirs."

Every professional software developer writes code that works, but there is a silver lining between the code that just works and the code that works well and stands the test of time. A solid design guarantees that bug fixing will be a breeze, because bugs will be where they should be! Put differently, when the architecture is not compromised, the bugs are cheaper to resolve and do not contribute much to the technical debt.

All art starts with a blank canvas

A developer’s ability to turn their thoughts into something tangible, is the definition of being creative. The screen is a blank canvas waiting to be filled with code and to produce a unique solution — if the work was not unique, it would simply be ‘copy and paste’! No two programmers will ever write identical code, not even if their final software appears, on the outside, to be the same. Everyone has an expression of their own voice as a programmer. And this by itself is magnificent! In my experience, getting to understand another developer’s code, is like getting into their minds and seeing the world from their perspective.

The thrill of being able to code is very hard to explain. It is not just lines of code… We can visualise the system we slowly build and as data flows within it, it becomes real. It is intellectually and emotionally rewarding, because it is such an achievement to see something from conception to completion. No matter how small or big it is!

As a closing note, before I move on to the next section, here is an extract from Donald Knuth, author of the ‘Art of Computer Programming’ series (for the non-programmers out there, it is like the bible of computer science), when he was asked to justify the choice of the word art in the book’s title:

"Computer programming is an art, because it applies accumulated knowledge to the world, because it requires skill and ingenuity, and especially because it produces objects of beauty."

Algorithmic brilliance is the ultimate sophistication

Algorithms are the recipes that make effective programming possible. They are the backbone of programming as they describe in a systematic way all the steps to solve a problem. Although logic and analytical skills are left-brain activities, problem solving is a creative process using our right-side ‘mental muscles’. The secret to effective problem solving is the mastery of complexity which, as we discussed earlier, can have an adverse impact on our programs.

All the best algorithms I have designed, reviewed, or studied have one thing in common: they rely on beautiful ideas for tackling computational tasks more efficiently, while avoiding unnecessary detail and complexity. The most essential algorithmic inner workings that contribute to an awe-inspiring codebase, to name a few, are: simplified Boolean algebra, inventive combinatorics techniques, wise selection of data structures, concise use of regular expressions, good resource utilisation and compliance with the SOLID principles.

There is an interesting paradox here though, that makes the algorithm creation a sophisticated task:

"There is no algorithm for creativity" — Andy Hargreaves

Coding style matters

So far we examined how good code needs to be simple and well designed. I will now turn to the aesthetic aspect of programming: the code layout. The visual appearance of well-formatted code is strongly associated with its quality and many programmers who take pride in their work, derive great satisfaction from polishing it, as they realise that a programmer is remembered for their style and discipline and very rarely for the code itself.

Code is not only written for the machines to execute, but for other programmers to read and decipher before they try to review, extend, correct, refactor or debug it. As such, an accurate and consistent representation of the logical structure of the code is essential.

Indentation and white space usage is key here. Similarly, the best layout schemes hold up well under code modification: changing one line of code shouldn’t require changing several others. That being said, the holy grail of programmers’ communication is self documenting code, which means: just enough comments, meaningful naming conventions, usage of small, easily testable, pure functions etc.

"Good code is its own best documentation" — S. McConnell

So, beautiful code is lucid, easy to read and comprehend. It has a balance to it. Its structure, shape and architecture reveals its intent. Each part is relatively small and has a single purpose. It is like a mosaic: all the little code pieces fit perfectly together, but at the same time they can come apart easily when one element needs changing!

Epilogue

Every time we write code, we are taking a non-deterministic path towards an unknown solution; one that has never been created before! It is a journey of self-discovery and an expression of our intellect. This is exactly what art is!

We are not the kind of artists that create visual delight for others to admire. But when we love something we put beauty into it!'

This article was written by Semi Koen and published on Medium.com.

HiTalent

Arrange a

Callback

Drop us your Email and we'll arrange a call to discuss how we can help