Is global constants an anti-pattern?

后端 未结 7 1648
再見小時候
再見小時候 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:09

    Global variables are problematic because they introduce largely unnecessary dependencies across modules. These dependencies make it harder to debug problems and reuse code.

    I'd say that truly global constants are also problematic for the same reason, So instead of having a singleton called MyGlobals containing a constant like MyGlobals.HTTP_SUCCESS_OK, package like constants together in their own classes, such as HttpStatus.SUCCESS_OK.

提交回复
热议问题