Refactoring: Cheat-Sheet

Please visit my other Object-Oriented Programming articles.

Reaping the most benefits from refactoring

Use the following quick cheat-sheet when refactoring.

Remove unhelpful commentsRemove comments which state the obvious or replace unfocussed blocks of code with clearly-named methods following the Singe Responsibility Principle (SRP).
Remove zombie code – code commented-out to no longer be used.
Remove comments repeating intent which should be in source control (previous changes).
Variable declarationsWait to declare/use when need them – avoid laundry-list variables at the top of methods, which are too easy to forget. Limit the scope of variables.
*Visual Studio: r-click -> Find All References to determine where variables are used.
Excessive indentationExcessive indented code – “arrow” code due to its arrow shape of indentation, sign of high cyclomatic complexity.
Positive ConditionalsUse VS shortcut to invert If statements.
R-click -> Quick Actions & Refactoring.
Validation ChecksEnsure incoming data is valid and if not return early, avoiding cyclomatic complexity.
Variable namesEnsure names convey intent. Read all uses of the variable to determine a more durable name.
Can r-click to rename so VS auto-updates all uses of new variable name.
Related blocks of codeReplace with with a SRP method.
R-click -> Quick Actions & Refactoring -> Extract Method.
Literal ValuesReplace literal values use in logic with Db table-drive methods, which are easier to maintain.