How to Keep Method Size Under Control

Do you ever open a source code file and see a method that starts at the top of your screen and kind of oozes its way to the bottom with no end in sight? When you find yourself in that situation, imagine that you’re reading a ticker tape and try to guess at where the method actually ends. Is it a foot below the monitor? Three feet? Does it plummet through the floor and into the basement, perhaps down past the water table and into the earth’s mantle?

Visualized like this, I think everyone might agree that there’s some point at which the drop is too far, though there’s likely some disagreement on where exactly this is. Personally, I used to subscribe to the “fits on a screen” heuristic and would only start looking to pull out methods if it got beyond that. But in more recent years, I think even smaller. How small? I dunno–five or six lines, max. Small enough that you’ll only ever see one try-catch or control flow statement in there. Yeah, seriously, that small. If you’re thinking it sounds kind of crazy, I get that, but give it a try for a while. I can almost guarantee that you’ll lose your patience for looking at methods that cause you to think, “wait, where was loopCounter declared again–before the second or third while loop?”

If you accept the premise that this is a good way to do things or that it might at least be worth a try, the first thing you’ll probably wonder is how to go about doing this from a practical standpoint. I’ve definitely encountered people and even whole groups who considered method sizes like this to be impractical. The first thing you have to do is let go of the notion that classes are in some kind of limited supply and you have to be careful not to use too many. Same with modules, if your project gets big enough. The reason I say this is that having small methods means that you’re going to have a lot of them. This in turn means that they’re going to need to be spread to multiple classes, and those classes will occupy more namespaces and modules. But that’s okay. If you encounter a large application that’s well designed and factored, it’s that way because the application is actually a series of small, focused components working together. Monolithic doesn’t scale well.

Getting Down to Business

If you’ve prepared yourself for the reality of needing more classes organized into more namespaces and modules, you’ve really overcome the biggest obstacle to being a small-method coder. Now it’s just a question of mechanics and practice. And this is actually important–it’s not sufficient to just say, “I’m going to write a lot of methods by stopping at the fifth line, no matter what.” I guarantee you that this is going to create a lot of weird cross-coupling, unnecessary state, and ugly things like out parameters. Nobody wants that. So it’s time to look to the art of creating abstractions.

As a brief digression, I’ve recently picked up a copy of Uncle Bob Martin’s Clean Code: A Handbook of Agile Software Craftsmanship and been tearing my way through it pretty quickly. I’d already seen most of the Clean Coder video series, which covers some similar ground, but the book is both a good review and a source of new and different information. To be blunt, if you’re ever going to invest thirty or forty bucks in getting better at your craft, this is the thing to buy. It’s opinionated, sometimes controversial, incredibly specific, and absolute mandatory reading. It will change your outlook on writing code and make you better at what you do, even if you don’t agree with every single point in it (though I don’t find much with which to take issue, personally).

The reason I mention this book and series is that there is an entire section in the book about functions/methods, and two of its fundamental points are that (1) functions should do one thing and one thing only, and (2) that functions should have one level of abstraction. To keep those methods under control, this is a great place to start. I’d like to dive a little deeper, however, because “do one thing” and “one level of abstraction per function” are general instructions. It may seem a bit like hand-waving without examples and more concrete heuristics.

Extract Finer-Grained Details

What Uncle Bob is saying about mixed abstractions can be demonstrated in this code snippet:

public void OpenTheDoor() { GrabTheDoorKnob(); TwistTheDoorKnob(); TightenYourBiceps(); BendYourElbow(); KeepYourForearmStraight(); } 1 2 3 4 5 6 7 8 public void OpenTheDoor ( ) { GrabTheDoorKnob ( ) ; TwistTheDoorKnob ( ) ; TightenYourBiceps ( ) ; BendYourElbow ( ) ; KeepYourForearmStraight ( ) ; }

Do you see what the issue is? We have a method here that describes (via sub-methods that are not pictured) how to open a door. The first two calls talk in terms of actions between you and the door, but the next three calls suddenly dive into the specifics of how to pull the door open in terms of actions taken by your muscles, joints, tendons, etc. These are two different layers of abstractions: one about a person interacting with his or her surroundings and the other detailing the mechanics of body movement. To make it consistent, we could get more detailed in the first two actions in terms of extending arms and tightening fingers. But we’re trying to keep methods small and focused, so what we really want is to do this:

public void OpenTheDoor() { GrabTheDoorKnob(); TwistTheDoorKnob(); PullOpenTheDoor(); } private static void PullOpenTheDoor() { TightenYourBiceps(); BendYourElbow(); KeepYourForeArmStraight(); } 1 2 3 4 5 6 7 8 9 10 11 12 13 public void OpenTheDoor ( ) { GrabTheDoorKnob ( ) ; TwistTheDoorKnob ( ) ; PullOpenTheDoor ( ) ; } private static void PullOpenTheDoor ( ) { TightenYourBiceps ( ) ; BendYourElbow ( ) ; KeepYourForeArmStraight ( ) ; }

Create Coarser Grained Categories

What about a different problem? Let’s say that you have a method that’s long, but it isn’t because you are mixing abstraction levels:

