Add cop for Metrics/PerceivedComplexity number gaming #1015
Labels
No labels
P0
P1
P2
P3
bug
category/code-change
category/cross-repo
category/eval-run
category/investigation
category/no-test-target
category/owner-ruling
category/prose-artifact
category/recurring-chore
category/structure
category/umbrella
crc-refactor
create
delete
enhancement
filed-by/agent
filed-by/user
frozen
lint-rule
needs-info
needs-triage
next
plugin/cc-architect
plugin/os
plugin/os-adr
plugin/os-aidd-lint
plugin/os-backlog
plugin/os-context
plugin/os-doc-hygiene
plugin/os-sdlc
plugin/os-vault
project/cc-os
ready-for-agent
ready-for-human
recurring
review
rule-proposal
update
waiting
wayfinder:grilling
wayfinder:map
wayfinder:map
wayfinder:research
wayfinder:task
wayfinder:task
wontfix
worklist/deviations
worklist/lint-rule
worklist/new-implement-build
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
jared/cc-os#1015
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem
The built-in cop
Metrics/PerceivedComplexitycan be gamed by two edits that keep the code as complex. RuboCop cannot change that cop. Analysis: docs/research/lint-lessons/metrics-perceived-complexity.md, sections 2c, 6a, and 6b.{ _1 ... }(andit) as 0. It scores{ |x| ... }as 1. Section 6b:@watch.to_h { [_1, ... ? ... : nil] } if @watchscores 4 against 5 for the named form.paths.select { a && b }becomespaths.select { a }.select { b }. The&&point disappears. Section 6a: this takeslint_targetfrom 5 to 4.Both edits pass the gate and keep the smell.
Reproduce
n/a: design ticket. Sketch for the numbered-parameter case:
bundle exec rubocop --only Metrics/PerceivedComplexityon a method withpaths.select { _1.end_with?(".rb") && _1.start_with?(root) }scores one point less than the named-parameter form.Approach
Add a new Sdlc cop. The implementer picks the name and the department (Sdlc/Metrics or Sdlc/Structural). The implementer probes each shape first and ships only the shapes that are decidable with no false positives.
(a) Numbered-parameter and
itblocks. Decidable. Candidate rule: report a conditional, ternary,&&, or||inside a numbered-parameter oritblock whose method is on RuboCop's own iterating-method list, because that is the point the built-in cop drops. Consider the alternative: a perceived-complexity variant that scores numbered blocks like named blocks. Use RuboCop's own list of iterating methods, not a hand-kept list.(b) Two consecutive
select,reject, orfiltercalls on one receiver chain. OPEN QUESTION (Jared to rule): this shape may have false positives.selecton a Sequel dataset selects columns, not rows. A receiver that is not an Enumerable cannot be told from syntax. The two blocks may be intentionally staged for readability. Probe plugins/ first. Ship (b) only if a deterministic receiver check removes the false positives. Otherwise leave (b) out and record the miss.Required work:
Sdlc/Metricsinplugins/os-sdlc/.rubocop.ymlAcceptance criteria
paths.select { _1.end_with?(".rb") && _1.start_with?(root) }, when the cop runs, then it reports the numbered-parameter block.itin place of_1, when the cop runs, then it reports the block.paths.map { _1.to_s }, when the cop runs, then it reports nothing.paths.select { a }.select { b }on an Enumerable receiver, when the cop runs, then it reports the second call. Given a Sequel datasetselectchain, then it reports nothing.Owner sketch
Non-goals
Changing
Metrics/PerceivedComplexity. Fixes at the #1004 and #1005 sites. The lesson text.Origin
Metrics/PerceivedComplexityoffenses in #1004 and #1005 (docs/research/lint-lessons/metrics-perceived-complexity.md)&&← DESIGN (analysis sections 2c, 6a, 6b)