Where is the best place to locate enum types?

前端 未结 10 2117
自闭症患者
自闭症患者 2020-12-25 11:16

I have found that there is generally a singe type or namespace that takes in any particular enum as a parameter and as a result I have always defined those enums there. Rece

10条回答
  •  执笔经年
    2020-12-25 12:14

    I think you put Enums and Constants in the class that consumes them or that uses them to control code decisions the most and you use code completion to find them. That way you don't have to remember where they are, they are associated with the class. So for example if I have a ColoredBox class then I don't have to think about where they are at. They would be part of ColoredBox. ColoredBox.Colors.Red, ColoredBox.Colors.Blue etc. I I think of the enum and constant as a property or description of that class. If it used by multiple classes and no one class reigns supreme then it is appropriate to have an enum class or constants class. This follows rules of encapsulation. Isolating properties from dissimilar classes. What if you decide to change the RGB of Red in Cirle objects but you don't want to change the red for ColoredBox objects? Encapsulating their properties enables this.

提交回复
热议问题