shtukbasehub

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.

Packagecode-refactoringv0.1.020 files

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

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.