Today's Question: 


Different programming are similar in helping people build staff work as people want them to. But they all have their own features which differentiate them from other programming languages. The language type may be different, for example Java is OOP, some may have different syntax. A programming motto usually can best describe the characteristic of the programming language. For example, "Write once Run everywhere" for Java.Do you know any other programming language motto?
Do you know the mottos of programmig languages?
Description My Answer

Technical Article => Programming =>  Programming

How small should a function be?

Source : Peter    Date : 2012-12-18 13:58:07  

"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.

  1. Void init()  
  2. {  
  3.         Config_init();  
  4.         Socket_init();  
  5.         Db_init();  
  6.         Int I = 0;  
  7.         For (I = 0;I < max_chn_num;i++)//Initialize channel
  8.         {  
  9.                 G_user_chn[i].status = status_init;  
  10.                 ……  
  11.          }  

The above function initializes all channels, but it is not at the same abstraction level of initialization functions. We should encapsulate them:

  1. void chn_init()  
  2. {  
  3.         Int I = 0;  
  4.         For (I = 0;I < max_chn_num;i++)//初始化所有通道  
  5.         {  
  6.                 G_user_chn[i].status =status_init;  
  7.                 ……  
  8.         }  

How small should a function be? The most excellent function can be as follow:

  1. int max(int a, intb)  
  2. {  
  3.         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 :

Save as PDF Mark as read Mark as important
By clicking the "Mark as read" button, this article will be marked as read. It will be removed from the homepage's latest news and the article list on the "Technical article" page in following visits and it will be put to your read list which you can find in "Amin->Article read list". There you can unmark the read articles.
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(2304) Comment(0) Version(0)

Share on Facebook  Share on Twitter  Share on Google+  Share on Weibo  Share on Reddit  Share on Digg  Share on Tumblr    Delicious 

 Previous : The new TV rating standard: Nielsen + Twitter
 Next : 20 bit operations programmers should know

  ::Related Articles

  ::Comment Zone  (Total 0 comment)

No comment for this article.



:: Other versions

No other versions available yet.

:: Recent articles

:: Most read

:: Contribute

Want to share with the world your understanding about technology? Want to record the process you solve a technical problem? Want to make the world benefit from your understanding and solution? Write them down. You make the world better, the world makes us better.

Write article

:: Find us

Back to top