Práctica recomendada para manejar muchas funciones individuales

I have many small functions which every is doing a single thing like for example: pingServer, checkUserValidAccount, countDistance.

It is not worth of wrapping every function into single class.

What is the best practice in c++ to handle such a different many small functions?

Maybe writing some class called Helpers like for example NetworkHelpers?

preguntado el 08 de noviembre de 11 a las 09:11

3 Respuestas

Placing them in a namespace is an option. I don't see the need for a class. An instance of a class is mean to represent a state, but you're describing a bunch of free functions, so a stateless system.

"It is not worth of wraping every function into single class." - this is not a valid argument for making the decision not to write a class. A class can have a single member and a single method, but if there is logic behind it, you should write it.

If your functions and your logic mandates the use of a class, where your functions have the same logic but operate differently depending on the object, by all means, write a class. If your sole purpose is to group the functions together, when their logic doesn't really rely on the same instance of a class, group them in a namespace.

An example based on your question:

namespace NetworkHelpers
   //free function, use a namespace
   bool pingServer(std::string hostname);

//alternative, where the object has a state:
class ServerConnection
   std::string _hostname;
   NetworkHelpersClass(std::string hostname) 
      _hostname = hostname; 
   bool pingServer()
      return NetworkHelpersNamespace::pingServer(_hostname); 

As you can see, inside the namespace, the function doesn't depend on anything other than the parameter.

Inside the class, since it has a state and you create an object for each server (so similar behavior, yet different depending on the object), you call the function with no parameters as the object has a state.

Espero que esto lo aclare.

respondido 08 nov., 11:13

Where by "function" you mean "class"? :) - themel

@themel, yes, I edited my answer just as soon as I hit the post button. :) - Luchian Grigore

I would add a further note: when it comes to choosing the namespace, it's a good habit to put the function in the same namespace (not std) as one of its arguments (choice to be made here, obviously). If it does not take any argument that lives in a namespace (except from std), then consider the namespace of its return type. If there is no namespace invovled at all... then create one. - Matthieu M.

Unlike some languages, C++ does not require that you put every function in a class. If you have functions that don't belong in a class, put them in a namespace.

respondido 08 nov., 11:13

If I may add another best practice other than namespaces: group your functions in DLLs minding their dependencies. Avoid circular linking and create low-level libraries with the smallest number of dependencies as possible.

respondido 09 nov., 11:19

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.