Should ToString be used for critical information?

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

My opinion, since reading various bits and pieces over the years, is that ToString() has a rather weak contract , that is, it redefines (if you want) the display of some significant things.

Look, did I say the display is there? The code I came across relied on a textual representation of instances of this type to be very specific; adding anything other than expected will cause all sorts of problems.

So my question is if the textual representation of the object is crucial, you should use ToString() or use a more explicit method / property, for example. AsText ?

+6
source share
7 answers

Personally, I share your concern. Microsoft documentation indicates that the ToString() method

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

The Oracle documentation for Java Object.toString() even a little stronger:

The result should be a concise but informative presentation that is easy to read to the person.

I see this as a convincing indication that ToString() should pass information that is convenient for people. A method that returns data that should be processed by other parts of the application should have a more informative name. In my opinion, even AsText() is too general.

+4
source

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

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

If a textual representation of the string for non-output purposes is necessary, I would prefer a separate method (which may or may not use ToString() to do its job.) This helps consumers as well as developers; it would be very unfortunate if the new encoder wanted to add some information about the debug dump in ToString() and broke the users of the class.

UPDATE As MattDavey points out, if you implement IFormattable , then this is a good compromise: your consumers call ToString() , but given specific formats and a reliable contract for what it means. Still different from what your peers do, but an option that may be more amenable to them.

+6
source

I do not think there is a definite answer.

I would argue if ToString() , because when creating an API in .NET it is valuable when using common naming conventions in .NET, instead of using less familiar names like AsText() . This convention is executed, for example, by the StringBuilder class, since its ToString() returns critical information.

+1
source

Good question.

To be more explicit, I would create a different method for different formats.

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

Note: there is probably a library that does this for you .. in java there is. don't know in c #

As you say, parsing toString () can lead to problems over time, as the new developer may not know that toString () has a specific format.

+1
source

In my opinion, ToString() is ultimately a method that we can use in any desired way, for example 5.ToString() convert an int to a string and return it regardless of whether it was the opposite, in many situations we rely on this information that is returned from int.ToString() for further operations.

0
source

No question, no definite answer to your question. In my opinion, methods like ToString or AsText should only be used to ensure the internal state of an object, for example. for registration. In an object-oriented language, functional aspects must be obtained using a well-defined interface, for example. GetOrderId , GetUserName .

0
source

No, I would not do that. For example, if I have a person object, ToString() may return this.firstname + " " + this.lastname. It uses it to automatically display, for example, adding items to a list. When an object is added, the name of the person is displayed. I do not think that I would put critical or senstive information in an override.

0
source

Source: https://habr.com/ru/post/894730/


All Articles