How to Accurately Count Lines of Code in Any Project

How to Accurately Count Lines of Code in Any Project

A professional guide to measuring lines of code in software projects using industry-standard tools.

Alex Rodriguez
2024-01-10
4 min read
Table of Contents

Introduction

Measuring lines of code (LOC) is a valuable practice for assessing project complexity, tracking development progress, and optimizing code efficiency. In this article, we'll explore the most efficient ways to count LOC across any software project, covering:

  • Built-in command-line methods
  • Third-party tools
  • Pros and cons of each approach
  • Hands-on demonstrations
  • A comparison of tokei and cloc to determine the best choice

Methods to Count LOC

1. tokei (Recommended)

tokei is a high-performance, Rust-based tool designed for speed and accuracy in LOC analysis.

Installation

BASH
BASH
cargo install tokei

Usage

BASH
tokei

Example Output

BASH
-------------------------------------------------------------------------------
Language          files    blank    comment    code
-------------------------------------------------------------------------------
JavaScript        12       320      90         1250
TypeScript        8        190      60         920
CSS               4        110      25         450
-------------------------------------------------------------------------------
Total             24       620      175        2620
-------------------------------------------------------------------------------

If cargo and rust are not installed, you can use the following command:

BASH
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
BASH
source $HOME/.cargo/env
cargo install tokei

alternatively, you can use the following command:

BASH
sudo apt install rustc cargo

Pros & Cons

✅ Exceptional speed and performance

✅ Provides a detailed breakdown by language

✅ Supports multiple programming languages

❌ Requires Rust & Cargo installation

2. cloc

cloc (Count Lines of Code) is a widely used tool that accurately counts lines while ignoring empty lines and comments.

Installation

BASH
npm install -g cloc

Usage

BASH
cloc --include-ext=js,jsx,ts,tsx,css,scss,html .

Example Output

BASH
-------------------------------------------------------------------------------
Language          files    blank    comment    code
-------------------------------------------------------------------------------
JavaScript        12       300      100        1200
TypeScript        8        200      50         900
CSS               4        100      30         400
-------------------------------------------------------------------------------
Total             24       600      180        2500
-------------------------------------------------------------------------------

Pros & Cons

✅ Simple installation and usage

✅ Ignores blank lines and comments for accuracy

✅ Works across multiple platforms

c Slower than tokei

3. wc -l (Basic CLI Method)

The wc -l command provides a quick count of lines but lacks filtering capabilities.

Usage

BASH
find . -name "*.js" -o -name "*.ts" -o -name "*.tsx" -o -name "*.jsx" | xargs wc -l

Example Output

BASH
2300 total

Pros & Cons

✅ No additional installations required

✅ Works on Linux and macOS

❌ Counts blank lines and comments

❌ Not available on Windows by default

4. git ls-files (For Git Projects)

This method counts lines only in files tracked by Git, making it useful for repositories.

Usage

BASH
git ls-files '*.js' '*.jsx' '*.ts' '*.tsx' | xargs wc -l

Example Output

BASH
1800 total

Pros & Cons

✅ Works efficiently within Git repositories

✅ Filters out ignored and unnecessary files

❌ Ignores untracked files

Choosing Between tokei and cloc

Feature tokei cloc
Speed ⭐⭐⭐⭐⭐ ⭐⭐⭐
Accuracy ⭐⭐⭐ ⭐⭐⭐⭐
Detailed Breakdown
Ignores Comments
Windows Support
Installation Complexity Requires Rust Easy

When to Use tokei

  • You need the fastest LOC counting tool
  • Your project is large and performance is a priority
  • You want support for multiple languages

When to Use cloc

  • You prefer an easy-to-install solution
  • You need an accurate count while ignoring empty lines
  • You work on smaller projects where speed is less critical

For professional development workflows, tokei is the superior choice due to its unparalleled speed and detailed output. However, cloc remains a strong alternative for those who prioritize ease of installation over performance.

Conclusion

Method Speed Accuracy Works on Windows? Requires Install?
tokei ⭐⭐⭐⭐⭐ ⭐⭐⭐
cloc ⭐⭐⭐ ⭐⭐⭐⭐
wc -l ⭐⭐⭐⭐ ⭐⭐
git ls-files ⭐⭐⭐ ⭐⭐⭐

For most developers, tokei is the best choice due to its high-speed performance and detailed reporting. If you require a simpler setup, cloc remains a viable alternative.

Measure your project's complexity with tokei today and enhance your development workflow!

Share this article:
42 likes
Alex Rodriguez

Alex Rodriguez

JavaScript Instructor

JavaScript expert and former bootcamp instructor. Specializes in making complex concepts simple and engaging for new developers.

Related Articles

Never Miss an Update

Get the latest tutorials, tips, and insights delivered straight to your inbox.

No spam, unsubscribe at any time.