views:

515

answers:

5

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.

A: 

I've seen this practice before too, in fact once I was on a project where I had to search for a constants file which led me to an XML file which would finally give me the property name that I was looking for. And then I had to look at the property file too, since the value was what I really wanted.

I think this an example of the stuff that Jeff and Joel were talking about on the last podcasts, where developers are blindly following a practice that they've heard about (in this case, the practice to never have a string literal in your code) without thinking about whether or not it's really appropriate given the matter at hand.

bpapa
Yes, if a constant in referenced only once then its value is dubious. Having a singly referenced constant *can* be good if the constant name replaces a magic number, but in this case even that doesn't apply.However, if the key names appear all over the code then I'd stick with constants.
dwc
In this case though? Why would a property key name ever change? It's the value that's much more likely to change.
bpapa
If the application uses those indirections, then there must exist functions to do the lookup. Write a 4 line main function to call those functions rather than searching the files yourself. Good programmers are lazy.
Pete Kirkham
Wait, so you think it's a good use of time to write code instead of simply using a "search" function in an IDE? And I'm writing this code to get around a really obtuse design? Talk about putting band-aids over shotgun wounds...
bpapa
You said you had to search in three files, not one. Either it's not a problem to do that search, and your first answer is invalid, or it is a problem, so write a script for it if you have to do it more than twice.
Pete Kirkham
So instead of fixing the design, just create another unit of work around it. And then maybe later create something else to work around this script that I've created. And then change something and break all of these things that depend on it. I see the light now.
bpapa
You haven't given any reason for why the design has three indirections. Someone looked at the requirements, and decided that was a good design. If the drivers for the indirection aren't important, then 'fix' the design. So far you've only said it's bad because you had to search through some files.
Pete Kirkham
I once got some water up my nose when swimming. That doesn't mean that I stopped washing. If water gets up your nose, get a nose clip. If in your project you find you're having to follow indirections through multiple files, fix that. That's nothing to do with using constants in a single file.
Pete Kirkham
+11  A: 

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

Ferdinand Beyer
I agree the "typo argument" has some value during development, but NOT when debugging production issues. And I would argue that appropriate unit tests mitigate the risk of typos anyway (to say nothing of the production issues), so the cost of the indirection still greatly exceeds the benefit.
jcrossley3
+3  A: 

Even in the day of easy global search and replace (which isn't a new thing) using a constant lets you know that the String is just for that property file. This is good because:

  • A constant means typos will get compiler errors, a String wouldn't
  • a constant lets you separate the key "ID" for one property file verses the key "ID" for another XML file. Same String, different meaning.
  • a global search and replace could break lots of things, whereas your IDE will let you search for all usages of a constant very easily, and change only the relevant ones.

In a lot of cases, it is just a good habit that programmers got into, but good habits are there for a reason.

Nick Fortescue
+4  A: 

If a value needs to be changed without a recompile you inevitably need some redirection but doing yet another is pretty foolish unless the key needs to be referenced in more than one place (itself a possible sign of poor separation of concerns).

Key strings should be sufficiently descriptive that they cannot collide with others outside their scope (normally class) and keeping literals within a single class unique is neither complex nor likely to be so serious a concern as to merit their declaration in single block. Therefore (IMO) this practice is simply someone slavishly following rules without understanding the rule's original intent.

If you need to quote an alternate guideline to them to justify the relaxing of this one may I suggest KISS.

ShuggyCoUk
jcrossley3
A: 

Because autocomplete works better on identifiers for constants, but if all your key values are "com.foo.bar.whatever" you get no feedback.

Pete Kirkham
Sounds like it's time for the IDEs to have better support for properties files.
bpapa