Introduction
ReSharper is a .NET IDE plug-in to give more sophisticated editing, navigation, refactoring, and code generations.
Link
Refactoring Definition:
"Refactoring is the exercise of taking functional code, and while not increasing its functionality, increasing its quality"
-- Debugging is therefore the act of altering the code to fix a specific bug, while not intrinsically increasing the quality of the code around it.
Oh, ReSharper, how do I love thee, let me count the ways:
Refactoring should be a key skill of a software engineer. Once formal conception design is complete, there are always grey-areas or bad implementations to correct, or continuous improvement opportunities as we get a more complete understanding of how the software should be constructed.
The key concept, or rather precept, in refactoring is quality. To understand refactoring we therefore need to understand code quality (See article
What is quality code?).
Mantra's :-}
1st Mantra Of Refactoring:
If you are reading code, you should be planning how to refactor it.
2nd Mantra Of Refactoring:
Compiler-refactors, or automation-assisted refactors are King.
3rd Mantra Of Refactoring:
The single bane of refactoring is the introduced error.
3rd Mantras Corollary:
Unit tests and Refactoring is a match made in Heaven.
4th Mantra Of Refactoring:
If a bug is logged against a class, that class should be refactored.
Books
Refactoring is well covered as a key discipline/skill in print:
Refactoring: Improving the Design of Existing Code
Refactoring to Patterns
Design Patterns: Elements of Reusable Object-Oriented Software (Gang-Of-Four book!)
Rules Of Thumb:
- Refectoring should be explicitly listed on the project work breakdown.
- Refactoring should account for 10-30% of the effort during the construction and maintenance phases.
- Large-scale (non-compiler/automated) refactoring candidates should be listed, and then planned for project deadtime.
- Refactoring should be used as the key principal in Code Review sessions, altering code to discuss ideas and better quality implementations.
- All developers should engage in refactoring work this includes Architects, Leads, Senior and Junior developers.
- In general, a refactor that reduces total amount code, is normally successfull.
- The first tool of basic refactoring is the 'helper method'.
As my mentor (Anthony Steele) once said "Forget about OO, must people don't understand procedural programming"... Meaning, that most developers do not leverage the power of basic procedural programming to reduce or hide complexity.
- Refactoring should, in general, reduce the 'contract' the code has with the outside world. That is to say, a piece of code, or a class, or a method, should expose the smallest amount of complexity to the outside world.
Refactoring is a team skill
Refactoring is a colaborative dialogue -- if the team is constanly refactoring, but without cohesion the results will reflect this, the code will be a patchwork of divergent ideas. In my code world view consistency is more important the correctness(within bounds of reason). It is better to all do the wrong thing the same way, that to sometimes to the right thing in many diferent ways. If you have exprience in larger project with more than one strong personality -- you will recognise the truth of it. In part this is true, because you can organise a large-scale refactor of a first case (consistently wrong), but not the second case (partially right).
Back to the key point: Refactoring is a colaborative dialogue. The team should be constantly discussing posibilities for refactoring, in the short, medium and long term. Easy, medium and extreme refactors. The ongoing refactoring dialog has many advantages:
- It keeps things fun and interresting
- It serves to establish a common lingo/terminology and common design patterns (and
anti-patterns!)
- It introduces new ideas for design and implementation, and provides a basis for evaluating them (What would the new refactor give us? What is the cost? What is the Risk?).
Evaluating a Refactor
In very time terms, before we start a refactor we always ask the same questions:
1. What would the new refactor give us? This is the expected code quality increase. We evaluate the current and future benefits.
2. What is the cost? Essentially, man hours; but also includes the effort needed to motivated the required time from management and the rest of the team. It is important to include the oppurtunity cost of more features/functionalty what could have been implemented.
3. What is the risk? Knock-on bugs!