Namespace + functions versus static methods on a class

Viewed 83637

Let's say I have, or am going to write, a set of related functions. Let's say they're math-related. Organizationally, should I:

  1. Write these functions and put them in my MyMath namespace and refer to them via MyMath::XYZ()
  2. Create a class called MyMath and make these methods static and refer to the similarly MyMath::XYZ()

Why would I choose one over the other as a means of organizing my software?

9 Answers

I want to summarize and add to other answers. Also, my perspective is in the world of header-only.


Namespaces

Pros:

  • simple solution for naming hierarchies
  • they carry no semantics, so it is simpler to read
  • can live in different files (headers)
  • can be extended
  • ADL
  • shortcut can be defined (using).
  • Plays well with operator overload
  • Can be used for branding (you can design your code and put a namespace over it without much though)

Cons:

  • everything is public
  • private things need unnamed namespace so it is not explicit
  • ADL (yes, some people despise ADL)
  • can be extended (this can be a bad thing, specially in combination with ADL, semantics of existing code can change by extending the namespace)
  • functions need to be defined (or declared) in order of use

Classes with static methods

Pros:

  • can have private components (function, variables) and they are explicitly marked.
  • classes can be friended
  • can be type-parametrized (templates)
  • can be template parameters themselves
  • can be instantiated
  • can be passed to functions (static functions behave like non-static method by default).
  • it is easier to find patterns and go from groups of independent functions and convert them to a proper class (eventually with non static members)
  • dependencies among classes is well defined
  • functions (the static method) can be defined in any order

Cons:

  • No ADL
  • cannot be extended
  • needs the keyword static everywhere (opportunity to make fun of the language)
  • an overkill to solve the naming problem alone. Difficult to read in that case.
  • the functions (static methods) always need qualification (myclassspace::fun). There is no way to declare shortcuts (using).
  • almost useless for operator overload, needs complicated friend mechanism for that.
  • can not be used for branding.
  • you need to remember end it with ; :)

In summary, classes with static methods are better units of code and allow more meta programming, and except for ADL and some syntactic quirks, can replicate all the features of namespaces, but they can be an overkill sometimes.

Companies, such as Bloomberg, prefer classes over namespaces. If you don’t like ADL or operator overload, classes with static methods is the way to go.

IMO, it would be nice if namespace and classes are integrated to become two sides of the same coin. For example identify a namespace in the language as a class were the methods are static by default. And then be able to use them as template parameters. I wouldn't be sure what to do with ADL (may be it could be restricted to symbolic operators functions alone, e.g. operatorX, which was the original motivation for operator overload and ADL in the first place)

Why would I choose one over the other as a means of organizing my software?

If you use namespaces, you will frequently hit a language defect that functions which call each other must be listed in a specific order, because C++ can't see definitions further down in the file.

If you use classes, this defect does not occur.

It can be easier and cleaner to wrap implementation functions in a class than to maintain declarations for them all or put them in an unnatural order to make it compile.

Related