Namespace + Functions Versus Static Methods on a Class

Using a namespace in place of a static class in C++?

There's no such thing as a "static class" in C++, so from a C++ point of view you're not using it "as a static class", you're using it "as a namespace". It's certainly accepted practice to use namespaces to group functions together.

It's up to you, though, how big you want the groups to be. It's not unusual for C++ libraries to use a single namespace for the whole public interface. That might come as a surprise to someone who is used to (say) Java, where classes are often used to group together smaller numbers of static methods. Since C++ was here first, you could say that Java is using classes as namespaces.

So, in C++ you don't tend to see classes similar to java.util.Collections or java.lang.Math, full of static members. If you want groups of functions like that in C++, use namespaces.

The exception (isn't there always a special case in C++?) is traits types like std::numeric_limits<T>, where the template parameter makes the class do something that a namespace can't do. You could define a namespace numeric_limits containing function templates max<T>(), min<T>() etc, but it's not as good. Firstly, it groups things slightly differently, the type T appears "lower down the hierarchy". Secondly it doesn't do everything that a traits type does, because there's no such thing as an "object template" that would let you define a value numeric_limits::digits<T>.

I don't know C# well enough to comment on the practical uses of static classes there, but AFAIK it's just a class restricted to having no non-static members, so it's analogous to those Java classes.

C++ : static method VS global function VS namespace VS

It really depends on what you want to do. If you are just writing a very small program, I don't really see anything wrong with writing it as a global function. However, if your program is at least moderately sized, I would recommend using a static method, because you would be grouping its functionality with the class it deals with.

Of course, you could also do that with a Namespace. However, I imagine that you would have a Line class anyway, since it seems like it would lend itself well to an object oriented style of programming. With that in mind, I don't really see a reason to make it a namespace instead of a static method of class Line. That's certainly where I would look first if I were looking for an intersection method!

I don't see anything wrong with the last option either, but I prefer #2.

PHP: Static method in Class vs Global function in Namespace?

Is there any reason to prefer one method over the other?

(below assumes you're asking/referring to a difference between a static class method and a function in a namespace)

Historically (before namespaces arrived), people were forced to use classes with static functions to not pollute a global namespace. This is one of the reasons you're likely to encounter this method more often.

You might prefer to use a static method if you need access to class's data or if you think of leaving a door to overriding them in descendant classes (the so-called 'flexibility and functionality' by some).

You might prefer a namespaced function when all you want is ...uh, ahem... a function. Think of some util, functional or collections functions like compose/partial, retry , filter/map/some/every etc. You are probably not expecting this to be overwritten in a child class, right? Neither it need access to some static class member.

There is one particular annoying problem with (namespaced) functions though, that classes don't suffer from. And it's autoloading. To put it short, there is no way to autoload a function at the moment. And this is another reason you'd likely encounter classes with static methods (when they don't necessarily need to be) more often in practice.

When to use static class functions instead of namespaced functions?

Is the namespace function I described a valid alternative?

Yes

Would it ever make sense to define a class that only has static methods? It seems like it would make more sense to use a namespace.

If the methods are utility methods that can be utilized in many classes, then it would make more sense for them to be in namespace.

If the methods are specific to the class then make them static inside the class itself, which would result in better encapsulation.

Is a class with only static methods preferable to a namespace?

One non-stylistic difference is that you can use a class as a template parameter, but you cannot use a namespace. This is sometimes used for policy classes, like std::char_traits.

Outside of that use case, I would stick to a namespace with regular functions.



Related Topics



Leave a reply



Submit