hash_bucket()

Archive for May 2008

image…

Posted on: May 29, 2008

Påminelse:

Google ar ett foretag.
Google agerar enligt samma kapitalistiska intressen som alla andra foretag.
Google verkar i samma marknadsekonomi som alla andra foretag.

Latt att glomma bort nar DN kippar efter andan med rubriker som

Google vill förbättra världen

Advertisements

This article will introduce the Conditional Attribute and give an example of its usage. The technical level is kept low to make the text as accessible as possible. More info can be found in the official C# Programming guide, in particular the section called ‘Conditional’.

When it comes to debugging, the world (of IDEs) is full of tools to make life easier for us as developers. The latest version of Visual Studio (2008) features a vast array of built in debugging aids that help diagnose and keep track of a running application/assembly. This is all well, and maybe what I am about to say next may sound a bit old or odd, but I for one would never do away with my trusty old output console. I have talked about this before in a post that gave a brief walk through on how to open console windows from an existing and running application in Windowed mode (that is sporting one of those fancy graphical interfaces that seem to be so in fashion lately ;) ).

I have written a small dll that I hook up to must of my projects, through which you can ‘report’ debugging messages to one of 5 pre-defined ‘channels’. The report can be set to write to:

1) The Visual Studio output console (default)
2) A regular text file (you set the path)
3) A console window
4) Email
5) Wreq (my own custom project management tool)

This little tool uses reflection to keep track of what the application is up to, but you can also tell it to ‘report’ any message you choose at any time.

Eg:

DebugReporter.Report(
MessageType.Information,
“[Application Name]”,
“Creating and setting Auto Save path” +
“\nPath: ” + _path.ToString());

or:

DebugReporter.ReportMethodEnter();

DebugReporter.ReportMethodLeave();

One thing about reporting debug info this way is that when you switch to a Release build you want all of those messages to go away automatically, so that you do not have to comment out all these calls.

One way of acheiving this would be to use the #if-endif construct such as:

#if DEBUG
DebugManagers.DebugReporter.Report(
DebugManagers.MessageType.Information,
“[Application Name]”,
“Creating and setting Auto Save path” +
“\nPath: ” + _path.ToString());
#endif

This pre-processor directive would tell the compiler to skip the encapsulated block of code. However, in a typical small application I usually end up with a lot (hundreds) of calls like this and decorating them all with #if-endif just isn’t a very elegant solution. Fortunately the designers of the CLR thought about this and gave us a much better way of dealing with this situation.

One thing you can easily discern from the above code is that all these calls access the same static method on the same static class. Thus, instead of decorating all the method calls, what if we could decorate the method itself?

In .net there is a concept called Attributes that allows you to add custom behaviour and ‘properties’ to methods. The framework contains a lot of pre-defined attributes that deal with for example security, compiler-options, discoverability and much more. Also, as a developer you can define your own attributes, but that is beyond the scope of today’s topic. (See the ‘.NET Framework Developer’s Guide’, in particular the section on ‘Writing Custom Attributes’.)

One such pre-defined attributes is the ‘Conditional’ attribute. This works pretty much like an inverse version of the #if-endif construct. it tells the compiler when to, for example execute a particular method.

Note: This is not a pre-processor directive in the form we know it from of a C/C++, in fact the .net framework as no such concept since the code is JITed upon execution. Thus the above statement that the compiler executes a piece of code is sort of correct, if by compiler to mean the CLR, and by executing you mean Just-In-Time compilation… The C# Programming Guide calls this a ‘preprocessing identifier’.
(See the ms-help page ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/dv_csref/html/e1c4913b-74d0-421a-8a6d-c14b3f0e68fb.htm)

When using the Conditional attribute you define a constant condition that has to be met for any code-path that access the decorated mehod to be executed. Naturally this can help us in the debuggin scenario. One such constant that is defined automatically when building a Debug build in Visual Studio is the DEBUG constant. Thus we can do the following:

[Conditional(“DEBUG”)]
public static void Report(…)
{

}

Using this attribute is what invokes the magic. Now, if the DEBUG constant is not set, any method call to the Report method will simply be excluded from the build, keeping our Release build nice and neat and without cluttering the code with those #if-endif. In fact, the method itself wont event be present in the emitted MSIL :). That, is clean!

There are a few restrictions on using the Conditional Attribute. In particular, the method you want to decorate must have a return type of Void.

Note: This actually makes a lot of sense as the method will no longer be available and the compiler would have a hard time tracing all the possible usages of the eventual return value that might be expected by later code…

I still see a lot of (c#) code written that is full of #if-endif (almost exclusively to defer execution paths in/out of debugging), but I would highly recommend you start experimenting with using the Conditional Attribute instead (where applicable of course ;).

There are still some legitimate use for the #if-endif of course, for example when you want to skip a particular code-block within a method, or when the method you call has a return type other than Void. Thus you should not rely on the Conditional attribute to solve all of your conditional execution, but keep it around as a powerful and simple technique. Also, it might give you an excuse to learn more about the power of Attributes in .NET, and how to write your own custom Attributes.

recap…

Posted on: May 3, 2008

Since things have calmed down a bit I figured I’d take the opportunity to tell you what exactly happened right after the birth of our daughter 2 weeks ago. Some of you have emailed me and expressed worries as the why we got so quite, I appreciate your kind words. Also, I figured I’d share this experience with you (and whomever it may concern) as it might prove valuable to others as well.

Before you read any further, let me just point out that S is now, given the state of things, doing well and the baby is also fine. :) Also, the photos below are taken from various resources online, they ARE NOT S.

