directory-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Emmanuel Lecharny <>
Subject Re: [Code Issues] Don't use toString() to pretty print
Date Sun, 30 Aug 2009 19:37:25 GMT
Alex Karasulu wrote:
> Let's stop playing Picasso with the toString() method please.  This method
> should be chosen carefully. I've already gotten bit a few times because
> this.
I would say that toString() methods are mostly valuable for logs and 
debug purposes. Any other usage is a violation of the contract, which is 
summarized by Joshua Bloch in his book (effective Java) :

The general contract for toString says that the returned string should 
be “a concise but informative representation that is easy for a *person* 
to read”.

Expecting that the toString() method will return something that should 
be processed by a program exceed the contract.

I don't think that we should play Picasso either. Something closer to 
hyper realism painting is probably more appropriate.

There is one exemple of bad usage of toString() in the server : 
LdapDN.toString(). And there is a JIRA about it :

In most of the case where the format of the printed object is already 
well defined, we should not try to put the nose over the left eye, and 
the mouth close to the belly... LdifEntry.toString() is most certainly 
not respecting the spirit of the toString() method...

> Thanks,

cordialement, regards,
Emmanuel Lécharny

View raw message