Worst toString() Ever…

I think I just wrote the single worst toString(), ever!

public String toString() {
if (this != null)
return “**** implement me”;
return “i am null”;

Heck, it’s late Friday evening. I’m in a hurry…. Get over it…. I will obviously fix this, but I’m still disgusted I’m using it.

Can you throw out something worse?

Ok, back to work.

This Post Has 5 Comments

  1. Jason Brice

    You should check out the commonclipse plugin for eclipse. It’s free, and it will automatically generate toString, hashCode, compareTo, and equals methods for you. Very handy if you’re doing Spring/Hibernate stuff. (Be careful though you can run into some problems if any of those methods make use of lazily-loaded collections.)

  2. Tony

    return new ReflectionToStringBuilder(this).toString()

  3. Erik Weibust

    Thanks for the tip Jason. I saw Commonclipse mentioned on an AppFuse tutorial I was working on that brought about this crazy toString.

  4. Erik Weibust

    Good stuff Tony. I’ll have to compare that to what Commonclipse can do.


  5. Nathan Smith

    It’s hard to get worse, seeing as how if this != null was to fail, the toString() call would’ve thrown an NPE, meaning you’d never see “i am null”

Leave a Reply