Split Temporary Variable in Dart: One Variable, One Job

Split temporary variable means one variable, one job: if a local is assigned twice for two different purposes, rename per assignment. In Dart, declaring each split variable `final` makes the refactoring self-checking.

Last updated: October 23, 2026 A variable named temp holding a perimeter on line 4 and an area on line 9 is lying to you twice. Split Temporary Variable ends the lie: when a local is assigned more than once for different purposes, give each assignment its own variable โ€” the split temporary variable Dart developers apply first. One variable, one job. *Tested on Flutter 3.38 / Dart 3.10 (stable, October 2026). All snippets are pure Dart and run with dart run โ€” final locals and string interpolation used here are stable since Dart 2.x.* ๐ŸŽฌ Watch: Fowler on why temps complicate everything No dedicated video exists for this exact refactoring โ€” but Fowler's keynote has a section on exactly why temp variables make refactoring harder. ๐ŸŽฌ Watch: Software Design in the 21st Century At 30:00 Fowler demonstrates how temp variables complicate refactoring and why reducing them makes methods safer to change. 1:00:00 โฑ 1:00:00 ยท Martin Fowler โ€” click to play inline, or watch on YouTube . (The same talk's 25:00 mark covers the sibling technique, Replace Temp with Query โ€” the two refactorings are often confused, and the FAQ below separates them. Channel name unverified; duration is approximate.) Split temporary variable Dart refactoring: minimal working example Before: temp means perimeter, then โ€” without warning โ€” means area. After: two variables, two names, zero ambiguity. // BEFORE: one variable, two jobs void printStats({required double height, required double width}) { double temp = 2 * (height + width); print('Perimeter: $temp'); temp = height * width; // same variable, completely different meaning print('Area: $temp'); } void main() { printStats(height: 3, width: 4); // Perimeter: 14.0 // Area: 12.0 } // AFTER: one variable, one job voidโ€ฆ

Explore