octobot/skills/engineering/bdd/refactoring-r.md
Lukasz Boryn bb35812d45 refactor(skills): unify sibling-file naming on lowercase-kebab topic names
Renames the 8 UPPERCASE offload files to topic-named lowercase-kebab and
updates all 33 referring sites, including four OKF cites: blocks that
okf-check would otherwise fail on.

moor's generic reference file splits into graduate.md + collapse.md — it held
two topics, and one-topic-per-file is the rule being enforced; the two links
in moor/SKILL.md lose their anchors and address the files directly.
write-readme's becomes ecosystems.md.

Fixes the doctrine first, in both authorities that state it: CLAUDE.md 5 no
longer prescribes a generic catch-all name, and conjure-a-skill step 4 gains
the casing rule it omitted. Without that, the next skill author reads CLAUDE.md
and recreates the violation.

Both the new doctrine and the LEARNINGS note state the rule positively, naming
no offender: the blueprint's locked Verify greps for the old filenames, so
prose that lists them trips the gate meant to prove the sweep finished.

Implements blueprint 014; flips it to implemented with its index line.
Verify: 0 error(s), 0 warning(s), exit 0.
2026-07-30 13:04:33 +02:00

116 lines
4.0 KiB
Markdown

# Refactor candidates (R)
After every bdd cycle goes green, scan for these. Refactor only with a
green test bar.
## Duplication → extract a function
Three lines that recur in two places: probably noise. Three *similar* lines
in three places, with a shared shape: extract.
```r
# Before — same column rename pattern in three pipelines
df1 |> rename(gene = gene_symbol, expr = expression)
df2 |> rename(gene = gene_symbol, expr = expression)
df3 |> rename(gene = gene_symbol, expr = expression)
```
```r
# After
normalize_cols <- function(df) rename(df, gene = gene_symbol, expr = expression)
```
Resist extracting on first sight — premature abstractions are worse than
duplication.
## Long functions → private helpers, public interface unchanged
A 200-line `query_modb()` can be split into `prepare_request()`,
`fetch_pages()`, `parse_response()` — *kept inside the same `box` module
without `box::export()`*. Tests still target `query_modb()`. The helpers
are an internal convenience, not part of the contract.
```r
# R/modb/client.R
box::export(query_modb) # only this is public
query_modb <- function(id) prepare_request(id) |> fetch_pages() |> parse_response()
prepare_request <- function(id) { ... }
fetch_pages <- function(req) { ... }
parse_response <- function(raw) { ... }
```
## Shallow modules → combine or deepen
Two modules that always travel together, each exporting one thin function?
Merge them. A module that exports eight helpers users always call in the
same order? Deepen — collapse them behind one entry point. See
[`deep-modules-r.md`](deep-modules-r.md) for the R shape, and
[`../design-codebase/deepening.md`](../design-codebase/deepening.md) for the
dependency-aware strategy (replace, don't layer — old tests on the shallow
modules become waste once the deepened interface has its own tests).
## Feature envy → move logic to where the data lives
A function in `R/plot/plot_bxp.R` that does heavy reshaping of a
`modb_result` tibble probably belongs in `R/modb/client.R` (or wherever
`modb_result` is constructed). The test of `plot_bxp` then becomes about
plotting, not about reshape.
```r
# Before — plot module reaches into modb internals
build_bxp_plot <- function(modb_raw) {
df <- modb_raw$data |> filter(...) |> pivot_longer(...) |> rename(...)
ggplot(df, aes(...)) + geom_point()
}
```
```r
# After — modb module owns its shape
# R/modb/client.R
as_long_expr <- function(modb_raw) modb_raw$data |> filter(...) |> pivot_longer(...)
# R/plot/plot_bxp.R
build_bxp_plot <- function(modb_raw) {
ggplot(as_long_expr(modb_raw), aes(...)) + geom_point()
}
```
## Primitive obsession → S3 value objects
Passing `dataset_id = "DS001"`, `genes = c("ENSG1","ENSG2")`, `cohort =
"PRODUCTION"` everywhere? Wrap them in a constructor + validator:
```r
new_query <- function(dataset_id, genes, cohort = "PRODUCTION") {
stopifnot(is.character(dataset_id), length(dataset_id) == 1L)
structure(
list(dataset_id = dataset_id, genes = genes, cohort = cohort),
class = "modb_query"
)
}
validate_query <- function(q) { ... ; q }
```
Now `query_modb()` takes one `modb_query`, every test builds one with
`new_query(...)`, and validation lives in one place.
## Existing code the new code reveals as problematic
The most valuable refactor after a TDD cycle is sometimes *not* in the code
you just wrote. New tests often expose nearby code that is untested,
implicit, or duplicated. Flag it; don't necessarily fix it in the same
commit, but capture the finding so it isn't forgotten.
## `targets` pipelines
Refactor target definitions to keep transforms pure: each `tar_target` is
a function of upstream targets, no side effects. If a target writes a file
out-of-band, that file is invisible to `targets`' invalidation and your
tests can't reason about it. Return the data; let `targets` cache it.
## Never refactor while red
If the suite is red, fix the test or fix the code — do not "tidy while
fixing". Two changes mixed together are two regressions waiting to happen.