Mark Needham

Thoughts on Software Development

Coding: Using 'ToString'

with 5 comments

An interesting conversation I've had recently with some of my colleagues is around the use of the ToString method available on all objects created in Java or C#. It was also pointed out in the comments on my recent post about wrapping DateTimes in our code.

I think the original intention of this method was to create a string representation of an object, but its use has been overloaded by developers to the point where its expected use is as a mechanism for creating nice output when debugging the code or viewing unit test failures.

The nice thing about it in C# at least is that if you are using an object in your UI you can just put the object into the view and the ToString method will be implicitly called when the object needs to be rendered.

The problem with doing that is its implicitness – other developers might change the ToString method when debugging some code to give more useful output and the display logic of our application has now changed, potentially without us realising until a higher level functional test stops working.

The method name itself is not really that intention revealing anyway – what string format is it creating? A display string? A debugging string? It's not that clear.

The approach we are now taking is a bit more explicit and involves having a more explicit method on objects to achieve the same end result.

Method names such as 'ToDisplayFormat' or 'ToServiceFormat' help us to explain a bit more clearly what we are doing while still getting our object to render a different representation of itself.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • HackerNews
  • StumbleUpon
  • Twitter

Written by Mark Needham

February 26th, 2009 at 11:43 pm

Posted in Coding

Tagged with

5 Responses to 'Coding: Using 'ToString''

Subscribe to comments with RSS or TrackBack to 'Coding: Using 'ToString''.

  1. [...] Wrapping DateTime and Coding: Using "ToString" (Mark [...]

  2. [...] Wrapping DateTime and Coding: Using "ToString" (Mark [...]

  3. The thing I hate about ToString() is that it's implemented inconsistently. Most objects just return the name of the class. In most cases useless. I think I'd prefer a 'Not Implemented' exception.

    GaryA

    27 Feb 09 at 7:48 pm

  4. Hi

    Going back to the granddaddy of OO lanaguages, Smalltalk, I would say the intent of toString() is to answer a String representation as a developer would want to see it. In Smalltalk this method is often called printString.
    displayString Smalltalk method answers a String representation as a user would want to see it.

    So basically it has made explicit the different users. In Java I would create a different method for displaying purposes as you can't trust everyones interpretation of toString().

    Carlo

    11 Mar 09 at 7:53 am

  5. For debugging the most normal choice would be DebuggerDisplayAttribute : http://msdn.microsoft.com/en-us/library/system.diagnostics.debuggerdisplayattribute.aspx

    Andrei Rinea

    21 May 10 at 8:52 pm

Leave a Reply