How to count lines of code: A complete overview (2024)

This blog was originally published in the Typo blog.

How to count lines of code: A complete overview (3)

Counting lines of code can be used as one of the metrics to assess your developers’ productivity and the efficiency of code bases. Therefore, in this blog, we dive into the different methods to count lines of code and what we think about this metric.

Let’s dive in!

There are several ways to count LOC, some more efficient than others with different objectives. So we have a list for you to use the best method that suits your needs.

Manual Counting

Counting lines of code manually involves going through each line of code and tallying the lines while excluding comments, blank spaces, and non-executable lines.

To do this, open the source code file and count the lines with a paper or text editor. Go through it line by line and check for executable code.

Once you are done, note the total count.

However this method might sound simple, yet it is tedious and prone to errors. Automated tools are more accurate and efficient, and we will discuss them in the following methods.

Count Lines of Code Command

The Count Lines of Code (CLOC) command is a tool that automates counting lines of code.

Here’s how to use the CLOC command:

  • Install the CLOC tool on your system.
  • Open the terminal or command prompt and navigate to the code directory. You can use the command ‘cd’ followed by the path to reach it.
  • Run the CLOC command: cloc <directory> (replace <directory> with the path to the code).
  • If you want to count the lines of the entire directory, simply replace the directory with a dot ‘.’
  • Wait for the results, which will include the total lines, blank lines, comment lines, and lines of code for different programming languages.

Here is an example of the tool and its results.

How to count lines of code: A complete overview (4)

The automated categorization of lines into different types and comprehensive statistics using CLOC makes it superior to manual counting. You can trust the reliability and efficiency of this tool to save you a lot of time and effort.

Use statements to count lines of code

While the above methods show the lines of code, if your code increases in more blanks, comments, and statements, then the number gets inflated without any value added to the functionality.

Therefore, statements count is much more helpful in understanding the executability and functional efficiency of the code.

In languages like C, C++, C#, or Java, a statement ends with a semicolon (;). And in languages like basic and VB, several statements can be added with a colon (:). Thus, the way it is done differs, but the logic remains the same.

The executability (XQT) is calculated by dividing the number of executable statements (STMTX) by all the statements (SMT).

Here are some specifics of this method:

  • Counting statements provides insight into how the arrangement of statements affects the flow control within a program. Loops and conditional statements show you how many times a code section can be executed based on the scenario,
  • You can identify the different code branches based on the multiple paths a code could take in each iteration.

While this does provide code complexity information, it still needs to be a fool-proof method. Factors such as data dependency and error handling can hamper the executability of the code.

IL Instructions

When measuring developer productivity, it’s crucial to select the appropriate metrics. Simply counting lines of code may not be dependable. Instead, experts advise measuring the number of executable statements within the runtime environment, which is a more comprehensive and precise comprehension of progress.

Here are the steps:

  • Compile the code: Use a language-specific compiler to transform the high-level code (e.g., C#, Visual Basic) into an intermediate language (IL).
  • Obtain the IL code: After compilation, you’ll have an assembly or executable file containing IL instructions.
  • Analyze the IL code: Open the IL code using a text editor or an IDE that supports IL syntax highlighting. You’ll see a series of IL instructions representing lines of code.
  • Count the lines: Simply count the number of lines in the IL code to determine the total lines of IL code.

Following these simplified steps, you can easily count the lines of IL code in your program.

Counting lines of code has been a long-time metric; however, every tool and method gives different answers.

Using it as a tool for productivity can be problematic because it is not comparable or fair unless you compare the same team’s code within the same team for the same code, language, and style.

Here are some more points for you to consider:

  • One developer may write concise code, and another might write more extended code to accomplices the same task. If LOC is a metric, then you might not be fairly judging the quality of their code.
  • Lines of code are reused through existing libraries, and the rest is written to achieve the desired outcome. However, in such cases, you cannot ignore the reused code nor determine it as original. But that doesn’t negate the developer’s efforts.
  • Productivity for developers is beyond writing code. Planning, testing, debugging, peer-to-peer reviews, and collaboration can take much time. Therefore reducing productivity to LOC will not be a fair assessment.

LOC can give you glimpses into code complexity, code length, and executability, but that’s where its importance should stop.

Typo’s CEO and Founder, Ksh*tij Mohan, says, “Measuring developer productivity solely by lines of code is like assessing a painting by its brushstrokes. It focuses on solution complexity rather than the complexity of the problem at hand. And like most metrics, it means very little without context.”

Therefore, we believe you can count the lines of code all you want, but don’t use it as a metric to determine which code is better or which developer is more productive. Use it as intended — as a metric to help you along the way.

How to count lines of code: A complete overview (2024)
Top Articles
Craigslist Greencastle
Finger Lakes Entries & Results for Monday, 8-12-2024
Dayton Overdrive
Authentication Portal Pvusd
Www.craigslist.com Springfield Mo
Craigslist Holland Mi Pets
Jobs Hiring Start Tomorrow
Chubbs Canton Il
James Cameron And Getting Trapped Inside Your Most Successful Creation
Estragon South End
Craigslist Cars For Sale By Owner Oklahoma City
Craigslist Boats Rochester
When Is Hobby Lobby Opening In Olean Ny
Greyhound Bus Station Syracuse Ny
Trizzle Aarp
Ups Customer Center Locations
1v1 lol unblocked Game- Play Unblocked Game Online for Free!
Appleton Post Crescent Today's Obituaries
How Much Is Cvs Sports Physical
Shadbase Get Out Of Jail
Pdinfoweb
Craigslist Apts Near Me
G4 Vore
Bayada Bucks Catalog 2023
Cars & Trucks By Owner
I Wanna Dance With Somebody Showtimes Near St. Landry Cinema
Wmu Academic Calendar 2022
In Branch Chase Atm Near Me
Sweeterthanolives
Raya And The Last Dragon Voice Cast: Who's Voicing Each Character
Dutchessravenna N Word
Heiwa Coin
Trailmaster Fahrwerk - nivatechnik.de
Demetrius Meach Nicole Zavala
Guide for The Big Con
80 For Brady Showtimes Near Brenden Theatres Kingman 4
Wells Fargo Arena Des Moines Seating Chart Virtual View
My Perspectives Grade 10 Volume 1 Answer Key Pdf
Fact checking debate claims from Trump and Harris' 2024 presidential faceoff
South Carolina Craigslist Motorcycles
Joe Aloi Beaver Pa
Ticketmaster Lion King Chicago
Payback Bato
Cetaphil Samples For Providers
Po Box 6726 Portland Or 97228
Dimensional Doors Mod (1.20.1, 1.19.4) - Pocket Dimensions
Publix Coral Way And 147
Gotham Chess Twitter
Trivago Anaheim California
Kaiju Universe: Best Monster Tier List (January 2024) - Item Level Gaming
8X10 Meters To Square Meters
Clarakitty 2022
Latest Posts
Article information

Author: Chrissy Homenick

Last Updated:

Views: 6250

Rating: 4.3 / 5 (54 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Chrissy Homenick

Birthday: 2001-10-22

Address: 611 Kuhn Oval, Feltonbury, NY 02783-3818

Phone: +96619177651654

Job: Mining Representative

Hobby: amateur radio, Sculling, Knife making, Gardening, Watching movies, Gunsmithing, Video gaming

Introduction: My name is Chrissy Homenick, I am a tender, funny, determined, tender, glorious, fancy, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.