Is global constants an anti-pattern?

2020-01-29 05:55发布

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?

7条回答
迷人小祖宗
2楼-- · 2020-01-29 06:36

Global constants are fine.

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

查看更多
登录 后发表回答