aspose file tools*
The moose likes Beginning Java and the fly likes Use of identifiers in complex boolean expression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Use of identifiers in complex boolean expression" Watch "Use of identifiers in complex boolean expression" New topic
Author

Use of identifiers in complex boolean expression

Barb Rudnick
Greenhorn

Joined: Feb 03, 2005
Posts: 20
Greetings,
I'm working through the cattle drive and my nitpicker made this suggestion

"This boolean expression is kinda long and scary. Introducing some identifiers might make it more readable."

regarding a boolean expression that resembles this:



I'm not sure what she means.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

One way:



Of course, don't really use nonsense words: use words which add meaning. Instead of "snarfblatt", use a description of what fooValue*10 actually represents.

Another way to do this:



I personally like the last one better.
[ October 11, 2005: Message edited by: Ernest Friedman-Hill ]

[Jess in Action][AskingGoodQuestions]
Barb Rudnick
Greenhorn

Joined: Feb 03, 2005
Posts: 20
I like the 2nd solution better too...with a well named identifier the expression will be more self-documented.

Thank you. Have a great day.
[ October 11, 2005: Message edited by: Barb Rudnick ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I like the last one, too. Now replace the 100, 80 and so on with (Java's closest thing to) CONSTANTS and maybe we can figure out what it does!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Kenneth Albertson
Ranch Hand

Joined: Sep 18, 2005
Posts: 59
The discussion has been about introducing identifiers into boolean expressions, but I wonder if that is actually the best approach here. If you start with this:why not begin by simplifying it to this:Then reduce (fooValue > 10) && (fooValue > 80) to just (fooValue > 80), which gives:For greater clarity, I would be tempted to lay it out like this (omitting one set of parentheses that, IMHO, add nothing):Now you could introduce some constants, as Stan suggests.

So I am not disagreeing with what has been said about introducing identifiers, but I don't believe that it would be the best approach in this particular case. The meaning of the last expression above is, to me, extremely clear.
Barb Rudnick
Greenhorn

Joined: Feb 03, 2005
Posts: 20
OK then�this is what I love about these forums. Finding out how many ways there are to skin a cat (Btw... I'm a lurker here... I come in and read just for the fun of it.)

The expression I posted bears a vague resemblance to the actual expression in my program. Being an exercise from the drive, I can't post the actual code for fear of severe beating by various nitpickers and, frankly, I have enough bruises as it is.

Before I posted, I had considered all the options presented in the replies; but I needed a bit of validation as to whether or not I was on the right track. It's good to know that my instincts are intact, even after 10 years of green screens and procedural languages.

Here's what I considered with respect to the actual code:

1) Define constants for the values on the right side of each expression. I discounted this because these values really have no meaning by themselves and would not make the logic any more understandable.

2) Introduce identifiers for the left side of each expression. Again, by themselves for the actual code, these have no real meaning.

3) Combine 1 and 2. The logic is no more understandable, probably even more confusing.

4) Introduce an identifier for each expression in its entirety. This seemed to make the most sense to me, but I wasn't quite sure how to go about it. When I saw Ernest's examples, I saw the light. (Btw, Ernest, the simplicity of those sample methods made me simplify some code in my next exercise. It's amazing how one good example can cause an epiphany!)

Anyhow� at the risk of being beaten, I'll share with you the actual names of the methods I created for approach #4:

isEvenlyDivisibleByFour
isNotEvenlyDivisibleByOneHundred
isEvenlyDivisibleByFourHundred

Did I chose the right approach? Imho, yes.

Thank you all!

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Yep, that's definitely what I was recommending.
Kenneth Albertson
Ranch Hand

Joined: Sep 18, 2005
Posts: 59
What about a fifth approach? Use just one function:

isLeapYear()
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Use of identifiers in complex boolean expression