public void CookQuesadilla() { ChopOninons(); ShredCheese(); GetOutThePan(); AddOilToPan(); TurnOnTheStove(); SprinkleOnionsAndCheeseOnTortilla(); PutTortillaInPan(); CookUntilFirm(); FoldTortillaAndCookUntilBrown(); FlipTortillaAndCookUntilBrown(); RemoveCookedQuesadilla(); RemovePanFromStove(); ScrubPanWithBrush(); ServeQuesadillas(); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public void CookQuesadilla ( ) { ChopOninons ( ) ; ShredCheese ( ) ; GetOutThePan ( ) ; AddOilToPan ( ) ; TurnOnTheStove ( ) ; SprinkleOnionsAndCheeseOnTortilla ( ) ; PutTortillaInPan ( ) ; CookUntilFirm ( ) ; FoldTortillaAndCookUntilBrown ( ) ; FlipTortillaAndCookUntilBrown ( ) ; RemoveCookedQuesadilla ( ) ; RemovePanFromStove ( ) ; ScrubPanWithBrush ( ) ; ServeQuesadillas ( ) ; }

These items are all at the same level of abstraction, but there are an awful lot of them. In the previous example, we were able to tighten up the method by making the abstraction levels consistent, but here we’re going to actually need to add a layer of abstraction. This winds up looking a little better:

public void CookQuesadilla() { PrepareIngredients(); PrepareEquipment(); PerformActualCooking(); FinishUp(); } private static void PrepareIngredients() { ChopOninons(); ShredCheese(); } private static void PrepareEquipment() { GetOutThePan(); AddOilToPan(); TurnOnTheStove(); } private static void PerformActualCooking() { SprinkleOnionsAndCheeseOnTortilla(); PutTortillaInPan(); CookUntilFirm(); FoldTortillaAndCookUntilBrown(); FlipTortillaAndCookUntilBrown(); RemoveCookedQuesadilla(); } private static void FinishUp() { RemovePanFromStove(); ScrubPanWithBrush(); ServeQuesadillas(); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 public void CookQuesadilla ( ) { PrepareIngredients ( ) ; PrepareEquipment ( ) ; PerformActualCooking ( ) ; FinishUp ( ) ; } private static void PrepareIngredients ( ) { ChopOninons ( ) ; ShredCheese ( ) ; } private static void PrepareEquipment ( ) { GetOutThePan ( ) ; AddOilToPan ( ) ; TurnOnTheStove ( ) ; } private static void PerformActualCooking ( ) { SprinkleOnionsAndCheeseOnTortilla ( ) ; PutTortillaInPan ( ) ; CookUntilFirm ( ) ; FoldTortillaAndCookUntilBrown ( ) ; FlipTortillaAndCookUntilBrown ( ) ; RemoveCookedQuesadilla ( ) ; } private static void FinishUp ( ) { RemovePanFromStove ( ) ; ScrubPanWithBrush ( ) ; ServeQuesadillas ( ) ; }

In essence, we’ve created categories and put the actions from the long method into them. What we’ve really done here is create (or add to) a tree-like structure of methods. The public method is the root, and it had thirteen children. We gave it instead four children, and each of those children has between two and five children of its own. To tighten up methods, it’s perfectly viable to add “nodes” to the “tree” of your call stack. While “do one thing” is still a little elusive, this seems to be carrying us in that direction. There’s no individual method that you look at and think, “boy, that’s a lot of stuff going on.” Certainly its a matter of some art and taste, but this is probably a good way to think of it–organize stuff into hierarchical method categories until you look at each method and think, “I could probably memorize what that does if I needed to.”

Recognize that Control Flow Uses Up an Abstraction

So far we’ve been conceptually figuring out how to organize families of methods into well-balanced tree structures, and that’s taken us through some pretty mundane code. This code has involved none of the usual stuff that sends apps careening off the rails into bug land, such as conditionals, loops, assignment, etc. Let’s correct that. Looking at the code above, think of how you’d modify this to allow for the preparation of an arbitrary number of quesadillas. Would it be this?

public void CookQuesadillas(int numberOfQuesadillas) { PrepareIngredients(); PrepareEquipment(); for(int i = 0; i < numberOfQuesadillas; i++) PerformActualCooking(); FinishUp(); } 1 2 3 4 5 6 7 8 public void CookQuesadillas ( int numberOfQuesadillas ) { PrepareIngredients ( ) ; PrepareEquipment ( ) ; for ( int i = 0 ; i < numberOfQuesadillas ; i ++ ) PerformActualCooking ( ) ; FinishUp ( ) ; }

Well, that makes sense, right? Just like the last version, this is something you could read conversationally while in the kitchen just as easily as you do in the code. Prep your ingredients, then prep your equipment, then for some integer index equal to zero and less than the number of quesadillas you want to cook, increment the integer by one. Each time you do that, cook the quesadilla. Oh, wait. I think we just went careening into the nerdiest kitchen narrative ever. If Gordon Ramsey were in charge, he’d have strangled you with your apron for that. Hmm… how ’bout this?

public void CookQuesadillas(int numberOfQuesadillas) { PrepareIngredients(); PrepareEquipment(); PerformActualCooking(numberOfQuesadillas); FinishUp(); } private static void PerformActualCooking(int numberOfQuesadillas) { for (int index = 0; index < numberOfQuesadillas; index++) { SprinkleOnionsAndCheeseOnTortilla(); PutTortillaInPan(); CookUntilFirm(); FoldTortillaAndCookUntilBrown(); FlipTortillaAndCookUntilBrown(); RemoveCookedQuesadilla(); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public void CookQuesadillas ( int numberOfQuesadillas ) { PrepareIngredients ( ) ; PrepareEquipment ( ) ; PerformActualCooking ( numberOfQuesadillas ) ; FinishUp ( ) ; } private static void PerformActualCooking ( int numberOfQuesadillas ) { for ( int index = 0 ; index < numberOfQuesadillas ; index ++ ) { SprinkleOnionsAndCheeseOnTortilla ( ) ; PutTortillaInPan ( ) ; CookUntilFirm ( ) ; FoldTortillaAndCookUntilBrown ( ) ; FlipTortillaAndCookUntilBrown ( ) ; RemoveCookedQuesadilla ( ) ; } }

Well, I’d say that the CookQuesadillas method looks a lot better, but do we like “PerformActualCooking?” The whole situation is an improvement, but I’m not a huge fan, personally. I’m still mixing control flow with a series of domain concepts. PerformActualCooking is still both a story about for-loops and about cooking. Let’s try something else:

public void CookQuesadillas(int numberOfQuesadillas) { PrepareIngredients(); PrepareEquipment(); PerformActualCooking(numberOfQuesadillas); FinishUp(); } private static void PerformActualCooking(int numberOfQuesadillas) { for (int index = 0; index < numberOfQuesadillas; index++) CookAQuesadilla(); } private static void CookAQuesadilla() { SprinkleOnionsAndCheeseOnTortilla(); PutTortillaInPan(); CookUntilFirm(); FoldTortillaAndCookUntilBrown(); FlipTortillaAndCookUntilBrown(); RemoveCookedQuesadilla(); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 public void CookQuesadillas ( int numberOfQuesadillas ) { PrepareIngredients ( ) ; PrepareEquipment ( ) ; PerformActualCooking ( numberOfQuesadillas ) ; FinishUp ( ) ; } private static void PerformActualCooking ( int numberOfQuesadillas ) { for ( int index = 0 ; index < numberOfQuesadillas ; index ++ ) CookAQuesadilla ( ) ; } private static void CookAQuesadilla ( ) { SprinkleOnionsAndCheeseOnTortilla ( ) ; PutTortillaInPan ( ) ; CookUntilFirm ( ) ; FoldTortillaAndCookUntilBrown ( ) ; FlipTortillaAndCookUntilBrown ( ) ; RemoveCookedQuesadilla ( ) ; }

We’ve added a node to the tree that some might say is one too many, but I disagree. What I like is the fact that we have two methods that contain nothing but abstractions about the domain knowledge of cooking and we have a bridging method that brings in the detailed realities of the programming language. We’re isolating things like looping, counting, conditionals, etc. from the actual problem solving and story telling that we want to do here. So when you have a method that does a few things and you think about adding some kind of control flow to it, remember that you’re introducing a detail to the method that is at a lower level of abstraction and should probably have its own node in the tree.

Adrift in a Sea of Tiny Methods

If you’re looking at this cooking example, it probably strikes you that there are no fewer than eighteen methods in this class, not counting any additional sub-methods or elided properties (which are really just methods in C# anyway). That’s a lot for a class, and you may think that I’m encouraging you to write classes with dozens of methods. That isn’t the case. So far what we’ve done is started to create trees of many small methods with a public method and then a ton of private methods, which is a code smell called “Iceberg Class.” What’s the cure for iceberg classes? Extracting classes from them. Maybe you turn the first two methods that prepare ingredients and equipment into a “Preparer” class with two public methods, “PrepareIngredients” and “PrepareEquipment.” Or maybe you extract a quesadilla cooking class.

It’s really going to vary based on your situation, but the point is that you take this opportunity pick nodes in your growing tree of methods and sub-methods and convert them into roots by turning them into classes. And if doing this leads you to having what seems to be too many classes in your namespace? Create more namespaces. Too many of those in a module? Create more modules. Too many modules/projects in a solution? More solutions.

Here’s the thing: the complexity exists no matter how many or few methods/classes/namespaces/modules/solutions you have. Slamming them all into monolithic constructs together doesn’t eliminate or even hide that complexity, though many seem to take the ostrich approach and pretend that it does. Your code isn’t somehow ‘simpler’ because you have one solution with one project that has ten classes, each with 300 methods of 7,000 lines. Sure, things look simple when you fire up the IDE, but they sure won’t be simple when you try to debug. In fact, they’ll be much more complicated because your functionality will be hopelessly interwoven with weird temporal couplings, ad-hoc references, and hidden dependencies.

If you create large trees of functionality, you have the luxury of making the structure of the tree the representative of the application’s complexity, with each node an island of simplicity. It is in these node-methods that the business logic takes place and that getting things right is most important. And by managing your abstractions, you keep these nodes easy to reason about. If you structure the tree correctly and follow good OOP design and practice, you’ll find that even the structure of the tree is not especially complicated since each node provides a good representative abstraction for its sub-tree.

Having small, readable, self-documenting methods is no pipe dream. Really, with a bit of practice, it’s not even very hard. It just requires you to see code a little bit differently. See it as a series of hierarchical stories and abstractions rather than as a bunch of loops, counters, pointers, and control flow statements, and the people that maintain what you write, including yourself, will thank you for it.