Cloc, count the source code lines in your project

about clock

In the next article we are going to take a look at Cloc. If you work as a developer, you may need to share your progress and statistics of your code with the boss or colleagues. For such cases, I personally know of few programs available to analyze the source code. One such program is 'Cloc'.

Using Cloc, we will be able to easily count lines of code from different programming languages. Counts blank lines, comment lines, and source code lines. At the end it shows us the result in an ordered format of columns. Cloc is a free, open source, cross-platform utility completely written in the Perl programming language.

General characteristics of Cloc

Cloc offers us different characteristics for a program of this style. These include the following:

  • Es Easy to install and use. It does not require dependencies.
  • It is an open source program and multi platform.
  • We will be able to produce results in various types of formats, such as; plain text, SQL, JSON, XML, YAML, or comma separated values.
  • It offers us possibilities of use with git.
  • We will also be able to count code in directories and subdirectories.
  • Can also be used with compressed files such as tar, Zip files, Java .ear files, etc.

Cloc installation

Utility Cloc is available in the default repositories of most Unix-like operating systems. So we can install it using the default package manager on Debian and Ubuntu, as shown below. You just have to open a terminal (Ctrl + Alt + T) and type:

install apt cloc

sudo apt install cloc

It also can install using a third party package manager like NPM.

npm install cloc

npm install -g cloc

Count the lines of the source code

To see how it works, let's take a look at a simple example. I have a program, the typical one 'Hello World'written in C. Below I show you the code that contains the only file:

cloc file c example

For count the lines of code in the hello.c program, just run:

count lines from cloc c files

cloc hola.c
  1. The first column will show us the name of the programming languages ​​that the code is made of source. As can be seen from the output above, the source code of the program is written in the C programming language.
  2. In the second column we will see the number of files in each programming language. For this example, it will display 1 because it is the number of files that the code contains.
  3. The third column shows the total number of blank lines. We have zero blank lines in our example code.
  4. In the fourth column we will see the number of comment liness.
  5. And the last and fifth column shows the total lines, not including comments, of the source code dice.

Count the lines of compressed files, the contents of a directory and subdirectories

The example is just a program with only seven lines of code, so counting the lines in the code is not a big deal. In case we are interested in counting bigger things, take a look at the following example:

cloc large file

cloc archivo.zip

According to the previous output, Cloc is going to show us the result of a compressed file in seconds, with a nice column format. We can see the gross total for each section at the end, which is very useful when it comes to analyzing the source code of a program.

Cloc not only counts individual source code files, but also files within directories and subdirectories, etc.

Count the lines of code of the files contained in a directory:

cloc dir/

If we need to count the lines of code of the files located within a Subdirectory, we will write:

cloc dir/sub/directorio

Cloc help

Cloc can recognize various programming languages. To se her full list of recognized languages, run:

cloc --show-lang

If you want to know more about cloc, check the help section typing in terminal (Ctrl + Alt + T):

help cloc

cloc --help

Who wants, can consult more information about this app in your repository GitHub.


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.