« You turn left. You're still in a maze of twisty encoding thingies, all alike | Main | DOAP Slap »

Attack, el Object, attack!

[update: Mike Kozlowski says it best]

"I think you're right that functional programming at least allows for the possibility of avoiding null reference problems (if the called functions are smart about it), but I'm not sure that's worth giving up the very real benefits of OO. And at any rate, I think the advice those people were giving was "Don't use .toString() for debugging purposes," which seems like a nice tip to avoid a mild irritant."

Chicken Little - that'll teach me!

Simon Brunning:

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

I take two things away from that, and a question:

  • Prefer as first class functions as you get your hands on
  • Prefer a null-object over non-object
  • What about all the other java.lang.Object calls - should we never call them directly either?

If the answer to the question is yes (and I think it might be - what's special about toString() other than being popular?) then its logical conclusion sounds like an argument for preferring functional programming over object programming (and I think it might be - what's special about your object's methods other than not being on Object?).

(Now would be a really good time for a Smalltalker or OO type to pull me out of my mental quicksand by telling me there's a huge difference between object-oriented and heap-oriented programming... or something.)

[the hives: abra cadaver]


August 12, 2004 07:45 PM

Comments

Mike Kozlowski
(August 12, 2004 11:46 PM #)

I don't think what you said follows from that advice. The objection to .toString() isn't that it's inherited from Object -- it's that (by using it during System.out.println() style debugging) you're calling it in situations where a null pointer is more likely than normal, and inside exception handlers, where a null exception is more harmful than usual.

If you want to avoid all potential NullPointerExceptions, you'd need to avoid all non-static methods. I doubt anyone would advise that...

Bill de hra
(August 13, 2004 03:52 AM #)

"I don't think what you said follows from that advice. "

I suppose you're right. Still, advice never to use a top level Object method feels odd.

"If you want to avoid all potential NullPointerExceptions, you'd need to avoid all non-static methods."

I suspect that's what functional programming tends to do, in a roundabout way.

Craig McClanahan
(August 13, 2004 05:55 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.

Mike Kozlowski
(August 13, 2004 05:45 PM #)

Craig, the problem isn't with a particular toString() implementation -- it's that you can't call instance methods of null objects without throwing an exception. Even if the first line of toString() was [ if this == null return ""; ], it wouldn't work, because the exception'd be thrown before it was called.

Bill, I think you're right that functional programming at least allows for the possibility of avoiding null reference problems (if the called functions are smart about it), but I'm not sure that's worth giving up the very real benefits of OO. And at any rate, I think the advice those people were giving was "Don't use .toString() for debugging purposes," which seems like a nice tip to avoid a mild irritant.

Trackback Pings

TrackBack URL for this entry:
http://www.dehora.net/mt/mt-tb.cgi/1379