By Tim King · Updated
Code Line Counter
Comment Ratio and Longest Line Stats for C-Style, Python, SQL, HTML and CSS
OneTask is free, with no sign-up, and built by one person.
If this tool saved you time, a coffee helps keep new ones coming.
How the Code Line Counter works
This code line counter splits a source file into code, comment and blank lines and reports the comment ratio and longest line. Pick the language family so it knows which comment markers and string quotes to expect. Everything runs in your browser, so private code stays private.
Lines of code is the oldest software metric and still one of the most quoted. It sizes estimates, appears in audit and acquisition reports, feeds licence-compliance scans and gives a rough sense of how big a change is. The number is only useful when it is counted the same way each time, which means separating real code from comments and blank space.
How the code line counter classifies lines
Each line lands in exactly one bucket:
- Blank lines contain nothing but spaces or tabs.
- Comment lines contain comment text and no code, including every line inside a multi-line block comment.
- Code lines contain at least one character of code. A line with code and a trailing comment,
such as
x = 1 // reset, counts as code.
This follows the convention used by well-known counters such as cloc, so the figures are comparable with the reports those tools produce. Physical lines are counted, not statements, so a long call split across five lines counts as five.
Why strings matter
A naive counter treats every // or # as the start of a comment, which goes wrong on code such as
url = "https://example.com" or color: "#fff". This tool tracks string literals for the chosen
language and ignores comment markers inside them. In C-style languages, backtick template strings
can span several lines, and those lines count as code. Escaped quotes inside strings are handled
too.
Choosing the language
The options group languages that share comment syntax:
- C-style covers JavaScript, TypeScript, Java, C, C++, C#, Go, PHP, Rust, Swift, Kotlin and Dart:
//line comments and/* */blocks. - Python uses
#comments. Triple-quoted strings on their own lines are counted as comments, because that is how docstrings are used in practice. - HTML and XML use
<!-- -->blocks. Inline scripts and styles count as code. - CSS, SCSS and Less use
/* */blocks. SCSS and Less also allow//, so lines starting with it count as code here; switch to C-style for files that use those heavily. - SQL uses
--line comments and/* */blocks. - Shell, Ruby, Perl and YAML share
#comments. A shebang line counts as a comment. - Lua uses
--and--[[ ]]blocks.
Reading the comment ratio
The comment ratio is comment lines divided by code plus comment lines, leaving blanks out. There is no correct figure, but typical open-source projects sit between 10 and 30 percent. Libraries with generated API documentation often run higher. A ratio near zero on a large file suggests code that will be hard for the next person to change. A very high ratio can mean commented-out code or licence headers inflating the count, both worth a look.
Blank lines are usually 10 to 20 percent of a file. Far fewer can mean dense code that is hard to scan; far more often comes from copy and paste or generated output.
The longest line
The report gives the longest line's length and line number. Most style guides cap lines somewhere between 80 and 120 characters: PEP 8 asks for 79 in Python, Google's Java guide for 100, and Prettier wraps at 80 by default. A line far past your limit is often a long string, a minified chunk or a deeply nested expression worth splitting.
What line counts cannot tell you
Lines of code measure size, not value or effort. The same feature can be 50 lines in one language and 500 in another, and the shortest solution is often the best one. Use the count to compare a file with itself over time, or similar files with each other, rather than to rank developers or estimate cost on its own.
For a whole repository, a command-line tool that walks every file and detects the language from the extension will be faster. This tool is for a quick check on a single file or a pasted snippet in a code review, where installing anything is overkill.
FAQ
Does a line with code and a comment count as code?
Yes. Any line with at least one character of code is a code line, even if it ends in a comment. This matches cloc and most other line counters, so your numbers stay comparable with their reports.
Are blank lines inside a block comment counted as blank?
Yes. A line with nothing but whitespace is blank wherever it appears. Lines inside a block comment that hold any text count as comment lines, including lines that are just an asterisk.
Which option should I use for TypeScript or Go?
Use C-style. It covers every language that uses // line comments and /* */ block comments, which
includes TypeScript, Go, Java, C#, Rust, Swift, Kotlin and PHP.
Is my code uploaded?
No. The count runs in your browser with JavaScript. Your source code is never sent to a server, so the tool is safe for proprietary or unreleased projects.
See also: all Dev Utilities tools, Formatters & Validators, Data & Encoding, Security & Identifiers, SEO, Accessibility, AI.
Related tools
JavaScript Formatter
Expand minified JavaScript into indented code, or squash it back.
Indent Code
Shift every line right by a set number of spaces or tabs.
Character Counter
Characters against SMS, meta and social post limits.
Word Counter
Words, six readability scores, keyword density and platform limits.
.htaccess Generator
Build an Apache .htaccess from toggles for HTTPS, caching, headers and more.