About 4 days before the delivery S started getting an itchy rash on her belly, it looked like little red bumps that appeared first along her stretch-marks and then rapidly spread across her entire stomach. She had some stretch-marks due to the straining of her skin, so at first we figured it was something that had to do with general fatigue of the skin, but as the itchiness grew stronger and stronger it became almost unbearable to her and she couldn’t sleep at night. In the end she could think of nothing to do but to cool her belly with ice pads. Normally you’re not supposed to let your belly grow cold during pregnancy, but since the itching was become nothing short of pure torture there was nothing else to do.

Getting rashes and itchiness during pregnancy is not that uncommon, so we we’re planning on consulting with the doctors on our next check up to the hospital but at first we wanted to do some research on our own. S found a webpage that talked about a rare skin disorder (1/100) called PUPPP (there is a lot of dedicated information on the web, just google for it) that looks something along the lines of the photo below.

PUPP

S’s symptoms seemed to fit perfectly, and since this disorder has no negative impact on the child (though it gives the mother pure hell) she figured she’d be able to stand it until the next checkup 1 day later. Also, this rash goes away almost immediately after birth, so worst case scenario she’d had to stand it for another week.

No need to however, the very same day that she found the page, her water broke.

It was just after midnight, the morning of April 18 that the water broke. We rushed to the hospital and about 8 hours later our daughter was born.

During the first 24 hours after delivery the rash on her belly almost went away and so we rejoiced and figured we were safe. However, as we would later learn, this was a false illusion probably induced by S being entirely washed out after the delivery (no pain relief what so ever during the birth…)

On the second day after the delivery the rash on her belly came back, grew even stronger and rapidly started spreading over her legs and thighs. The rash itself also changed character slightly and turned into red, bumpy plateaus of blisters and spots. This got us worried of course, and we started suspecting that it was not PUPPP after all. We alerted the doctors, but they seemed mostly clueless, and since it she was still able to endure the recommended we wait and see for a few days.

PG

On the third day after delivery, the rash and itchiness had grown so strong that S could no longer stand up or sit up straight, thus she could no longer breast feed and for the next few days she was not even able to visit the child. Also, due to the rash she could not sleep and was rapidly losing body strength. (Not to mention the mental strain)

I wish for no pregnant woman to have to go through this, but S is a fighter beyond any superlatives and I cannot express in words how strong she has been throughout this whole experience…

Again we appealed to the doctors to let her see a dermatologist, and as they were finally beginning to see the seriousness of the situation they set her up for a consultation. In the meantime I was doing some research on my own and had narrowed her possibilities down to either a very rare form of PUPPP or an even more uncommon disorder called Pemphigoid gestationis.

Pemphigoid gestationis is the result of hormone changes that occur during pregnancy and since the body’s hormone balance changes extremely rapidly right after giving birth, it usually comes into full bloom immediately after delivery. In some cases it leads to pre-term delivery and/or fetal death. Luckily, Linn had already been born and had not been affected at all, so at least we could relax on that matter. Usually this disorder comes back with subsequent pregnancies, in which case the symptoms usually grow even stronger.

The dermatologist confirmed our suspicions as possible causes and scheduled a skin sampling, thus S was kept in the hospital for an extra 2 days. The results of the test will be delivered next week.

At first, to coup with the itching and to prevent further spreading (by this time the rash covered most of her lower body, arms and belly) S was given a steroid cream to be applied directly to the rash itself, in effect this meant we had cover almost her entire body in the stuff. However, this cream seemed to have little or no effect and since it required such a large dose, after a couple of days she was given an oral medicine instead. This seemed to relax the itching slightly, and on Friday the 25th she and Linn could finally leave the hospital.

At the time of writing S’s condition has improved a lot and she is now able to move around more or less as usual, when her body temperature rises however, the red rashes come back to some extent, and so the doctors have recommended that she relax and take it as easy as possible. The itchiness has not gone away yet, but at least it seems manageable now.

According to online sources this condition usually goes away within 3-4 weeks, though in some rare cases it can go on for up to a year. Given S’s progress though I think she will be free of this torture within a week or two.

S, I really wish I could have done more for you during this time, saying that I love and respect you even more after seeing you go through this hell without losing spirit wont relieve the pain or the fatigue, but I do believe the worst is over and given just a little bit more time I’m sure this torture will go away.

Further reading on PUPP:

Further reading on Pemphigoid gestationis


.

This blog has no clear focus. It has a focus though, it's just not very clear at the moment...

Dev Env.

Visual Studio 2008 Prof / NUnit / Gallio / csUnit / STools (ExactMagic) / doxygen / dxCore / TypeMock / TestDriven.net / SequenceViz / CLRProfiler / Snoop / Reflector / Mole / FxCop / Subversion / TortoiseSVN / SlikSVN / CruiseControl.net / msbuild / nant

Blog Stats

  • 81,435 hits