| 手段 | const / final / val / readonly |
|---|---|
| 范围 | 局部变量 → 字段 → 集合内容 |
| 收益 | 变更点集中,推理简单 |
对照
对照
// JS
const total = items.reduce((s, i) => s + i.price, 0);
// Java
final BigDecimal total = ...;
// Rust
let total = ...; // 绑定默认不可变把能不变的变量都声明为不可变:缩小可变状态范围,审查时一眼分清「会变的地方」;与 Split Variable、Replace Temp with Query 协同使用,副作用面持续收窄。
| 手段 | const / final / val / readonly |
|---|---|
| 范围 | 局部变量 → 字段 → 集合内容 |
| 收益 | 变更点集中,推理简单 |
// JS
const total = items.reduce((s, i) => s + i.price, 0);
// Java
final BigDecimal total = ...;
// Rust
let total = ...; // 绑定默认不可变