如何处理“许多小功能”的想法?对于类,无需传递大量参数?
随着时间的推移,我开始欣赏许多小功能的思维方式,而且我真的很喜欢它,但我很难摆脱我的害羞,将其应用到类中,尤其是那些拥有超过少数非公开成员的类变量。
每个附加的辅助函数都会使界面变得混乱,因为代码通常是特定于类的,而我不能只使用一些通用的代码。 (据我有限的知识,无论如何,仍然是一个初学者,不知道那里的每个库等等)
所以在极端情况下,我通常创建一个辅助类,它成为需要操作的类的朋友,所以它可以接触到所有非公开的内幕。
另一种选择是需要参数的自由函数,但即使过早优化是邪恶的,而且我还没有真正分析或反汇编它...... 我仍然害怕有时传递我需要的所有东西,即使只是作为参考,即使这应该是每个参数的一个简单地址。
这一切都是一个偏好问题,还是有一种广泛使用的方法来处理这类事情?
我知道试图将东西强加到模式中是一种反模式,但我担心代码共享和标准,并且我希望至少让其他人阅读起来相当不痛苦。
那么,你们如何处理这个问题呢?
编辑: 一些例子促使我提出这个问题:
关于免费功能: DeadMG 对于如何让自由函数在没有参数的情况下工作感到困惑。
我对这些函数的问题是,与成员函数不同,自由函数只知道数据(如果您将数据提供给它们),除非使用全局变量等。
然而,有时,我有一个巨大而复杂的过程,为了可读性和理解性,我想对其进行分解,但是有很多不同的变量,它们在各处使用,将所有数据传递给自由函数,而自由函数对每个函数都是不可知的。一点会员数据,看起来简直就是噩梦。 点击查看示例
这是将数据转换为我的网格类格式的函数片段接受。 例如,需要所有这些参数才能将其重构为“finalizeMesh”函数。 此时,它是一个巨大的计算机网格数据功能的一部分,尺寸信息和尺寸以及缩放信息的位被到处使用,交织在一起。
这就是我所说的“自由函数有时需要太多参数”的意思。
我认为这表现出糟糕的风格,并不一定是非理性本身的症状,我希望:P。
如果有必要的话,我会尽力把事情弄清楚。
Over time I have come to appreciate the mindset of many small functions ,and I really do like it a lot, but I'm having a hard time losing my shyness to apply it to classes, especially ones with more than a handful of nonpublic member variables.
Every additional helper function clutters up the interface, since often the code is class specific and I can't just use some generic piece of code.
(To my limited knowledge, anyway, still a beginner, don't know every library out there, etc.)
So in extreme cases, I usually create a helper class which becomes the friend of the class that needs to be operated on, so it has access to all the nonpublic guts.
An alternative are free functions that need parameters, but even though premature optimization is evil, and I haven't actually profiled or disassembled it...
I still DREAD the mere thought of passing all the stuff I need sometimes, even just as reference, even though that should be a simple address per argument.
Is all this a matter of preference, or is there a widely used way of dealing with that kind of stuff?
I know that trying to force stuff into patterns is a kind of anti pattern, but I am concerned about code sharing and standards, and I want to get stuff at least fairly non painful for other people to read.
So, how do you guys deal with that?
Edit:
Some examples that motivated me to ask this question:
About the free functions:
DeadMG was confused about making free functions work...without arguments.
My issue with those functions is that unlike member functions, free functions only know about data, if you give it to them, unless global variables and the like are used.
Sometimes, however, I have a huge, complicated procedure I want to break down for readability and understandings sake, but there are so many different variables which get used all over the place that passing all the data to free functions, which are agnostic to every bit of member data, looks simply nightmarish.
Click for an example
That is a snippet of a function that converts data into a format that my mesh class accepts.
It would take all of those parameter to refactor this into a "finalizeMesh" function, for example.
At this point it's a part of a huge computer mesh data function, and bits of dimension info and sizes and scaling info is used all over the place, interwoven.
That's what I mean with "free functions need too many parameters sometimes".
I think it shows bad style, and not necessarily a symptom of being irrational per se, I hope :P.
I'll try to clear things up more along the way, if necessary.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
而
private
辅助函数则不会。。除非绝对不可避免,否则不要这样做。您可能希望将类的数据分解为更小的嵌套类(或普通的旧
struct
),然后在方法之间传递这些数据。这不是过早的优化,这是一种完全可以接受的防止/减少认知负荷的方式。您不希望函数采用三个以上的参数。如果超过三个,请考虑将数据打包在
struct
或class
中。A
private
helper function doesn't.Don't do this unless it's absolutely unavoidable. You might want to break up your class's data into smaller nested classes (or plain old
struct
s), then pass those around between methods.That's not premature optimization, that's a perfectly acceptable way of preventing/reducing cognitive load. You don't want functions taking more than three parameters. If there are more then three, consider packaging your data in a
struct
orclass
.我有时会遇到与您所描述的相同的问题:越来越大的类需要太多辅助函数才能以文明的方式访问。
发生这种情况时,如果可能且方便的话,我会尝试将班级分成多个较小的班级。
Scott Meyers 在《Effective C++》中指出,友元类或函数大多不是最佳选择,因为客户端代码可能会对对象执行任何操作。
也许您可以尝试处理对象内部的嵌套类。另一种选择是辅助函数,它使用类的公共接口并将其放入与类相关的命名空间中。
I sometimes have the same problems as you have described: increasingly large classes that need too many helper functions to be accessed in a civilized manner.
When this occurs I try to seperate the class in multiple smaller classes if that is possible and convenient.
Scott Meyers states in Effective C++ that friend classes or functions is mostly not the best option, since the client code might do anything with the object.
Maybe you can try nested classes, that deal with the internals of your object. Another option are helper functions that use the public interface of your class and put the into a namespace related to your class.
另一种让你的类不显得臃肿的方法是使用 pimpl 惯用语。将您的私有实现隐藏在指向实际实现您正在执行的任何操作的类的指针后面,然后向您的类的使用者公开功能的有限子集。
有很多方法可以实现这一点。 Vault 中的 Boost pimpl 模板相当不错。使用智能指针也是处理此问题的另一种有用方法。
http://www.boost.org/doc/libs /1_46_1/libs/smart_ptr/sp_techniques.html#pimpl
Another way to keep your classes free of cruft is to use the pimpl idiom. Hide your private implementation behind a pointer to a class that actually implements whatever it is that you're doing, and then expose a limited subset of features to whoever is the consumer of your class.
There are many ways to implement this. The Boost pimpl template in the Vault is pretty good. Using smart pointers is another useful way of handling this, too.
http://www.boost.org/doc/libs/1_46_1/libs/smart_ptr/sp_techniques.html#pimpl
那么,让我彻底弄清楚这一点。您还没有分析或拆解。但不知何故,你打算......让函数工作......没有参数?确切地说,您建议如何在不使用函数参数的情况下进行编程?成员函数的效率并不比自由函数高或低。
更重要的是,你会想出很多合乎逻辑的理由来解释为什么你知道自己错了。我认为这里的问题出在你的头脑上,这可能源于你完全不理性,我们任何人的任何答案都无法帮助你。
采用参数的通用算法是现代面向对象编程的基础——这就是模板和继承的全部要点。
So, let me get this entirely straight. You haven't profiled or disassembled. But somehow, you intend on ... making functions work ... without arguments? How, exactly, do you propose to program without using function arguments? Member functions are no more or less efficient than free functions.
More importantly, you come up with lots of logical reasons why you know you're wrong. I think the problem here is in your head, which possibly stems from you being completely irrational, and nothing that any answer from any of us can help you with.
Generic algorithms that take parameters are the basis of modern object orientated programming- that's the entire point of both templates and inheritance.