Sophisticated debugging in .NET

December 18th, 2007

Every programmer must sometimes use a debugger ;) In fact, most of our job is filled with debugging rather than writing code.
In the .NET framework there are many great features we can use to make us life easier. Today I am gonna present you a few very useful things from System.Diagnostics namespace.

Debugger class:
This is a class which allows us a communication with a attached debugger. It has multiple members but there are 3 of them I want you to notice.:

  • Break - It just sends a break signal to a debugger. Very useful when we want to conditionally stop the execution of our application
  • Launch - It can launch a debugger and attach it to a process
  • Log - It posts a message to the attached debugger

IMHO, the most useful of those three methods is Break. Priceless when we have an unexpected situation in a code. Thanks to this method we are able to break an execution of a program. Look at this snippet:

C#:
object foo = Bar.GetSomething();
if(foo == null)
{
    Debugger.Break();
}

Cool, isn't it? ;)

Debug class:
Another good class which provides us helper methods in debugging process. Noticeable are:

  • Assert - Evaluates a condition and displays a message if a condition is false. Well known for a "test driven" programmers.
  • Fail - Outputs a failure message. Quite similar to the previous one but it doesn't evaluate an expression.
  • Indent and Unident - useful for output formatting.
  • Write, WriteLine and Print - write information to the Debug / Trace listeners
  • WriteIf, WriteLineIf - The same as above but only when specified condition is met

Ok, those classes are in fact primitive but here is something that is very useful in debugging complex hierarchical objects.
Ladies and Gentleman - please welcome Debug attributes. Thanks to them a developer is able to declaratively specify how an application should behave during debugging process.
Let's say that you do not want a debugger step into a piece of code (because it's tested and works well) - no problem, there is DebuggerHiddenAttribute. Actually it stops a breakpoint from being set inside anything that it decorates.

C#:
class Foo
{
    [DebuggerHidden()]
    public void Bar()
    {
        // impossible to debug here :)
    }
}

Impossible to set a breakpoint

Let's say that we want to determine how a class or a property is displayed in a debugger window. There are two other attributes: DebuggerBrowsableAttribute and DebuggerDisplayAttribute. This code will do a trick with local variable / watch window in Visual Studio:

C#:
[DebuggerDisplay("Our Foo class")]
class Foo
{
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    private int _bar = 10;

    [DebuggerDisplay("Value of Bar is: {_bar}")]
    public int Bar
    {
        get
        {
            return _bar;
        }
    }
}

and a debugger view:

Debugger attributes

Nice :) You can ofcourse set some parameters to those attributes. There are more attributes such as DebuggerStepThroughAttribute but I recommend further reading on MSDN.

Bloggers, projects, comments

December 14th, 2007

I read a lot of blogs, mostly technical ones. IMHO it's the best place to broaden one's knowledge.
But I observe very different approaches in two different programming "camps".

Open Source "camp" - very structured, often maintains well driven projects. Rather opened for the community. On blogs, one can read deep thoughts and cool ideas in comments. It's not a perfect world but projects are developed well. Projects have their milestones, one can participate if he feels skilled enough. Documentations grow very fast and at some stage it is possible to use a program or library.

Microsoft "camp" - or rather people who work there, sometimes MVP's. It's so called "mutual friendly society". Each blog looks very similar to one another. People have many crazy ideas, often very good and acceptable. But there are things that piss me off.
Every project or feature is in fact very simple thing - like "we have just added 'get latest version on checkout' in TFS CTP 43523424.1432123 version". And comments like "Oh, you're great", "You rule, man". Rest of comment's are "pingbacks". The most funny thing is that when somebody tries to criticize any idea or a project the comment is just not approved by "almighty" owner. Many projects released as CTP or presented as screencasts are just unusable. Many of them are bloated with so many unused features. But everything looks great, many screenshots, only "wow" and "it's amazing".
I personally am more and more dissapointed when many good ideas are wrapped in shitty piece of paper. For example, LINQ to SQL - introduced as something amazing but in fact, it's only a poor version of Java's Hibernate. I think that in 5 years it should be able to operate on something more complicated than "Northwind" database.

EasyXML 0.2

December 11th, 2007

Today I fixed annoying bug in EasyXML. It crashed when it was pinned to the start menu in Windows. I also added "Start with Windows" option and changed starting behavior a little bit. Now, EasyXML starts minimized.

Go to EasyXML project's page

Designed by SirMike © All rights reserved

Valid XHTML 1.0! Valid CSS!