Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

Good question.

To be more explicit, i would create different method for different format.

Ex: toJson() -> JSON representation of the object toXML() -> XML representation of the object. ...etc

Note: there is probably a Library that does that for you.. in java there is. dont know in c#

As you say, parsing a toString() might lead to problem over time, since a new developer might not know that the toString() have a specific format.

In my opinion ToString() after all is a method that we can use it in any desirable way, for instance 5.ToString() convert the int to string and return it no matter if it was to use for display or not, on the contrary, in many situations we are rely on that info that is returned from int.ToString() to do further operations.

No question, there is no definite answer to your question. In my opinion methods like ToString or AsText should only be used to provide the internal state of an object, e.g. to log it. In an object oriented language functional aspects should be derived by using a well defined interface, e.g. GetOrderId, GetUserName.

No, I would not do that. For example, if I had a person object, ToString() might return this.firstname + " " + this.lastname. It's using it for automatic display purposes, like adding items to a list box. When the object is added, the person's name is displayed. I don't think I would put critical or senstive information in the override.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top