tags:

views:

269

answers:

7

Is it worth extracting private methods for code that only gets called once in a class, or leaving the code in the parent method (maybe) with a comment that says what it does?

+13  A: 

It is always worth extracting methods if it clarifies the intent of the code.

This is especially true for very long methods. While comments are fine, they do not delineate (at least not in a very solid way) where the process it explains ends and where the next one begins. Sometimes common abstractions will only become more obvious after you've extracted the method.

If you are into automated unit-testing (not necessarily TDD), it will also be much, much easier to unit test smaller chunks of methods -- although you might have to make your methods public for that, depending on the testing framework you use.

The point is, you can't go wrong with smaller methods, especially if they have descriptive names.

Jon Limjap
I very much agree. I refactor in this way all the time and the degree to which it adds readability to code is tremendous. Abstraction is very much useful even down to the level of a short (e.g. 1-line) method. Refactoring in this way makes it tremendously easier to see the flow of your original method and to change design or implementation of the main method or any component of it.
Wedge
I've been thinking about this answer. I reckon you should change "ou can't go wrong with smaller methods, especially if they have descriptive names." to "You can only go wrong if the methods DON'T have descriptive name"
mcintyre321
+1  A: 

Of course! The smaller your methods, the easier to maintain your software.

Arjan
What's wrong with 1-liners? If that's the level of encapsulation you need, then that's that. Believing that a 1-line method isn't "meaty enough" to be a "real method" is intellectually limiting. Start looking for 1-line methods that can be extracted that would be useful, you'll probably find more than you'd think.
Wedge
Yes, you're absolutely right, I should not have stated "(Well, one-liners would not be too useful of course.)" in such a definitive way... Don't know how to write it down better then you already did, so I guess I'll just remove that sentence. :-) Thanks!
Arjan
+2  A: 

There is a huge benefit in doing this. It's all about information hiding, and making intentions very clear.

In Refactoring to Code they called this Composition Methods where you break a large method down into many smaller methods. This does two things.

First it reduces the ammount of information you need to worry about when working on the parent method.

Secondly the name of the method should indicate its intention which makes the code more readable.

JoshBerke
It also makes variable scope issues much, much easier. Scanning 100-lines of code to find out where variables are used is a non-trivial waste of time compared to examining a method that is less than a dozen lines.
Wedge
+3  A: 

Yes, it is.

  • As said above: It will generally clarify the intent of the code.
  • Even if it is only used once now, it will make it easier to reuse the code in the future.

An additional point: For simple helper methods, you might want to make them static (such that they do not depend on or alter the state of their instance), then you can make them public for even easier reuse.

sleske
+1  A: 

For me it depends on how large the code is, and how related it is to what the parent method does. If it is a lot of code (say more than 5-10 lines of code, or more than 50% of the total amount of code in the parent method) I would extract it to a private method, for code structure and readability. Otherwise I would leave it in the parent method with a descriptive comment.

I think that maintainability and readability can be degraded if there are too many, very small methods in the code. I strive for a good balance. But whenever I doubt; I extract it as a private method.

Fredrik Mörk
A: 

Just a dumb estimate, but I'd say the majority of private methods get called in only a couple of places in the average class. However as mentioned above, it makes your code much more readable and easier to test/maintain if you break all functionality into logical blocks. Refactoring is a good habit

Andrew Corkery
A: 

You should refactor to private based on the design of your API & methods/properties you want to expose when using elsewhere. You shouldn't be considering it's usage to decide whether or not to make it private/public.

Chad Grant