这只是一个一般的帮助问题,我想知道在 C++ 应用程序的代码中拥有一组小函数比拥有一个包含解决问题所需的所有语句的长复杂函数有什么优势?
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 ?
编辑 此助记符归功于 OP 中的评论者.
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 - 可维护性.更小、更简单的功能更容易维护.
M - Maintainability. Smaller, simpler functions are easier to maintain.
U - 可理解性.更简单的函数更容易理解.
U - Understandability. Simpler functions are easier to understand.
R - 可重用性.通过将常见操作移至单独的函数来鼓励代码重用.
R - Reuseability. Encourages code reuse by moving common operations to a separate function.
D - 可调试性.调试简单的函数比调试复杂的函数更容易.
D - Debugability. It's easier to debug simple functions than complex ones.
E - 可扩展性.代码重用和可维护性使函数在 6 个月内更容易重构.
E - Extensibility. Code reuse and maintainability lead to functions that are easier to refactor in 6 months.
R - 回归.重用和模块化导致更有效的回归测试.
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.
这篇关于在 C++ 中将代码分解成几个小函数有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!