This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

[Newbie] What'is the best practice?

 
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...
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 3389
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 ]
 
(instanceof Sidekick)
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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • 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: 3389
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?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!