directory-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alex Karasulu <akaras...@gmail.com>
Subject Re: [Code Issues] Don't use toString() to pretty print
Date Mon, 31 Aug 2009 01:22:13 GMT
On Mon, Aug 31, 2009 at 12:58 AM, Emmanuel Lecharny <elecharny@apache.org>wrote:

> Alex Karasulu wrote:
>
>> OK to be more clear on what I desire:
>>
>> We have things like getString() and toString().  Sometimes we have pretty
>> print functionality in toString().  If you have an object which you want
>> turned into a string representation toString() might just do the job like
>> for an Enum or something like that.  You basically intuit that you can
>> toString() the damn thing.  Like Value.toString() is the string value.
>>
>> If you want to have some extensive pretty print output for debugging and
>> logging then put that into a getPrettyString() or something like that
>> instead of toString().  Value or some other object out there like the
>> SchemaObject and subclasses have the toString() doing a pretty print and I
>> just wanted the string representation of the value.  If I want to pretty
>> print something then let's just have a toPrettyString() or something very
>> explicit.  Hard to tell if I should use Value.toString() or
>> Value.getString().
>>
>>
> Well, again, toString() is used for logging or, most important, debugging
> purpose. In this case, the more we have, the prettier it is, the better.
>
> However, as I said,  when the String format is very well known, then adding
> some information is useless (like printing a LdifEntry as something not
> looking like a LDIF).
>
> I insist on the debugging point because the toString() method is what is
> used by *all* the IDE when you want to watch a variable.
>
> getPrettyString() is most certainly not the good thing to use in this case,
> you can't use it while debugging.
> --
>

I disagree in general with this toString() just being used for logging.
It's a bad practice that even Josh Bloch should not characterize as only for
logging.  And the reason for this is that the JVM automatically converts a
type to a string representation by calling toString() whenever a string is
needed.  This is dangerous and one should not presume it's always safe to
just use toString() as an "only for logging output" method that can be
leisurely set to anything u like.

Sometimes people are surprise when the compiler does the interchange between
the type and the String returned by toString().  I just think any method
that the compiler treats in a special way like this should be considered
carefully.

Also regarding your point about IDE's using toString(): usually there can be
some alignment but if not you can always setup a custom renderer in the
IDE's debugger.  Most people rarely ever use this feature since they just
dig into the object via the variable viewer.

--
Alex Karasulu
My Blog :: http://www.jroller.com/akarasulu/
Apache Directory Server :: http://directory.apache.org
Apache MINA :: http://mina.apache.org

Mime
View raw message