Found the missing Linq operators: MoreLinq

I complained before that there are operators like the IEnumerable<T>.Each() are somewhat missing from the default implementation of Linq, but I never really gave it too much thought since then.

Until today I read a blog post from Jon Skeet (Mr. Stackoverflow himself) about how hard it is to name methods in his MoreLinq effort.

So, curious me I immediately peeked at the source and found some really cool stuff in there that I may very well use in the future.
There aren’t only missing things in there like list concatenation, also generator methods for sequences (great when doing mock expectations) but also cool things I never thought of before but that might come in handy like a consume method that triggers Linq execution immediately without consuming memory (You could do that with .ToList() but that would allocate a IList<T> in memory).

So, don’t miss out on the fun in MoreLinq. I’m sure there is something cool in there for everyone.
And also, don’t forget to suggest to Jon Skeet good method names if you come up with one :

Read more →

Rant: BIOS update procedure fail

Upon helping a friend of mine assembling a new kick-ass gaming PC. Once the whole system was put together he called me the next day stating that the system feels incredibly slow and crashes whenever he starts up a 3d game.

I rushed to help and after some tinkering we found out that the CPU (a Core 2 E8500 2x3.16 Ghz) was operating at almost 80°C. By contrast, my Core2 E6600 (2x2,4) reaches 36°C under full load.
So, since the CPU cooler was spinning, I am quite convinced that it has something to do with a faulty mainboard or (rather unlikely) a faulty CPU.
My initial thought was to try updating the BIOS in case the problem may go away.

So I went to the ASUS download site, selected the appropriate mainboard model (P5Q) and downloaded a zip file containing the newest BIOS. Now imagine my excitement when I was presented with a .ROM file inside that zip. Pretty cool huh?
So I went on to download the Afudos BIOS update tool V2.36 that should install the .ROM bios. 
Started it: sorry doesn’t work on Windows. (WTF?)

So, following the instructions provided by ASUS I’m supposed to:

Please insert a clean, unformatted disk into A:\ drive and boot the system into DOS mode. In DOS mode, please type in C:\> FORMAT A: /S or click on“My Computer”icon under Windows O/S, right click on drive A:\ and choose“Format”. By using the procedure above, you can create a boot disk without AUTOEXEC.BAT and CONFIG.SYS files.

Drive A:\ ? DOS? Autoexec.bat? Config.sys?

ASUS: Are you out of your mind?

When I needed to flash my Dell XPS M1330 Bios with a new version, I didn’t even have to leave my browser to do so. Some weird ActiveX thingy just started and updated my BIOS revision while I was casually checking my email. And ASUS is really telling me this is the way to go if I want to update my brand new socket 775 P45?
C’mon, that’s so 1994 – not even funny any more.

Read more →

Do you really know what LinQ does?

LinQ is by far the most empowering language technology I’ve seen in years, and it has really helped me in many cases get to a more functional style of programming, enabling clearer syntax and better overall code.

But, it also has it’s pitfalls.
Since LinQ attaches a .Count method to any enumerable, why would you still use IList for read-only collections? It’s so damn easy to simply write code like this:

IEnumerable<string> strings = new[] {"hello", "world"};
Console.WriteLine("Number of Strings: {0}", strings.Count());


What would be the benefit when using a IList<string> or ICollection<string> like this?

IList<string> strings = new[] {"hello", "world"};
Console.WriteLine("Number of Strings: {0}", strings.Count());

IEnumerable<T>.Count() would be a O(n) operation if it would follow the IEnumerable semantics through enumerating through all items. Since the definition of a Enumerable is that you need to iterate over every item in a linked list to determine it’s length.
Actually, the implementation (I looked with Reflector into the Count() method) does exactly that:

int num = 0;
using (IEnumerator<TSource> enumerator = source.GetEnumerator())
{
    while (enumerator.MoveNext())
    {
        num++;
    }
}
return num;

But, that would always guarantee a O(n) execution time and would slow most applications to a crawl (since it’s so easy to use .Count() everywhere) Microsoft implemented a little shortcut right before the above code:

ICollection<TSource> is2 = source as ICollection<TSource>;
if (is2 != null)
{
    return is2.Count;
}

So, they are breaking the Liskov Substitution Principle on purpose to speed up the execution time of .Count().


That’s why calling .Count() doesn’t hurt so much as long as you are calling it on a IEnumerable that’s also an ICollection, all you’re doing is a cast and a field read.

