January 2008 - Posts

In a previous post, I talked about various attempts at frameworks that are trying to do Design By Contract (DBC) in .NET.  Many simply just come across as simple validation frameworks without the heart of DBC which is the contract.  Where is the contract?  Well, dig through my code and find out!  It's something that I've been harping on time and time again is that without transparency to the caller and callee of the contract, it's just another validation framework.  You'll hear either myself or Greg Young on that, I'm sure.

The only approach that has worked so far has been Spec# in the .NET world.  Unfortunately, still a research product, isn't available for us to use commercially.  So, until that time in which it is thoroughly weaved into the .NET framework as a whole outside of the internals of System.Core.dll, we have to look at alternatives.

Eiffel

Before we look at another framework, I just want to walk you through a sample of the original DBC language, Eiffel.  This gives you an idea what I'm talking about.

my_list.do_all (agent (s: STRING)
     require
         not_void: s /= Void
     do
         s.append_character (',')
     ensure
         appended: s.count = old s.count + 1
     end)

As you can see, it's clearly stated as part of the language what the requirements are, and what its return will be.  That's the really slick part about it that we don't have unless we go to Eiffel.NET, but it's not using the latest and greatest .NET APIs, so it's pretty much a nonstarter.  So, let's look at yet another framework.

LinFu DesignByContract

Philip Laureano, who has done a lot with LinFu including Aspect Oriented Programming (AOP) among other things has taken a stab at DBC for all .NET languages and not just C# which Spec# has done.  He posted the LinFu.DesignByContract2 article on CodeProject that is well worth checking out. 

Earlier, he posted one about the LinFu framework on CodeProject as well which includes the following:
  • A simple IoC container
  • Design by Contract framework
  • Dynamic Proxy
  • Reflection (Mixins, Duck Typing and Multiple Dispatch)
  • Delegates with lambdas for universal event handling
But, let's get back to the DBC part, you'll remember the basic tenets of the philosophy:
  • What do we expect?
  • What do we return?
  • What do we maintain?
We also throw in a few Eiffel constructs in terms of inheritance which are also quite important.  And they are:
  • The precondition may only be weakened by inheritance
  • The postcondition can only be strengthened by inheritance
Well, enough about the basics, let's look at the framework itself.  Let's first look at a simple sample I whipped up:

public class StringCollection
{
   private int count;
   string[] strings = new string[5];

   [EnsureCountIncremented]
   public virtual void Add([NotNull] string value)
   {
        // NotNull doing
        // Debug.Assert(!string.IsNullOrEmpty(value));

        // Add value and redefine   

        // EnsureCountIncremened 
        // Debug.Assert(count == count + 1);       
   }
}

So, what you see here is that I put a postcondition making sure that the count is incremented by one as I'm adding a string to my array.  Also, I have the precondition that my string value is not null.  But, how does that work?

In order to do that we need to implement a few interfaces.  Let's look at the one for preconditions.

public interface IMethodContract
{
    IList<IPrecondition> Preconditions { get; }
    IList<IPostcondition> Postconditions { get; }
}

public interface IPrecondition : IMethodContractCheck
{
    bool Check(object target, InvocationInfo info);
    void ShowError(TextWriter output, object target, InvocationInfo info);
}

Ok, so now, I need to implement that somehow for my custom attribute.  That would look similar to this:

public sealed class NotNullAttribute : Attribute, IPrecondition
{
     public bool Check(object target, InvocationInfo info)
     {
          string value = target as string;
          if(!string.IsNullOrEmpty(value))
               return true;
     }

     public void ShowError(TextWriter output, object target, InvocationInfo info)
     {
          output.WriteLine("string value cannot be null or blank");
     }

     public bool AppliesTo(object target, InvocationInfo info)
     {
          return target is string;
     }

     public void Catch(Exception ex)
     {

     }
}

So, it's pretty interesting and clever how this is being done.  But still without the static checking of Boogie and so on, it still falls well short of real DBC. 

For those interested in postconditions and invariants, those are also supported through the interfaces as shown below:

public interface IPostcondition : IMethodContractCheck
{
    void BeforeMethodCall(object target, InvocationInfo info);
    bool Check(object target, InvocationInfo info, object returnValue);
    void ShowError(TextWriter output, object target, InvocationInfo info,
        object returnValue);
}

public interface IInvariant : IContractCheck
{
    bool Check(object target, InvocationInfo info, InvariantState callState);
    void ShowError(TextWriter output, object target, InvocationInfo info,
        InvariantState callState);
}

So, as I said, quite interesting and clever the way of handling the DBC concepts through interfaces.  The libraries were also written to support the Eiffel constructs of inheritance.  So, very well written I must admit.

Conclusion

I just began to scratch the surface with what you can do with this framework and it's very well written.  But, still at the end of the day, it's not statically checked through a Boogie and a theorem prover to ensure the contracts hold.  That's the crucial part missing in this equation.  I'm sure more will come to mind on the subject.  Until next time...

kick it on DotNetKicks.com
I often rethink or have additions to my posts.  This topic of what's coming in C# vNext is definitely one of them.  I'm always looking for ways to push the envelope to see what I can get from the language.  I have to credit such people as Scott Bellware and others to look more outside of the C# bounds and look to such things as Ruby and even F# to really open my eyes to the possibilities of this language.  I'd love to see more F# features in the language as they have slowly seeped in and now pretty ubiquitous.

