Ever sit back and look at the code you have just written and marvel at its elegance? Sometimes, you are just able to break apart a problem into the right places and solve it with a beautiful piece of code that does just what it needs to do and no more. Elegant code is a goal that can be strived for, but should everything we write be considered elegant?
Elegance is a beauty that emerges when clarity and concision – two ideals that pull against each other – are rightly balanced. Mario T. Lanza, programmer, on Quora
What Is Elegant Code?
Programming has its roots in mathematics. That is why when programmers talk about elegant code, they do it in the same way that mathematicians talk about an elegant solution to the problem.
Elegant code should have three attributes: simplicity, modularity and new insight, according to Tikhan Jelvis, another programmer posting on Quora.
Simplicity means that the code simply does what it needs to do: solves the problem, and no more. It can even be thought of as using the fewest lines of code possible to provide the most concise solution to a problem that can be made.
Modularity is a function of simplicity. Problems have natural fault lines in them. Elegant code will be written inside those fault lines. Modularity implies that a problem has been broken down to as simple as it can get, and the pieces of code that are written tackle those small pieces.
Insight is the most elusive of the three. For code to provide insight to a problem, it should see something inherent in the problem that no one has seen before. It breaks the problem down in a way that was previously unforeseen. It teaches us something about the essence of the problem and ways to use code to tackle problems of that type.
Is Elegant Code Practical?
While striving to make code as elegant as possible is something that good programmers should do, does all of the code that a programmer writes need to be elegant? That really boils down to the question, does my code solve the problem?
There are plenty of inelegant ways to solve a problem. Sometimes while seeking elegance, we can brute force our way to a solution. The brute force method is faster to develop, transparent to the end user and gets the job done. At the end of the day, solving the problem is the most important part.
Elegant code also comes at a cost. That cost is in time and money. Sometimes, the schedule of a project just won’t allow the time needed to fully develop an elegant solution. A limited budget can put a major damper on the time that you have to get to the solution, let alone refine it to an elegant one.
Unfortunately programming is often a game of compromise. To get to the solution in the allotted timeframe and budget, you must sacrifice your elegant code for functional code.
Izenda Provides Elegant Business Intelligence Solutions
Come see what Izenda’s embedded self-service business intelligence and analytics has to offer your business.