What is the advantage of breaking code into a few small functions in C ++?

This is just a general question of help. I am trying to find out what is the advantage of having a set of small functions in the C ++ application code using one long complex function containing all the operators needed to solve the problem?

+8
c ++ function visual-c ++ break
source share
5 answers

Change Credit for this mnemonic is sent to commentators at OP.

Violation of large functions to a few smaller ones can lead to KILLING ! Which, in this case, may be good. :)

M - Maintenance. Smaller, simpler functions are easier to maintain.

U - Understand. Simpler functions are easier to understand.

R - Reuse. Offers code reuse by moving common operations into a separate function.

D - Debugging. It’s easier to debug simple functions than complex ones.

E - Extensibility. Code reuse and maintainability lead to features that are easier to reorganize after 6 months.

R - regression. Reuse and modulation lead to more effective regression testing.


There are several potential advantages to breaking large functions into smaller functions. In order, they fell out of my brain:

  • It encourages code reuse. Often in large functions, you must do more or less the same thing many times. Summarizing this into one common function, you can use this one block of code in several places.

  • Reusing code can help in reliability and maintainability by isolating potential errors to one place, not several.

  • It’s easier to understand the semantics of a function when there are fewer lines of code and a lot of calls for well-named functions.

  • If you disagree with functions with multiple return points, breaking up large functions can help reduce them.

  • This helps identify and isolate (potential) problems with subtle data dependencies that are otherwise difficult to notice.

It is important to note, however, that you are handling the bad. There are also several potential drawbacks to breaking large functions:

  • If a large function worked before, trying to modulate it, it can create defects.

  • In multi-threaded applications, you can enter deadlocks and race conditions if your synchronization policies are thin or simply incorrect.

  • You can enter a performance hit from function calls.

+19
source share

Clearer code, which means it's easier to understand and maintain.

+3
source share

One big complex function is: complex.

Separation of the code into separate functions simplifies the work with your code. First, when you are looking for a piece of code that performs a specific task, it will be easier to find it in its own function.

Secondly, making changes to a function is much easier when it's simple - you don’t need to understand a lot of code to change this function.

In addition, it may be easier for you to reuse this code in another project when it is divided into smaller functions that can be used for other purposes, and not just one large function.

0
source share

The advantages of splitting a program into several functions:

  • Most programs have some basic features that are needed in several places. The presence of this function in a separate function means that it is more obvious when the same functionality is used, and you only need to fix problems with it only once.
  • When parsing a program into functions, you can enter several levels of abstraction in the code. In the main function, you get a broad overview of what the program does, and each level down in the call tree shows more detailed information on how certain aspects are implemented.
0
source share

In medical device systems, breaking code into smaller pieces reduces the need for regression testing and narrows the effect of changes to a smaller area.

For example, suppose we have 15 functions for 3 topics in a single file.

If I change one of the functions in the file, everything needs to be rebuilt and checked .
If I divide the file into 3 separate files with 5 functions each, I only need to rebuild 5 functions and repeat 5 functions. Testing 5 functions requires less testing time than 15 functions.

In addition, when teams of people work on the same code base, code division reduces the likelihood that two or more people are working on the same file. Several people working in the same file have many conflicts, for example, one case that is accidentally deleted during registration.

0
source share

All Articles