Back to posts

Code Bloat or refactor: Simple Ways to Measure Your Project

Identify unnecessary code and streamline your project with simple refactors.

checking for code

How to Count Lines of Code in Your Project: A Developer's Guide to Project Analysis

When you're deep in development mode, sometimes you pause and wonder: "Just how big is this codebase I'm managing?" Whether it's a work project or a side project you've been building with Cursor (which probably has some unnecessary code), knowing your project's size helps you make informed decisions about refactoring, maintenance, and complexity management.

While counting lines of code isn't the most accurate way to measure productivity or code quality, it's incredibly useful for getting a feel for your project's scope and identifying areas that might need refactoring to reduce bloat.

The Quick and Dirty Approach: PowerShell One-Liner

If you need a rapid count of your TypeScript and JavaScript files, here's a simple PowerShell command:

1Get-ChildItem -Recurse -Include *.ts,*.tsx,*.js,*.jsx | Get-Content | Measure-Object -Line
2
3

The Problem: This approach is like using a chainsaw to trim a bonsai tree. It counts everything—blank lines, comments, and even files in node_modules or .next directories. Plus, on large projects, it can make your terminal feel sluggish.

Let's do better.

The Professional Way: Using cloc or tokei

For accurate, meaningful code analysis, use dedicated tools like cloc (Count Lines of Code) or tokei. These tools provide detailed breakdowns and exclude irrelevant content.

Option 1: Using cloc

cloc is a mature, reliable tool that's been around for years. Here's how to get started:

Step 1: Install cloc

Choose your preferred installation method:

1npm install -g cloc        # Node.js method
2choco install cloc         # Chocolatey (Windows)
3brew install cloc          # Homebrew (macOS)


Step 2: Run It

Navigate to your project directory and run:

1cloc .
2

Sample Output:

1----------------------------------------------------------------------------
2Language                     files          blank        comment           code
3----------------------------------------------------------------------------
4TypeScript                      30            400            200           3500
5JavaScript                       5             80             40            600
6----------------------------------------------------------------------------


You get the real deal—actual code lines, properly categorized.

Pro tip: If cloc isn't working in VS Code on Windows, try running it in Git Bash or PowerShell directly in your project folder.

Excluding Unwanted Directories

To avoid counting build artifacts, dependencies, or editor configurations:

1cloc . --exclude-dir=node_modules,.next,.cursor
2

Or exclude all hidden directories (like .git, .vscode, etc.):

1cloc . --not-match-d='^\.'
2

Option 2: Using Tokei (The Speed Demon)

If you're working with a massive codebase and need results fast, Tokei is your friend. Written in Rust, it's blazingly fast.

Installation and Usage:

Visit Tokei's GitHub repository for installation instructions.

Basic usage:

1tokei .
2

Excluding directories:

1tokei . --exclude node_modules --exclude .next --exclude .cursor
2

Automating the Process

Add these scripts to your package.json for easy access:

1{
2  "scripts": {
3    "count-lines": "cloc . --exclude-dir=node_modules,.next,.cursor",
4    "count-tokei": "tokei . --exclude node_modules --exclude .next --exclude .cursor"
5  }
6}


Now you can run:

1npm run count-lines
2

Perfect for CI pipelines, code audits, or regular project health checks.

Quick Reference Commands

cloc:

1cloc . --exclude-dir=node_modules,.next,.cursor
2

Tokei:

1tokei . --exclude node_modules --exclude .next --exclude .cursor
2

PowerShell (Quick & Dirty):

1Get-ChildItem -Recurse -Include *.ts,*.tsx,*.js,*.jsx | Get-Content | Measure-Object -Line
2

Final Thoughts

Remember: lines of code ≠ better code. Use these tools to develop intuition about your project's complexity, identify refactoring opportunities, and maintain a healthy codebase. Whether you choose cloc for its maturity or tokei for its speed, you'll have the insights you need to make informed decisions about your code.

Contact

devDaman