A few words about Extension Methods and Regex builder library

LINQ is great. It’s what everyone has been talking about for some time now, and it’s the biggest thing that comes with .NET 3.5. But I guess that those saying that with time, people will appreciate other new things in C# 3.0/VB9.0 were right. I’m not even sure that LINQ will be THE feature I’m gonna use the most.

For some time now, in my spare time I’ve been working on a project that I’m going to publish up on codeplex. I know that I’ve chosen the wrong order here (first, created site for project, then started talking about it, and in the end, (hopefully) publish it), but what the heck… I already had to change plans about it, since right after I started working on it in spring I had to stop and move to other things, and it wasn’t until recently when I finally got some time for it.

In a few words, project is called Regular Expressions Builder Library and its name says it all. The idea is, to use skills you already have (some .NET language) to create regular expressions without having to learn all its rules, like what characters to escape, what is the name of that certain Unicode group and so on. Build regular expressions with strongly typed easy to comprehend manner with help of compile time checking and intellisence.

Funny thing is, that postulate of easiness seems to be the hardest to implement. The idea is to make the API as simple for someone with little idea about regular expressions as possible, so I decided to utilize Fluent Interfaces and name classes, properties and methods in such a manner, that it will be easy to read.

I started in .NET 2.0 and tried a few approaches, but the best I could come up with looked like this:

Pattern pattern = Pattern.Define().As(

    new Chars("Kot").

    Count(Times.AtLeast(2))).

    FollowedBy(Any.Except('a','b','c')).

    Start(

        At.BeginingOfStringOrLine);

It’s not bad I guess, you can easily see what I want to accomplish with this regex pattern, but I was not satisfied with this new Chars(“Kot”). First of all, It’s a lot of typing to say “I want here those characters in this order”, second is that I really don’t want a new operator here. It has to do with simplicity and readability. Every pattern consists of at least one Group, every group consists of given sequences of characters (class Chars), any of (or except) characters from given group (class Any) and subGroups. To make it easier to read I wanted to save new operator for subgroups, and Chars and Anys start with some static method.

The unfunny thing is I couldn’t come up with anything concise and smarter than Chars.Sequence().

So then I decided to try another approach and move to .NET 3.5. It’s about time to start exploring its depths. I just started working with it so I don’t have much yet, but at least now, thanks to this little class:

public static class StringExtensions

{

    public static Chars Count(this string text, Times times)

    {

        return new Chars(text).Count(times);

    }

}

I’m able to rewrite first piece of code to look like this:

Pattern pattern = Pattern.Define().As(

    "Kot".Count(Times.AtLeast(2))).

    FollowedBy(

    Any.Except('a','b','c')).

    Start(

        At.BeginingOfStringOrLine);

Looks better, doesn’t it?

As almost everything however, Extensions Methods seems to be a double edged sword. You can pin methods to classes, but you can easily overdose, and have too many extension methods. Maybe it’s because I just started, but I think that, especially with such commonly used type as string, when you (as basically I’m going to) make string mimic other type, you can have it mimic yet more types, and instead if 4 types with 10 public members each, you end up with string having 40 options for you in intellisence. Good luck with that. Sad thing is, that since string is sealed, there’s not really much you can do about it.

Ok, this post is already too long so, I’ll post more about my adventures with C# 3.0 and Regex builder library next time. In the meanwhile? How do you like the API? How do you like the idea of such a library? If you have any comments please post them below or by mail, via form on this blog.

No spellchecker in Windows Live Writer beta 2

While publishing previous post (first one using beta 2 of WLW) I noticed one feature that was there in beta 1 and now apparently has been removed: spellchecker! I have no idea why they would remove such usefull feature, especially, that it worked very well in beta 1. I guess I’ll have to do copy/paste to and from OpenOffice Writer to check my spelling, since I’m neither native english speaker, nor good speller Winking.

[UPDATE]

While reading David Hayden’s post on WLW I noticed, that one of the features in beta 2 is

Inline spell checking

What the…? Why then I don’t get any red underlining when I make a typo? Why then there is NO SIGN of spellchecking in options? Now it’s even more weird Confused.

Partial Methods – the most useless c# 3.0 feature?

I feel puzzled. On the one hand, I KNOW that Anders, Wes, and other people working on c# 3.0 language/compiler, are very smart people, yet I fail to see solid arguments why extend c# 3.0 with partial methods. Wes tried twice explain this feature, and I have read both posts twice, just to make sure I didn’t lose the gist of it.

To quote him:

Partial methods are methods that enable lightweight event handling.  Here is an example declaration:

partial class C

{

   static partial void M(int i);

}

The idea is that you have partial method declaration (notice the ‘;’ instead of {/*body*/}) and (optionally) implementation. If you don’t implement the partial method, all code that calls it will be removed during compilation. Since partial method MUST return void it seems pretty harmless. You don’t loose functionality and save several processor cycles.

All seems to be great, but let’s think of what you loose instead.

First and the most important: you loose code clarity.

Let’s say you wrote some code like this:

public partial class Class1

{

   public Class1()

   {

      partial void CurrentYear(ref int number);

      void DoSometiong()

      {

         int num = 0;

         CurrentYear(num);

         Console.WriteLine(num.ToString());

      }

   }

}

Big question: What will show up in the console? ‘0’? You are unable to tell! You’d have to find out wheatear the method was actually implemented or not. Since it would be probably in different file (what is reasonable – we’re dealing with partial classes here), and maintained by some other developer. If he/she forgot to implement this method you would not receive compilation time error (you probably would see warning but, it’s so easy not to notice them). If you don’t have tests to test this method you could ship broken app! This vagueness of code and potential source of errors is IMHO too high price for having several processor cycles saved. I would rather see it as a refactoring tool feature (removing private ‘OnEvents’ methods and events that never get called), rather than language element.

I guess that it will be one of those features that are there so that you could use them when you really need to but in 99 cases out of 100 you will not.

Technorati tags: , ,