- Is there a private variable, let's call it x that is private to the function that is being set within each (or at least a majority) of the if/case legs whose value is a method call?
- Is x is being returned at the end of the method?
- You got nested conditionals?
Here is what it would would like:
- result private variable at the beginning of the function
- result is being returned at the end of the function
- Nasty nasty nasty nested conditional that sets result to the value of a method
If the above is true or the code you are refactoring looks similar to this, you might want to look into using guard clauses. Sounds more complicated than it really is. Basically see if you can refactor it like so:
- Remove the private variable thats being set during each if/else leg.
- Return the method or the condition that is setting it.
Simple as that. Here is the code above refactored:
- No more nested if's
- After each if/else leg, we are returning the method.
Given that some legacy methods you are trying to refactor are not as trivial as this and each have their own unique level of suck - this is always a good start. This keeps your method simple, less complex and highly testable because each execution path is linear. Guard clauses baby, a good alternative to the strategy pattern.