aspose file tools*
The moose likes Java in General and the fly likes Returing null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Returing null" Watch "Returing null" New topic
Author

Returing null

Arun Thakur
Greenhorn

Joined: Mar 15, 2006
Posts: 17
Hi all,

I have a method which returns a Vector(which is intialized in that method). The method accepts parameters as null(intentially passed) and method makes connection with database and add into that vector using the argument.
Should the vector be null???

Thank you
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1007
    
    3
Originally posted by Arun Thakur:
Hi all,

I have a method which returns a Vector(which is intialized in that method). The method accepts parameters as null(intentially passed) and method makes connection with database and add into that vector using the argument.
Should the vector be null???

Thank you


Are you asking whether the method should ever return a null (as opposed to non-null but empty) Vector?

That is a reasonable possibility, but the answer depends on what makes the most sense in your program. For instance, I could imagine a program that returns a Vector of all the invoices that a customer (given by the argument) has outstanding. If the customer has some such invoices, then the Vector will have elements. If the customer is found in the DB but has no outstanding invoices, then the returned Vector would be non-nul, but empty. If the customer was not found in the DB, the returned Vector would be null. And lastly, if the database access actually caused some type of Exception, then the method would throw an Exception.

There are those that would say if the customer isn't found, then the method should throw an Exception. That is a possibility, but I like to reserve Exceptions for situations that really are exceptional. (...hence the name.) Having a clerk enter a person's name and that name NOT being in the customer table doesn't feel like an exceptional situation to me. Trying to connect to my Oracle server and not being able to DOES.

Did that answer your question?
[ March 15, 2006: Message edited by: Ryan McGuire ]
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
You should never return null (nor accept it as a method parameter). Prefer to signal than return a "dynamically-typed nothing". Java implements signals using exceptions. Unfortunately, a signal/exception results in an implicit unrolling of the call stack until the handler - this is a language limitation.

Ignore the baloney about "exceptions are for exceptional conditions", "runtime exceptions are for this and that", etc. The biggest pain of writing software properly is when you have a forced dependency on a prolific violation.


Tony Morris
Java Q&A (FAQ, Trivia)
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Ignore the baloney...

