tags:

views:

362

answers:

14

Its prudent to break a long function into a chief function and helper functions.

I know that the outside the module only chief function will be called, but its long length may prove to be intimidating.

Textbooks put a limit on the number of lines, but I feel that this is too rigid.

P.S. I am programming in Python and need to process incoming, messages. The function returns a tuple containing the message but in Python's internal data types. So you can see somewhat independent code for each message type.

Duplicate Question

http://stackoverflow.com/questions/475675/when-is-a-function-too-long

+3  A: 

One good rule of thumb is that if it doesn't fit on a single screen it is worth thinking about splitting it up. But only if it makes sense to split it up, some long functions are perfectly readable and it doesn't make any sense to slavishly split them into multiple functions just for the sake of it.

andynormancx
+2  A: 

I'm not a big fan of breaking a function into multiple functions unnecessarily. It's not a hard and fast thing - if there are things that seem like distinct logical units, then by all means, break those out and think about them separately. But don't just break things out for the sake of some guideline like "one page per function" or "N lines per function".

Paul Tomblin
+1  A: 

I was recently discussing this with a friend. He suggested refactoring to separate concerns and I must say I have to agree. That is, one function should do one thing, if it does more than one thing, split it up. If not, let it be together, it makes no sense to split up a function, only to have it obfuscate the meaning. After all, a function is a block of code that does one thing!

batbrat
Well that "one thing" is quite subjective. I asked about breaking into a chief function and helper functions. So here you see a chief function is doing much of the thing.
Xolve
Ok. I'll say that a chief function with helpers is ok, so long as the task the chief function does can be broken into logical steps, each of which represents a separate concern.
batbrat
I'd also avoid breking up into functions, when the chief function is deeply nested in loops.
batbrat
+1  A: 

The limit in term of number of lines is often impractical becuase it doesn't account for readability well. It's better to try to seperate groups of lines of code that have just a few inputs and just a few outputs and make this a separate functon. It's not always possible - then it's often wise to just leave the code as it is and not to refactor for the sake of refactoring.

sharptooth
A: 

personally I break a function if it either saves total lines or total processing time.

if I only run the helper once per chief function I don't bother

cobbal
+3  A: 

I think you need to go about this from the other end of the problem. Think bottom-up. Identify small units of work, as small as possible, and start composing your code that way. You will only run into spaghetti-code issues when you code top-down and don't keep a structured approach.

If you already have spaghetti code and need to refactor, you pretty much have to start over. It is probably more work to break up existing spaghetti code than to rewrite it, and the result may not be as good.

I don't think there should be a hard number for the lines of code in a method either, but well written code does not have methods with more than 5 to 10 lines in the lower layers, and 20 to 30 lines in the business logic. To give you some kind of metric.

cdonner
A: 

The point is that in principal it's better to have specialiced functions. But where one sets the limit depends very much on 1) the "usual" programming style in certain languages. (one can observe that, object-oriented langauges tend to shorter procedureds than let's say C or the like 2) it depends on your way of programming. Every hard limit must be questioned. IMHO. Overall there will probably some "natural" distribution of programs 3) I think what one should keep on one's mind is that a function should do a certain task take for example some function for parsing it is usually much longer than a function just settin some field in a structure. Or getting back just consider how a event loop in the Windows API may look. So that all suggests that there may be good reasons for long methods...

Friedrich
A: 

If there is independent code (in your case specifics for each message type) those areas should be broken out.

chills42
A: 

Size matters not. Judge me by my size do you? - Yoda

Your main concerns are readability, simplicity and maintainability. A good indicator is if you need to write comments to explain a section of a function then that section is a good candidate for a separate function.

codeelegance
A: 

If you didn't write it and it's already in production: NEVER!!! If you break it up, you're likely to break it, it's that simple.

If you are writing it and you're not sure, the on screen rule apples as others have said.

Adam Hawes
I completely disagree. Write unit tests and then refactor.
Eric Weilnau
Gaaa.. that's how "legacy" code remains legacy code. Bad code should always be considered for refactoring.
Ryan Emerle
I have a simple rule, and that's "if it ain't broke don't fix it". You can refactor perfectly fine working code if you want (even if it is ugly) but the uglier it was the more likely you'll fuck it up when you rewrite it.
Adam Hawes
A: 

There are many reasons to break a long function into its constituent pieces. Most important is:

  • readability
  • maintainability
  • code clarity/intent

Some functions simple cannot be broken into smaller pieces without negatively impacting the listed goals, so there is no hard-and-fast rule.

Ryan Emerle
+2  A: 

Never write a function that, when printed on fanfold paper, is taller than you are.

joeforker
+1 for lol value.. lol
Adam Hawes
It is real advice, but it is a couple of decades out of date.
joeforker
+1  A: 

Well since I am coding in Python so I have the liberty to write functions inside functions, unlike C, C++ or Java. This i feel is a better choice.

Xolve
+2  A: 

I like the rule of thumb that you should break out the subfunction if you can think of a good domain-relevant name for it.

When someone can understand the top-level function without necessarily having to look up the definition of the sub-function, you've likely made a net gain. (But when you break it down too far, your names start referring to your implementation artifacts rather than the domain)