# Tuesday, May 20, 2008

This is another one in the series "heck, I never thought of that"... Like most of these articles, if you already knew this trick, ignore me...

Let's say you have a generic class with a new() constraint on the type parameter. This means that you are allowed to create new objects of the generic type, like this:

class GenericClass<T> where T:new()
{   
   public void SomeMethod()   
   {      
      T obj = new T();
      ...
   }
}


Pretty straightforward stuff, BUT there is a possibility that type T implements IDisposable, meaning that you should clean up after using any object of type T (using the Dispose method or a "using" block.

The trivial way of solving this problem is:

class GenericClass<T> where T:new()
{
   public void SomeMethod()
   {
      T obj = new T();

      ...

      if (obj is IDisposable)
         ((IDisposable) obj).Dispose();
   }
}

 

Not too bad, but we can do better:

class GenericClass<T> where T:new()
{
   public void SomeMethod()
   {
      T obj = new T();

      using (obj as IDisposable)
      {
         ...
      }
   }
}


Pretty neat, don't you think? What actually happens is that the using block creates a "hidden" variable of type "IDisposable" and will call Dispose() on it when exiting the scope of the using block. If T does not implement IDisposable, the hidden variable will be null, and the compiler will not try to call Dispose().

kick it on DotNetKicks.com
Tuesday, May 20, 2008 11:29:08 PM (W. Europe Daylight Time, UTC+02:00)  #    Comments [5] -

Many of you know me from some open source projects that are hosted on CodePlex:

The next project has been in development for quite a while and is closely related to the other projects, as it is being used in ProMesh.NET and it is based on the LazyParser.NET expression parsing engine.

So I'd like to introduce SharpTemplate.NET, a lightweight general-purpose template engine for .NET 2.0. It allows you to incorporate template parsing in your .NET applications. Perfect for code generators, reporting tools, mass-mailing applications, etc..

The syntax of SharpTemplate.NET is not fixed, unlike other engines (NVelocity, StringTemplate, ...). The syntax is fully configurable with just a few lines of code and some regular expression magic. Currently, the following syntaxes are built in:

  • SharpVelocity (Velocity-like syntax)
  • ProMeshHtml (syntax used by ProMesh.NET)
  • XML
  • "DoubleCurly"

To give you an idea of how it works, here is an sample of a template (using Velocity syntax):

<table>#foreach (product in Products)
   <tr><td>$product.Name</td>
   #if (product.Stock > 0)
      <td>In stock</td>
   #else
     <td>Backordered</td>
   #end
  </tr>
#end
</table>


The same template in "ProMesh.NET" syntax:

<table>
<!--$[foreach product in Products]-->
   <tr><td>$[product.Name]</td>
   <!--$[if product.Stock > 0]-->
      <td>In stock</td>
   <!--$[else]-->
     <td>Backordered</td>
   <!--$[endif]-->
  </tr>
<!--$[endfor]-->
</table>


The code to parse and render the template:

SharpTemplate template = new SharpTemplate<Velocity>();
 
ParserContext data = new CSharpContext();
 
data["Products"] = GetProducts();

string renderedFile='template.RenderFile("template.htm", data);


Well, you get the idea. If you want to play with it, the code can be downloaded on CodePlex

kick it on DotNetKicks.com
Tuesday, May 20, 2008 10:26:17 PM (W. Europe Daylight Time, UTC+02:00)  #    Comments [4] -

# Tuesday, March 11, 2008

Like many tips & tricks concerning programming languages, what I will present here will be so utterly obvious to some C# developers, but could be an eye-opener to others.

How often do you write something like this?

        if (token == "A")
            tokenNumber = 1;
        else if (token == "B")
            tokenNumber = 4;
        else if (token == "C")
            tokenNumber = 5;
        else if (token == "X")
            tokenNumber = 10;
        else
            tokenNumber = 20;


How about writing it like this?

      tokenNumber = (token == "A") ? 1:
                    (token == "B") ? 4:
                    (token == "C") ? 5:
                    (token == "X") ? 10:
                                     20;


It's the same thing, but it looks cleaner, and the generated IL code is almost the same (it's even a bit shorter).

The reason this works is because the ternary operator (?:) is one of the few right-associative operators in C#. The other 2 are the assignment operator (=) and the lambda operator in C# 3.0 (=>)

