Comment Policy
Type: Policy. How to write comments in production code.
Purpose
A comment helps a colleague understand a decision whose reasons are difficult to recover from code alone. Module purpose, important constraints, and invariants provide useful context. Clear names and code structure already explain ordinary mechanics; the comment adds what is needed for maintenance.
Basic rules
An optional local comment explains a decision, invariant or constraint that is hard to recover from code alone. It adds context rather than repeating obvious actions.
Keep comment style and language consistent within a file. Write natural-language text in Russian. API, type and protocol names and established technical terms may remain in English when translation would reduce precision.
A comment does not extend the code’s contract. Guarantees, invariants, execution order, safety and constraints must be supported by code, types or tests.
Refactoring history belongs in Git, ADRs and design notes. Comments describe the current reason for the current decision.
Upper architectural block
Each production module that participates in runtime, defines a layer contract or has a nontrivial responsibility must have one top-level architectural comment, subject to the exceptions below. It describes the module as a whole: the file’s role, allowed responsibility, boundaries and responsibilities that belong elsewhere.
Template:
The module's role in the system is [file role].
That role covers [responsibility 1], [responsibility 2], and [responsibility 3].
[Responsibility outside this scope] belongs to another module.
Good example:
The application use-case module orchestrates one application scenario through ports.
- Coordinates application services.
- Calls ports and checks application invariants.
- Builds the final response contract.
CLI and HTTP details and direct SDK calls belong to other modules.
Bad example:
File for network fit.
Such a comment does not explain the role of the module, its boundaries and acceptable responsibilities.
Exceptions
Upper architectural block is optional for:
index.ts/ barrel export files;- generated files;
- test fixtures;
- trivial files with one obvious type or constant;
- temporary local helper files that do not define a production contract;
- trivial Svelte composition under the conditions described there.
Functions and methods in utility modules
Every function and method in a module serving as a utility or tool must have a comment. This includes private and nested helpers, returned handlers, and object methods. The rule also applies to build tools and test helper modules. A module's role is determined by its purpose, rather than its filename or directory.
Place the comment before the declaration and describe the operation's purpose and any relevant constraints, side effects, or local state. A clear name does not remove this requirement. A module comment does not replace comments on individual operations; do not restate implementation lines.
JSDoc public contracts
JSDoc for public functions, classes and interfaces must be concise and specific. It describes only contract details:
- input restrictions;
- side effects;
- errors;
- lifecycle;
- important guarantees;
- external API restrictions.
In other modules, JSDoc may be omitted if the meaning is completely clear from the name, types, and code.
Bad:
getCandles()returns candles.
Better:
getCandles()returns normalized candles for the application layer. External provider errors propagate to the calling scenario.
When a comment is needed
A comment is needed if it explains:
- module role;
- layer boundary;
- is a non-obvious invariant;
- workaround;
- external API limitation;
- the reason for the unusual branch;
- reason for cleanup logic;
- maintaining compatibility with the old contract;
- intentionally empty branch if the lack of action is part of the logic;
- placeholder / TODO with an explanation of why there is no implementation yet;
- the reason why the dependency is allowed here.
Good:
The external runtime starts only when processing the first range with enough data.
session ??= await this.trainer.start(request.training);
Good:
An empty batch counts as a successful use-case result because the caller already applied its filters.
if (ranges.length === 0) {
return emptyResult();
}
Good:
The output includes the legacy profile field expected by the CLI caller. Within the pipeline, the profile has already been selected through
mlProfile.
response.featureProfile = profile.featureProfile.name;
When a comment is not needed
No additional comment is needed if:
- the meaning is already clear from the function name, type and code;
- comment repeats the line literally;
- comment describes trivial mechanics;
- comment explains the syntax of the language;
- comment becomes outdated faster than the code itself;
Bad:
Start trainer
session = await this.trainer.start(request.training);
Bad:
Check if ranges is empty
if (ranges.length === 0) {
return emptyResult();
}
Bad:
Increment counter
count++;
Remove an optional comment if it can be deleted without losing meaning.
TODO and placeholder
TODO is valid only if it explains:
- why the implementation was postponed;
- what restriction is currently in effect;
- Under what condition should you return to this place.
Good:
TODO. Move target normalization to the ML profile when a second target profile is added. Production profiles currently share one target contract.
Bad:
TODO: fix later
If a module or branch is intentionally empty, this must be indicated explicitly.
Good:
The module is reserved for an application hook for post-training diagnostics. Diagnostics are CLI-only until the application response stabilizes. Writing to stdout and handling CLI flags belong to the CLI.
Workaround-comments
Workaround should explain the external constraint, not just describe a workaround.
Good:
PyTorch BCE with AMP can produce unstable values in mixed precision. Training on probability targets uses the logits contract.
loss = binaryCrossEntropyWithLogits(predictedLogits, targetProbability);
Bad:
Use BCEWithLogits
loss = binaryCrossEntropyWithLogits(predictedLogits, targetProbability);
Comments and architectural boundaries
A comment is useful when it protects the boundary of a layer.
Good:
The infrastructure adapter maps data from an external market data SDK to an application-level port.
- Calls T-Invest SDK.
- Converts SDK DTOs into domain/application DTOs.
- Normalizes external provider errors.
Feature engineering, ML profile selection, and use-case orchestration belong to other modules.
This block helps not to mix adapter, domain logic and application use-case.
Comments and tests
Comments do not replace tests.
If a comment captures an important invariant, it is desirable that it be protected by a test.
For example, if a comment says that the order features is a production contract, this should be checked by the registry/profile test.
Indicator order is part of the feature matrix's production contract. The Python model receives a positional matrix without column names.
const indicators = [
'CloseLocationValue',
'SignedStreak',
'VolRatio',
];
Application practice
- Write clear code first.
- Then add required comments; add other comments only where their absence would hide the reason for a decision.
- When reviewing, remove noisy comments as confidently as dead code.
- After refactoring, check comments for relevance.
- Do not use a comment to excuse a poor name or an unclear module responsibility.