Coding: Using 'ToString'
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.
[...] Wrapping DateTime and Coding: Using "ToString" (Mark [...]
Dew Drop - February 26, 2009 | Alvin Ashcraft's Morning Dew
27 Feb 09 at 12:02 am
[...] Wrapping DateTime and Coding: Using "ToString" (Mark [...]
Dew Drop - February 26, 2009 | Alvin Ashcraft's Morning Dew
27 Feb 09 at 12:02 am
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
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