"The well-designed functions are often relatively small, large function design is often a mess or there is a lot of room for optimization."
Maybe you think there is no need to discuss the size of functions, because the nature of the function design is cohesive, its size is only its manifestations. But it is necessary to discuss about the size of function because the statement above .
First let's understand the concept of minimum code processing unit : a basic operation (assignment, comparison, etc.) or a function call is seen as a minimum processing unit. The reasonable number of minimum processing unit in a function should not be more than 7. If it's more than 7, you should consider to split it into other functions (Why 7? People can handle no more than 7 information units at the same time). The minimum number is not limited, even if only one, it's fine.
We should split a function into smaller functions in following cases:
1, Can not see all of the code of the function with one viewport.
If the function is too long so that we can not see all the code of a function at one glance, we should split it. We should not ask the readers to scroll the screen The beautiful function should allow the reader to know what it does and how to do.
2. Too many local variables.
If there are over 7 local variable in a function, we should consider splitting function. If there are too many local variables, it means that we need to record too many states, which will increase the burden of our brain.
3. Too much indentation.
Too much indentation means too many nested statements . Either loops or judgments can indicate complex logic of the function.
4 If you use Ctrl + c and Ctrl + v
Don't Repeat Yourself(DRY). If you are using copy and paste, then you should put the repeated codes in to a separate function.
5, Not at the same abstraction level.
For example, there is an initialization function, you need to initialize the configuration, sockets, database connections or channel status.
- Void init()
- {
- Config_init();
- Socket_init();
- Db_init();
- Int I = 0;
- For (I = 0;I < max_chn_num;i++)//Initialize channel
- {
- G_user_chn[i].status = status_init;
- ……
- }
- }
The above function initializes all channels, but it is not at the same abstraction level of initialization functions. We should encapsulate them:
- void chn_init()
- {
- Int I = 0;
- For (I = 0;I < max_chn_num;i++)//åˆå§‹åŒ–所有通é“
- {
- G_user_chn[i].status =status_init;
- ……
- }
- }
How small should a function be? The most excellent function can be as follow:
- int max(int a, intb)
- {
- return a > b?a:b;
- }
This function is very small, it has only one line. But the significance of its existence is : we can know clearly that the function is to return the bigger value of a and b.
The biggest obstacle of small function : performance
For novice programmers, the biggest obstacle is that they cannot appreciate the advantages of a small function and have no experience to split large function into smaller functions.
For programmers with some experience, the biggest obstacle of small function is performance issue.
For performance, remember do not prematurely optimize. The bottleneck of a program may not be what think they are. We need use tools to determine the real bottleneck, 20% of the code affects 80% of the performance, we need to find that 20% of the code before optimization. The function call will consume a loss of resources and affect performance, but this may not be the program performance bottleneck?
Many people are questioning the max function instance I cited above, if the number of calls during execution is too many, the performance issue can be ignored and we can gain the readability and clarity of code If the number of calls is so large that it has become a performance bottleneck, we can complete programming quickly with other optimizatio methods. The bottleneck of the program will not be many.
Original author : 常高伟 Source : http://www.cnblogs.com/chgaowei/archive/2011/09/07/2170265.html