What I like

I enjoyed Jeremy Miller's post about what he likes about C# 3.0.  I'd definitely have to concur with a lot of those things such as his and some of my own:
  • Object Initializers
  • Lambda Expressions
  • Extension Methods
  • Anonymous types
What I'm Undecided About

So, what am I eh about?  Well, automatic properties is one of them.  Not doing much for me just yet, especially for my domain models.  Also partial methods have a particular use, but once again, it looks like a large potential for abuse.  Bart De Smet has a pretty good writeup on them though worth checking out here.  If you check out Wes Dyer's blog, he also has a good example and why you would use them.  To me, it looks like Aspect Oriented Programming in a way, but I much prefer using Windsor interception, and hopefully soon the StructureMap interception.

Do We Know Any More?

Charlie Calvert, C# Community Liason, and Mads Torgersen, C# Program Manager, wrote a post recently about the future focus of C#.  The first topic in this series is about dynamic lookup.  What dynamic lookup is, is the ability to distinguish a type at runtime instead of static compile time. 

But why is this useful?  Well, in order to interact with dynamic runtimes, this is the best way to do it.  The idea of using this for COM interop is also pretty interesting.  I've done more than my fair share in this lifetime.  Many of the things you see on PInovke.NET are ones that I either put up there or refined quite a bit, especially while using unsafe C#.

With this upcoming, could Duck Typing be far behind?  After all, it has been proposed at least for VB9, although dropped.  Of course in .NET, it's already supported on the foreach keyword as noted by Krzysztof Cwalina due to the fact you don't need to implement IEnumerable, and only need GetEnumerator().  But, will it go any further than that like Ruby?

kick it on DotNetKicks.com
Sorry I never posted a welcome notice to this blog.  Anyhow, welcome to my new home on the net.  If you don't know me already, my former blog is at geekswithblogs.net.  If you're here for the first time, great! 

So, who am I?  Well, I live and work in the Washington, DC area and actively involved in the .NET and Agile side of things in the area.  I run DC ALT.NET with the help of Phil McMillan, who ought to blog more BTW..  I'm also active in planning the ALT.NET Open Spaces, Seattle event coming in April.  We're going to have a great crowd there, but space is limited.

Anyhow, my interests are many including:
  • Agile/XP
  • Functional Programming (FP)/Object Oriented Programming
  • Compilers
  • ASP.NET
  • BizTalk (pays the bills)
  • SharePoint
So, hope you stay tuned!
Central Maryland Area .NET Professionals (CMAP) is holding their next meeting on Tuesday, February 5th, 2008.  This month looks to be a great one by Michael Wolf on the User Experience (UX).  In the agile methodology, it is quite an essential piece that often gets overlooked.  It's obvious too many times after looking at The Daily WTF to see plenty of evidence of that.

Alon Salant posted a good set of Agile User Experience Resources that's pretty extensive on how UX fits into the Agile/XP practice.  This includes the Yahoo UX group, articles, practices and so on.  I've used this as a resource a couple of times when thinking about UX.

Anyhow, here are the details:

UX is the new UI: It's not the framework, it's how you use it.
By: Michael Wolf

As developers we love our frameworks and the elegance of code. Yet we often spend so much time staring at code we forget the essential user centered problem were trying to solve. In addition, due to the amazing and ever evolving frameworks/ tools / controls we have at our disposal, we often forgo what's most usable for what's most available. In this talk we will delve into these problems using examples written in Microsoft Silverlight, including the winning solution from the Microsoft Phizzpop Design Challenge LA.

About Michael Wolf - Michael Wolf has spent over a dozen years building web applications and is currently a Senior Rich Internet Application Developer for global leader Cynergy (www.cynergysystems.com). He recently was a member of the winning team for Microsoft's PhizzPop Development Challenge in LA, successfully developing an interactive media platform for a mock media conglomerate in 3 days using Microsoft Silverlight technology. Starting with his first web application in vi using Mosaic in 1994, Michael has been heavily involved in application development holding numerous senior development positions at Washington, DC and Baltimore, Md base companies including Real Magnet, Blue Atlas Interactive and Bally's Total Fitness.

Date/Time:
2/5/2008 - 6:30PM

Location:
HCC Business Training Center
6751 Columbia Gateway Drive
Columbia, MD 21046

In addition to all of that, Mike is part of DC ALT.NET, and it's great to have him as part of our group.  Hope to see a great turnout!

kick it on DotNetKicks.com
Update: Added more F# samples and the foundations of functional programming

In a previous post, I've begun a pretty fun adventure into F#.  I'm still working on quite a few samples to post here shortly, but in the mean time, I've collected a bunch of samples that I think are pretty cool and well worth a look.  I'm currently digging through Robert Pickering's book "Foundations of F#".  I'm especially interested in his DSLs which I hope to cover here shortly.

For those interested in the foundations of functional programming, check out Bart De Smet's blog for his series on the subject:
This is an ongoing adventure into F# as I think it has quite a future as a first-class citizen in the .NET space.  Now, if we can say the same for Spec#.  I don't mean to be a link blog by any means, but it helps me to gather these things up as I go along:

Wrapup

kick it on DotNetKicks.com
More Posts