Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should ToString be used for critical information?

I just came across some code that overrides ToString() and returns some critical information (not just debug information). Users of this type called ToString() and parse that critical data.

My opinion, from reading various bits and pieces over the years, is that ToString() has a rather weak contract, i.e. override it (if you want) to display some meaningful stuff.

See I said display there? The code that I came across relied on the textual representation of instances of this type to be very specific; adding anything other than what's expected would cause all sorts of problems.

So, my question is, if the textual representation of an object is critical, should ToString() be used or should a more explicit method/property be used, e.g. AsText?

like image 265
Steve Dunn Avatar asked Aug 10 '11 15:08

Steve Dunn


3 Answers

This seems like a pretty bad plan. If users of the type need data, then that type should expose methods to return that data. Why are people parsing the string representation of an object when they have access to the object?

There are of course serialization scenarios, but those are well-defined, and rarely use .ToString() to do their job.

If the textual representation of a string for non-output purposes is required, then I would prefer a separate method (that may or may not utilize ToString() to do its job.) This helps consumers as well as implementors; it would be really unfortunate if a new coder wanted to add some debug dump info in ToString() and broke the class's consumers.

UPDATE: As MattDavey points out, if you implement IFormattable, then that's a good compromise: your consumers call ToString(), but with specific formats in mind, and reliable contract of what that means. Still different from what your colleagues are doing, but an option that's maybe more amenable to them.

like image 156
dlev Avatar answered Sep 25 '22 01:09

dlev


Personally I share your concern. Microsoft's documentation states that the ToString() method

[...] converts an object to its string representation so that it is suitable for display.

Oracle's documentation for Java's Object.toString() is even little stronger:

The result should be a concise but informative representation that is easy for a person to read.

I see these as strong indication that ToString() should convey information that is convenient for humans. A method that returns data that is to manipulated by other parts of the application should have a more informative name. In my opinion even AsText() is too generic.

like image 24
Nicola Musatti Avatar answered Sep 23 '22 01:09

Nicola Musatti


I don't think there is a definite answer.

I would argue for the case of using ToString(), because when creating an API in .NET it is appreciated when common naming conventions in .NET are used, instead of using less familiar names such as AsText(). This convention is followed for example by the class StringBuilder, as its ToString() is returning critical information.

like image 44
JBSnorro Avatar answered Sep 22 '22 01:09

JBSnorro