Line of code (LOC) is a software metric that counts the number of lines in a program’s source code. It is used to estimate the size of a software project, measure programmer productivity, and predict maintenance effort. The counting method can vary, but the basic idea is to count every line that contains code, a comment, or a blank line, depending on the specific definition used.
Formula
The formula for calculating Lines of Code is straightforward:
LOC = Total lines – Comment lines – Blank lines
Where:
- Total lines – the total number of lines in the source file(s), including blanks, comments, and code.
- Comment lines – lines that contain only comments or are part of a block comment.
- Blank lines – lines with no characters (or only whitespace).
This gives you the number of lines that contain actual code. Some definitions also count lines with both code and a trailing comment as code, which is what this formula does.
Worked Example
Using the default values from the calculator:
- Total lines = 100
- Comment lines = 20
- Blank lines = 10
Step 1: Add comment and blank lines: 20 + 10 = 30
Step 2: Subtract from total: 100 – 30 = 70
So the LOC is 70 lines. This means 70 lines contain actual source code.
Why LOC Matters
LOC is one of the oldest and most widely used metrics in software engineering. It gives a rough measure of project size, which can be used to:
- Estimate effort and cost for future projects based on historical data.
- Compare the size of different projects or modules.
- Track changes over time (e.g., how many lines added per release).
- Assess code density or complexity in combination with other metrics.
However, LOC is often criticized because it rewards writing more lines rather than writing better code. A 100-line solution that is clear and efficient is usually better than a 300-line solution that does the same thing but is convoluted.
How LOC is Counted
There are several ways to count lines of code, and the method can significantly affect the result. The most common distinctions are:
- Physical LOC – counts every line in the file, including blank lines and comments. This is the simplest method.
- Source LOC (SLOC) – excludes blank lines and comment-only lines. This is what most people mean when they say “LOC.”
- Logical LOC – counts statements or instructions, not physical lines. For example, a line with multiple statements (e.g.,
a = 1; b = 2;) might count as two logical lines.
Tools like cloc (Count Lines of Code) and wc -l (Unix word count) are commonly used. wc -l counts physical lines, while cloc can distinguish between code, comments, and blanks.
Common Mistakes and Pitfalls
When counting LOC, people often make these mistakes:
- Counting blank lines as code – this inflates the number and makes the code seem larger than it is.
- Ignoring multi-line comments – a block comment spanning 10 lines counts as 10 lines, but some tools may count it as one logical comment.
- Counting generated code – code that is auto-generated (e.g., from a parser generator) should usually be excluded, but many people forget to filter it out.
- Not accounting for different languages – a line of C++ is not the same as a line of Python; comparing LOC across languages is misleading.
Another common issue is that LOC is often used to measure productivity, but this is flawed. A programmer who writes 1000 lines a day is not necessarily more productive than one who writes 200 lines a day if the latter produces fewer bugs and more maintainable code.
How to Choose the Right Counting Method
The method you choose depends on your goal:
- If you want a quick size estimate, physical LOC is fine.
- If you want to compare codebases, use SLOC to avoid blank/comment inflation.
- If you are doing complexity analysis, logical LOC is more meaningful because it reflects the number of operations.
For most purposes, SLOC is the standard. Many tools default to it. Always state the method when reporting LOC, because a number without context is meaningless.
Limitations and Alternatives
LOC has significant limitations:
- It does not measure quality, efficiency, or readability.
- It varies wildly with programming language and style.
- It can be gamed (e.g., by putting each statement on a new line).
Alternatives and complements to LOC include:
- Function points – measure functionality based on user inputs, outputs, and files.
- Cyclomatic complexity – measures the number of linearly independent paths through code.
- Code coverage – measures how much of the code is executed during tests.
- Maintainability index – combines LOC, complexity, and other factors into a single score.
These metrics give a more rounded view of software quality than LOC alone.
Frequently Asked Questions
Does LOC include comments and blank lines?
It depends. Physical LOC includes everything; SLOC (source lines of code) excludes comments and blank lines. The calculator above uses the SLOC definition, which is the most common in practice.
What is the difference between LOC and SLOC?
LOC is a general term that can mean any line count. SLOC specifically refers to source lines of code, which are the lines that contain actual code instructions, excluding comments and blanks. In practice, many people use LOC to mean SLOC.
How many lines of code is a typical project?
It varies enormously. A simple script might be 50 lines, a small web app might be 5,000 lines, and a large enterprise system can be millions of lines. For example, the Linux kernel has over 20 million lines of code.
Can LOC be used to measure programmer productivity?
It is sometimes used, but it is not a good measure. Writing more lines does not mean writing better code. A developer who writes 100 lines of efficient, bug-free code is more productive than one who writes 500 lines of messy code with the same functionality.
What is a good LOC count for a function?
There is no universal rule, but many developers prefer functions to be under 50 lines for readability. Some coding standards suggest keeping functions under 20 lines. The key is that the function does one thing and is easy to understand.
How can I count LOC in my project?
You can use command-line tools like cloc or wc -l. For example, cloc . in a project directory will give a breakdown of code, comments, and blanks for many languages. Alternatively, you can use the calculator above by entering your total lines, comment lines, and blank lines.
Related
Formula used
in t | Total lines | lines | 100 | 0 | 1000000 in c | Comment lines | lines | 20 | 0 | 1000000 in b | Blank lines | lines | 10 | 0 | 1000000 out loc | Lines of code (LOC) | lines | t - c - b | 0 out sloc | Source lines of code (SLOC) | lines | t - c - 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.
More cards in this drawer
- OLED TVs in Dark Rooms: Why They Look Better and When They Don’t
- Philips DST7060/20 7000 Series Iron: Specs & Fit
- Coal Fueled Jet Aircraft: Could It Work?675 L
- Remote Desktop on Raspberry Pi: How to Enable It
- Word Select Last Line in Paragraph?
- Google Assistant IFTTT Recipes: 7 Useful Automations