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.
About the package
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.
Norms 8
| ID | Norm | Level | Mode | Enforcer |
|---|---|---|---|---|
| RF-R01 | A function is no longer than the functionLines threshold of lines of code (60 by default); a long one is split into named parts. |
SHOULD | observes | sensor |
| RF-R02 | A file is no longer than fileLines lines of code (600), a class or impl block is no longer than classLines (300). |
SHOULD | observes | sensor |
| RF-R03 | The same block of minLines significant lines (8) is not written in two places. |
SHOULD | observes | sensor |
| RF-R04 | A function has no more than maxParams parameters (5); self, this and cls do not count. |
SHOULD | observes | sensor |
| RF-R05 | Blocks inside a function are nested no deeper than maxNesting levels (4). |
SHOULD | observes | sensor |
| RF-R06 | A method accesses its own object no less often than a foreign one: a parameter no more than envyMin times (8) and no more than twice as often as self/this. |
MAY | observes | sensor |
| RF-R07 | One change of meaning does not require small edits in many files; if it does, the shared knowledge is gathered in one place. |
SHOULD | observes | judge |
| RF-R08 | Refactoring (rework without a change of behavior) and a change of behavior go in different commits; refactoring does not change tests, except renames. |
SHOULD | observes | judge |
"Blocks": the enforcer stops a violation. "Observes": it only notes it. Level MUST means required; SHOULD means yes by default.
Actions 7
| Name | What it does | File | When |
|---|---|---|---|
| long-functions | A function is short norm RF-R01 | checks/long_functions.ts | check |
| large-modules | A file and a class are small norm RF-R02 | checks/large_modules.ts | check |
| duplicate-blocks | The same code is not repeated norm RF-R03 | checks/duplicate_blocks.ts | check |
| long-params | A function has few parameters norm RF-R04 | checks/long_params.ts | check |
| deep-nesting | Nesting is shallow norm RF-R05 | checks/deep_nesting.ts | check |
| feature-envy | A method works with its own data norm RF-R06 | checks/feature_envy.ts | check |
| review | A prompt for a reviewing agent over the package norms: by diff or by files | actions/review.ts | shtuk run |
shtuk check runs the checks; shtuk run code-refactoring <name> runs any action. "After install" actions run by themselves only if the package comes from a trusted source.
Pages 9
They go into the project at these paths.
- /norms/code/
- Settings: Code: smells after Fowler page
- /norms/rules/code-refactoring/
- A function is short RF-R01
- A file and a class are small RF-R02
- The same code is not repeated RF-R03
- A function has few parameters RF-R04
- Nesting is shallow RF-R05
- A method works with its own data RF-R06
- One change in one place RF-R07
- Rework is separate from a change of behavior RF-R08
Package files 10
They go into .shtukbase/packages/code-refactoring/; actions run from there.