That’s also why in my testing IEnumerable.Count() wasn’t soo much slower than IList.Count since the only difference that slowed IEnumerable was the typecast (I’m too lazy to generate some data on a non ICollection IEnumerable with a real O(n) execution time).

 image

Just keep in mind that once you are iterating over a “real” IEnumerable that has no collection underneath, you should try to avoid calling .Count() too often since it’s not only a cast/read but a iteration over all elements of a list.
Also keep in mind that usually when working with the extension methods on IEnumerable you risk to perform a O(n) operation, so use it wisely (especially when you don’t control the source of your IEnumerable<T>, you could get passed anything).

Read more →

How to loose any credibility

I just listened to Stackoverflow.com Podcast #39 and am completely shocked.

I really like the podcast, I really like Jeff and Joel talk casually about stuff, but when Joel today talked about the SOLID principles (again), I almost screamed.

There were discussions throughout the community when Jeff and Joel said before on podcast #38:

Last week I was listening to a podcast on Hanselminutes, with Robert Martin talking about the SOLID principles. (That's a real easy-to-Google term!) It's object-oriented design, and they're calling it agile design, which it really, really isn't. It's principles for how to design your classes, and how they should work. And, when I was listening to them, they all sounded to me like extremely bureaucratic programming that came from the mind of somebody that has not written a lot of code, frankly.

(quoted from the transcript wiki)

Especially the last line coming from a business guy like Joel to a programmer like Robert C. Martin (the topmost book in my drawer here at work bears his name!) just seemed incredibly silly and plain ignorant.
But, I didn’t blog about it since everyone else did (even Uncle Bob)

I also thought that Rob Conery had a very good post (although too amicable in tone towards Jeff&Joel) about the whole thing and I really couldn’t add anything to it besides my personal feeling that Joel & Jeff were way off track and judged about stuff they don’t have any authority to judge over. Not knowing stuff doesn’t enable you to criticize it.

So, now this time Joel Spolsky ruined any credibility he ever had in the field of writing software when he came up with an example of a method that should not be tested because it’s too hard to write a test for it.

The whole is an example from Copilot, a VNC thingy FogCreek is selling:

I need to write a function that's basically a little button on a toolbar that is going to reduce the JPEG compression.
[snip] So imagine how much code you'll need to write to change a parameter on your JPEG compression library from a 37 to a 10 lets say. Right? And that's all it's gonna do, so this is 5, 10, 20 lines of code to implement this feature, let's say.
But to implement the test, once you have to somehow create a JPEG that is the same as this other JPEG that you have, but compressed at a different level.
And there is no way to actually construct this test in advance, other than actually running it, or if you did, it would be extremely hard. It would take lot of work to write a test that connects to another machine...

He then goes off to talk about having the test run over two different machines (one probably emulated for the test), talking about different versions of the JPEG compression library etc etc etc.

I couldn’t think of a more stupid thing to do. Nothing, never, ever.
Here is the whole test it would need to test the feature (the button that changes the compression rate)

[Fact]
public void Test()
{
    SetCompressionRate(37);
    Assert.Equal(37, jpgCompressionLib.CompressionRate);
}

Anything more than that, is an integration test and I’m no longer testing the feature, but the JPG Compression library and it’s ability to adapt to the new compression rate.
Anything besides verifying that the compression rate gets successfully set on the library runs out of scope of a unit test, and is no unit test.

And that’s basically it. This one innocent example meant to prove his point just dug the whole deeper. So far as for me it has become clear that Joel Spolsky has no credibility whatsoever left over in the field of software development.

Oh, and by the way. You really should listen to that podcast, it gets even better when Joel continues to make an ass of himself when he starts to bash on PHP as being a poorly designed ripoff of VBscript, that language features in .NET 3.5 have advanced programming far more than these object oriented design principles, etc.

Oh and by the way: I’m always open to discussion so feel free to leave a comment if you think I am wrong here.

Read more →

Simplest thing to do

Today a friend woke me up by phone wanting to ask me how to best do internationalization (i18n) in .NET. He already had read some guides that involved compiling satellite assemblies with global resource files etc, and that didn’t work all that well.
My first thought went to that article I had read half a year before when I was tasked with i18n, and I tried to remember how I did it back then.

I remembered: I didn’t.

At no point in time was the scope of the application so big that it had warranted to jump through the hoops Microsoft hat set up to get to real internationalization.
If you Google for the thing or ask on StackOverflow, you realize rather fast that you don’t want to have any of this i18n “goo” in your application logic, so the smartest thing is to simply abstract everything away into a separate class that handles all those things.

And once you have it abstracted from your application, nobody forces you any more to use the .NET i18n stuff any more.
Simple switch statements will do if you only need 2 languages for a couple of strings, even reading something on the Globalization stuff from .NET takes longer than simply writing something similar to this:

public class LocalizationManager : ILocalizationManager
{
    private IDictionary<CultureInfo, IDictionary<string, string>> dictionary;

    public LocalizationManager(IDictionary<CultureInfo, IDictionary<string, string>> dictionary)     {         this.dictionary = dictionary;     }

    public string Translate(CultureInfo culture, string name)     {         var translationDictionary = dictionary[culture];         if (!translationDictionary.ContainsKey(name))             throw new Exception("No translation found");         return translationDictionary[name];     } }

Now, the only trouble here is to fill that dictionary with values, and that can even be simply hardcoded into the application.
To further facilitate the use of this you can now write Facades for your languages like this:

public class Translation : ITranslation
{
    private readonly ILocalizationManager localizationManager;
    private readonly CultureInfo culture;

    public Translation(ILocalizationManager localizationManager, CultureInfo culture)     {         this.localizationManager = localizationManager;         this.culture = culture;     }

    public string Translate(string name)     {         return localizationManager.Translate(culture, name);     } }

And you can go even further (if you like hardcoding as much as I do) and write another facade like this to have intellisense on your translations:

public class SpecializedTranslation
{
    private readonly ITranslation translation;
    public SpecializedTranslation(ITranslation translation)
    {
        this.translation = translation;
    }

    public string GetLoginMessage()     {         return translation.Translate("LoginMessage");     } }

Also mind that since everything follows a rather simple Interface I can easily change these implementations later if I need to. But for something simple, this will do just fine.
Also I advise to use this through an IoC container like Windsor so you could even go as far as to swap hardcoded translations through the configuration.

Read more →

Linq provider differences do affect implementation

After having done some Linq to objects when working with NHibernate I discovered a major flaw in the whole abstraction when using Linq to Sql today.

Imagine the following code:

IQueryable<string> strings = new List<string> {"test", "Test2"}.AsQueryable();
var result = strings.Where(p => p.Contains("test"));

It works perfectly in a Linq to Sql setting since Sqlmetal will translate the contains call to a LIKE operation, and the query will return two results (since LIKE has no case sensitivity).

It simply won’t if you query objects in memory since the standard Contains operation is case sensitive and you will only get one result.

The only way I found to query case insensitive is with IndexOf() like this:

IQueryable<string> strings = new List<string> {"test", "Test2"}.AsQueryable();
var result = strings.Where(p => p.IndexOf("test", StringComparison.OrdinalIgnoreCase) > -1);

Note the StringComparison.OrdinalIgnoreCase that makes this ignore casing.
Now, the catch here is that Sqlmetal (the thing that magically generates Sql from the expression tree), can’t translate IndexOf into Sql and you’ll get an exception.

So, we’re screwed right? :)
Yes! We are, since Contains is not marked as virtual we can’t just subclass List<string> and override the behavior of Contains (assuming we could interfere with the objects creation through some factory). We are bound to write different code for querying Linq2Sql and Linq2Objects. That’s what I call a rather leaky abstraction.

