Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 461
Erius Avatar asked May 12 '11 10:05

Erius


1 Answers

Every additional helper function clutters up the interface

A private helper function doesn't.

I usually create a helper class which becomes the friend of the class that needs to be operated on

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 structs), then pass those around between methods.

I still DREAD the mere thought of passing all the stuff I need sometimes, even just as reference

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 or class.

like image 116
Fred Foo Avatar answered Oct 12 '22 23:10

Fred Foo