Skip to content

The neighbours

By the end of this chapter you know the other tools that answer "how much code", and which question each of them is really answering. They are characterised here, not benchmarked — the lab ran only wc head-to-head, because a race between definitions proves nothing.

wc -l — the abacus

wc -l counts newline characters. It knows no languages, no comments, no blanks — and chapter 11 showed both its virtues (it is everywhere and means the same thing on every machine) and its two failure modes (the unterminated last line it misses, and the comment banner it counts as code). Reach for it when the question is "roughly how big is this file", never when the question mentions code.

cloc — the elder

cloc has been counting code since 2006, in Perl, with its own language definitions and its own answers to the definition questions — including treating Python docstrings as comments where tokei's default says code. If a project's history, a report or a colleague's number comes from cloc, expect the two tools to agree on plain files and diverge exactly where this guide found rules speaking. Neither is the referee of the other; chapter 11's procedure — pin the definition, then compare — is how you reconcile them.

scc — the estimator

scc counts in the same family way, very fast, and then goes further: it estimates the effort and cost of the code from the counts, using a standard cost model. If you want that estimate, it is the tool; read its dollar figures as the model's opinion about the count, not as a second fact the count produced.

GitHub Linguist — the census-taker

The language percentages on a GitHub repository come from Linguist, which answers a different question: not "how many code lines" but "what is this repository made of", measured in bytes, with vendored and generated files classified away by its own heuristics. A repository can be "90% Python" by Linguist while tokei finds most of its lines in a data file Linguist disregards. Both are right; the questions differ.

The hand-rolled counter

Sooner or later someone writes ten lines of shell that grep for // and call the rest code. Chapter 6 is its obituary: strings containing markers, block comments spanning lines, nesting, docstrings — each rule exists because the simple version fails on real files. If you must count something tokei does not know, the honest hand-rolled route is to state your definition as plainly as this guide states tokei's, and keep the specimens that prove it.

Check: name the neighbour you would use to (a) sanity-check a file's rough size, (b) reproduce a decade-old project report, and (c) explain a GitHub language bar. If cloc, wc and Linguist each got a job, you have this chapter.