Is using a lot of static methods a bad thing?

There are two kinds of common static methods:

  • A “safe” static method will always give the same output for the same inputs. It modifies no globals and doesn’t call any “unsafe” static methods of any class. Essentially, you are using a limited sort of functional programming — don’t be afraid of these, they’re fine.
  • An “unsafe” static method mutates global state, or proxies to a global object, or some other non-testable behavior. These are throwbacks to procedural programming and should be refactored if at all possible.

There are a few common uses of “unsafe” statics — for example, in the Singleton pattern — but be aware that despite any pretty names you call them, you’re just mutating global variables. Think carefully before using unsafe statics.

Leave a Comment