sept. 03

You probably create regularly new objects. Let's imagine you create a new class like this :

Of course you will use it and you will want in some moments to debug and see the value of your class. By default, here is what the debugger watch will show :

So of course, you know that the ToString method is used for the display and will update your object definition with non-fonctional - and probably not tested - code, to have a nicer debug display.

 

So you are now writing kind of debug code in your production environment. Definitely an error.
So let's remove the ToString method and let's achieve that differently !

And here it is ! Now your application contains only functional code and you can also have a nice debug experience !
Note that you can use the syntax "{name}" where "name" is either a internal member, a property, ... on which Visual Studio will call the ToString() method if necessary.

So to conclude, your application should never contain debug code. You may want to use the "ConditionalAttribute" or the "#if" syntax, but the best is not introduce debug code. And in such a case, the DebuggerDisplayAttribute is probably the solution that will perfectly suits your needs !

    
Tags: |

Commentaires

Didier Pieroux

Posted on mercredi, 5 septembre 2007 13:02

My attention has been drawn on this post because the exact opposite advice is found in the Java world. For instance, in the book of Joshua Bloch, one of the lead Java framework architect, the advice 9 title reads “Always redefine toString()” (translation back in English from a French version of the book).

So I am wondering: “what does motivate such a difference?”

In Java, toString() is defined at the level of the root class Object. It is invoked whenever it is necessary to convert implicitly an object to a String. For instance, it is common to write:

void String f(MyClass myInstance) {
  return “bla bla” +  myInstance;
}

This is equivalent to returning “bla bla” + myInstance.toString().

Because this implicit conversion mechanism from an object to a string is part of the language, it is also widely used by the framework itself. Therefore, it is assumed that toString() returns the correct string representation of the object, and it is the responsibility of the developer to override toString() accordingly.

So, my question is: is it different in C#? More specifically, does the framework also widely uses ToString() internally or not ? Is ToString() a “language idiom” in C# as it is in Java ?

From a more general point of view, what the post really claims is that ToString() should not be overridden for debugging purpose with debug code. That rule also holds for Java of course, for the very same reason that toString() is used by the framework in “production”. And, generally speacking, I totally agree on the fact that cluttering the code of the application with debug code is probably a bad idea, whatever the language ;)

PS: I like the use of the DebuggerDisplay attribute...  Nice feature indeed!

Pierre-Emmanuel Dautreppe

Posted on jeudi, 6 septembre 2007 10:04

Hello Didier !

Nice to see you here !
Indeed you are right. The .NET and the Java object model are quite close, and even very similar for what is related to the ToString method.
Indeed, the ToString method is defined on System.Object, return the type name by default, and will be used by the framework whenever it needs a string and that we provide him with another kind of complex objects.

So you may need ToString methods. Note by the way that the book "Effective C#" has among its 50 advices : "Always redefine ToString on your object" Smile

I like using provocative titles, waiting for reactions and so for a good start for technical discussion ;)

My point (I do here a mix between my thoughts and the "Effective C#" comments) is that the .NET framework has many way of converting objects to string. The ToString methd of course, IFormattable interface and IFormatter base class. Each of them adding a certain amount of complexity "of course".
In the real world, we are dealing with complex objects and functional entities. How can a whole loan be represented in a single (short) string ? Using the name, the rate, the amount, all of them ? In the real world we would ave to deal with much more complex representations than just a "summary" line. So in the end, most of the ToString methods I see (most of them, but not all of them) are used for having a nice experience purpose which is I think a big mistake. Functional code in functional application, nothing more.

But of course, when ceating a new object, we should always wonder : "can this be represented in a short string way? Do we functionally need that ? Will we use this object in a ConboBox (without specifying which data to display ?"

I thing the ToString method shall be used with parcimony only when the object we create has a functional sense of having one.
What do you think ?

Didier Pieroux

Posted on jeudi, 6 septembre 2007 11:11

"I thing the ToString method shall be used with parcimony only when the object we create has a functional sense of having one.
What do you think ?"

I think that it would be foolish to disagree on that. But as opinions - and mine in particular - are of small importance in front of facts, I decided to quickly check how I work in practice.

On the 132 business classes of the Etic application that I wrote about 2.5 years ago (gui and test related classes not accounted for), toString() has been overridden 21 times. About 15% of the classes thus.

That's probably in agreement with the notion of "parsimony" ;)

Pierre-Emmanuel Dautreppe

Posted on vendredi, 7 septembre 2007 11:15

And just for information, we should have used the word "parsimoniously" or "sparingly" Smile
"Parcimonie" in french gets translated into "parsimoniousness"...

http://www.jamakbada.co.kr/home/190377

Posted on mardi, 17 septembre 2013 02:31

But that would only be half the story because The Conjuring is based on a true story shtick. The Conjuring HouseThe house in The Conjuring," which told the story of the Smurl family of West Pittston, Pennsylvania who alleged that a demon inhabited their home between 1974 and 1989. He was also excellent as part of the movie did they view as being fake.

Also visit my website - Am anh kinh hoang ( http://www.jamakbada.co.kr/home/190377 - http://www.jamakbada.co.kr/home/190377 )

Ajouter un commentaire




biuquote
  • Commentaire
  • Aperçu immédiat
Loading