我如何知道我的代码调用堆栈何时过于复杂?

发布于 2025-01-16 12:10:13 字数 405 浏览 4 评论 0原文

我知道这可能是非常主观的并且取决于实施,但我希望有人仍然可以提供一些指导。我是一名新的 SWE,我正在尝试了解我的团队的代码库有多复杂。我已经阅读了各个函数的认知复杂性文档,但我想知道是否有关于函数调用堆栈可以/应该有多复杂的一般准则。例如,如果我有一个函数调用另一个函数,另一个函数又调用另一个函数,那么我的函数调用堆栈只有三层深,从概念上来说,这仍然很容易调试和理解。但是,如果我有一个函数在代码中调用 100 多个其他函数,那么在调试时就会变得非常笨拙且难以真正排序。当然,这部分取决于函数的复杂程度,但即使相当简单的函数堆叠得太远也可能变得非常难以导航。

因此,考虑到这一点,是否有任何关于如何管理调用堆栈的指南,以便您的代码不会变得太复杂而其他开发人员无法进行排序?我知道这可能不会采用“3,也许4”的形式,但任何有关如何以这种方式管理代码复杂性的指导都会有所帮助。谢谢!

I know this is likely to be highly subjective and dependent on implementation, but I'm hoping someone could still provide a little bit of guidance. I'm a new SWE and I'm trying to get a feel for how complex my team's code base is. I've read through the Cognitive Complexity documentation for individual functions, but I'm wondering if there are any general guidelines on how complex the function call stack can/should be. For example, if I have a function that calls another function, which calls another function, my function call stack is only three levels deep, which is still pretty easy to debug and understand conceptually. However, if I have one function that calls 100+ other functions in our code, that starts to get very unwieldy and difficult to really sort through when debugging. Of course, that's partially dependent on how complex the functions are, but even fairly simple functions stacked too far could become pretty tough to navigate.

So with that in mind, are there any guidelines on how to manage your call stack so your code doesn't get too complex for other dev's to be able to sort through? I know this likely wouldn't take the form of "3, maybe 4", but any guidance on how to manage code complexity in this way would be helpful. Thanks!

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

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

发布评论

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

评论(1

╰ゝ天使的微笑 2025-01-23 12:10:13

这是一个很好的问题,我使用这个经验法则:

使用尽可能少的函数,尽可能少的函数长度超过屏幕的 2/3。

如果您必须滚动才能阅读您的函数,这使得调试变得更加困难。

It's a great question, I use this rule of thumb:

Use as few functions as possible, as log as no function length goes over 2/3 of the screen.

If you have to scroll to read your function, it makes it harder to debug.

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