Is global constants an anti-pattern?

后端 未结 7 1641
再見小時候
再見小時候 2020-12-03 03:29

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 inter

7条回答
  •  南方客
    南方客 (楼主)
    2020-12-03 04:04

    I believe the global variables problem is that they create global state. Global constants don't do this, but they indeed is responsible for some contextless constants, which may be bad.

    What I'd suggest if you need stuff like that is create enums (in case you have int constants) or static classes for constants, so you can give them some context (Math.PI, for instance)

提交回复
热议问题