This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

boolean constants

 
Örjan Karlsson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a sort of programming-philosofical-ish question about the use of boolean constants.

This is my example:

This is pretty straight forward class, with some fields, constants and some methods.

So is the use of boolean constants (YES and NO in the example) "good" programming or does one over do things programming like this. In my opinon it makes the code somewhat more readable.

Im interested in other peoples views on this subject.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These constants already exist in the java.lang.Boolean class. I would just use them, e.g. :


And of course these already exist as literal constatnts, so I'm not sure what extra benefit using other constants provides?




...though in your code I suspect these are redundant. Its probably safer to perform checks like:


rather than:


[ December 08, 2008: Message edited by: Paul Sturrock ]
[ December 08, 2008: Message edited by: Paul Sturrock ]
 
Örjan Karlsson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about from a readability standpoint, using YES and NO in the code combined with descriptive field names and methods?

However i dont think anyone who reads code is having problems with true and false.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Up to you really. Personally I reckon they clutter the clas a little. If the constants were for some constant specific to the class I'd use them, otherwise I'd just return the existing constants.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic