ShtukbaseStandard

Code: smells after Fowler

code-refactoring · version 0.1.0
Code8 norms6 checks

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

NormWhat holdsLevelEnforcer
RF-R01A function is shortSHOULDcheck long-functions
RF-R02A file and a class are smallSHOULDcheck large-modules
RF-R03The same code is not repeatedSHOULDcheck duplicate-blocks
RF-R04A function has few parametersSHOULDcheck long-params
RF-R05Nesting is shallowSHOULDcheck deep-nesting
RF-R06A method works with its own dataMAYcheck feature-envy
RF-R07One change in one placeSHOULDjudge judges/shotgun-surgery.md
RF-R08Rework is separate from a change of behaviorSHOULDjudge 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:

KeyDefaultWhat it means
functionLines60length of a function in lines of code (RF-R01)
fileLines600length of a file in lines of code (RF-R02)
classLines300length of a class or impl block in lines of code (RF-R02)
minLines8length of a repeated block in significant lines (RF-R03)
maxParams5number of a function's parameters (RF-R04)
maxNesting4nesting depth of blocks (RF-R05)
envyMin8accesses to a foreign object that make a method count as envious (RF-R06)
distinctMin4distinct 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

IDNormLevelModeEnforcer
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

NameWhat it doesFileWhen
long-functionsA function is short
norm RF-R01
checks/long_functions.ts
check
large-modulesA file and a class are small
norm RF-R02
checks/large_modules.ts
check
duplicate-blocksThe same code is not repeated
norm RF-R03
checks/duplicate_blocks.ts
check
long-paramsA function has few parameters
norm RF-R04
checks/long_params.ts
check
deep-nestingNesting is shallow
norm RF-R05
checks/deep_nesting.ts
check
feature-envyA method works with its own data
norm RF-R06
checks/feature_envy.ts
check
reviewA prompt for a reviewing agent over the package norms: by diff or by filesactions/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.

Package files 10

They go into .shtukbase/packages/code-refactoring/; actions run from there.