So I was looking at Google's I/O App, iosched, and I notice they implement constants through a nested interface. For example in their ScheduleDatabase class they have:
public class ScheduleDatabase extends SQLiteOpenHelper {
interface Tables {
String BLOCKS = "blocks"
String TRACKS = "tracks"
}
}
But I have been reading online that it is poor java practice to create a interface to just store constants. But as you can see in the ScheduleDatabase class it kind of makes sense why they went with this approach. So I was wondering if this is also poor practice?
I think this is a coding style issue which is a matter of debate. On one hand you could argue that interfaces are for defining a contract between classes and these constants aren't really part of that contract (they're data), on the other you could argue that the encapsulation of these constants into a namespace is a good thing and organizes the code better.
I can't really say for sure whether this pattern is good or bad. If you have only a few constants then I'd say definitely don't use it, but if you have a bunch of constants which you can logically organize into groups then you could consider it. Certainly I'm more comfortable with the nested interface version of this pattern over the regular interface version.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With