2

I've got a bunch of functions(func1(),func2(),...) in a header file to which I want to give some scope. I know of 2 implementations:

  1. class bunchOfFunctions
    {
    public:
    static void func1();
    static void func2();
    ...
    };
    
  2. namespace bunchOfFunctions
    {
    void func1();
    void func2();
    ...
    };
    

In both the options, I can access the functions in the same way i.e. by bunchOfFunctions::func(). I prefer the namespace method(lesser typing), but I've seen the 1st method of implementation also at my workplace.

Which option is better? Is there any other option?

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
Vishal Sharma
  • 1,670
  • 20
  • 55
  • In OO programming you combine data and methods into objects, so if you got both, a class makes most sense. If your class would only contain static functions, you can consider namespaces, although they are usually though of as larger units than classes. – Gerriet May 16 '19 at 07:04

3 Answers3

3

Apart from StroryTeller highlighted points,

Spread: A namespace can be spread into multiple files where as Class must be defined in a single place.

Readability and Understandability: Generally developers inherent understanding of what a Class is and what a namespace is.

nayab
  • 2,332
  • 1
  • 20
  • 34
2

"Better" depends on your definition for better. What qualities are you looking for in the scoping? There is no one size fits all answer here. But here are some properties of both approaches:

  1. Necessity to qualify the function name.
    With a class you must write bunchOfFunctions::func1() when utilizing those functions. Meanwhile a namespace allows you to pull a function in with a using declaration
    using bunchOfFunctions::func1;
    
    and use it unqualified in most scopes. If you wished you could even make all the members of the namespace available to unqualified name lookup with a using directive. Classes don't have an equivalent mechanic.
  2. Aliasing.
    There is no difference. To alias the class one writes
    using BOF = bunchOfFunctions;
    
    while aliasing the namespace is done with
    namespace BOF = bunchOfFunctions;
    
  3. Privacy.
    A class can have private members. So you could put function declarations in there under a private access specifier and use them in public inline members. Namespaces have no such mechanism. Instead we rely on convention and put declarations under a "do not touch this" internal namespace, often called bunchOfFunctions::detail.
  4. Argument dependent lookup.
    C++ has a mechanism that allows it to find function declarations from an unqualified call, by examining the namespaces that contain the arguments to the call. For that to work, the function must be in an actual namespace. Static members of classes are not subject to such a lookup. So for any member types you may have, only a namespace will allow calls via ADL.

These four are off the top of my head. Judge your needs for yourself. As for "Is there any other option?", those are the only two ways you have to group such functions in C++ today. In C++20, we'll have modules! That will offer another level of grouping for code.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
0

In addition the information given above I would add that going back to the standard definition of what a class and what a namespace is can help with decisions like this.

A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.

In object-oriented programming, a class is an extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods).

While these may sound similar they are rather specific. So if your functions are related, but not necessarily to the same object I would think hard about whether to put them in a class; particularly if grouping these functions together in a class may violate the Single Responsibility Principle: https://en.wikipedia.org/wiki/Single_responsibility_principle

Community
  • 1
  • 1
AdaRaider
  • 1,126
  • 9
  • 20