Please visit my other Object-Oriented Programming articles.
Reaping the most benefits from refactoring
Use the following quick cheat-sheet when refactoring.
Topic | Task |
Remove unhelpful comments | Remove 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 declarations | Wait 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 indentation | Excessive indented code – “arrow” code due to its arrow shape of indentation, sign of high cyclomatic complexity. |
Positive Conditionals | Use VS shortcut to invert If statements. R-click -> Quick Actions & Refactoring. |
Validation Checks | Ensure incoming data is valid and if not return early, avoiding cyclomatic complexity. |
Variable names | Ensure 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 code | Replace with with a SRP method. R-click -> Quick Actions & Refactoring -> Extract Method. |
Literal Values | Replace literal values use in logic with Db table-drive methods, which are easier to maintain. |