Where do you keep Constants used throughout your application?

前端 未结 5 1199
小蘑菇
小蘑菇 2021-02-05 03:59

Is interface an acceptable place to store my

public static final Foo bar

Do you extrapolate them to be read from outside of the program? Do you

5条回答
  •  猫巷女王i
    2021-02-05 04:18

    I'd put each constant into the class or interface it's most closely related to (e.g. because it will be use by its methods).

    A very seductive but ultimately very foolish idea is to have one "constants class" (or interface) that contains all constants used in the application. This looks "neat" at first glance, but is not good for maintainability because you want to group things by the functionality they implement, not by technical details like being constants (would you put all interfaces into a dedicated package? All abstract classes?).

    The idea is also foolish because any change to that class/interface then (because of constant inlining) requires all classes that use any of the constants to be rebuilt - i.e. pretty much the entire app. So the bigger the app gets, the more frequently you need such a full rebuild, and the longer it takes. I worked on such a project, where this issue led to a 15 minute pause about every other day for every developer...

提交回复
热议问题