Code: smells after Fowler
The school of Martin Fowler ("Refactoring", 1999; 2nd ed. 2018): code smells: long function, large class, duplicates, long parameter list, deep nesting, feature envy, shotgun surgery; six heuristics and two judges.
Code: smells after Fowler
The school. A code smell is a sign that the code should be reworked. A smell is not an error in itself, but a bad decision usually hides next to it. The rework goes in small steps, and after every step everything works.
The author. Martin Fowler (with Kent Beck for the chapter on smells). Source: Martin Fowler, "Refactoring: Improving the Design of Existing Code", Addison-Wesley, 1999; 2nd edition 2018. The catalog of smells is chapter 3, written together with Kent Beck. The norms below retell the ideas in our own words; they are not quotes.
When to take it. Living code that is changed often: applications, services, utilities. Especially when agents write the code: they readily grow functions and copy blocks.
When not to take it. Generated code, vendored libraries, throwaway scripts, data tables in code. Exclude them by path.
Norms
| Norm | What holds | Level | Enforcer |
|---|---|---|---|
| RF-R01 | A function is short | SHOULD | check long-functions |
| RF-R02 | A file and a class are small | SHOULD | check large-modules |
| RF-R03 | The same code is not repeated | SHOULD | check duplicate-blocks |
| RF-R04 | A function has few parameters | SHOULD | check long-params |
| RF-R05 | Nesting is shallow | SHOULD | check deep-nesting |
| RF-R06 | A method works with its own data | MAY | check feature-envy |
| RF-R07 | One change in one place | SHOULD | judge judges/shotgun-surgery.md |
| RF-R08 | Rework is separate from a change of behavior | SHOULD | judge judges/two-hats.md |
All norms are in observe mode: the checks print findings and pass, the judges are a procedure for the reviewer. A check you trust on your code is moved by the owner to mode: enforce in the norm's page, and then a finding fails shtuk check.
How to connect
shtuk install <path>/apps/standard/packages/code-refactoring --dir <project>
shtuk check --dir <project>
shtuk run code-refactoring review # a prompt for the reviewer over the uncommitted change
shtuk run code-refactoring review --diff main..HEAD
shtuk run code-refactoring review src/a.ts src/b.py
The package requires norms-core and code-lib (the shared code-parsing library and shared settings). It is installed on top of Basic.
Settings
Which code to read is the shared page /norms/code.md of the code-lib package: roots (roots), exclusions (exclude), languages, whether to read tests. This package's thresholds are the page /norms/code/code-refactoring.md:
| Key | Default | What it means |
|---|---|---|
functionLines | 60 | length of a function in lines of code (RF-R01) |
fileLines | 600 | length of a file in lines of code (RF-R02) |
classLines | 300 | length of a class or impl block in lines of code (RF-R02) |
minLines | 8 | length of a repeated block in significant lines (RF-R03) |
maxParams | 5 | number of a function's parameters (RF-R04) |
maxNesting | 4 | nesting depth of blocks (RF-R05) |
envyMin | 8 | accesses to a foreign object that make a method count as envious (RF-R06) |
distinctMin | 4 | distinct fields of a foreign object (RF-R06) |
The same page has its own exclude list (path masks from the project root). One place is released by the comment code-allow: <check name> on its line or the line above.
Honestly about the checks
The checks are rough heuristics: regular expressions and bracket parsing for TypeScript/JavaScript, Python and Rust, with no parsers and no dependencies. They err both ways, which is why they stand in observe. The meaning of a norm is held by the judge; the heuristic only shows where to look. 6 checks, 2 judges.