도구 가이드
Developer 무료 회원가입 불필요

Code Line Counter

Count total, blank, comment, and code lines in any pasted code with language auto-detection.

도구를 불러오는 중…

이 도구에 대해

The Code Line Counter analyses pasted source code and breaks it down into four categories: total lines, blank lines, comment lines, and actual code lines. It automatically detects the programming language based on syntax patterns and applies the correct comment rules — double-slash (//) for JavaScript, TypeScript, Java, C, C++, C#, and Swift; hash (#) for Python, Ruby, Shell, and YAML; double-dash (--) for SQL and Lua; block comments (/* … */) for C-family languages; and HTML/XML comments (<!-- … -->). Results are displayed with progress bars showing the proportion of each line category, making it easy to gauge comment density and blank-line ratio at a glance. Useful for code review, metrics reporting, estimating documentation coverage, or satisfying coding standards that require a minimum comment ratio.

사용 방법

  1. 1 Paste your source code into the textarea.
  2. 2 The tool auto-detects the language and displays a label.
  3. 3 Override the language using the dropdown if detection is incorrect.
  4. 4 Read the four counters: Total, Blank, Comment, and Code lines.
  5. 5 Check the progress bars to visualise the proportion of each category.

Not all lines are created equal

"How many lines is this?" is a question with several answers. A file's raw line count includes blank spacers and comment blocks that explain the code but don't execute. What engineers usually care about is lines of code (LOC) — the lines that actually do something — plus how well-documented those lines are. This tool splits any pasted source into four buckets: total, code, comment, and blank. It then reports the percentages and a comment density figure, all computed in your browser. Crucially, it understands comment syntax per language, so it counts a # line as a comment in Python but as code in a language where # means something else.

How the counting works, line by line

The tool splits your text on newlines and walks each line in order, trimming surrounding whitespace first. The classification rules are:

  • A line that is empty after trimming counts as blank.
  • A line that begins with the language's line-comment marker (//, #, --, …) counts as a comment.
  • Inside a block comment (/* … */, <!-- … -->, Python triple-quotes, Lua --[[ … ]]), every line counts as a comment until the closer appears — the tool tracks the open/closed state as it scans.
  • Everything else is code, derived as total minus blank minus comment.

Because it tracks block state across lines, a multi-line license header or a docstring is fully attributed to comments rather than leaking into the code count.

A worked example

Consider this snippet:

// configure the server
const PORT = 3000;
 
/* startup
   banner */
start(PORT);

That is six lines total. Line 1 is a line comment. Lines 4 and 5 are a two-line block comment (the opener and its continuation). Line 3 is blank. Lines 2 and 6 are code. So the tool reports total 6, code 2, comment 3, blank 1. Comment density is comments divided by code-plus-comment — here 3 ÷ 5 = 60% — which tells you how heavily the executable lines are documented, independent of blank spacing.

Language detection and the languages it knows

Leave the selector on Auto-detect and the tool guesses the language before counting. It first checks for a shebang line (#!/usr/bin/env python → Python), then scores the text against keyword and syntax patterns — def and import point to Python, const/=> to JavaScript, SELECT/INSERT to SQL, <div>/<!DOCTYPE to HTML, and so on — and picks the highest-scoring match. It covers fourteen language families including JavaScript/TypeScript, Python, Java/Kotlin, C/C++/C#, Ruby, Go, Rust, PHP, SQL, HTML/XML, CSS, YAML, Lua, and Shell. The detected language is shown so you can confirm it, and you can always override the guess manually — important for short snippets where there isn't enough signal to detect confidently.

Genuine use cases

  • Sizing a task or estimate. A quick LOC count gives a rough sense of how much real code a file or paste contains, stripped of blanks and comments.
  • Checking documentation coverage. Comment density flags files that are either undocumented (near 0%) or so heavily commented they may be hiding unclear code.
  • Reviewing a pull request paste. Drop in a diff or new file to see at a glance how much is substance versus boilerplate and whitespace.
  • Teaching and grading. Compare two student solutions by their code-to-comment-to-blank breakdown rather than raw length.

Common mistakes and limitations

  • Trusting LOC as a quality metric. Fewer lines can be better or worse code; LOC measures size, not value. Treat it as context, not a score.
  • Inline trailing comments. A line like x = 1; // note is counted as code, because the line has executable content before the comment. The tool classifies whole lines, not fragments.
  • Comment markers inside strings. A string literal containing // or # at the very start of a line is rare but would be miscounted; the counter matches by leading marker, not by parsing the full grammar.
  • Auto-detect on tiny snippets. Two or three lines often lack enough keywords to score reliably — set the language manually when the detected guess looks off.

Privacy

Your source code is analyzed entirely in the browser. Splitting, detecting, and counting are all done in local JavaScript with no network request, so even proprietary code is safe to paste, and the tool runs the same way offline.

자주 묻는 질문

{# Alpine.js — self-hosted. (The previous jsdelivr CDN tag had a stale SRI integrity hash, so the browser refused to run it and window.Alpine was never defined — silently breaking every FAQ accordion and Alpine tool.) #}