Read more →

override vs new in C#

The class diagram is rather simple:

image

Yes, it’s that simple. I have an Image and I want to modify the behavior of the Url getter method for it (I can’t compose the URL string inside a Linq to Sql expression since I’d have to call a resolveFileType method that can’t get translated into SQL). So, I figured the best way to solve that problem is to have a subclass with a custom implementation of Url to handle that. I supply the required information through the ctor and when called the Url gets composed. The general idea is to assign an BoxImage to an Image field, hiding the custom implementation from the rest of the code.

Since I had not marked my fields as virtual I could not override them, I thought just writing new would do the job.
That failed miserably.

Actually, the new keyword does not hide the base classes method. Instead, a new method gets created that lives only on the derived class and that won’t get called if you are calling the base type.

public class Image
{
    public bool Url()
    {
        return true;
    }
}

public class BoxImage : Image {     public new bool Url()     {         return false;     } }

public class Image_Fixture {     [Fact]     public void Should_Return_False()     {         Image img = new BoxImage();         Assert.True(img.Url());         Assert.False(((BoxImage)img).Url());     } }

The unit test should be self explanatory, only if the type that gets the call is actually a BoxImage the new method will get called, if not, the base implementation will get used and your polymorphism goes out of the window :).

Doing this right would require the Url method to be virtual so you can properly override it:

