C 函数仅调用一次和圈复杂度

发布于 2024-09-02 06:03:38 字数 192 浏览 11 评论 0原文

我认为这个问题更多的是关于风格: 我有一个具有非常高 CC 的算法(还有很多行!)。我想减少它,这很容易,因为有一些代码可以分组。问题是,以这种方式做事,我会有一个“大”函数调用“小”函数,而这些函数只被调用一次。

在我看来,尽管函数被调用一次,但将大函数分成小块对于代码的易读性更好(在本例中)。

你觉得怎么样?遇到类似情况你会怎么做?

I think this question is more about style:
I have an algorithm that has a very high CC (and a lot of lines!). I want to reduce it, and it's easy since there are pieces of code that can be grouped. The problem is that doing things this way I would have a "big" function calling "small" functions which are only called once.

In my opinion breaking a big function in small pieces is better for the legibility of the code (in this case) despite functions are called once.

What do you thik? How do yo do in similar cases?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

潜移默化 2024-09-09 06:03:38

将一个大的函数分解成更小的、大部分独立的块是一个非常好的主意。它使代码更具可读性,控制流程更清晰。

如果函数是静态的并且仅调用一次,编译器可能会在没有询问的情况下为您内联它们,因此无需担心运行时成本。

Breaking a big function into smaller, mostly separate chunks is a perfectly good idea. It makes the code more readable and the control flow more clear.

If the functions are static and called only once, the compiler will probably inline them for you without even being asked, so there's no need to worry about runtime cost.

李白 2024-09-09 06:03:38

函数只被调用一次并没有什么坏处。
它们使您的代码保持整洁,并且您不会丢失任何内容,只需添加函数调用,对于只调用一次的函数,不会产生真正的性能影响。

There is nothing bad with having functions that are only called once.
They keep your code tidy and you don't lose anything, just adding function calls, no real performance hit for functions you only call once.

天荒地未老 2024-09-09 06:03:38

除了内联之外,还有很多函数只被调用一次。

假设我们有一个这样的结构:

typedef struct foo {
     char *foo;
     int bar;
     double foobar;
} foo_t;

我们编写了一些东西来初始化/分配它:

foo_t *foome(void)
{
    foo_t *ret;

    ret = (foo_t *) malloc(sizeof(struct foo));

    ...
    ...
}

但是为什么当 foome()main( )?因为我们希望下一个处理我们程序的人能够查看 main() 并立即理解我们想要完成的任务。

我宁愿看到具有数十个一次性函数的代码,如果这意味着复杂的算法在单个(或关闭)屏幕上读起来就像一本书。当我必须上下滚动一百行并试图保持自己的位置时,我无法告诉你我的头有多疼。

这可以保持理智并有助于环境,因为现在我不需要打印出所有 60 页的算法并将它们排列在桌子上只是为了能够阅读它:)

Going a step beyond inlining, there are a lot of functions that are called only once.

Lets say we have a structure like this:

typedef struct foo {
     char *foo;
     int bar;
     double foobar;
} foo_t;

And we write something to initialize / allocate it:

foo_t *foome(void)
{
    foo_t *ret;

    ret = (foo_t *) malloc(sizeof(struct foo));

    ...
    ...
}

But why did we go through all that trouble when foome() is called only once, in main()? Because we want the next person who has to deal with our program to be able to look at main() and immediately understand what we were trying to accomplish.

I'd rather see code that has dozens of one time functions, if it means a complex algorithm reads like a book in a single (or close) screen. I can't tell you how much my head hurts when I have to scroll up and down n hundred lines while attempting to keep my place.

This saves sanity and helps the environment, because now I don't need to print out all 60 pages of your algorithm and arrange them on a table just to be able to read it :)

极致的悲 2024-09-09 06:03:38

正如已经说过的,将一个大的功能拆分为几个较小的功能有很多优点。可读性(具有正确的命名),局部变量的分组(函数中使用的临时变量更接近,从而提供更好的缓存行为),这些函数之一可能可以在其他地方重用,这是以前不可见的。

As already said, splitting a big function in several smaller has many advantages. Readability (with proper naming), grouping of local variables (temporaries used in the functions are closer together giving a better cache behaviour), it can happen that one of these functions can be reused elsewhere, thing which was not visible before.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文