• Post Reply Bookmark Topic Watch Topic
  • New Topic

returning null from a method  RSS feed

 
faisal usmani
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,

returning null through a member function ? , is it a good practise or or not in java. I dont remember the date , but it has been discussed earlier, that time i could not understand what should be the best practise.

I am working on a database , looking for a particular record , the column is of varchar(2)type , now if i have an entry in that column i should return the entry otherwise i can return null or may be a zero length string . , its upon me ..

So what should i do



regards
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you return null from a method where the calling method expects an object, it will probably throw an Exception.
You either need to ensure that people expect null returns (javadoc comments will help) and are able to handle them (a simple "if(foo != null) bar();" will often suffice), or not return nulls.

In the example you suggest, a zero length String will probably allow the calling method to do nothing without crashing the whole app. That assumes of course that your return type is String . . .

Other people may have different opinions: please tell us.
 
faisal usmani
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell , i hope more people come and throw light on this
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends if an empty string is a valid value for that database column. If it is then returning an empty string from your method is ambiguous.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To recap some of the recent discussion: You can make a good argument that the method signature says it returns a string so it really ought to return a string. There's no way to indicate "returns null in some condition" except documentation which is always troubling. Nulls put a burden on the clients to test for them and avoid null pointer exceptions. So overall, I'm not happy about returning nulls.

I haven't found a better choice though when you seriously have to distinguish between "the user entered an empty string" and "the user didn't enter anything" or such things. So, happy or not, I still use them sometimes.

On a new tangent ...

I've seen papers suggesting it would be better to never return primitives and native library types. So getName() wouldn't return a String, it would return a DomainSpecificKindOfName object. I've never tried that style; it sounds like a lot of work but it would also be a great place to encapsulate special states like "the user never entered this at all" or even the proper behaviors for that condition.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by faisal usmani:
...i hope more people come and throw light on this

Be careful what you wish for. :roll:
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To complete the summary of what was discussed, there was an alternative proposed which is analogous to Haskell's Maybe and Just.

Without going too far, it starts with the following declarations:

and instead of returning null e.g.

the declaration becomes non-imperative e.g.

By the way, you've asked a question that surrounds one of the biggest problems that has plagued imperative programming ever since its inception so it certainly doesn't belong in the beginner forum.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Tony]: By the way, you've asked a question that surrounds one of the biggest problems that has plagued imperative programming ever since its inception so it certainly doesn't belong in the beginner forum.

The thing is, beginners do need to decide whether to return null from a method or not. The original poster had seen one of the more advanced discussions, and not understood it. So he asked here. Moving this thread won't magically give the poster more knowledge and experience. Can't we try to answer in terms that are accessible to a beginner? Maybe referring to another thread for the benefit of those who want to follow the more advanced reasoning?
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something nobody has yet suggested in this thread, which might not be suitable for a raw beginner, so avert your eyes, anybody who falls in that category.

Ilja Preuss did mention the solution in the other thread which marc weber quoted.

If you have a field which is supposed to be filled in, and which isn't, then the third possibility, maybe better than empty String or null, is to throw an Exception. If you can find an SQLFieldNotFilledInException ready-made, all well and good, otherwise you might have to create your own. And if (unlike Ilja Preuss) you don't use a subclass of RuntimeException, you can use a "throws SQLFieldNotFilledInException" declaration, which will tell the compiler to force any calling methods to handle the possibility of no returns.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, you've asked a question that surrounds one of the biggest problems that has plagued imperative programming ever since its inception


I'd guess database and COBOL folks believe they pretty well solved it. Makes me wonder a bit why we're having so much trouble with it still. Accursed progress.
[ August 25, 2006: Message edited by: Stan James ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!