Scout Refactor & Complex Tramp Data
Scouts' Rule
The Scouts have a rule, "Leave a place at least as clean as you found it." The Scout Rule as adopted to refactoring applies this principle to code, if you touch the code and there's an opportunity to fix something, even if beyond the scope of your assignment, fix it.
Some simple examples (focused on improving encapsulation):
- if a clause, method, or class isn't used or is used only be a unit test, delete it
- if a clause, method, or class is used only by another class, move it to that class and mark it private
- if a clause, method, or class is used only by another package, move it to that package and mark it protected
Tramp Data
One code smell that's somewhat common is Tramp Data. Tramp Data is a when a method argument is simply passed to a sub-method without being directly visited or used. When this happens several levels deep, it creates undue cognitive load as the developer has to hold onto the full call stack before identifying why/how the argument is used.
Compounding Tramp Data is when the argument is non-primative.
Example:
class Widget {Here, not only does the developer have to delve several layers down to understand how the argument is used, anytime he/she wants to change Widget, it may be necessary to perform this investigation. First, it would be better to alter the method signature/call to doX(widget.a). This way at least, the impact of changes to Widget cease at the doX call. This eliminates 3 layers of call stack from the developer's limited cognitive load.
Trim the Tree
By starting at leaf methods like doZ and working your way up, successively pruning inputs, you'll soon find your overall code easier to follow. This can be easily applied in Scout fashion, opportunistically upon method visit.
Conflicts
This refactor sometimes conflicts with a "Too Many Parameters" code smell. Static code analysis may in fact discourage this practice because a real world example may replace a single Widget-like input with supernumerary parameters.The warning here wouldn't be incorrect. It may be appropriate to create a new subset grouping of Widget's members for this sect of functionality or refactor Widget itself.
Developers should be aware of both "Too Many Parameters" and the problem explored here. It's best to weigh the trade-offs rather than naively follow the code-analysis.
Comments
Post a Comment