• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NX: Error messages

 
Chris Harris
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I was thinking about storing the error message shown to the user within a .properties file. This allows me to remove the long String error description with error numbers and makes the more configurable (even international). These error numbers refer to keys within the properties file.
I know this may be over the top of such a small app but it actual makes live a little easier.
I would normally put the error message in a different file than the system properties but would I fail because not all properties are in suncertify.properties?
Is any one else doing this?
Thanks
Chris
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This allows me to remove the long String error description with error numbers
Um, I'm not sure I like the sound of that; using plain numbers isn't going to help readability much. I wouldn't want to see

But this might be OK:

Though I would probably prefer

Readability is more important than saving typing here. And the role of using a constant is not to save typing either; it's to make sure that I don't accidentally mistype something in the String when I need to use it later. (That is, if I mistype, I want a compiler error message, not a blank error message at runtime.)
But aside from that point...
I'm not sure if anyone else is doing this, though a few people mentioned considering it in the past. It's almost certainly unnecessary, but unlike many of our other dicussions this extra feature doesn't really add much to complexity if you do it right - it just takes more time for you to figure out how to set up in the first place. The result however is probably a better program which will be more extensible later. I'd say if you want to spend a little extra time now, this is a good thing to do. It may not actually help your score (I'm sure you can get a perfect score without doing this) but it shouldn't hurt either.
I would normally put the error message in a different file than the system properties but would I fail because not all properties are in suncertify.properties?
What you're talking about aren't user-configurable properties, they're programmer-configurable. That is, we don't expect users to deal with this stuff; it's a convenience for future developers. As such, I'd say put these properties in a different file, preferably something jarred up in runme.jar where the user won't even see it. I would think the best solution is to use a ResourceBundle rather than Properties, since this is Sun's standard way of internationalizing text.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chris,
As Jim mentioned in his last paragraph, a ResourceBundle is the "standard" way of handling internationalization in Java.
Since the instructions tell us to use standard APIs instead of making our own, it would definately be a good idea for you to look at them.
Regards, Andrew
 
Chris Harris
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Jim and Andrew,
Thanks for your advice. Within the ResourceBundle would you use a small text description as the key or a number? i.e
DB_NOT_FOUND = Unable to locate database. Please contact System Admin.
or
1 = Unable to locate database. Please contact System Admin.
I agree with Jim that within the code I would have:
public static final String DB_NOT_FOUND = "DB_NOT_FOUND"
or if numbers
public static final String DB_NOT_FOUND = "1"
I know that this is a very small issue but if I am going to do this, I would like to get it right.
Thanks Chris.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chris,
The ResourceBundle uses Strings as the keys. I do agree with Jim that this will make your code much easier to read and maintain.
The API gives good examples of setting up and using ResourceBundles, and one of their examples is:

Personally I would prefer a constant OK_KEY instead of "OkKey", but either way it is fairly clear what this bundle is likely to be used for.
Regards, Andrew
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic