What is the optimal trade off between refactoring and increasing the call stack?

What is the optimal trade off between refactoring and increasing the call stack?

坚持沉默 发布于 2021-11-28 字数 604 浏览 742 回复 4 原文

I'm looking at refactoring a lot of large (1000+ lines) methods into nice chunks that can then be unit tested as appropriate.

This started me thinking about the call stack, as many of my rafactored blocks have other refactored blocks within them, and my large methods may well have been called by other large methods.

I'd like to open this for discussion to see if refactoring can lead to call stack issues. I doubt it will in most cases, but wondered about refactored recursive methods and whether it would be possible to cause a stack overflow without creating an infinite loop?

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。

扫码加入群聊

发布评论

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

评论(4

太阳哥哥 2022-06-07 4 楼

I think it's highly unlikely for you to get a stackoverflow without recursion when refactoring. The only way that I can see that this would happen is if you are allocating and/or passing a lot of data between methods on the stack itself.

够运 2022-06-07 3 楼

When I was a kid, and computers had 64K of RAM, the call stack size mattered.

Nowadays, it's hardly worth discussing. Memory is huge, stack frames are small, a few extra function calls are hardly measurable.

As an example, Python has an artificially small call stack so it detects infinite recursion promptly. The default size is 1000 frames, but this is adjustable with a simple API call.

The only way to run afoul of the stack in Python is to tackle Project Euler problems without thinking. Even then, you typically run out of time before you run out of stack. (100 trillion loops would take far longer than a human lifespan.)

南城旧梦 2022-06-07 2 楼

I guess it's technically possible. But not something that I would worry about unless it actually happens when I test my code.

孤独岁月 2022-06-07 1 楼

Excluding recursion, I wouldn't worry about call stack issues until they appear (which they likely won't).

Regarding recursion: it must be carefully implemented and carefully tested no matter how it's done so this would be no different.