• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using constants in GUI is this overkill?

 
Ling Chung
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i've made a Constants class for my GUI, which also holds names of buttons etc. Is it better to hard code these values?
Here is an example :

Thanks for you help!!
 
Satish Avadhanam
Ranch Hand
Posts: 697
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ling, I don't know if its ok or not. But out of curiosity, why are you declaring constants for button names also? I mean when we can directly create button like this, new Button("Book"), what is the use of declaring like constants? I'm not an OOAD guy, so maybe I am missing something here. What I meant to say is when you have only one book(or other which are used only once) button, there is no need to declare it as constant and then using it right?
Please correct me if I'm wrong. Thanks.
 
Ling Chung
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Satish, i guess it all got a bit out of hand...
You only have to use constants in case they are used throughout different parts of your application.
 
Burk Hufnagel
Ranch Hand
Posts: 814
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Satish,
One advantage is that when the marketing folks come by and say, "Could you change "Book" to "Request Reservation" you only have to look in one place. And, if you build your tool tips using the constants then they get fixed automatically.
I'm not doing it this way, but I've been considering doing something similar but with a properties file instead of a constants class. That would let a non-programmer change the text with no recompiling.
Burk
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use a ResourceBundle instead. It's usually used for i18n, but it would be much cleaner in this instance also.
 
Mogens Nidding
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there's a vague requirement saying that everything configurable should be so from the GUI. So if you put something in properties files with the purpose of making it configurable, then it conflicts with the requirements, and is thus beyond overkill.
About the use of explicit string constants (yada-yada-yada String BOOK_LABEL = "Book") as opposed to string literals: It's definitely a matter of taste. I think you shouldn't add indirection unless it makes things less awkward. Reading a 'new JButton("Book")' is certainly easier on the eye than reading 'new JButton(BOOK_LABEL)'. And if I decide to change it from "Book" to "Request Reservation", I think I can manage to do a search-replace in a single file. If the same literal appears in far-apart places (different classes, or just far apart in the same file), I would reconsider.
 
Mogens Nidding
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ling, don't get me wrong, by the way. I think you should keep your constants - they're fine! As I said, it's a matter of taste.
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True. I didn't use constants in my program at all. Although, putting your stuff into a ResourceBundle properties file doesn't mean that it's meant to be configurable. It's just meant to put it in one place so you can change is more easily, if you needed to (probably wouldn't ever happen).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic