What Is Refactoring?
Definition
Refactoring is a disciplined technique for restructuring existing code without changing its external behavior. Each refactoring is a small, behavior-preserving transformation: rename a variable, extract a method, replace a deprecated command with its modern equivalent. Applied consistently, refactorings keep a codebase readable, maintainable, and approachable to new contributors.
A change that alters behavior — replacing an algorithm with a faster one, fixing a bug, adding a feature — is not a refactoring. Behavior preservation is the defining property.
Inspiration: Martin Fowler's Refactoring
DFRefactor is inspired by Martin Fowler's book Refactoring: Improving the Design of Existing Code. The second edition (Addison-Wesley, 2019) updated the original 1999 catalogue of refactorings and codified the modern practice of small, behavior-preserving code transformations.
Fowler's central observation is that refactoring is not a one-time cleanup activity. It is a continuous discipline that runs alongside normal development. Done well, it keeps a codebase responsive to change and approachable to new contributors.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
— Martin Fowler
DFRefactor automates a subset of these refactorings for the DataFlex language. The functions it provides apply behavior-preserving transformations to legacy syntax, replacing deprecated commands with modern equivalents, removing unused declarations, and normalizing formatting and casing.
Why refactor DataFlex code?
The DataFlex language dates from the 1980s. Over four decades it has accumulated syntactic forms that newer versions have deprecated, retired, or hidden from the documentation. Code from character-mode DataFlex or the early Visual DataFlex era — Calc/MoveStr, [Found]/[FindErr] indicators, LT/EQ operators, command-style Pos/Replace, Shadow_State — usually still compiles in current Studio releases, but it is hard to read, hard to teach, and does not work well with modern Studio tools (statement completion, syntax coloring, the modern grid and list classes).
One boundary matters more than the rest: DataFlex 2021 (version 20.0) introduced native 64-bit and full Unicode support. Code that was written before that release usually contains the most legacy syntax, and gains the most from refactoring — Unicode in particular interacts with how source is read, written, and encoded. DFRefactor itself requires one of the two most recent DataFlex Studio versions installed to compile and run, but it can refactor source from any earlier version.
Bringing legacy DataFlex code up to modern standards has two main benefits:
Maintainability. Readable code is cheaper to maintain. Clear identifiers, consistent casing, single-purpose methods, and accurate comments reduce the time required to locate and correct defects. Refactoring also removes misleading or outdated comments, which are a recurring source of confusion.
Extensibility. Refactored code is easier to extend. Modern idioms and recognizable patterns make it possible to add new features without disturbing existing structure. Code that adheres to current Studio conventions can be edited more safely with Studio's own tooling.
Why automated refactoring?
Manual refactoring is reliable but slow. Across a large codebase, hand-applying even simple transformations — changing every Calc statement to Move, every Eq to =, every Current_Object to Self — is tedious and error-prone.
Automated refactoring has three advantages:
- Less manual work. A function that has been tested with many kinds of input can be applied to thousands of lines in seconds.
- Fewer errors. Refactoring functions in DFRefactor are checked by unit tests in the UnitTests program before each release.
- The same result every time. Running the same set of functions over the same source produces the same result. This makes it possible to integrate refactoring into a CI process or to apply identical transformations across multiple branches.
DFRefactor is intentionally conservative: each function targets a narrow, well-defined transformation. Users are encouraged to apply a small number of functions per run and to commit (or back up) source code between runs. See Program Settings for the built-in backup mechanism.