You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@directory.apache.org by Alex Karasulu <ak...@apache.org> on 2009/08/29 23:28:30 UTC

[Code Issues] Don't use toString() to pretty print

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.

Thanks,

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

Re: [Code Issues] Don't use toString() to pretty print

Posted by Stefan Seelmann <se...@apache.org>.
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.

+1


Re: [Code Issues] Don't use toString() to pretty print

Posted by Jesse McConnell <je...@gmail.com>.
I am a fan of useful toString() representations of complex
objects...however I generally draw the line at deep information..

my rule of thumb is:

toString() - fine for state and context information about this object

dump() - fine for complex debugging/info for this object and other
objects it references

sometimes it handy to make it dump(PrintStream) so you can do
foo.dump(System.out);

cheers,
jesse

--
jesse mcconnell
jesse.mcconnell@gmail.com



On Mon, Aug 31, 2009 at 03:11, Emmanuel Lecharny<el...@apache.org> wrote:
> Alex Karasulu wrote:
>>
>> OK it's not that big of a deal for me, just a mere annoyance.  I've stated
>> my concerns and you remain adamant on yours.
>>
>
> Yeah. We don't have to agree on everything anyway :) Here, it's much more a
> matter of style than technical matter. As I have Joshua on my side ... ;)
>
> --
> --
> cordialement, regards,
> Emmanuel Lécharny
> www.iktek.com
> directory.apache.org
>
>
>

Re: [Code Issues] Don't use toString() to pretty print

Posted by Emmanuel Lecharny <el...@apache.org>.
Alex Karasulu wrote:
> OK it's not that big of a deal for me, just a mere annoyance.  I've stated
> my concerns and you remain adamant on yours.
>   
Yeah. We don't have to agree on everything anyway :) Here, it's much 
more a matter of style than technical matter. As I have Joshua on my 
side ... ;)

-- 
--
cordialement, regards,
Emmanuel Lécharny
www.iktek.com
directory.apache.org



Re: [Code Issues] Don't use toString() to pretty print

Posted by Alex Karasulu <ak...@gmail.com>.
OK it's not that big of a deal for me, just a mere annoyance.  I've stated
my concerns and you remain adamant on yours.

Alex

On Mon, Aug 31, 2009 at 10:23 AM, Emmanuel Lecharny <el...@apache.org>wrote:

> Alex Karasulu wrote:
>
>> I disagree in general with this toString() just being used for logging.
>>
>>
> You are right : it's also used for debugging ;)
>
>> 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.
>>
> Wrong. It generally prints out an @, not a representation of the object. An
> even if Joshua may be wrong sometime, I think that this usage of toString()
> is a common practice.
>
>>  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.
>>
>>
> It's not dangerous : it's the contract. There is nothing wrong in producing
> a String from an objetc, whne it's dangerous to produce a specific format
> not agreed on before when it's not candy for the user's eye. Again the
> contract said 'for the person', not for the program.
>
>> 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.
>>
>>
> Do you have an example ?
>
>> 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.
>>
>>
> And I don't want to use this feature. It always is a waste of time to have
> to open an object to get what's inside it when a well designed toString()
> method would have produced the exact representation of the object. When you
> are debugging, you want to get straight to the bug, not to have to tune your
> IDE for hours to get what you want.
>
> In general, and this is something very true for ADS, lacks of logs and
> lacks of decent toString() methdos induce long and painfull debugging
> session. We are debugging this server more than we are writing code, now
> that we have delivered stable versions, and this is something that won't
> slow down. In any case, I would favor pretty toString() methods over
> anything else.
>
>
> --
> --
> cordialement, regards,
> Emmanuel Lécharny
> www.iktek.com
> directory.apache.org
>
>
>


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

Re: [Code Issues] Don't use toString() to pretty print

Posted by Emmanuel Lecharny <el...@apache.org>.
Alex Karasulu wrote:
> I disagree in general with this toString() just being used for logging.
>   
You are right : it's also used for debugging ;)
> 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.
Wrong. It generally prints out an @, not a representation of the object. 
An even if Joshua may be wrong sometime, I think that this usage of 
toString() is a common practice.
>   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.
>   
It's not dangerous : it's the contract. There is nothing wrong in 
producing a String from an objetc, whne it's dangerous to produce a 
specific format not agreed on before when it's not candy for the user's 
eye. Again the contract said 'for the person', not for the program.
> 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.
>   
Do you have an example ?
> 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.
>   
And I don't want to use this feature. It always is a waste of time to 
have to open an object to get what's inside it when a well designed 
toString() method would have produced the exact representation of the 
object. When you are debugging, you want to get straight to the bug, not 
to have to tune your IDE for hours to get what you want.

In general, and this is something very true for ADS, lacks of logs and 
lacks of decent toString() methdos induce long and painfull debugging 
session. We are debugging this server more than we are writing code, now 
that we have delivered stable versions, and this is something that won't 
slow down. In any case, I would favor pretty toString() methods over 
anything else.

-- 
--
cordialement, regards,
Emmanuel Lécharny
www.iktek.com
directory.apache.org



Re: [Code Issues] Don't use toString() to pretty print

Posted by Alex Karasulu <ak...@gmail.com>.
On Mon, Aug 31, 2009 at 12:58 AM, Emmanuel Lecharny <el...@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

Re: [Code Issues] Don't use toString() to pretty print

Posted by Emmanuel Lecharny <el...@apache.org>.
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.

-- 
--
cordialement, regards,
Emmanuel Lécharny
www.iktek.com
directory.apache.org



Re: [Code Issues] Don't use toString() to pretty print

Posted by Alex Karasulu <ak...@gmail.com>.
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().

Alex

On Sun, Aug 30, 2009 at 10:37 PM, Emmanuel Lecharny <el...@apache.org>wrote:

> 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 :
> https://issues.apache.org/jira/browse/DIRSERVER-808
>
> 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
> www.iktek.com
> directory.apache.org
>
>
>


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

Re: [Code Issues] Don't use toString() to pretty print

Posted by Emmanuel Lecharny <el...@apache.org>.
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 : 
https://issues.apache.org/jira/browse/DIRSERVER-808

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
www.iktek.com
directory.apache.org