formerly known as Cnu
[OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Return null - Better than relying on side effects or empty maps, but still requires and extra check
Throw exception - This is the one I prefer
Add isSuccessful() method to class that the client can call to see the status
SCJP 1.4, SCWCD 1.3, SCBCD 1.3
I disagree. An empty map makes entire sense as the return type is Map, so why return a different type? I really dislike null being returned in a case like this.
I disagree. An exception should only be thrown if something went wrong, eg database server has gone down. Note that it may be perfectly valid for no prices to be available, so why throw an exception?
I disagree once more . You should not put the onus on the developer of the client code to check for something going wrong. If something does go wrong, throw an exception. You get much more robust code this way.
[OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
formerly known as Cnu
disagree. An empty map makes entire sense as the return type is Map, so why return a different type? I really dislike null being returned in a case like this.
Originally posted by Bear Bibeault:
And I completely disagree with this. Why go through the overhead of creating and discarding an empty Map when a null serves the purpose quite nicely?
I love null being returned in cases like this.
As for an exception, I would only throw an exception in an exceptional case. If it's an expected usage case that the Map cannot be populated I would not use an exception to indicate that condition. I would return the null.
Never send an exception to do an if statement's job.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
And I completely disagree with this. Why go through the overhead of creating and discarding an empty Map when a null serves the purpose quite nicely?
I love null being returned in cases like this.
SCJP 1.4, SCWCD 1.3, SCBCD 1.3
"I'm not back." - Bill Harding, Twister
I very much prefer
if (priceProvider.hasPricesFor(product)) {
formerly known as Cnu
Originally posted by Jack Ryan:
I would vote for returning NULL. Client would anyway check for null before checking whether there are any elements inside.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
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
Originally posted by Ilja Preuss:
Why would he do that if he knows that the method never returns null?
"I'm not back." - Bill Harding, Twister
Originally posted by Jack Ryan:
How does he know that the method never returns null? By looking at the code,
by asking the person who wrote it.
But then "fair weather" programming has its own way!
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Jack Ryan:
In house developments generally do not witness so much discipline.
With frequently changing requirements the code any way keeps going unstable every other day.
And thats where the tendency to make the code 'bullet-proof' sneaks in. After all who likes a NullPointerException but that is besides the point.
null is fairly well used as an indicator for the absence of something. So can be an empty object or even an exception.
isEmpty() is however not always available. null is.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
it boils down to how contracts are defined. If you are using 3rd party libraries, you have got good documntation, samples, forums ... etc.
In house developments generally do not witness so much discipline.
With frequently changing requirements the code any way keeps going unstable every other day. And thats where the tendency to make the code 'bullet-proof' sneaks in.
SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Originally posted by Bear Bibeault:
And I completely disagree with this [returning a non-null but empty Map if there's no data]. Why go through the overhead of creating and discarding an empty Map when a null serves the purpose quite nicely?
I love null being returned in cases like this.
Originally posted by Ryan McGuire:
HOWEVER...
Since there seem to be two (almost ) equally valid schools of thought on the subject, be sure to javadoc your method so that client objects know what it'll return if there is no data.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
They weren't very bright, but they were very, very big. Ad contrast:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|