[Programmer's gossip] How long can a function be written?

Source: Internet
Author: User

The project team made code review, and several brothers had a debate on the issue of function length, which was hard to determine.
Isn't the function of Row 3 too long? Is it "completely unreadable "? How long is a function counted as "too long?
This is actually an old topic. The first thing I saw was Lin's "high quality c ++ programming". It seems that a function should be within 50 lines. 50 rows. Well, it's okay. I don't think it's too long. However, I think all the functions within 200 rows are OK. What about 300 rows? Maybe, it seems, maybe... It's not long.

Search on the Internet.
Some people think that the number of functions should not exceed 5. This guy must have written all the functions from 1 to 100.
Some people think that the number of functions should not exceed 20. Er -- check the input parameters of the function and leave two blank rows. If each braces still occupy a single row, there will probably not be a few lines left to work.Code.
Some people think that functions should not exceed 100 rows. But what is the essential difference between lines 100 and 200?
Some people think that functions can be written in hundreds of rows, as long as the process is not very complex. These hundreds of rows are actually vague.
Some people think the function should not exceed 1 K. Basically oneProgramThe member rarely writes such a long function.
There should be an upper limit on the number of function lines. Why? Because a function is too long, it seems inconvenient. Everyone agrees with this. Imagine that a function has written rows. What a terrible thing is that there is a case like this (chatting during dinner at night -_-! Orz ).

Separate closely related and reusable parts into functions based on functions.Most programmers agree with this design.
The problem is that everyone has different levels of tolerance.
For example, I cannot tolerate too many functions, but would rather write a code with no reusability in a function in the same function. Besides, dividing a function into multiple functions affects reading, because Vim is used. In addition, when there are more functions, you have to write a lot of code to check the input parameters and return values. This is quite disgusting (if you do not like to check function parameters, you will be jealous of some other code styles, it may even lead to a good written test ).
In addition, some programmers are more concerned about the Function length. In order to limit the length, they will separate several steps from a function and write them as different functions, reading this code with a high-end dd such as sourceinsight is indeed quite convenient.

If you are persistent in the function length, you will be disgusted with the function that exceeds the expected length. This may be the root cause of long function readability.
In fact, similarly,Too many member functions of a class can also cause headaches for users and researchers.Even herb Sutter, chairman of the C ++ Standards Committee, thinks that too many STD: String member functions are a bad design. After all, from the user's point of view, we hope that the interface is less clear, which can reduce the learning cost and reduce the chance of making mistakes.
So how can we draw a conclusion that "too much" or "too long? Different people have different conclusions. Here is an example. I wrote a summary design document and gave it to a boss a. He said, "It's too simple to write. I don't understand it ." After a rich discussion, boss a said: "Good ." Let's show it to boss B. Boss B flipped through and said, "How do you write a summary design like a detailed design ." Fainting... Vomit blood...

We used to say that programming is an art. Now I want to say that the Evaluation Program is a philosophy.
If you are interested, you can flip through MySQL, Apache, Linux kernel, or other open-source projects.Source codeThere are a variety of styles, and you can find a lot of fun, even thousands of functions, such as glibc's vfprintf function 1500 + lines.

Many similar topics can also be derived from this topic: for example, the maximum length of a class? How many member functions can a class have at most? How many files can a program have? Should a project use a small number of programs to do everything or break down the functions into several programs?
Helpless, this topic involving the program style will never come to a conclusion,Each project can vote for the strict execution of the standard pie that most people in a project team can accept.(I have also encountered cases where the majority vote result aborted due to some stubborn factions ). Note that such standards are usually objective and quantifiable. If you set some criteria that require subjective judgment to make the criteria easy to pass, it will be a waste of time.
As for the project team, there is no uniform standard to limit the function length. Therefore, I generally do not need the length of the function to judge the readability of the function.

Related links:

[Programmer's gossip] developer? Programmer? Coder?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.