Skip to content

chore: inline needlessly extracted single-use helpers - #14048

Merged
ndeloof merged 2 commits into
mainfrom
cleanup-needless-extractions
Aug 14, 2026
Merged

chore: inline needlessly extracted single-use helpers#14048
ndeloof merged 2 commits into
mainfrom
cleanup-needless-extractions

Conversation

@ndeloof

@ndeloof ndeloof commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Systematic "extract method" helps nobody when the extracted function has a single caller, no test, and no responsibility of its own: the reader jumps around instead of reading the flow top-down. This PR inlines the small helpers where the extraction had no real boundary — actual reuse, testability or a responsibility seam — and keeps everything that has one.

Inlined (single caller, no dedicated test, body was a plain slice of its caller's flow): one-line trampolines that other call sites already bypass (getExecTarget, attachContainer, logContainer, removeImage); helpers whose name hid a mutation or promised something else (prepareLabels mutated the map it received, checkSelectedServices filtered rather than checked, hasMore read like a predicate but restarted a spinner); and chunks whose name added nothing over the code itself (displayDryRunBuildEvent — longer than its caller, escapeDollarSign, buildVolume/Tmpfs/ImageOptions, viewFromStackList, extractEnvCLIDefined → canonical types.NewMappingWithEquals().ToMapping(), isPullPolicyValid → package-level list + slices.Contains).

Renamed/shared instead of inlined: validateNavigationMenuresolveNavigationMenu (it resolves and mutates, validates nothing); setServiceScale moved to scale.go and now used by runScale, which duplicated its four lines inline.

Deliberately untouched: the xxxCommand/runXxx cobra idiom (uniform across the package), symmetric families (toBake*, ensure*Down, …), callbacks/iterators, conversions with a real seam, everything protected by the gocyclo 16 threshold (prepareRun sits exactly at 16), and printEvent's color switch — it differs from colorFn on api.Working (SuccessColor vs nocolor), so unifying them would change the display.

No behavior change; golangci-lint (incl. gocyclo) and unit tests pass.

@ndeloof
ndeloof requested review from a team as code owners August 14, 2026 16:34
@ndeloof
ndeloof requested a review from glours August 14, 2026 16:34
thaJeztah
thaJeztah previously approved these changes Aug 14, 2026

@thaJeztah thaJeztah left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

left some suggestions

Comment thread cmd/compose/list.go Outdated
Comment thread cmd/compose/scale.go
Comment on lines +104 to +112
func setServiceScale(project *types.Project, name string, replicas int) error {
service, err := project.GetService(name)
if err != nil {
return err
}
service.SetScale(replicas)
project.Services[name] = service
return nil
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is used in two places, but possibly even worth considering inlining it in both places;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather keep this one: it has two real callers (applyScaleOpts in create.go and runScale), and the value-semantics dance (GetServiceSetScale → write back into project.Services[name]) is easy to get wrong — forgetting the write-back is a silent no-op. Before this PR runScale had exactly that duplication, drifting from the helper. Real reuse is the boundary this PR tries to preserve; happy to inline both if you feel strongly about it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right yeah, the project.Services[name] = service may be easily overlooked. Yup, that's fair, no problem!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I was actually looking; couldn't we just iterate over project.Services, but there was also "disabled services" etc to take into account).

Comment thread pkg/compose/down.go Outdated
Inline small functions that were extracted from their single call site
without a real boundary to justify it — no reuse, no dedicated test, no
responsibility of their own — so each caller now tells its whole story
top-down:

- getExecTarget, attachContainer, logContainer: one-line trampolines to
  getSpecifiedContainer / doAttachContainer / doLogContainer, which
  other call sites already use directly
- removeImage: single-statement wrapper, unlike its removeVolume
  sibling which has actual logic
- checkSelectedServices: named like a validation, actually a filter;
  the subtle rule (an unknown service is only an error with an explicit
  compose file) now reads where options.Services is rewritten
- prepareLabels: mutated the map it received while looking pure at the
  call site; the label writes are now visible in getCreateConfigs
- setDefaultTarget: mutation-by-pointer of the loop copy, now visible
  in the loop of injectFileReferences
- buildVolumeOptions/buildTmpfsOptions/buildImageOptions: nil-guard +
  field copies; the buildMountOptions switch now shows side by side
  what each mount type propagates (buildBindOption keeps real logic and
  keeps buildMountOptions under the gocyclo limit)
- displayDryRunBuildEvent: was longer than its only caller
- hasMore: read like a predicate, was a one-line spinner restart
- escapeDollarSign: wrapped a single bytes.ReplaceAll
- extractEnvCLIDefined: replaced by the canonical compose-go helper
  types.NewMappingWithEquals().ToMapping(), as run.go already does
- isPullPolicyValid: rebuilt the valid-values slice on every call; now
  a package-level list checked at the call site
- viewFromStackList: projection now sits next to the render closure
  that consumes exactly its three fields

No behavior change; single gocyclo threshold untouched.

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
validateNavigationMenu validated nothing: it resolves the TTY /
COMPOSE_MENU / --menu precedence and mutates opts.navigationMenu —
rename to resolveNavigationMenu.

runScale duplicated setServiceScale's GetService/SetScale/write-back
dance inline; move the helper next to its natural home in scale.go and
use it from both callers.

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>

@thaJeztah thaJeztah left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ndeloof
ndeloof merged commit ddc4b04 into main Aug 14, 2026
94 of 96 checks passed
@ndeloof
ndeloof deleted the cleanup-needless-extractions branch August 14, 2026 20:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants