How to deal with the idea of ​​"many small functions" for classes without passing many parameters?

Over time, I came to understand the mentality of many small functions, and I really like it, but I find it difficult to lose my shyness to apply it to classes, especially with more than a handful of non-public member variables.

Each additional helper function clutters the interface, as often the code is class specific, and I cannot just use some kind of common code. (To my limited knowledge, in any case, I’m still a beginner, I don’t know a single library there, etc.)

Therefore, in extreme cases, I usually create a helper class that becomes a friend of the class that needs to be worked on, so it has access to all non-public guts.

An alternative is free functions that need parameters, but even if premature optimization is evil, and I really haven’t profiled or parsed it ... I still DREAD a simple thought to convey all the things that I need sometimes, even as links, although this should be a simple address per argument.

Is this all a matter of preference or is there a widely used way to deal with such things?

I know that trying to force things into templates is a kind of anti-template, but I am worried about code exchanges and standards, and I want to get the material, at least, rather painful for other people to read.

So how do you guys handle this?

Edit: Some examples that prompted me to ask this question:

About free features: DeadMG is confused in the fact that the work of free features ... without arguments.

My problem with these functions is that, unlike member functions, free functions only know data if you give them to them, if global variables are not used, etc.

Sometimes, however, I have a huge complicated procedure that I want to disclose for ease of reading and understanding, but there are so many different variables that are used everywhere, passing all the data to free functions, which the agnostic for each bit of a member's data looks like a nightmare. Click for an example.

This is a snippet of a function that converts data to a format that my mesh class takes. For example, for refactoring, this will use all of these parameters for the finalizeMesh function. At the moment, this is part of the huge function of these computer cells, and bits of information about sizes and sizes and scaling information are used everywhere, intertwined.

What I mean by "free features, sometimes you need too many parameters."

I think this shows a bad style, and not necessarily a symptom of being irrational as such, I hope: P.

I will try to clarify the situation along this path, if necessary.

+6
source share
4 answers

Each additional helper function clutters the interface

A private helper function doesn't work.

I usually create a helper class that becomes a friend of the class that should work on

Do not do this if it is absolutely inevitable. You can break your class data into smaller nested classes (or plain old struct s), and then pass them between methods.

I still PLACE myself with a simple thought to convey everything I need, even in the same way as a link

This is not a premature optimization, it is a perfectly acceptable way to prevent / reduce cognitive load. You do not want functions to occupy more than three parameters. If there are more than three, consider packing your data in a struct or class .

+4
source

I sometimes have the same problems that you described: ever larger classes that need too many helper functions that are accessed in a civilized manner.

When this happens, I try to split the class into several smaller classes, if possible and convenient.

Scott Meyers claims in Effective C ++ that the classes or functions of friends are in most cases not the best, as client code can do something with the object.

Perhaps you can try nested classes that deal with the internals of your object. Another option is helper functions that use the open interface of your class and put in the namespace related to your class.

+1
source

Another way to keep your classes free from cracks is to use the pimpl idiom. Hide your private implementation behind a pointer to a class that actually implements everything you do, and then expose a limited set of functions to who is the consumer of your class.

 // Your public API in foo.h (note: only foo.cpp should #include foo_impl.h) class Foo { public: bool func(int i) { return impl_->func(i); } private: FooImpl* impl_; }; 

There are many ways to implement this. The Boost template template in the repository is pretty good. Using smart pointers is another useful way to handle this.

http://www.boost.org/doc/libs/1_46_1/libs/smart_ptr/sp_techniques.html#pimpl

0
source

An alternative is free functions that need parameters, but although premature optimization is evil, but I haven’t actually been profiled or parsed it ... I still MUST just thought about transferring all the material I sometimes need, even in the same way as a link, although this should be a simple address for each argument.

So let me fully understand this. You have not been profiled or disassembled. But for some reason, do you intend to ... create functions ... with no arguments? How exactly do you propose programming without using function arguments? Member functions are no more or less efficient than free functions.

More importantly, you come up with many logical reasons why you know you're wrong. I think that the problem is in your head, which may stem from the fact that you are completely irrational, and that none of the answers from any of us can help you.

General algorithms that take parameters are the foundation of modern object-oriented programming - that is the whole point of both patterns and inheritance.

-1
source

Source: https://habr.com/ru/post/887913/


All Articles