将所有函数转换为类

发布于 2024-11-02 14:02:41 字数 144 浏览 3 评论 0原文

我正在阅读 Fowler 的《重构:改进现有代码的设计》一书,其中提到用同名类替换函数调用,并调用该类的构造函数来代替原始函数调用。

我的问题是将所有函数(或多或少,除了非常琐碎的函数)转换为对象以使代码变得更加模块化是一个好主意吗?

谢谢,

I was reading the book Refactoring: Improving the Design of Existing Code by Fowler, where it says to replace function calls with same names classes and call constructors of that class in place of original function call.

My question is is it a good idea to convert all the functions(more or less, except very trivial ones) into objects so that the code becomes more modular?

Thanks,

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

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

发布评论

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

评论(4

洒一地阳光 2024-11-09 14:02:41

扩展一下我之前的评论:

在 C++ 中,绝对没有理由将所有函数转换为类,某种处理对象。当您只需计算并返回一个值时,函数可以很好地工作。

但是,如果您有一个大型函数,它创建内部状态并在处理过程中在多个位置使用该状态,或者(惊呼!)在全局变量中存储某些状态,那么该函数可能是一个伪装的类。在这种情况下,最好将状态存储在类对象中,并让几个较小的成员函数在该公共状态上完成工作。

另一方面,根据其参数计算值的函数不会通过将其放入类中而得到改进。

Expanding a bit on my earlier comment:

In C++ there is absolutely no reason to turn all functions into classes, some kind of processing objects. Functions work well when you just have to compute and return a value.

However, if you have a large function that creates an internal state and uses that in several places during its processing, or (gasp!) stores some state in global variables, that function might be a class in disguise. In that case it can be a good idea to store the state in a class object with several smaller member functions doing their work on that common state.

On the other hand, a function that only computes a value from its parameters is not improved by putting it into a class.

可是我不能没有你 2024-11-09 14:02:41

不,为什么会这样?如果您的功能在逻辑上是函数(无状态计算),并且没有令人信服的理由将其实现为类,那么只需将其实现为函数即可。

“用同名类替换函数调用并调用该类的构造函数来代替原始函数调用”的建议是完全错误的:如何

int y = f(x);

class f 替换并调用其构造函数?构造函数没有返回值!让它发挥作用的唯一方法是在class f 上重载operator(),这样您就可以使用其中一个,

int y = f(x)();
int y = f()(x);

这两者都是毫无意义的。 (另外,您必须记住您定义的每个函数对象都需要其中的哪一个。)

一定有一些您没有告诉我们的事情。

No, why would it? If you have functionality that is logically a function (stateless computation), and there's no compelling reason to implement it as a class, then just implement it as a function.

The advice to "replace function calls with same names classes and call constructors of that class in place of original function call" is plain wrong: how can you replace

int y = f(x);

by a class f and a call to its constructor? A constructor doesn't have a return value! The only way to get this to work is to overload operator() on the class f so you can use one of

int y = f(x)();
int y = f()(x);

both of which are pointless. (Also, you'd have to remember which one of these you need for every function object you define.)

There must be something you're not telling us.

恋你朝朝暮暮 2024-11-09 14:02:41

在我看来,将函数转换为类是绝对没有意义的。你为什么要这么做?

当您将函数转换为对象时,您不会获得任何东西。方法调用实际上是相同的普通函数调用,只是带有一个隐藏参数 this。在您的情况下,该参数是多余的,因为您的对象不具有任何状态。

我能想到的将函数转换为对象的唯一原因是将函数作为对象传递给其他函数,但为此目的,我们有函数指针或 boost::function 或 c++ 0x lambda。

In my opinion, converting functions into classes is absolutely pointless. Why would you want to do that?

When you convert your functions to objects, you're not gaining anything. A method call is in fact the same plain function call, only with a hidden argument, this. In your case, that argument would be redundant because your object is not bearing any state.

The only reason I can think of for converting functions to objects is passing functions as objects to other functions, but for that purpose we have function pointers or boost::function or c++ 0x lambdas.

小糖芽 2024-11-09 14:02:41

绝对不!
任何函数转换为类是没有意义的。

类用于管理对象的某些状态并隐藏信息,函数是全局的并且大多是无状态的。大量的丢失类使得软件效率低下并且难以维护。

Definitively no!
It is no point to convert any function to class.

Class is about to manage some state of object and hide information, functions are to global and mostly stateless. A lot of lose classes make software inefficient and unmaintainable.

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