This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
I have encountered some classes recently that can return a null value depending upon the object's state. I am wondering if this is an acceptable practice? I would say no, that toString() should always return a String and if null is the value you want to return, return "null". However, I was wondering what others more learned than I would have to say on this topic.
Java is smart enough that whenever you don't create a toString() method that when you instantiate an object in your main method and you want it to return something, you don't have to type in SOP(test.toString) you just have to type SOP(test) (test being the new object to the class). In order for it to return null you would have to do what Serge said and actually create the toString method and tell it to return null, but I wouldn't advise this because there would be no reason that I could see for toString to return null.
HTH
Gabe
Jay Damon
Ranch Hand
Joined: Jul 31, 2001
Posts: 279
posted
0
Serge/Gabriel,
I think you misunderstand the question. I don't want to create a class that returns a null value from toString(). I have inherited an application in which some of the classes do this. Personally, I think it is bad practice to return null from toString().
The Object.toString() Java says toString() "Returns a string representation of the object." IMO, null is not a string representation. "null" is a string representation.
I discovered this problem accidentally while creating a class to parse and display an object's String representation by invoking toString(). Because some objects return null, my class has to guard against this possibility by checking for a null value first. While that may be good practice, I think one should be able to expect toString() to actually return a String value.
I'd agree with you entirely. IMO there is no circumstance under which the null-String gives any useful information about any class. If you're having to check for null values that's even worse! I'd say this is a "code smell" that should definitely be avoided.
If toString() is used for debugging (as IMHO it almost almost always should be), I reckon it should give at the minimum the name of the class, and whatever attribute is most defining for that instance.
Cheers,
--Tim
Tim West
Ranch Hand
Joined: Mar 15, 2004
Posts: 539
posted
0
An addition, if you have access to it you might like to check out Item 9 in Bloch's 'Effective Java', which deals specifically with toString(). Some of his notes:
Providing a good toString implementation makes your class much more pleasant to use.
When practical, the toString method should return all of the interesting information contained in the object.
Whether or not you decide to specify the format of what's returned by toString, you should clearly document your intentions.
Whether or not you specify the format, you should provide programmatic access to all of the information contained in the value returned by toString. This removes the temptation of programmers who use your class to parse the result of toString to gain access to this information.
The explanations for these points are all in the book, I'll leave them there as something to think about
--Tim
(Mods: I hope that isn't too close to the book to be considered plaigarism. Remove it if it is. I'd heartily recommend the book, and I intended that this post do more to recommend it than to prevent its purchase by including its content ) [ June 23, 2004: Message edited by: Tim West ]