Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Java in General
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Ron McLeod
Paul Clapham
Tim Cooke
Devaka Cooray
Sheriffs:
Liutauras Vilda
paul wheaton
Rob Spoor
Saloon Keepers:
Tim Moores
Stephan van Hulst
Tim Holloway
Piet Souris
Mikalai Zaikin
Bartenders:
Carey Brown
Roland Mueller
Forum:
Java in General
A Java brainteaser for you
Dave Alvarado
Ranch Hand
Posts: 436
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi,
I'm using
Java
1.5 with the PMD code-checker. It is currently complaining about this method ...
public int getNumResults() { return this.budgetExpenseRecord != null ? 1 : 0; } // getNumResults
because it says, "Avoid if (x != y) ... else". However if I change the method to
public int getNumResults() { int numResults = 0; if (this.budgetExpenseRecord != null) { numResults = 1; } // if return numResults; } // getNumResults
it complains about a 'DD anomaly', because numResults gets defined in different scopes. Any idea how to write this method to satisfy this thing?
Thanks, - Dave
Paul Clapham
Marshal
Posts: 28296
95
I like...
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
It has a symmetry fetish? Then like this, maybe?
public int getNumResults() { int numResults; if (this.budgetExpenseRecord != null) { numResults = 1; } else { numResults = 0; } return numResults; }
Paul Clapham
Marshal
Posts: 28296
95
I like...
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Or perhaps this in response to the first complaint:
public int getNumResults() { return this.budgetExpenseRecord == null ? 0 : 1; }
Dave Alvarado
Ranch Hand
Posts: 436
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi Paul, Thanks for your suggestions. I modified your first solution to read
public int getNumResults() { int numResults; if (this.budgetExpenseRecord == null) { numResults = 0; } else { numResults = 1; } return numResults; }
and the warnings went away. Thanks! - Dave
David Newton
Author
Posts: 12617
I like...
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Yuck.
DD anomalies can often be safely ignored (as it explains); consider turning off the check, using a comment-based switch, or something else.
Paul Clapham
Marshal
Posts: 28296
95
I like...
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Well, I do agree that logic of the form
"If not X then do Y else do Z"
is usually easier to understand if it's written as
"If X then do Z else do Y"
although there are specific exceptions to that general statement.
David Newton
Author
Posts: 12617
I like...
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Agreed; I'm just yucking about the if/else expression that's more expressive as a ternary.
A sonic boom would certainly ruin a giant souffle. But this tiny ad would protect it:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Help with "Missing return Statement" error
Boolean vlaue for a video store program - please help!!
How to modify this program to use & display 1s and 0s instead of true and false?
How can I stop the object to be created
Evaluation order
More...