4 ms·
So one of the problems with that function is that it's global. The advantage of having a single class that either has a hidden constructor and only a static fun
by dep_b 6y ago
So one of the problems with that function is that it's global. The advantage of having a single class that either has a hidden constructor and only a static function or needs to be initialized but only offers the stateless function is that you know what concept the function is related to.
Sometimes I have an entirely stateless class that has a cluster of 10 functions, both private and public just to have that particular functionality grouped somewhere.
So class-as-a-namespace if you wish.
- noobermin 6y agoC++ has namespaces. Python has modules.
- ketzu 6y agoIf you use c++ (as the example in the article) you can just use explicit namespaces at least.
- dep_b 6y agoTrue, I'm working with Swift which doesn't have that concept. But you can't have collisions between similarly named functions that are in the main or a different namespace? For example when somebody defines it's own version of 'print'?
- ketzu 6y agoYou can have name collisions of functions with the same name, same namespace and same signature. But you can have nested namespaces, so namespaces are still the answer in c++. In languages where those are not available or where you are forced to use classes anyways (e.g., java) I see no problem with classes-as-a-namespace, but especially in java there is no point in discussing "classes that should be free functions", too, as those don't exist.