Elegant code is hard to put your finger on, describe or quantify.
In his book clean code, Robert Martin spent a full chapter with multiple people trying to do just this.
Each of them ended with different definition.
Here is my take on it:
Clean code is readable, sectionable and effortlessly extensible.
Clean code should be glanceable and reads like a story. It should be easy to understand. One of the first things to check while doing a code review is whether the code is easily understandable without the need to go into details.
Elegant code is usually written in different sections. Each doing one thing. When you see a bug in a well written repo you usually directly have an idea on where it is happening. The fix is usually in one and only one place.
Rails is a great example of elegant code in a sectionable framework. Views, Controllers, Models, Tasks, etc…
You can pick up any Rails repo and usually can find your way pretty quickly around it
3) Effortlessly extendable: Emphasis on effortlessly. It is just easy to extend the existing code to do more without doing a lot of modifications.
Couple of examples:
Start with Uber Rides and effortlessly add Uber Eats.
Start with a calculator app in the first iPhone and effortlessly generalize to the App Store in the next version.
Start with Google Search and specialize down to Google News.
There are more examples powered by engineers writing elegant extendable code.
One last note on this code are not born or are always elegant. Perfection is the enemy of progress. They are iterated upon and refactored to their current state.
Don’t aim in writing the perfect code. Aim at writing functional and simple code, keep complexity at bay and refactor every then and while and you will end up with elegant code in the long run.
Mastering Software Engineering Course on Maven
If you liked this article, I will be teaching a “Mastering Software Engineering” course on Maven where I will teach hard-learned lessons I acquired developing large-scale products at companies such as Uber, Airbnb, and Microsoft.
Thanks for reading Software Engineering Tidbits! Subscribe for free to receive new posts and support my work.