It might never have occured to you that you can express your opinion without resorting to calling the opinions of others baloney or nonsence or other such juvenile dismissals.
[ March 15, 2006: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Originally posted by Garrett Rowe:

It might never have occured to you that you can express your opinion without resorting to calling the opinions of others baloney or nonsence or other such juvenile dismissals.

[ March 15, 2006: Message edited by: Garrett Rowe ]


It occurs to me all the time.
Reality cannot be denied however. That something is accepted by the establishment does not in any way legitimise it.

In fact, one might argue against your assertion that it might never have occurred to me... this is your opinion (under your definition). Another one of your implications is that we are referring to "juvenile dismissals" - again, your opinion under your definition of the term. You've merely assumed it to be reality - so be it - I reserve the right to as well.

What is the difference between my expression and yours? One is accepted by the establishment (I'm sure many would agree with your statement), and one is accepted only by the small number of people who research the relevant topics (i.e. not many in this audience). Does this make my statement opinion and yours not opinion? This is the basis of the debate that rages - I certainly don't accept it. If anything, I have found (through experience only) that you can shortcut the conclusion of any given hypothesis simply by learning all about the established point of view, assuming it is false (called the method of contradiction by logicians), then attempting to prove it true. By doing so, you often prove the contrary (this is a well documented experience throughout history) - sometimes inadvertantly. It's a wonderful feeling, except of course, for the battle that you must now engage in with a majority should you ever wish to communicate (I like interacting, teaching, learning, etc.).

If for some reason you have religious faith or some unforeseen phenomena that somehow legitimises your statement while mine does not, then we merely disagree on what we have faith in - and so I simply suggest we go to the pub and have a beer. Arguing based on faith is ego-centric pointlessness. If otherwise, I suggest that one of us has something to learn - how we go about determining who and what that is, is what matters - at least to me.
Arun Thakur
Greenhorn

Joined: Mar 15, 2006
Posts: 17
I am doing JTesting actually. And the JTEst actually me assertNotNull(t0, ret), which I my opinion should be null. So should I leave it as it is or there is any other way to check that.??
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

If your requirements say you should return a Vector that contains data extracted from the database, then when there is no data extracted, you should return a Vector that contains no data. Zero is just a number too, there's no reason to do anything special for it.

But if you are proposing to return null instead of an empty Vector just because you wrote a test that checks for null, then I think you are carrying test-driven programming too far.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I work with a vendor framework (always have someone else to blame) that returns null for no rows, and it adds tons of null testing and exceptions when you forget to do check. I'd certainly rather deal with an empty Vector. Or some more modern collection.

When returning single objects instead of a collection it's a little harder to express "no object" than "no rows". Google up on the Null Object Pattern for a neat way to encapsulate what should happen when there is no object.



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
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Tony Morris:

It occurs to me all the time.
Reality cannot be denied however. That something is accepted by the establishment does not in any way legitimise it.


But one can question the conventional wisdom without calling an opinion just expressed by another Rancher "baloney." That's the difference between a reasoned debate and a flame war.

Martin Luther didn't nail up a sign that said "You're all full of baloney!"


[Jess in Action][AskingGoodQuestions]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Martin Luther didn't nail up a sign that said "You're all full of baloney!"


Hey, I paid $10,000 for that sign, and the auction house promised me that it was authentic.

Tony, I don't disagree with your statements with regards to using exceptions for improper parameter passing, and returning null. I completely agree with you there. However, you just seem to sometimes take a more agressive attack approach to when you post. Let's just be nice about it. Please.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Originally posted by Ernest Friedman-Hill:


But one can question the conventional wisdom without calling an opinion just expressed by another Rancher "baloney." That's the difference between a reasoned debate and a flame war.

Martin Luther didn't nail up a sign that said "You're all full of baloney!"


I wasn't referring to one particular person/post, when talking of "baloney". I was merely referring to "the establishment". If one subscribes to the establishment in this context, then I hold the opinion that they also subscribe to baloney. I don't like peppering my words to suit the agenda (i.e. ego defence) of others when my primary objective is deriving a closer truth (should one exist). I encourage anyone to place the established point of view under a heavy analysis using some universally recognised method of logic - I'd be most interested in any apparantly sound conclusions that contradicted my understanding/reasoning.

Indeed, if I was inadvertantly subscribing to "baloney", anyone who points it out to me would be most welcome to a free beer on me.

Ultimately, I put this issue down to a culture difference. By referring to "baloney" for some implies an ego attack while for others (i.e. myself), a path of investigation (the awful paradox is that ego and truth often conflict). Is my culture less legitimate that anyone else's?
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
I just realised that the post before my original one portrays what I subsequently refer to as "baloney" - this was entirely coincidental.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Tony Morris:
I just realised that the post before my original one portrays what I subsequently refer to as "baloney" - this was entirely coincidental.


OK. Thanks.
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Originally posted by Tony Morris:

Is my culture less legitimate that anyone else's?

If "your culture" ever amounts to ridicule of other posters, or taking an unfriendly posture, then yes. It is not accepted here.

JavaRanch is not secretly holding out for greater truths -- we love informing, educating, and helping the people who come here. But we also want them to feel welcome here. If being a friendly place for Java greenhorns to meet is at odds with your culture, then yes, your culture could find better places to visit.

I don't think it has anything to do with culture, though. I think you don't care to understand that people who feel at ease usually learn more.


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Originally posted by Michael Ernest:

If "your culture" ever amounts to ridicule of other posters, or taking an unfriendly posture, then yes. It is not accepted here.

JavaRanch is not secretly holding out for greater truths -- we love informing, educating, and helping the people who come here. But we also want them to feel welcome here. If being a friendly place for Java greenhorns to meet is at odds with your culture, then yes, your culture could find better places to visit.

I don't think it has anything to do with culture, though. I think you don't care to understand that people who feel at ease usually learn more.


No, I never aim to ridicule anyone. How people feel cannot be portrayed over an internet medium unless you make assumptions about context - often leading to errors (hence, I remain neutral and assume nothing).

I think you are making the mistake of assuming that I accept and concede to a majority culture - namely, western culture. Sure, I live in Australia and my tv is full of it - I recognise it immediately, but I reject it for me, personally. I simply don't like it.

I sometimes concede to it when I am communicating with others, for example, my 13 year old sister who is in no position to externalise the hitherto "culture" and learn that other people may think differently. I have assumed that the audience on this forum can transcend that level of ignorance. Often times I am mistaken, however, I am an optimist and I continue to assume the best of anyone.

I encourage you to truly identify where I have "ridiculed anyone" without applying any further context. I think you'll find that upon reflection (this requires absence of internalisation), that you must apply cultural context in order to identify the notion of "ridicule". I simply state, "I do not subscribe to this culture". If your answer is, "this forum does" as do all its members, then so be it - I will be forced into making a decision, as I often do. I simply assume that the contrary is true given that it is an globally-accessible medium.

I cannot rule out the possibility that you can come up with some absolute definition of "ridicule" that exists as a static entity external to our very existence and therefore culture, and I'd be most interested in such a notion - only I warn you that you are contradicting the work of many others before you, and it might help to learn of their work beforehand.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
If this discussion is going to be constructive (i.e. absent of personal attacks, etc. as are so common on forums), I wish to continue it in the appropriate place.

Funny how it started as "Returning null"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Returing null