Freigeben über


Extracting methods is just another level of indirection, or is it?

Over the last months (since I first tried the object calisthenics rules) I've been starting to create smaller and smaller methods. And this is a thing that have gotten me into discussions before. I've been met with arguments that having all these small methods makes the code harder to understand because you need to jump around in the code to get the whole picture. And I didn't really understand why I didn't feel the same way until today when I had a similar discussion with someone I hadn't discussed this before. It turns out that it is a trust issue. In order to make small methods actually being easy to understand you have to believe that each method called actually does what it says it's doing. And I'm not talking bout the comment for the method. I'm talking about the actual name of the method. If the method name is DoShowMessageBox you have to trust that is the only thing it is doing so the fact that the body is just MessageBox.Show or something else should not matter in the method where DoShowMessageBox is being called.

But just because you do not trust that each method does what it says it should do I still think small methods are beneficial. Establishing trust (i.e. understand) for a small method is in my opinion easier and faster than a large one so in the long run you'll build the trust you need. So in the long run you'll benefit from writing shorter methods and trusting each method to do what it says it does. If you want some more reasons you might want to listen to uncle bob.