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.
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
andcloc
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
Usage
Example Output
If cargo and rust are not installed, you can use the following command:
alternatively, you can use the following command:
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
Usage
Example Output
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
Example Output
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
Example Output
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!