TLDR; The minimalism lifestyle is a mindset in which you let go of unused belongings and unimportant attachments. But how is elegant code defined? I would define it similiar to Antoine de Saint-Exupéry's infamous definition of elegant design: Elegant design is achieved not when there is nothing left to add, but when there is nothing left to take away – In this sense, the best code is the one never written, but it does fulfill the job.
The minimalism lifestyle focuses on the true important needs, and letting go of unused belongings or unimportant attachments. It can have a huge impact in your life and I recommend to anyone who has a different relationship with old stuff, a toxic attitude of buying too much new stuff, to learn more about minimalism. I am a minimalist, and for me it became a very important mindset which counterbalances the status quo, the world of more. I wrote two other articles about that topic: Minimalism: The Art of Living with Less and Mindfulness in the Culture of Distraction. Even though, it became quite popular in terms of a way of living, it is also applied to a lot of other domains like music, architecture and the visual arts, see Artsy Minimalism.
Before we dive into minimalism and code, I want to start with an infamous definition of elegance also referenced in the hacker jargon file, also see where it comes from. In my opinion it connects elegant engineering and minimalism very well:
The French aviator, adventurer, and author Antoine de Saint-Exupéry, probably best known for his classic children's book The Little Prince, was also an aircraft designer. He gave us perhaps the best definition of engineering elegance when he said “A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away.” – Jargon File
Elegant code is what remains. Without decoration, without bloat. The pure functionality to fit the requirements – and the requirements only. On StackExchange, there are other possible definitions of elegant code, which I like to reference to give a broader perspective. But for the next sections, let me explain why minimalism is central to elegant code. Hence, let us start with avoiding code in the first place.
You start something new. You get the requirements right and begin outlining the infrastructure, components and the protocol which might be needed to get the job done. This is design work, and it should be effective and efficient, but also I think, as simple as possible. Lean. The same applies on a code level. Every statement you write, could be another bug, could distract you from the core of the logic, or could even be redundant or useless. The devil is in the details, so writing less, makes him easier to spot. Plus, every line you write, somebody else or even you have to read and understand over and over again.
Months or years after, the solution gets outdated, still it works. A possible sceneario could be in terms of the Pareto principle, that only twenty percent of the code base is needed for 80 percent of the business use cases. So why don't refactor? Maybe you have to get feature complete for the next release and the sales team keeps saying that only new features sell. But also on the engineering side, of course, working on new features is more fun. We need to get the priorities right. If the error handling or monitoring code for critical functionality became cryptic and untrusted over the years, you should decide slowly about refactoring it or not. Legacy code tells a story. Maybe a good, maybe a bad one. Most importantly, we should have learned something useful in retrospective.
We all know that technical debt can be a big burden, it slows down the development cycle, scares off new employees, scares off old employees, seriously, nobody wants a share of that old pie. Even when this old pie is still making money, even when it looks shiny and new. Removing code makes space for better, more suitable and elegant code. Even if it might feel like it, it is not equivalent of burning money or business value, see Sunk cost fallacy.
Experience over Lines of Code
The real value is in the heads of the people who worked on it. Programming is mind work, and writing code is not only translating the requirements into code, it is creative work which involves the experience in coming up with a precise design: More efficient, and also simpler to understand. Think about what is actually needed, here and now, not what was needed in the past or will be needed in the future (YAGNI - you arent gonna need it), you might want to write minimal code which only fits required functionality, not more - not less.
Striving towards Elegance
Of course, you should not forget about the principles of writing clean code: Design patterns, test-driven development, continous integration, abstraction principle and so on. Having a good test coverage will help you a lot getting rid of bloated or even unused code. I strongly believe, that striving towards elegant design is a very important trait every good engineer or programmer should have or should develop to excel in her or his profession. Similiar to Occam’s Razor of chosing the hypothesis with the least assumptions, we should write minimal code without code bloat, otherwise it fails to work, is hard to use, hard to maintain, and is less applicable.
Let me give you another perspective, which came into my mind. I will start explaining it for the minimalism lifestyle. By buying stuff or gaining attachments you remove emptiness in your life. You need the extra time to maintain and spare attention to these things which are nice to have but not ultimately important for you, like a second car, second bicycle, or whatever crosses your mind now. On the contrary, emptiness does not need to be maintained, and you would have more time for significant parts of your life. Shifting the perspective to code: While writing code, you remove emptiness. And emptiness does not need to be maintained. Emptiness can be seen as a reservoir or budget of time. Hence, if you remove too much emptiness, you end up having legacy code you are not able to maintain in time while also writing new code.
In my opinion, the principle of elegant code should be taught in lesson one of programming. It is simple but powerful like minimalism, the life-changing trait, itself. I hope this article will fire up your motivation of thinking smart, writing less and reflecting on requirements and code. But keep in mind, that nothing will change, until you respect and integrate it into your daily working routine. Therefore I am glad that we cultivate this attitude on a daily basis at work. Spread the article if you like it and let us discuss the idea.