How small should a function be?
"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
By clicking the "Mark as important" button, this article will be put to your important article list which you can find in "Amin->Article important list". Later when you want reread this article, it's easier for you to find it by checking the "Article important list".
Tags:Function size,Optimization Read(745) Comment(0)
Previous : The new TV rating standard: Nielsen + Twitter Next : 20 bit operations programmers should know
::Related Articles
::Comment Zone
No comment for this article.
::Comment
:: Other versions
No other versions available yet.
:: Recent articles
- HTML Email Guide
- Use of Erlang in WhatsApp
- Write high quality JavaScript and CSS with SublimeLinter
- Is Facebook developing RSS reader?
- About tmpfs
- Differences among Enter,F5 and Ctrl+F5 in webpage refresh
- China to expand 4G network across the country
- Yahoo is going to recycle inactive user IDs
- The big four in Taiwan can compete with Samsung
- Sundar Pichai : Excited to try out iOS 7
- more>>
:: Most read
- TIOBE : C overtakes Java as the No.1 programming language
- Sony is to release PlayStation4 in 2015
- Which programming language should I learn first?
- Disposable Email address
- 5 Free Open Source Chat Applications For Developers
- Never ever touch a programmer
- Hacking Vs. Programming
- TIOBE : Where is that next big programming language?
- Multitasking vs multiprogramming
- Google is developing advanced programming technology to simplify Web application development
- more>>
:: Most commented
- Hacking Vs. Programming
- Which programming language should I learn first?
- Error handling style in C
- 10 controversial programming opinions?
- C vs Java Complete Comparison
- Google+ is sick
- Disposable Email address
- Unix Philosophy
- 20 bit operations programmers should know
- PHP to get access token for Facebook app
- more>>
:: Contribute
Write article:: Find us