public class Image
{
    public virtual bool Url()
    {
        return true;
    }
}

public class BoxImage : Image {     public override bool Url()     {         return false;     } }

public class Image_Fixture {     [Fact]     public void Should_Return_False()     {         Image img = new BoxImage();         Assert.False(img.Url());         Assert.False(((BoxImage)img).Url());     } }

Oh, and by the way. If you got the impression I don’t know about the fundamentals of the language I’m using, you are right. I try to learn as I encounter the problems, one feature at a time, one minor annoyance at a time :).

Read more →

Linq to Sql Association weirdness

When your dbml designer shows an association, you’d expect to be able to traverse it in code don’t you?

image

So, funny thing. The Post entity lacks a “PostTags” Field that should be there according to the designer.

After a few minutes of tinkering, I realized that PostTag has no primary key of it’s own. Since in my real application it’s a m:n association table and no entity on it’s own I didn’t use a primary key. But apparently Linq to Sql needs a primary key to work with, so once I changed the table to have a PK magically a PostTags field appeared:

image

Once again I get how “version 1” Linq to Sql really is, but sadly there won’t be a version 2 since Microsoft is now pushing all resources towards the Entity Framework.

Why not EF? Too complex, once I reach a level of complexity that would justify the use of EF, I use NHibernate.
What I need out of Microsoft right now, is some Linq enabled ORM that’s dead simple with a very DB-centric view that works. Something like Linq to Sql v2.

Read more →

Google Reader Feature Request: Don&rsquo;t mark feed as new

I’ve been using Google reader for a very very long time now, and it’s besides Gmail one of the most important tools out there for me to stay informed. I don’t read newspapers, I don’t read blogs directly, I only stop once in Gmail and once in Reader.

The only thing I really consider “missing” from Reader is some way to declare a rule that Reader should never mark a feed as “unread”.

Why? Simple: Many online newspapers and sites give out full-text feeds and I subscribe to them to occasionally read some articles.
But, in case of sites like Engadget or Gizmondo, there is no way you can keep up with 30+ posts per day and still do work in between reading. So, my “News” folder in Reader grows pretty fast to 1000+ unread items and I have to go in and mark them all as read every now an then.

I consider those feeds as a constant stream of information, so I don’t want Reader to track their read/unread status. If I’ve got free time I go in there to see what’s going on right now, while most of the time I simply ignore those feeds.

What do you think is missing from Google Reader?

Read more →

IEnumerable&lt;T&gt;.Each as C# Extension Method

A very long time ago I went through the Ruby in 20 minutes tutorial when I saw this:

@names.each do |name|
  puts "Hallo, #{name}!"
end

When C# came out later I always wondered why there is no functional equivalent on the IEnumerable<T> interface, since it would be a perfect place for quick inline method calls without having to write a full foreach statement.

At that time my knowledge of extension methods and delegates was too limited to do this myself, but that doesn’t mean it has to stay that way.
I finally remembered that I never got to it last time and implemented it today.

Oh and it’s so damn easy too:

public static class EachExtension
{
    public static void Each<T>(this IEnumerable<T> enumberable, Action<T> action)
    {
        foreach (var item in enumberable)
        {
            action(item);
        }
    }
}

To use this .Each method now you simply need to be using the Namespace where the EachExtension is in and you can write code like this:

IEnumerable<string> x = new[] {"hello", "world", "how", "are", "you"};
x.Each(Console.WriteLine);

Or with multiple parameters:

IEnumerable<string> x = new[] {"hello", "world", "how", "are", "you"};
x.Each(p => Console.WriteLine("{0}", p));

Or, even whole inline method bodies:

IEnumerable<string> x = new[] {"hello", "world", "how", "are", "you"};
x.Each(p =>
           {
               Console.Write("Word:");
               Console.WriteLine(p);
           });

So, Lambdas are fun after all :)

Read more →