I’ve been trying to look at my code a bit more critically, which always feels like one of the hardest things to do.
- 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.
- 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.
- 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.
- 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.
- 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.