July 14, 2003
toString - just say no!

Java tip of the day - a must for newbies (are you reading this, Steve?), and a valuable reminder to the experienced: Kasia's The abuse and over-use of toString().

I was working on other people's code last week, and I wish they had read this! The debug code was using toString() all over the place, and NullPointerExceptions were frequent.

To sum up, the rules with toString() are: always override it, and never call it (directly). Instead, use String.valueOf() at all times.

Via Erik.

Posted to Java by Simon Brunning at July 14, 2003 10:55 AM
Comments

Haven't got this far in the book yet but it makes sense to me which must be a good thing. Now all I have to do is remember it.

Posted by: Stevan Rose on July 14, 2003 12:06 PM

Me too - hence the post!

Posted by: Simon Brunning on July 14, 2003 12:09 PM

BTW, Kasia points out that String.valueOf() is used *implicitly* whenever objects are coerced to strings. But given that the whole idea here is to bulletproof code, I'd prefer *not* to rely on this implicit behaviour, and to call String.valueOf() explicitly. YMMV.

Posted by: Simon Brunning on July 14, 2003 12:16 PM

OK, Simon. Following your logic, I shouldn't depend on new to allocate memory for me, I should call the OS directly to allocate memory to make sure my code is bulletproof.

Is that right?

Posted by: no one on July 14, 2003 03:55 PM

I *don't* like implicit coersions. A metter of taste, perhaps. Or are you talking about something other than calling String.valueOf() explicitly?

Posted by: Simon Brunning on July 14, 2003 04:43 PM

and if you are concating strings with

"Fate"+"is"+"Golem"


especially in J2ME you should be taken out back and shot!

Posted by: Fred Grott on July 14, 2003 10:13 PM

Fred - Isn't that a myth now?

I was sure most of the compilers magiked that away by now.

Posted by: Daniel Sheppard on July 15, 2003 02:30 AM

Note that I'm only advocating not using toString() - not avoiding non-static methods altogether! Nor am I suggesting that you should avoid explicit checks for null in general.

toString() is a special case, as far as I'm concerned, 'cos it's almost exclusivly used in exception handling and logging code, which should not fail under *any* curcumstances. Yes, if you have a null where you shouldn't, something has gone wrong and your program is almost certainly going to fail Real Soon Now. But you *need* your logging and exception handling code to work to help you work out what is going on...

See http://www.freeroller.net/page/ceperez/20030714#non_static_method_calls_considered and its comments for more on this.

Posted by: Simon Brunning on July 15, 2003 10:44 AM

It seems really naive to blame the *caller* of toString() when it's the stupid author of the underlying class who did the wrong thing. If you can't trust toString() to work correctly, 100% of the time, you're better off choosing someone else's implementation of whatever functionality you are trying to use.

Posted by: Craig McClanahan on August 13, 2004 05:54 AM
Post a comment
Name:


Email Address:


URL:



Comments:


Remember info?