ThisCalculator

Everyday

Count Lines of Code in Programming Language

Updated · Standard formula, shown below

Lines of code 800 lines
Code (excluding blanks)
900 lines
Result
Total lines
1000 lines
You entered
Comment lines
100 lines
You entered

Counting lines of code (LOC) in a programming language means measuring the size of a source code file or project by tallying its lines. You typically use a command-line tool like wc -l on Unix-like systems, or a dedicated code analysis tool, and you get a number representing the total lines, which can be further refined to count only physical lines, logical lines, or non-comment non-blank lines.

Formula

The basic formula for counting lines of code is straightforward:

LOC = Total Lines – Comment Lines – Blank Lines

Where:

  • Total Lines (t) – every line in the file, including empty lines, comments, and code.
  • Comment Lines (c) – lines that contain only comments or are part of a block comment.
  • Blank Lines (b) – lines with no characters or only whitespace.

This gives you the physical lines of code. Many tools also report logical lines, which count statements (e.g., a line with two statements separated by a semicolon counts as two logical lines).

Worked Example

Using the default values from the calculator above (Total = 1000, Comments = 100, Blank = 100):

  1. Start with total lines: 1000
  2. Subtract comment lines: 1000 – 100 = 900
  3. Subtract blank lines: 900 – 100 = 800

The result is 800 lines of code. This matches the calculator output.

Why Count Lines of Code?

Lines of code is a common, albeit imperfect, metric used for several purposes:

  • Project size estimation – a rough gauge of the scale of a codebase.
  • Productivity measurement – though widely criticized, some organizations track LOC per developer.
  • Code maintenance cost – more lines often mean more potential bugs and higher maintenance effort.
  • License compliance – some open-source licenses require counting lines for attribution.
  • Personal tracking – developers may count their own output for learning or fun.

How to Count Lines of Code

There are several ways to count lines, depending on your needs and environment.

Using Command Line Tools

On Linux, macOS, and Windows (with WSL or Git Bash), the simplest method is:

wc -l filename.py

This counts newline characters. To count across multiple files:

wc -l *.py

For a more detailed breakdown, tools like cloc (Count Lines of Code) are popular. They automatically detect programming languages and separate code, comments, and blanks.

Using Integrated Development Environments (IDEs)

Many IDEs have built-in statistics. For example:

  • Visual Studio Code – extensions like “Line Counter” can show LOC per file.
  • IntelliJ IDEA – has a “Statistics” plugin.
  • Eclipse – has a “Metrics” view.

Common Mistakes and Misconceptions

Counting lines of code seems trivial, but there are pitfalls:

  • Mixing physical and logical lines – a single physical line can contain multiple statements. For example, a = 1; b = 2; is one physical line but two logical lines.
  • Ignoring multi-line comments – a block comment spanning 10 lines is often counted as 10 comment lines, but some tools count it as one comment block.
  • Whitespace-only lines – some tools count them as blank, others as code if they contain indentation.
  • Different definitions of “code” – does a line with only a closing brace } count as code? Most tools say yes, but some exclude it.
  • Line ending differences – Windows uses \r\n while Unix uses \n. A file with Windows line endings will have the same line count, but some naive tools might miscount.

Alternatives to Lines of Code

Because LOC is easily gameable and doesn’t reflect complexity, other metrics are often used:

  • Cyclomatic complexity – measures the number of independent paths through code.
  • Halstead metrics – based on operators and operands.
  • Code coverage – percentage of code executed during tests.
  • Function points – measure functionality from a user perspective.
  • Code churn – how often lines change, indicating instability.

Limitations of the Metric

Lines of code should be used with caution:

  • It rewards verbose code – a developer who writes more lines may appear more productive, even if the code is inefficient.
  • It punishes concise, elegant solutions – a 10-line function that replaces a 100-line one is a win, but LOC would suggest the opposite.
  • It doesn’t account for code quality, readability, or performance.
  • Different languages have different densities – a feature that takes 100 lines in Python might take 300 in Java, not because it’s more complex but because Java is more verbose.

Frequently Asked Questions

What is the difference between physical and logical lines of code?

Physical lines are the actual lines in the file, counted by newline characters. Logical lines are the number of statements or expressions, regardless of how they are formatted. For example, a loop with a body on one line (for i in range(10): print(i)) is one physical line but two logical lines (the loop and the print).

How do I count lines of code in a GitHub repository?

You can clone the repository and use a tool like cloc locally. Alternatively, some online services (like GitHub’s own “Insights” for some repositories) show line counts, but these are not always accurate. The most reliable way is to clone and run cloc . in the project root.

Does a blank line inside a function count as a line of code?

No, blank lines are typically excluded from LOC counts. They are counted separately as “blank lines.” The formula subtracts them from the total.

What is a good number of lines of code for a project?

There is no “good” number. It depends on the language, the problem domain, and the team’s style. A small utility might be 100 lines, while a large enterprise application could be millions. Focus on readability and maintainability rather than a target line count.

Can I count lines of code in a PDF or Word document?

No, those are not source code files. LOC applies to plain text source files. If you need to count lines in a document, you would first export it as plain text.

Why do my line counts differ between tools?

Different tools define “code,” “comment,” and “blank” differently. For instance, some tools count a line with only a closing brace as code, others as blank. Some count inline comments (like code // comment) as both code and comment, others only as code. Always check the tool’s documentation and settings.

Related

Formula used
in t | Total lines | lines | 1000 | 0
in c | Comment lines | lines | 100 | 0
in b | Blank lines | lines | 100 | 0
out loc | Lines of code | lines | t - c - b | 0
out cloc | Code (excluding blanks) | lines | t - b | 0

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.