• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[Newbie] What'is the best practice?

 
Max Bean
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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."
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic