Tuesday, October 19, 2010

Refactoring pet peeves

Here's some stuff that irritates me in code
  • Class implementation (.cpp file), which is more than 10-15 pages
    • Have you created a monster class which does everything for everybody? Does it talk to the customer and is the customer at the same time? Does it clean the house and make a mess at the same time? Does it put the cabbage the goat and the wolf in the same boat, and hope they don't eat each other if you leave them alone for a second (see the wolf-goat-cabbage problem). Or does it hope that this fragile boat will actually survive the weight of all 3 animals and the peasant at the same time?
  • Class declarations, which take more than 2-3 pages. 
    • Same as above
  • Functions that take up more than 1-2 full screens
    • Very large functions become hard to read often involve unnecessarily complex interactions among locally declared variables. The longer your function goes on, the more variables you can declare within its scope, and the less clear your dependencies can become.
  • Function signatures that contains more than 4-5 parameters
    • OK, what is this function actually doing? Cmon let's get real, you don't really have a clue. At least consider creating a struct and passing that in instead.
    • If 4-5 of a class's member functions accept the same parameter, consider making it a data member of the class
  • Internal enums 
    • This is a special rant in and of it own
  • Internal classes
    • An internal class is is not your friend for the same reasons as an internal enum, except worse, as an internal class has a tendency to grow inside the belly of the outer class, making it even more likely than usual that the outer class will become a monster class. 

No comments:

Post a Comment