Coding and Dismantling Stuff

Don't thank me, it's what I do.

About the author

Russell is a .Net developer based in Lancashire in the UK.  His day job is as a C# developer for the UK's largest online white-goods retailer, DRL Limited.

His weekend job entails alternately demolishing and constructing various bits of his home, much to the distress of his fiance Kelly, 3-year-old daughter Amelie, and menagerie of pets.

TextBox

  1. Fix dodgy keywords Google is scraping from my blog
  2. Complete migration of NHaml from Google Code to GitHub
  3. ReTelnet Mock Telnet Server à la Jetty
  4. Learn to use Git
  5. Complete beta release FHEMDotNet
  6. Publish FHEMDotNet on Google Code
  7. Learn NancyFX library
  8. Pull RussPAll/NHaml into NHaml/NHaml
  9. Open Source Blackberry Twitter app
  10. Other stuff

Debugging Good Patterns #1 - ToString() Is Your Friend!

Here's a nice punchy one - so short a post, it might even warrant now't more than a tweet, but the blog's been a bit barren of late so here I am.

When you're debugging code (of course that's a rare thing now I'm living the TDD dream ;), if you frequently find yourself digging around in the Visual Studio Watch or Quickwatch windows investigating properties on objects, sub-objects, sub-sub objects, etc, then here's a good 'un. The quickwatch window becomes massively more useful with one simple addition - override the ToString() method on every class in your system so that it summarises the key properties, without you having to expand that object and hunt down those properties.

For example instead of a list having a value of "Count = 5", wouldn't it be great to see "5 transactions totalling £300"? And when you expand that list of transactions, instead of "Transaction", isn't it better to see 5 objects that read things like "Transaction Value £100 - Authorised £150, Taken £150, Refunded £50"?

You can of course use this ToString() representation all over the place, you can use it in exception messages, logging, you can enter an object's name into the Immediate window to see it, etc. It ain't gonna change your life, but it makes my bug-fixing life just that little bit more tolerable.


Categories: Architecture
Permalink | Comments (0)

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading