• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
Codemotion Magazine

Codemotion Magazine

We code the future. Together

  • Discover
    • Live
    • Tech Communities
    • Hackathons
    • Coding Challenges
    • For Kids
  • Watch
    • Talks
    • Playlists
    • Edu Paths
  • Magazine
    • Backend
    • Frontend
    • AI/ML
    • DevOps
    • Dev Life
    • Soft Skills
    • Infographics
  • Talent
    • Discover Talent
    • Jobs
  • Partners
  • For Companies
Home » Backend » Is It Always Best to Reduce Code Lines?
Backend

Is It Always Best to Reduce Code Lines?

Many devs follow the "Less is More" approach when it comes to SLOC. But is reducing code lines always the best solution? This article aims to shed some light on the matter.

April 1, 2022 by Codemotion

Table Of Contents
  1. Reducing Code Lines: Less is more?
  2. Some Benefits of Reducing SLOC
    • Fewer Bugs
    • Same Results as Longer Code
    • Less Code Maintenance Work
    • More Adaptability to Other Languages
  3. Best Practices For Reducing Code
    • Think Before Coding
    • Have Your Peers Review Your Code
    • Use Performance Analysis Tools
    • Follow the DRY rules (Don’t Repeat Yourself)
  4. Languages With Fewer Code Lines

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.

Removing unnecessary code will reduce code lines in your software, but it is the removal of unnecessary code what matters, not the act of reducing the quantity of code.


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.

Fewer code lines are not necessarily always better, but there are many best practices for better code maintenance and performance to keep in mind.

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.

Some programming languages are better for achieving less SLOC.

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

 

facebooktwitterlinkedinreddit
Share on:facebooktwitterlinkedinreddit

Tagged as:Full Stack Developer Languages

Meet the Codemotion Ambassadors!
Previous Post
What is Reactive Programming and How Can I Benefit from It?
Next Post

Related articles

  • Devtools: Boosting Your Web Development Workflow on Chrome and Firefox
  • How to Boost Performance With Lazy Loading
  • Micro Frontends: the Future of Web Development?
  • Front-end Refactoring for Performance Improvement
  • 7 Cybersecurity Threats You Must Know as a Web Developer
  • Black Friday: How to Manage Huge Traffic on Your App
  • 10 Commonly Used SQL Commands For Solid Databases
  • How to Easily Migrate from Spring Boot to Micronaut
  • Fast Document Similarity in Python (MinHashLSH)
  • This is Why The Future of Programming Is Composable

Primary Sidebar

Learn new skills for 2023 with our Edu Paths!

Codemotion Edu Paths for 2023

Codemotion Talent · Remote Jobs

Java Developer & Technical Leader

S2E | Solutions2Enterprises
Full remote · Java · Spring · Docker · Kubernetes · Hibernate · SQL

AWS Cloud Architect

Kirey Group
Full remote · Amazon-Web-Services · Ansible · Hibernate · Kubernetes · Linux

Front-end Developer

Wolters Kluwer Italia
Full remote · Angular-2+ · AngularJS · TypeScript

Flutter Developer

3Bee
Full remote · Android · Flutter · Dart

Latest Articles

web accessibility standards, guidelines, WCAG

Implementing Web Accessibility in the Right Way

Web Developer

devops, devsecops, cibersecurity, testing

3 Data Breaches in Web Applications and Lessons Learned

Cybersecurity

The influence of Artificial Intelligence in HR

Devs Meet Ethics: the Influence of Artificial Intelligence In HR

AI/ML

google earth engine

What is Google Earth Engine and Why It’s Key For Sustainability Data Analysis

Data Science

Footer

  • Magazine
  • Events
  • Community
  • Learning
  • Kids
  • How to use our platform
  • Contact us
  • Become a Contributor
  • About Codemotion Magazine
  • How to run a meetup
  • Tools for virtual conferences

Follow us

  • Facebook
  • Twitter
  • LinkedIn
  • Instagram
  • YouTube
  • RSS

© Copyright Codemotion srl Via Marsala, 29/H, 00185 Roma P.IVA 12392791005 | Privacy policy | Terms and conditions

Follow us

  • Facebook
  • Twitter
  • LinkedIn
  • Instagram
  • RSS