I receive this question the other day by a friend at that moment I realize that I've never thought about it. How do I know where to start when refactoring? And how do I know is the right time? The second question is a little bit obvious the right time is any time. Refactoring is the art of getting your code in shape and you should be doing it often; but the first one is a little bit trickier and I will try to share some tips to consider when you are doing this skill.
Code Climate, reek, roodii, flay, etc...
Sometimes what you should do is just to use any code metrics tool and start refactoring what they suggest is bad code.
- Advantages: Very quick to spot where are some problems.
- Disadvantages: There are some things that this tools points out that probably are better left alone just because even when they are code smells is the best thing to do at that particular moment in time.
What, private methods? Why? Well this is a technique that I picked up from Sandi Metz; probably she said it somewhere. She tries not to use private methods on classes; so I decide that when I see some private methods that's an indication that there's probably a concept hiding.
- Advantages: You probably will start to add new smaller classes which will brings more reuse of code.
- Disadvantages: Sometimes you really don't need to extract a new class to use a gsub on something value. The fact that you will have more classes will implies that you will have some physical restructuration of your code base to find things faster.
Follow your heart
Nop; this is not a joke you can always use your intuition to find where the code is smelly and just refactor on that.
- Advantages: You will feel like a human compiler
- Disadvantages: If you don't really know about code smells you probably will start to introduce some of your own.