Friday 15 June 2007

Open Source is the root of all Evil

We don't use open source.

I hear this all the time and is always accompanied with a look of pride and a slight gloat. I can understand I guess. I was young once but more and more of these people are in there 30's, 40's and 50's. So I turn to them and ask "So ahh Bill, how long you been with X?" 10 years + is always the answer. Ahhh... the institutionalized then.

Enterprise Risk

Enterprise Risk is the number one reason given. Do the people that say this even know what that means? I would have thought it meant assessing risks in a pragmatic way to avoid any loss for your company. OK lets take this scenario. Say I'm doing some financial transactions using .Net on Server 2008 with MSSQL. It goes wrong. O CRAP! Who's fault is it? Who do we blame? Where do we point the finger? Microsoft? Even if there is a hidden bug that has been documented it is still the developers fault. No ifs, ands or buts. The first rule in programming is its always the developers fault.

Open Source is the root of all Evil

This is something that I have heard for years and I firmly blame the Microsoft "Sales Pitch" for this. Microsoft has been implying, if not outright saying, that if you use open source you are more likely to get attacked because it is less secure. It's how they sell their products and services and it filters from the boardrooms to the mangers and so on. The biggest and most common misconception is: "Anyone can change the Source".

No. Not just anyone can change the source on the base version that is released to the publicWhile it is true you can download and change whatever your like, that change will not be reflected in the version that is distributed to the public. It boggles my mind how people cannot see the beauty in that...

Lets roll back a moment to our 'O CRAP' financial transaction. Lets pretend the problem was in a MS library. Lets take a real leap and say that it's open source. So as a developer we need to fix the problem to make it go away. So we pull the source down to make a change and submit it to the project administrators. They say thank you. Check and scrutinize what you have done. Test our change and release it to the public. No more problem for me, or anyone else. It doesn't become a piece of obscure documentation that is in a knowledge base that has well over a million articles.


Open Source Tools Libraries
I really believe that a lot of the open source tools/technologies are much better then those that that you have to pay fo, Sourcesafe being the most obvious example. Subversion and GIT(just to name two) are heads above Sourcesafe. Other tools such as TortoiseSVN intergate into windows to help users that perfer UI to commandline.

Nunit, NHibernate, Rhino Mocks and Structure map, to name a few, are awesome open source liabaries that can only aid a developer. There are hundreds of these tools out there that a lot of developers are not able to use due to out dated company policies. Big compaines like microsoft end support for things all the time


What are these strange things? Where did they come from? How long have then been around? How did I function before these? Microsoft has just released competing version of these products. But really how well is V1 going to be? Nunit is on version 2.5 and Nhibernate on 2. Do you really think Microsoft is going to do a better job than the whole open source community working on these products for years?


No Open Source allowed here
Most of the large companies stand by their "No Open Source Rule" usually citing support and maintenance. But big companies like Microsoft end support for things all the time the only difference is with an open source product you can continue to upgrade and update you are not forced onto an entirely different stack/server/library ect. I have to admit though I used to buy into the whole "open source is insecure mumbo jumbo" and stuck strictly to MS libraries. But hey I also used to masturbate a whole lot more before I could find a woman that would sleep with me.... What's my point? If you're refuse to use open source then you're only screwing yourself.

Understanding IEquatable

Since Generics came along in C# 2.0 we have had a new interface to allow for type safe comparisons. That Interface is IEquatable which is a vast improvemnet over the previous Equals that was hung off object in the BCL.

MSDN Says:

The IEquatable interface is used by generic collection objects such as Dictionary, List, and LinkedList when testing for equality in such methods as Contains, IndexOf, LastIndexOf, and Remove. It should be implemented for any object that might be stored in a generic collection.

If you implement IEquatable, you should also override the base class implementations of Object.Equals(Object) and GetHashCode so that their behavior is consistent with that of the IEquatable.Equals method. If you do override Object.Equals(Object), your overridden implementation is also called in calls to the static Equals(System.Object, System.Object) method on your class. This ensures that all invocations of the Equals method return consistent results.

So how would I use IEquatable? I always liked using code to demonstrate.

public class IEquatableOverride
{
    private static Dictionary<Person,Person> ObjectDic = new Dictionary<Person,Person>();
    private static Dictionary<int, Person> IntDic = new Dictionary<int, Person>();


        public static void  LoadDictionary()
        {
            Person p = new Person();
            p.PersonId = 101;
            p.First = "John";
            p.Last = "Doe";
            p.Age = 30;
            p.Address = "My Address";

            ObjectDic.Add(p, p);
            IntDic.Add(p.PersonId, p);

            Console.WriteLine(" Can Find Object "+ObjectDic.ContainsKey(p));
            Console.WriteLine(" Can Find by Key " + IntDic.ContainsKey(p.PersonId));


            Person p2 = new Person();
            p2.PersonId = 101;
            p2.First = "John";
            p2.Last = "Doe";
            p2.Age = 30;
            p2.Address = "test";

            ///R1
            Console.WriteLine(" Can Find Object " + ObjectDic.ContainsKey(p2));
            Console.WriteLine(" Can Find by Key " + IntDic.ContainsKey(p2.PersonId));

        }
    }

    public class Person : IEquatable<Person>
    {
        public int PersonId { get; set; }
        public string First { get; set; }
        public string Last { get; set; }
        public int Age { get; set; }
        public string Address { get; set; }

        //Commenting this out result in #R1 being false
        public override int GetHashCode()
        {
            ///In propper use this should be immutable
            return First.GetHashCode() ^ Last.GetHashCode() ^ Age.GetHashCode();
        }

        public bool Equals(Person person)
        {
            if(person == this)
                return true;
            if(person.First == this.First && 
               person.Last == this.Last && 
               person.Age == this.Age)
                    return true;

            return false;
        } 
  }


So as you can see above if we don't override GetHash then when compareing values in a Dictionary we would get erroneous results. This seems to work but why would I override GetHash if I wasn't using the object as the key and why do I need to override Object.Equals I thought that was the whole point of having Generics?

Yes that is the point of generics but there was life before generics one example would be an ArrayList. Anybody remember those bad boys? Well if we create a new ArrayList and add p to it and then compare it to p2 like so:
arrayList.Add(p);
Console.WriteLine("Can Find in ArrayList " + arrayList.Contains(p2));

Can anybody guess what the output of the above line will be? Yep you guessed it False. It is still using the old Object.Equals if we add this to our person class everything turns peachy.


public override bool Equals(object obj)
        {
            Person person = obj as Person;
            if (person == this)
                return true;
            if (person == null)
                return false;

            if (person.First == this.First &&
               person.Last == this.Last &&
               person.Age == this.Age)
                return true;

            return false;
        }

OK but what if we just use generic lists that implement IEquatable so I don't to worry about Object.Equals and I am using a primary key for my Dictionary key. So I am not going to bother with Overridding GetHashCode either.

Nope wrong again, LinQ is also a friend to GetHashCode so even though I am currently not using LinQ functionality in my results if I ever chose to do so and used a method like Distinct or Group By I would not get the results I would expect. So in short even though we have our brand new fancy interface we need to be careful how we use it.