Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the point of using constants for property keys?

Lately, I've come across a lot of Java code that relies on "properties files" for configuration. But instead of plain old string literals, the code uses constants (static final Strings) to retrieve the property values .

I find this extra level of indirection annoying because I need to perform TWO lookups in EITHER direction. If I start with the property observed in the config file, I have to first search for the property name to find the Java constant, and then search again to find the references to the constant in the code. If I start in the code, I have to find the actual value of the constant before I can then determine the value of the property in the config file!

What's the point?

I understand the value of using constants to reference keys in a resource bundle, usually in support of i18n. I'm referring to simple, non-user-facing config values. The only reason I can think of is to make it easy to change the property name later, but this benefit is far less than the annoyance IMHO, especially given the ease of a global search and replace.

like image 324
jcrossley3 Avatar asked Feb 15 '09 15:02

jcrossley3


1 Answers

For one thing, you cannot mistype the keys when using constants without getting a compiler error.

like image 158
Ferdinand Beyer Avatar answered Oct 05 '22 10:10

Ferdinand Beyer