Coding isn’t easy. It’s one thing to grasp the programming language and understand how algorithms work but it’s an entire different ballgame when it comes to developing a complex application that doesn’t make you want to pull out your hair whenever you have to troubleshoot it.
In many ways, getting on the path to writing perfect code shares plenty of similarities with becoming an expert at photography, cooking or drawing. It’s a tough journey but the benefits are more than worth it. Perfect code makes troubleshooting easier, reduces the time needed for maintenance and ensures the application’s logic is clearly communicated.
The following are the principles you must apply if you want to consistently write great code.
Descriptive Names
Functions, classes and variables are simply an interface between the developer and the application’s underlying logic. Ergo, when you use ambiguous and unclear names for functions, classes and variables, you inadvertently obfuscate the application’s logic from not just yourself but also other programmers who will go through the code in future.
Complex applications may contain numerous pages of code so even if you wrote it yourself, chances are you may not remember the meaning when you come back to it months later. So instead of calling a variable dab (whose meaning would be difficult to decipher) it would make more sense to spell it out as distancebetweenAB.
Delete Redundant Code
This is a problem that both novices and seasoned programmers find themselves grappling with. Coding is about finding the best way to perform a task. So as they go about optimizing and fixing the code, some programmers will comment out the old code then rewrite the new code just below it.
Even though the new code works better, they’ll choose to leave the old code there just in case they’d want to go back to it sometime in future—something that rarely happens. Over time, the application’s code contains numerous redundant blocks of code that only serve to clutter up source files and impair readability.
Commenting out and retaining old code may have had some merits two decades ago but now with source control tools, it’s an obsolete practice. You can use a code-level application monitor, such as https://stackify.com/how-to-monitor-iis-performance/ to quickly identify redundant code.
Readability Trumps Cleverness
Too many developers conflate ‘perfect code’ with ‘clever code’ as though condensing 20 lines of code into one line somehow automatically makes the application better. It may take up less screen space but is the code easier for a third party to understand? Sometimes clever code is perfect code but unfortunately, this is the exception and not the rule.
Whenever you are building a new program, always think about how easy it will be to troubleshoot once it’s ready for testing or production. Programmers love clever code because there’s an adrenaline rush akin to what one feels when they solve a riddle or puzzle. It’s unlikely to deliver perfect code though.
Always write code that the next person who’s going to read it won’t have difficulty making out.
Consistent Style
Programming tutorials often tout conflicting coding styles. This can be a major problem especially for newbies who won’t understand the demerits of this until they are well into their programming career when they have to unlearn the inconsistent styling. There’s no one coding style that is necessarily the best in all circumstances or for all languages. What’s important is to adopt a consistent style throughout.
Do you want braces to be on their own line? Do you want method calls preceded by a space? Do you prefer using tabs instead of spaces? All of these are valid styles. What you do not want is to have these styles in one section of code but a different style in another section of code. Make up your mind from the outset on the style you are going to use then stick with it from start to end.
Good Necessary Comments
Good comments are perhaps the oldest principle of good programming. As soon as newbie developers are taught how to use comments, they’re urged to apply them whenever there’s an opportunity to do so. Nevertheless, it’s possible for an application have too many comments.
Don’t be pedantic by describing things that are pretty obvious to the average programmer. As a rule of thumb, comments exist to explain the why and not the what of the code snippet. To avoid too many comments, make sure the code is written in a way that a reader will immediately understand what it does.
A novice programmer seeking to build perfect code faces similar challenges as a budding novelist who is learning the ropes of clean readable prose. While you can read a lot of books about how to do it, it’s only when you start to do it and practice the right techniques that you’ll eventually master it.