Monthly Archives: September 2009

Improve readability with LINQ

At first i was not very fond of LINQ but it seems that i am finally convinced that LINQ may improve readability:

bool IsCompleted()
{
 var boxesNotOnGoal = from cell in Cells
                      where cell.HoldsBox() && !cell.IsGoal()
                      select cell.Piece;

 return boxesNotOnGoal.Any();
}

Brad Abrams and extension methods to the rescue..

Currently i am implementing Sokoban and i was pondering which methods i should add to my Cell class:

  • bool HoldsWall
  • bool HoldsBox
  • bool HoldsPlayer

Or

  • bool HoldsPieceOfType(Type type)

Which option should i choose? With the aid of extension methods (in the same namespace) i can have them both:

namespace Sokoban.Domain
{
 public static class ExtensionMethods
 {
  public static bool HoldsBox(this Cell cell)
  {
   return cell.HoldsPieceOfType(typeof(Box));
  }

  public static bool HoldsPlayer(this Cell cell)
  {
   return cell.HoldsPieceOfType(typeof(Player));
  }

  public static bool HoldsWall(this Cell cell)
  {
   return cell.HoldsPieceOfType(typeof(Wall));
  }
 }
}

In case you don’t like this solution, blame Brad Abrams who inspired me to implement it this way with his session at Visug yesterday.

Remark: Because the possible pieces in Sokoban are very well known (the game/requirements are not going to change) thus one should choose the first option (No extension methods required.)

Tim.TransitionTo("MCPD: Enterprise Application Developer 3.5")

I figured that it might help convince people that i stay up to date with current technology so i decided to take (and pass) the following exams: 70-568 and 70-569. This means that i have now earned the following credentials:

  • MCPD: Enterprise Application Development 3.5
  • MCTS: .NET Framework 3.5, Windows Forms Applications
  • MCTS: .NET Framework 3.5, ASP.NET Applications
  • MCTS: .NET Framework 3.5, Windows Communication Foundation Applications
  • MCTS: .NET Framework 3.5, ADO.NET Applications

About under construction pages

Why do people visit a website? To find information. Thus, when you publish a page to announce that the information is not there (yet) you are wasting your visitors time. Instead of creating the actual content you wasted time while your were creating an under construction page. A loss-loss situation instead of a win-win one.

Admitted, in some cases it can be helpful to give your visitors an idea of what they can expect (eg: links to articles that are not written yet appear in red in the Presentation Patterns wiki), but in general: Under construction pages stink. And here is someone who shares that feeling: This icon says more about me than it does about my web page.

Instruct T4 to use C# v3.5

Consider this simple T4 template:

<# for (var i = 0; i < 10; ++i) { WriteLine("hello"); } #>

Trying to build the project results in a compilation error because ‘var’ is an unknown type. A bit of research learned me that i should instruct the processor to use a specific c# version like this:

<#@ template language="C#v3.5" inherits="Microsoft.VisualStudio.TextTemplating.VSHost.ModelingTextTransformation"  #>
<# for (var i = 0; i < 10; ++i) { WriteLine("hello"); } #>

Problem solved :)