kick it on DotNetKicks.com
Tuesday, March 11, 2008 2:33:05 PM (W. Europe Standard Time, UTC+01:00)  #    Comments [24] -

# Friday, March 07, 2008

Every decent programming book tells you to write readable code. Few people do.

Code is read a lot more often than it is being written. Fact.

Programming style is all about personal taste. Agreed.

On the other hand, well... let's just take a very small piece of code from the BCL written by Microsoft:


    Type p = this;
    if (p == c) 
        return false; 
    while (p != null) {
        if (p == c) 
            return true;
        p = p.BaseType;
    }
    return false; 


Without changing any of the code (except spacing and indentation):


    Type p = this;
    
    if (p == c) 
        return false; 
    
    while (p != null) 
    {
        if (p == c) 
            return true;
        
        p = p.BaseType;
    }
    
    return false; 


I know the code is longer, and there are better examples to be found, but what makes code readable is being able to see the structure of the source code. This can be accomplished by using correct spacing, empty lines and correct placement of braces.

I especially want to stress the importance of using empty lines. Their primary purpose is to visually separate blocks of code. Note the example above. The original code has no empty lines whatsoever, which makes code very hard to read (even if you wrote the code yourself).

When should you use empty lines (IMHO) :

  • Before and after every control block ("if", "for", "while")
  • Before and after return statements
  • After blocks of variable declarations
  • After blocks of assignments
  • In between assignments if they "don't belong together"
  • (a curly brace counts as an empty line of course)

This is a small code fragment which illustrates some of these points:


    newSetMethod = typeBuilder.DefineMethod( /*ommitted*/ );

    ILGenerator ilGen = newSetMethod.GetILGenerator();

    ilGen.Emit(OpCodes.Ldarg_0);
    ilGen.Emit(OpCodes.Ldstr , baseProperty.Name);
    ilGen.Emit(OpCodes.Ldarg_1);

    if (baseProperty.PropertyType.IsValueType)
    {
        ilGen.Emit(OpCodes.Box,baseProperty.PropertyType);
    }

    ilGen.EmitCall(OpCodes.Call,setFieldMethod,null);

    ilGen.Emit(OpCodes.Ret);


Of course, there's more to readability than inserting empty lines, but it's a point which is often neglected. The most often heard excuse is that it makes code shorter. (sigh)

I don't want to start a religious war, just trying to express a (strong) opinion.

kick it on DotNetKicks.com
Friday, March 07, 2008 9:04:43 PM (W. Europe Standard Time, UTC+01:00)  #    Comments [16] -
 |  | 

How many times have you written some timing code around a method call? If the answer is never, move on, skip this article...

For timing purposes, I always use a simple "TimeRunner" class that allows you to accurately measure the time it takes to execute a specific method. A kind of "poor man's profiler".

The usage is very simple:

public class Program
{
    public static void MySlowMethd()
    {
        for (int i=0;i<1000000;i++) {}
    }
    
    public static void Main()
    {
        TimeSpan ts = TimeRunner.Run(MySlowMethod); // Run once
        
        Console.WriteLine("Time per invocation: {0}", ts);
        
        ts = TimeRunner.Run(1000, MySlowMethod); // Run 1,000 times
        
        Console.WriteLine("Time per invocation: {0}", ts);
    }
}


That's it.

Here's the class:

public delegate void Action(); // leave this out if you're using C# 3.0

public class TimeRunner
{
    protected TimeRunner() {}

    public static TimeSpan Run(int repeatCount, Action f)
    {
        Stopwatch stopwatch = Stopwatch.StartNew();

        for (; repeatCount > 0; repeatCount--)
            f();

        stopwatch.Stop();

        return new TimeSpan(stopwatch.Elapsed.Ticks);
    }

    public static TimeSpan Run(Action action)
    {
        return Run(1,action);
    }

    public static TimeSpan Run<T>(Action<T> action, T data)
    {
        return Run(1, action, data);
    }

    public static TimeSpan Run<T>(int repeats, Action<T> action, T data)
    {
        return Run(repeats, delegate { action(data); });
    }
}
 
kick it on DotNetKicks.com
Friday, March 07, 2008 12:04:30 PM (W. Europe Standard Time, UTC+01:00)  #    Comments [2] -