Reducing Code Lines: Less is more?
In the past, when the size of a typical storage medium was measured in kilobytes, there was a pressing reason to reduce the lines of code used in a program or game. After all, you can only fit so much data on a five-and-a-quarter floppy disk. Storage is less of a concern these days, though there is still a school of thought that the fewer lines of code there are, the better, especially when it comes to maintaining your code.
But is that really the case?
Naturally, there are developers who disagree— those who say that it is not necessary to reduce the number of lines in order to write good code, so which is it?
As with many things in life, the answer is far from black and white. It is certainly the case that unnecessary code should be avoided. Code that doesn’t need to be there can cause unforeseen problems, make your software less performant, and make it harder for other developers to perform code maintenance.
The reality is that the answer to this dilemma depends on a few factors, the most significant of which being;
- The programming language being used
- The development environment
- The hardware being developed for
Some programming languages are particularly verbose, making it impossible to reduce code lines beyond a certain level because the fundamental components of the code are themselves large. An example of this is Python, which is a high-level language. However, Python is also one of the most popular languages, and needs to be used in an efficient manner.
On the other hand, while Python is far from cumbersome, some languages are far more streamlined, and not only lend themselves well to a reduced codebase, but even perform noticeably better when less code is being used.
Regarding the development environment, it is always good practice to make your code clear and understandable, even if you have no reason to believe anyone else will ever see it. If you are working in a team environment, however, it is essentially a responsibility to ensure your code can be understood by others, and that sometimes means utilising more lines of code than you would otherwise have needed.
Finally, the hardware being developed for is relevant, even in an age where the average phone has tens of gigabytes of storage. For example when developing for the web, the less data that needs to be transmitted, the better.
So, clearly, whether fewer lines of code is inherently better depends on a lot of things, but for professional developers, there is a deciding factor. Lines of Code (LOC, or SLOC) is one of the most widely used metrics for code. It is often used as a “first-glance” indicator of how complex a codebase is, and, as a result, how difficult it is to carry out code maintenance. In other words, while there may be reasons for and against the arbitrary reduction of code size in a pure coding sense, the industry as a whole sees reduced SLOC as desirable.
Some Benefits of Reducing SLOC
You can’t make an informed decision on whether to reduce code lines without first knowing what the benefits are. Here are some of the most significant reasons to trim the fat from your code;
Fewer Bugs
Removing SLOC doesn’t guarantee fewer bugs—in fact, it can create bugs if you’re not careful—but it does reduce the chances of them cropping up. The longer your code, the more complex it becomes, and the more complex it becomes, the more room there is for things to go wrong. With any program, there is the possibility of new additions causing problems with existing code. This possibility is significantly increased by having more existing code.
Same Results as Longer Code
Assuming you are not just slicing out essential bits of your code, the end result of trimming down your source code will still be the same output. Given all of the advantages of reducing your code, this removes one critical potential downside. If the result is going to be the same, why not just make your codebase smaller?
Less Code Maintenance Work
Code maintenance is an integral part of modern development. Hardware changes so fast these days, and new methods are always being developed. Having a smaller, less cumbersome codebase makes maintaining your code much easier.
More Adaptability to Other Languages
In today’s age of interconnected systems, cross-platform software is a must. Sometimes, making your application cross-platform means porting it to another language. There are other reasons to port your code to another language, of course, but when you do, having fewer lines of code to port will make your life much easier.
Best Practices For Reducing Code
If you that less is more when it comes to SLOC, there are some best practices to observe that can also be applied in other areas and activities of programming, and that will help you keep your code light and functional.
Think Before Coding
Few things can lead to messy, bloated code as easily as writing code without a plan. By working through the logic before you start writing your code, you essentially create the shape of what is to come. Think of working through the logic as drawing the outline of a picture, which you will then “colour in” with your code. This should help you avoid writing unnecessary code that will later need to be reworked or removed.
Have Your Peers Review Your Code
In almost any process of creation, it helps to have your work looked over by a new set of eyes, especially when you’ve been working on the same project for a long time. A colleague will often be able to spot problems that you have been missing.
Use Performance Analysis Tools
If you are something of a perfectionist, you may run the risk of falling into a spiral of constantly trying to remove just one more line, but there will come a time when you need to stop. Using performance analysis tools can help you decide when that time is. If you are finding it increasingly hard to find ways to reduce code lines and the performance is unaffected by your changes, it’s probably time to move on.
Follow the DRY rules (Don’t Repeat Yourself)
One of the biggest causes of unnecessarily long code is repetition. Wherever possible, write your code in such a way that your code is reusable, such as by enclosing code blocks in a function if the language supports that (such as Python, or Java). This not only reduces the length of your code, it makes it much easier to make wide-reaching changes on those parts of the code, since one change will take effect across the whole codebase, wherever that function is used.
Languages With Fewer Code Lines
As mentioned above, some languages lend themselves better to more efficient use of code lines than others. Here are some good languages for developers who like to keep things compact;
- Python
- Slant
- Elixir
- Smalltalk
Of course, this is by no means a comprehensive list of languages that involve fewer lines of code in general, but it’s a good starting point. And, generally speaking, it’s good practice to make your code as efficient as possible regardless of the language. Just remember not to do this at the expense of other necessities, such as making your code clear and understandable to your colleagues, especially if it’s a program or software that will require lots of code maintenance later on.
Recommended article: Devtools: Boosting Your Web Development Workflow on Chrome and Firefox