<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>A View From Above: toString() is Evil</title>
    <link>http://www.ghostganz.com/blog/articles/2008/06/24/tostring-is-evil</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Anders Bengtsson on programming and other things</description>
    <item>
      <title>toString() is Evil</title>
      <description>&lt;p&gt;&lt;em&gt;...or some other provocative title.&lt;/em&gt;&lt;/p&gt;


	&lt;p&gt;The good old &lt;strong&gt;&lt;code&gt;toString()&lt;/code&gt;&lt;/strong&gt; method, with us since Java 1.0, has at least two different meanings:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;em&gt;Displaying&lt;/em&gt;: How the object should appear to the user, in the &lt;span class="caps"&gt;GUI&lt;/span&gt;, on a web page, etc.&lt;/li&gt;
		&lt;li&gt;&lt;em&gt;Inspection&lt;/em&gt;: How the object should appear in debug output, logs, debugger tools etc.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Both are in some way &amp;#8220;a string representation of the object&amp;#8221;. The default implementation in &lt;code&gt;java.lang.Object&lt;/code&gt; suggests inspection, e.g. &amp;#8220;&lt;code&gt;java.lang.Object@c37f31&lt;/code&gt;&amp;#8221;, but many APIs, like &lt;span class="caps"&gt;AWT&lt;/span&gt;/Swing, use it for displaying the object to the user.&lt;/p&gt;


	&lt;h4&gt;Problems&lt;/h4&gt;


	&lt;ul&gt;
	&lt;li&gt;It&amp;#8217;s hard to tell which usage is intended when reading the code.&lt;/li&gt;
		&lt;li&gt;Debuggers will use &lt;code&gt;toString()&lt;/code&gt;, which can cause confusing side-effects.&lt;/li&gt;
		&lt;li&gt;Since every object has a &lt;code&gt;toString()&lt;/code&gt;, the &lt;span class="caps"&gt;IDE&lt;/span&gt;&amp;#8217;s usage search becomes unusable.&lt;/li&gt;
		&lt;li&gt;It&amp;#8217;s hard to tell if a &lt;code&gt;toString()&lt;/code&gt; method is dead code or not.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;In addition, a lot of code implements it when it has a more specific meaning. For instance, generating &lt;span class="caps"&gt;HTML&lt;/span&gt; is better done as &lt;code&gt;toHTML()&lt;/code&gt; than as &lt;code&gt;toString()&lt;/code&gt;.&lt;/p&gt;


	&lt;h4&gt;What Others Do&lt;/h4&gt;


	&lt;p&gt;Ruby solves this differently than Java. There are two methods, &lt;strong&gt;&lt;code&gt;inspect()&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;to_s()&lt;/code&gt;&lt;/strong&gt;, where the default implementation of &lt;code&gt;to_s()&lt;/code&gt; in &lt;code&gt;Object&lt;/code&gt; uses &lt;code&gt;inspect()&lt;/code&gt;. This separates the two intentions, but still has &lt;code&gt;to_s()&lt;/code&gt; available on every object.&lt;/p&gt;


	&lt;p&gt;We can&amp;#8217;t do much about &lt;code&gt;java.lang.Object&lt;/code&gt;, but we still have options.&lt;/p&gt;


	&lt;h4&gt;Suggestion&lt;/h4&gt;


	&lt;p&gt;Use &lt;code&gt;toString()&lt;/code&gt; only for logging and debug output.&lt;/p&gt;


	&lt;p&gt;If the method has a more specific meaning, communicate that instead, e.g. &lt;code&gt;title()&lt;/code&gt; or &lt;code&gt;name()&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;If the value to display has a specific format you can communicate that instead, e.g. &lt;code&gt;toHTML()&lt;/code&gt; or &lt;code&gt;asLeetSpeak()&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;If the value to display is nothing other than a string, still avoid &lt;code&gt;toString()&lt;/code&gt;. Call it something like &lt;code&gt;displayString()&lt;/code&gt;, or maybe even &lt;code&gt;asString()&lt;/code&gt; to avoid problems.&lt;/p&gt;</description>
      <pubDate>Tue, 24 Jun 2008 18:00:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:1cc95189-2cf5-4791-a7ca-99f232e79f15</guid>
      <author>Anders</author>
      <link>http://www.ghostganz.com/blog/articles/2008/06/24/tostring-is-evil</link>
      <category>Java</category>
      <category>Programming</category>
    </item>
    <item>
      <title>"toString() is Evil" by Stephen Houston</title>
      <description>BTW, it seems that newlines aren't recognised in comments...</description>
      <pubDate>Sat, 28 Jun 2008 19:42:05 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:fcb95746-96a7-4c5b-b6c8-f57ae07bb343</guid>
      <link>http://www.ghostganz.com/blog/articles/2008/06/24/tostring-is-evil#comment-105</link>
    </item>
    <item>
      <title>"toString() is Evil" by Stephen Houston</title>
      <description>Erm... Shouldn't the front end really do the markup of the object contents rather than the object itself?

