问题
This is just a general help question, I'm trying to know What is the advantage of having a set of small functions in a C++ application's code over having one long complex function containing all the statements necessary to solve a problem ?
回答1:
Edit Credit for this mnemonic goes to the commenters in the OP.
Breaking big functions up in to several smaller ones can lead to MURDER! Which, in this case, might be a good thing. :)
M - Maintainability. Smaller, simpler functions are easier to maintain.
U - Understandability. Simpler functions are easier to understand.
R - Reuseability. Encourages code reuse by moving common operations to a separate function.
D - Debugability. It's easier to debug simple functions than complex ones.
E - Extensibility. Code reuse and maintainability lead to functions that are easier to refactor in 6 months.
R - Regression. Reuse and modularization lead to more effective regression testing.
There are a few potential benefits to breaking big functions up in to smaller functions. In the order they fell out of my brain:
It encourages code-reuse. Often in large functions you have to do more-or-less the same thing many times. By generalizing this in to a single common function, you can use that one block of code in multiple places.
Code-reuse can aid in robustness and maintainability by isolating potential bugs to one place rather than several.
It is easier to understand the semantics of the function when there are fewer lines of code and a bunch of calls to well-named functions.
If you are opposed to functions with multiple return points, breaking big functions up can help reduce them.
It helps to identify and isolate (potential) problems with subtle data dependencies that are otherwise hard to notice.
It's important to note however that you take the good with the bad with this. There are also a few potential drawbacks to breaking big functions up:
If the big function worked before, trying to modularize it may create defects.
In multithreadded applications, you might introduce deadlocks and race conditions if your synchronization policies are subtle or just plain wrong.
You might introduce a performance hit from the function calls.
回答2:
Clearer code which means it's easier to understand and maintain.
回答3:
One big complex function is just that: complex.
Dividing your code into separate functions makes your code much easier to work with. First, when you look for the part of code that performs a particular task, it will be easier to find if it's in its own function.
Second, making changes to a function is much easier when it's simple--you don't need to understand a large amount of code to modify that function.
Also, you may find it easier to reuse that code in another project when it is divided up into smaller functions that can likely be used for more purposes than just the single large function could be.
回答4:
The advantages of splitting a program up in multiple functions are:
- Most programs have some basic functionality that is needed in multiple places. Having that functionality in a separate function means it is more obvious when that same functionality is used and you also only have to fix problems with it only once.
- By splitting a program in functions, you can introduce multiple levels of abstraction in the code. In the main function you get a broad overview of what the program does and each level down on the call-tree reveals more details how certain aspects are realized.
回答5:
In medical device systems, breaking code into smaller pieces reduces the need for regression testing and narrows the effects of change to a smaller scope.
For example, let's assume we have 15 functions for 3 themes in one file.
If I change one of the functions in the file, everything needs to be rebuilt and retested.
If I split the file into 3 separate files of 5 functions each, I only need to rebuild 5 functions and retest 5 functions. Testing 5 functions requires less testing time than 15 functions.
Also, when teams of people are working on the same code base, dividing the code reduces the probability of two or more people working on the same file. Multiple people working on the same file has many conflicts such as one person's code being accidentally removed during a check-in.
来源:https://stackoverflow.com/questions/13256453/what-is-the-advantage-of-breaking-a-code-into-several-small-functions-in-c