将代码分解成C ++中的几个小函数的优点是什么? [英] What is the advantage of breaking a code into several small functions in C++?

查看:221
本文介绍了将代码分解成C ++中的几个小函数的优点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这只是一个常见的帮助问题,我想知道在C ++应用程序的代码中拥有一组小函数的优点是有一个长的复杂函数包含解决问题所需的所有语句? / p>

解决方案

编辑此记忆卡的付款金额将用于OP中的评论者。



将大功能分成几个较小的功能可能会导致 MURDER !在这种情况下,这可能是一件好事。 :)


M - 可维护性。更小,更简单的函数更容易维护。



U - 可理解性。简单的函数更容易理解。



R - 重用性



D - 可调试性,以鼓励代码重用。调试简单函数比复杂函数更容易。



E - 可扩展性。代码重用和可维护性导致在6个月内更容易重构的函数。



R - 回归。重用和模块化可实现更有效的回归测试。







打破大功能到更小的功能。按顺序他们从我的脑子里:


  1. 它鼓励代码重用。通常在大功能中,你必须多次做同样的事情多次。


  2. 代码重用可通过以下方式帮助实现稳健性和可维护性:


  3. 当代码数较少,调用次数较少时,更容易理解函数的语义


  4. 如果你反对具有多个返回点的函数,打破大的函数可以帮助减少它们。


$>

  • 它有助于识别和隔离潜在的问题。 b
    $ b

    不过要注意的是,你把这个好的坏。打破大功能也有一些潜在的缺点:


    1. 如果大功能以前工作,尝试模块化创建缺陷。


    2. 在多线程应用程序中,如果您的同步策略是微妙的或只是明显错误,您可能会引入死锁和竞争条件。 >

    3. 您可能会从函数调用中引入性能影响。



    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 ?

    解决方案

    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:

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

    2. Code-reuse can aid in robustness and maintainability by isolating potential bugs to one place rather than several.

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

    4. If you are opposed to functions with multiple return points, breaking big functions up can help reduce them.

    5. 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:

    1. If the big function worked before, trying to modularize it may create defects.

    2. In multithreadded applications, you might introduce deadlocks and race conditions if your synchronization policies are subtle or just plain wrong.

    3. You might introduce a performance hit from the function calls.

    这篇关于将代码分解成C ++中的几个小函数的优点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