Signs you should refactor

I’ve been trying to look at my code a bit more critically, which always feels like one of the hardest things to do.

  1. Methods are long. If the method is getting lengthy look it over and see if it tries to handle too many things. I can’t tell you a definitive number of lines before it is a warning sign because it varies depending on what you are doing. When you see or write a long method, just give it a read through and think about whether any of it would make sense in it’s own method.
  2. Too many parameters. When you have too many parameters, look at them and decide if some of the parameters are related. If there are any that are related, is there an existing object that ties them together? Should there be one? Consolidating the parameters or even a portion of the functionality into a new class may a good way to clean it up.
  3. Lots of temporary variables. When you use a lot of them, look through the class and see if you are duplicating any code. Do you find yourself using the same temporary variables often? Perhaps moving them to a method of their own, albeit small, will help keep the code DRY.
  4. Too many properties. If there are a lot of properties it may mean you have too much going on in this class. Look at the properties and see if grouping them into similar ones helps reveal a hidden class you can pull out.
  5. Not using accessor methods. This one I see the most benefit from when the class is utilized in other classes. By using accessor methods you don’t tie a value to the property. You have the ability to hook in or pass additional formatting or optional parameters in.

One thought on “Signs you should refactor”

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>