What if your object had multiple clients (HTML, XML, Swing, etc, etc), you'd end up having multiple toFoo methods on your object. That's not only clutter, it breaks the separation of concerns principal by having your objects know how they are rendered. 

Putting the display logic inside the object makes it more brittle, more resistant to change. Say you had a toHTML method on it for page A, then page B comes along that wants the object to write out the markup differently. Convert toHTML to be toHTMLForPageA and add a toHTMLForPageB method? What if its used in multiple places? toHTMLForWhenWeWantToDisplayTheObjectNormally?

toString should be used for debugging purposes only. The object contents should be accessed via its accessors and marked up by the front end itself.  </description>
      <pubDate>Sat, 28 Jun 2008 19:40:57 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:d22c9ffa-8b74-4ff3-b179-7e03d00b6053</guid>
      <link>http://www.ghostganz.com/blog/articles/2008/06/24/tostring-is-evil#comment-104</link>
    </item>
    <item>
      <title>"toString() is Evil" by Ignacio Coloma</title>
      <description>Actually, that is not exactly right. IIRC, the string representation of Swing objects are also intended for developer debugging (to display the nesting structure of Swing components). Nobody said otherwise.
Other than that, the "programming 101" comment is out of place. I found the post insightful.</description>
      <pubDate>Wed, 25 Jun 2008 16:28:10 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:bd9e9276-b516-49db-8939-62fbf0c2f0ce</guid>
      <link>http://www.ghostganz.com/blog/articles/2008/06/24/tostring-is-evil#comment-103</link>
    </item>
    <item>
      <title>"toString() is Evil" by ciczan</title>
      <description>Kasper, the JTree component uses toString() to display the node names. Not only for inspection.</description>
      <pubDate>Wed, 25 Jun 2008 07:57:07 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:e3367c09-ca50-407e-8055-78c34f144377</guid>
      <link>http://www.ghostganz.com/blog/articles/2008/06/24/tostring-is-evil#comment-102</link>
    </item>
    <item>
      <title>"toString() is Evil" by Linus S.</title>
      <description>I think I'm gonna add a mandatory asLeetSpeak() to our coding convention, rly gud id3a.</description>
      <pubDate>Wed, 25 Jun 2008 06:07:01 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:0ef4f769-b69b-468f-a105-01858fdace53</guid>
      <link>http://www.ghostganz.com/blog/articles/2008/06/24/tostring-is-evil#comment-101</link>
    </item>
    <item>
      <title>"toString() is Evil" by Kasper Graversen</title>
      <description>Hi..

Never seen the def. of toString as "Displaying: How the object should appear to the user, in the GUI, on a web page, etc." what reference do you have for this?

Other than that you are absolutely right, if you have a business requirement for a textual rep. of your objs. somewhere make a specific method for it. That's programming 101..

&lt;a href="http://firstclassthoughts.co.uk" rel="nofollow"&gt;http://firstclassthoughts.co.uk&lt;/a&gt;</description>
      <pubDate>Wed, 25 Jun 2008 05:14:51 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:2b254f4a-d9f6-4b4e-b264-51aad07dd91b</guid>
      <link>http://www.ghostganz.com/blog/articles/2008/06/24/tostring-is-evil#comment-100</link>
    </item>
  </channel>
</rss>
