Skip to content

S0003 function-lines

Rule: A function stays under its line limit.

Code S0003 (stable, never reused)
Id function-lines
Category size
Source spirit_level
Fix by hand

Level per tier

Tier Level Reason
small warn a function longer than a screen is changed without being read whole
small-workspace warn a function longer than a screen is changed without being read whole
medium warn a function longer than a screen is changed without being read whole
big warn a function longer than a screen is changed without being read whole
ai off
embedded off
ui off
egui off
units-strict off
units-paranoid off

Parameters

Name Type Default Meaning
max-lines integer 150 code lines of one function above which it is reported
target-lines integer 0 a lower limit to tighten toward: a function above it but within max-lines is reported as a warning at most; 0: none

Why

A function longer than a screen is read in parts and changed without seeing all of it. Long functions are where state machines hide and where edits collide. Test functions are not counted.

Tightening the limit

target-lines sets a lower limit to move toward. A function above the target but within max-lines is reported as a warning, even where the lint denies, so the limit can come down one step at a time without a flood of failures:

[lints.function-lines]
max-lines = 150
target-lines = 120

When the warnings are gone, max-lines takes the target's value and the target moves on.

How to fix

Name the steps and make each a function; a long match often wants one function per arm.

Configure

[lints.function-lines]
level = "deny"
max-lines = 150
target-lines = 0
reason = "why this project wants it at this level"

Allow one finding in the code with // spirit-level: allow(function-lines) <reason> (waivers); the code works there too, as in [lints.S0003].