← Back to blog

How to Count Lines of Code: A Complete Overview

In this post, we'll discuss the importance of counting lines of code including how to use VS code counter, cloc, and xargs.
January 17, 2022 • Nabendu Biswas

Famous software always declares its lines of code. For example, Google has 2 billion lines of code, MacOS has 85 million lines of code, and Facebook has 60 million lines of code.

But what does that really tell you? Is having more lines of code necessarily good?

Let's look at how to measure lines of code, and then how to avoid one of the biggest mistakes you can make as an engineering leader--using lines of code as a measure of individual or team productivity.

Remember, you get what you measure, and if you're only measuring lines of code, you're going to only get more lines of code, not necessarily quality.

Metrics for Counting Lines of Code

Most people think that to measure lines of code we just have to open our favorite code editor and it'll give us the lines of code in our project. But it'll only give us the lines of text for the file.

For instance, say I have a small Vue program that has only one component. So, in VS Code, I can go to the last line and check the lines of code—which is 89 in my case.

This way, I also have to go to each file, such as App.vue and App.css, to get the exact lines of code and then add them. This manual way is not a good method to count lines of codes.

Lines of code

Additionally, software engineers leave a lot of comments and empty lines because it makes the code more readable.

There is some value in knowing how many lines of text a file contains. Raw lines of code can provide an ongoing indicator when things might need to be broken down into smaller parts or refactored.

LinearB uses this metric as part of our high-risk work detection, notifying you of all the branches updated within the last seven days with less than 50% of new work and exceeding 300 lines.

Additionally, lines of code might be indicative of the sheer scale of what we've just been asked to review in a pull request. Our WorkerB bot not only notifies you of PRs that have been open for too long, but gives you context into the estimated code review time and related issue.

Set goals

But the next step in accurately counting lines of code is to identify another useful metric called source lines of code (SLOC) by trying to filter out comments and empty lines.

Source Lines of Code

Consider also the for loop below, which is common in almost every programming language on the planet:

for(let i=0; i<names.length; i++)


console.log('Name is ' + names[i]);


Here, you see curly brackets on two separate lines, and these shouldn't count as two source lines of code. Therefore, this for loop has four lines of code but two lines of source code.

Source lines of code is more accurate than a raw count, but there's still some built-in uncertainty. Another approach is to calculate statements.

Logical Lines of Code

So, to write a program to count logical lines of code, you could use semicolons to check the end of line rather than using a carriage return as an end of line.

This strategy will also help you get rid of comments, which generally start with // in most languages. So, this will count the number of lines of code perfectly in languages like Java, C++, and other traditional languages.

But in some languages, like JavaScript and Python, semicolons aren't necessary, so this strategy will fail. Thus, you need to have different strategies for different languages.

A program that helps you get lines of code in an application will have this complex logic in place. There are many programs available to count lines of code. We'll look into some of them next.

VS Code Counter

Now, if you're using a lightweight editor like VS Code for your development, you can use an awesome extension. The extension to count the whole project's lines of code is VS Code Counter by Kentaro Ushiyama. You just need to add it in VS Code from Extensions, and you're ready to use it.

Vs code counter

After that, open the command palette in VS Code by pressing Ctrl + Shift + P on Windows and Cmd + Shift + P on Mac. Now, type VSCodeCounter: Count lines in workspace. It will appear, so press enter.

Vs code counter workspace

It will open the Preview for you, which shows the Summary of our small Vue project and its structure.

Vs code counter workspace summary

Now, click on "Details" to get the detailed count for multiple files in our project. It includes comments and blank lines in the total count.

Vs code counter workspace details

Counting Lines of Code Using cloc

One of the best applications to count lines of code is the package of cloc. You can use it in multiple operating systems, but we will look into using it in Ubuntu. In our case, we're using Ubuntu in a Windows 11 system through WSL (Windows Subsystem for Linux).

Various commands are available for different versions of Linux. But since we're using Ubuntu, we'll install through the Ubuntu package manager of apt, through the command sudo apt install cloc.

Lines of code cloc

After the installation is complete, you can use cloc in various ways. If you want to get the lines of code of a single file, you can use cloc <filename>. We're using the command to get the lines of codes of a simple Vue file. It also recognized that it is a VueJS component.

Lines of code cloc vuejs component

We can check all of the lines of code in a file or directory by the command cloc --by-file. Our folder contains two Vue files, so we're getting the count of both of them as well as the sum.

Lines of code cloc by file

Now, the most useful cloc command is to get the lines of code of the whole project. You can do this by compressing the complete project first as tar.gz and then running command on it.

We'll compress our small VueJS application with the command tar -cvzf form-demo.tar.gz form-demo/. All of our code is in the form-demo folder.

Lines of code cloc demo

Now we'll run the command cloc form-demo.tar.gz to get the details of the lines of code of the whole project. Since this VueJS project contains a lot of node_modules, our lines of code also include those.

Lines of code cloc demo unique files

Counting Lines of Code Using xargs

You don't need any fancy tools on a Linux system to count the lines of code. You can simply use the built-in utility of xargs. We'll use this to count the lines of code in our small VueJS application. Inside our folder containing the VueJS application, run the below command to find the total lines of code:

find . -name '*.vue' | xargs wc -l

Lines of code xargs

Now, let's understand this command. Here, we're first using the find command to find all the source files ending with "vue." After that, we're piping the output to xargs and using wc -l to count the number of lines.

We don't need any fancy tools on a linux system to count the lines of code.

Although this command is useful, it doesn't come with the features of the cloc command. The cloc command has a sophisticated algorithm, which also removes the comments and other unnecessary things.


Now that you've seen various ways to measure lines of code, you can't stop there. There's too high a risk of gamification. Remember, measuring lines of code will only get you more lines of code, not necessarily quality.

Only use lines of code as an indicator of risky work, work that needs to be refactored, or work that may create bottlenecks in your pipeline delivery. Better yet, automate this process with LinearB. Book a demo today!

Improve your engineering organization at every level with linearb
Want to improve your engineering processes at every level? Get started with a LinearB free-forever account today!

Further Reading

Join our community of data-driven dev leaders

Each week we share stories and advice from engineering and product leaders striving to be better for their teams.

LinearB may send you email occasionally about how you can optimize productivity.
We will not share your information with anyone. Ever.