Is global constants an anti-pattern?

aberrant80 picture aberrant80 · Aug 12, 2009 · Viewed 10.4k times · Source

I've always thought having a class just for the sake of holding constants is a bad design. But recently, I've tried googling for it and found only that having an interface as a constants is bad an anti-pattern - no mention of using a class of constants.

I'm of the opinion that since a class of constants is really not much different from global variables, that's why I'm against it and tend to refactor away such classes. It creates a class of data that has absolutely no context. Those constants are much better off tied to whatever actually use them, to give them context and meaning, as well as making them encapsulated within a class.

What do other people think?

Answer

yfeldblum picture yfeldblum · Aug 12, 2009

Global constants are fine.

Global (non-constant) variables are the work of the devil.