Skip to content

Docstrings and embedded languages

By the end of this chapter you can explain the three kinds of line that are not quite what they look like — a comment that is really a string, a comment written to be read as prose, and a file that is two languages at once — and say what tokei does with each.

The docstring is a string

Python's module docstring looks like a comment and behaves like documentation, but in the language it is a string literal sitting at the top of the file. The lab's specimen, traps/docstring.py, is five lines of docstring, an empty line, and one line of code. Tokei counts it:

 |ixtures/traps/docstring.py            7            6            0            1

6 code, 0 comments. The five docstring lines are code because they are a string — and the empty line inside the string is code too, the exception promised in chapter 6: inside a string, there are no blank lines. The one blank is the genuinely empty line after the closing quotes.

Whether this is right depends on what you are counting for — which is the definition rule again. If your question is "how much documentation does this project carry", Python docstrings are documentation and tokei's default answer misleads you. Chapter 10 shows the setting, treat_doc_strings_as_comments, that flips this file to 1 code, 5 comments — and why it lives in a config file rather than a flag.

Doc comments are prose

Rust's /// and //! lines are comments that exist to be rendered as documentation. Tokei 15 treats them as embedded Markdown: in traps/nested.rs, the two doc-comment lines leave the Rust row (8 lines) and reappear as a Markdown child (2 lines, both comments), bringing the file's (Total) to the hand count of 10 lines, 4 code, 5 comments, 1 blank. The lines are partitioned between the two languages, not duplicated.

A web page is three files wearing one name

traps/page.html is a twelve-line page with a style block and a script block. Tokei reports:

  • HTML — 12 lines, all code: the host language keeps every line of its file.
  • CSS — 2 lines (1 code, 1 comment), the style block.
  • JavaScript — 2 lines (1 code, 1 comment), the script.

— and a (Total) of 16 lines for a file wc insists has 12. Here the embedded lines are counted twice: once as part of the host's 12, once under their own language. Compare the Markdown specimen in chapter 4, whose embedded line was partitioned. The difference is in how each host's definition hands lines to its guests; the lesson is that an embedded (Total) is worth checking against a physical count before you quote it.

Folding the children away

--compact (short form -C) hides the child rows: the same traps tree prints one row per language with the children folded out of sight. The totals do not change — the children are still counted, just not displayed (transcript tokei-lab/transcripts/t02-traps.txt). Use it when the split is noise; avoid it when the split is the story, as it was three times in this chapter.

Check: predict the count of traps/docstring.py under the definition "a docstring is documentation", then find the setting in chapter 10 that makes tokei agree with you. If your prediction was 1 code and 5 comments, you have this chapter.