Is using too much static bad or good?

I like to use static functions in C++ as a way to categorize them, like C# does.

Console::WriteLine("hello")

Is this good or bad? If the functions are used often I guess it doesn't matter, but if not do they put pressure on memory?

What about static const?

Answers


but is it good or bad

The first adjective that comes to mind is "unnecessary". C++ has free functions and namespaces, so why would you need to make them static functions in a class?

The use of static methods in uninstantiable classes in C# and Java is a workaround because those languages don't have free functions (that is, functions that reside directly in the namespace, rather than as part of a class). C++ doesn't have that flaw. Just use a namespace.


I'm all for using static functions. These just make sense especially when organized into modules (static class in C#).

However, the moment those functions need some kind of external (non compile-time const) data, then that function should be made an instance method and encapsulated along with its data into a class.

In a nutshell: static functions ok, static data bad.


Those who say static functions can be replaced by namespaces are wrong, here is a simple example:

class X
{
   public:
   static void f1 ()
   {
      ...
      f2 ();
   }

   private:
     static void f2 () {}
};

As you can see, public static function f1 calls another static, but private function f2.

This is not just a collection of functions, but a smart collection with its own encapsulated methods. Namespaces would not give us this functionality.

Many people use the "singleton" pattern, just because it is a common practice, but in many cases you need a class with several static methods and just one static data member. In this case there is no need for a singleton at all. Also calling the method instance() is slower than just accessing the static functions/members directly.


Use namespaces to make a collection of functions:

namespace Console {
    void WriteLine(...) // ...
}

As for memory, functions use the same amount outside a function, as a static member function or in a namespace. That is: no memory other that the code itself.


One specific reason static data is bad, is that C++ makes no guarantees about initialization order of static objects in different translation units. In practice this can cause problems when one object depends on another in a different translation unit. Scott Meyers discusses this in Item 26 of his book More Effective C++.


Agree with Frank here, there's not a problem with static (global) functions (of course providing they are organised).. The problems only start to really creep in when people think "oh I will just make the scope on this bit of data a little wider".. Slippery slope :)

To put it really into perspective.. Functional Programming ;)


The problem with static functions is that they can lead to a design that breaks encapsulation. For example, if you find yourself writing something like:

public class TotalManager
{
    public double getTotal(Hamburger burger)
    {
        return burger.getPrice() + burget.getTax();
    }
}

...then you might need to rethink your design. Static functions often require you to use setters and getters which clutter a Class's API and makes things more complicated in general. In my example, it might be better to remove Hamburger's getters and just move the getTotal() class into Hamburger itself.


I tend to make classes that consist of static functions, but some say the "right way" to do this is usually to use namespaces instead. (I developed my habits before C++ had namespaces.)

BTW, if you have a class that consists only of static data and functions, you should declare the constructor to be private, so nobody tries to instantiate it. (This is one of the reasons some argue to use namespaces rather than classes.)


For organization, use namespaces as already stated.

For global data I like to use the singleton pattern because it helps with the problem of the unknown initialization order of static objects. In other words, if you use the object as a singleton it is guaranteed to be initialized when its used.

Also be sure that your static functions are stateless so that they are thread safe.


I usually only use statics in conjunction with the friend system.

For example, I have a class which uses a lot of (inlined) internal helper functions to calculate stuff, including operations on private data.

This, of course, increases the number of functions the class interface has. To get rid of that, I declare a helper class in the original classes .cpp file (and thus unseen to the outside world), make it a friend of the original class, and then move the old helper functions into static (inline) member functions of the helper class, passing the old class per reference in addition to the old parameters.

This keeps the interface slim and doesn't require a big listing of free friend functions. Inlining also works nicely, so I'm not completely against static. (I avoid it as much as I can, but using it like this, I like to do.)


Need Your Help

Reading value from Spring Property file

java spring properties

Here is my code I'm trying to read " TestMe variable from My.properties file.

Is it possible to accept custom command line parameters with Inno Setup

command-line inno-setup command-line-arguments

I am preparing an installer with Inno Setup. But I'd like to add an additional custom (none of the available parameters) command line parameters and would like to get the value of the parameter, li...