| Author |
[Newbie] What'is the best practice?
|
Max Bean
Ranch Hand
Joined: Mar 09, 2006
Posts: 31
|
|
If I wanna write the method which return any kind of Object, what's the cleaner and most efficient code to check the initial state and check whether it's null or not? Another thing is the invoker. If I can guarantee that method will return at least null to the invoker, how can I protect NullPointerException in the point of view of the invoker? Please do not consider this question as stupidity. I'm really noobiest...
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
|
|
Joanne
|
 |
Raghavan Muthu
Ranch Hand
Joined: Apr 20, 2006
Posts: 3327
|
|
Originally posted by Maximilian Boonyoung: If I wanna write the method which return any kind of Object, what's the cleaner and most efficient code to check the initial state and check whether it's null or not?
For which you can have a code snippet like this.
Originally posted by Maximilian Boonyoung: Another thing is the invoker. If I can guarantee that method will return at least null to the invoker, how can I protect NullPointerException in the point of view of the invoker?
As you know the method could return *any kinda* object, you should typecast the returned object before its first use. Let's say you have an object being returned of type MyClassObj. (It can be of any other object like String, ArrayList etc). Does this help? [ August 17, 2007: Message edited by: Raghavan Muthu ]
|
Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
If that all worked for you, let's take another step and talk about why you want to return null. It's a pain for the caller to have to test and it's often unclear exactly what we're trying to tell somebody when we return null. We have ways of making life easier for the caller in almost any program.
|
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
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
Originally posted by Maximilian Boonyoung: ... Please do not consider this question as stupidity. I'm really noobiest...
I think it's a great question! In fact, it turns out there can be big differences of opinion about whether returning null is a good idea. How this is handled depends a lot on the context of the problem. So as Stan suggested, "let's take another step and talk about why you want to return null."
|
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
|
 |
Raghavan Muthu
Ranch Hand
Joined: Apr 20, 2006
Posts: 3327
|
|
Originally posted by marc weber: I think it's a great question! In fact, it turns out there can be big differences of opinion about whether returning null is a good idea.
Yes, very much. I agree with Marc. This makes us think on taking a decision should we really need to return null?
|
 |
 |
|
|
subject: [Newbie] What'is the best practice?
|
|
|