Microsoft Love/Hate or Love to Hate

For a long time I have seen Microsoft as being evil in tactics but good in what they provide for developers. I use Microsoft’s tools to do my job and find that the majority of my experience doing so is positive. This is why I consider that I have a love/hate relationship with them.

The other day however, I was working and got notified that I needed to install an update from Microsoft, so I went ahead and had it do the install. Keep in mind that I was in the middle of doing something when I was notified. When the install had finished it asked me if I wanted to restart now or restart later. I clicked “Restart Later” and believed that would be the end of it. Within five or ten minutes I was getting notified again with the exact same question. “Restart Now” or “Restart Later”. This was getting very irritating.

Now, if you find yourself asking, “why didn’t you just restart?”, well then you’ve never been in my situation before. I was running and debugging code and trying to fix a problem with my work. When my mind gets into it, it is counter productive to pull away and have to wait. My mind gets on other things and I don’t tend to return to my work with the same vigor as I had before

After several hours of being notified every couple minutes, I realized I needed to download a large amount of data, so I started that process. I had to leave for a while and figured when I came back the download would be finished.

Bwah haa haa haaa haaaa!!! (I can hear Steve Ballmer laughing out loud).

There’s going to be none of that!! You think you have control over your own computer? Well, you don’t. And apparently Microsoft thinks you shouldn’t. When I returned to my computer later, it had restarted without getting the ok from me!!!! What the *&%#$!!!

I suppose part of the problem is that Microsoft doesn’t like getting blamed for security problems and so they enforce that you restart and accept the update, but this is just ridiculous. At least give me a third option on the reminder dialog that says “I will restart my computer as soon as I’m done with my work, I promise.” next to the “Restart Now”, and “Restart Later” buttons. C’mon Microsoft, I love the dev tools, but this crap makes me love to hate you!

C# and the ‘ref’ Keyword

Lately I’ve been thinking about the nuances of the C# language. A friend of mine and I were talking about the merits of C# as compared to Java. Of course, I don’t want to open that can of worms here, however I did realize that I appreciate small details in the C# language that are not present in Java. One of those details is the keywords used in method parameter lists. These include ‘ref’, ‘in’, and ‘out’. The reason these are significant is that in both C# and Java, how are parameters passed? Well we’ve all been told that, of course, parameters are passed by reference and so we go along happily with this information because most of the time for all intents and purposes it is true. But the deeper truth of the matter is that while references are passed, the are actually passed by value.

Here’s is some code to prove it:

public class Person
{
    protected string name;
    public string Name 
    {
        set { this.name = value; }
        get { return this.name;}
    }
    protected string email;
    public string Email 
    {
        set { this.email= value; }
        get { return this.email;}
    }
    public override string ToString()
    {
        return this.name + ", " + this.email;
    }
}

public static void main ( string[] args )
{
    Person p = new Person();
    p.Name = "Matt Long";
    p.Email = "matt.long@matthew-long.com";
    System.Console.WriteLine( p.ToString() );

    ChangeMyMembers( p, "Bob Smith", "bob@yyyyy.com" );
    System.Console.WriteLine( p.ToString() );

    ChangeMe( p, "Wendy Johnson", "wendy@yyyyy.com" );
    System.Console.WriteLine( p.ToString() );

}
public static void 
    ChangeMyMembers( Person p, string name, string email )
{
    p.Name = name;
    p.Email = email;
}

public static void 
    ChangeMe( Person p, string name, string email )
{
    p = new Person();
    p.Name = name;
    p.Email = email;
}

If you were to run this program, what would you expect the output to be? Here is the actual output:

Matt Long, matt.long@matthew-long.com
Bob Smith, bob@yyyyy.com
Bob Smith, bob@yyyyy.com

Is that what you expected? Well, since this whole post is about the parameter keywords, I would expect your answer is no. But what happened?

When we passed the Person reference to the ChangeMe() method we passed a copy of the reference. That copy is now only available in the method scope. If we change that reference, it won’t change the original–only that local copy. So when we call p = new Person(); we have assigned that local copy of the reference to point to an actual place in memory, however, it goes away as soon as we leave the method.

Now, to my knowledge there is nothing you can do to address (no pun intended) this behavior in Java directly. In C# however, enter the keyword ‘ref’. If we simply change the ChangeMe() method signature to look like this:

public static void
    ChangeMe( ref Person p, string name, string email )

And then when we call ChangeMe(), we also use the ref keyword like this:

ChangeMe( ref p, "Wendy Johnson", "wendy@yyyyy.com" );

We now get the output we expected:

Matt Long, matt.long@matthew-long.com
Bob Smith, bob@yyyyy.com
Wendy Johnson, wendy@yyyyy.com

I am not by my comments here going to enter into a “which language is better” holy war, however, the parameter keywords in C# are a nice feature. Here is an excercise for the reader. Can you implement a swap method in Java? If so, how? How would you do it in C#?

If you need some pointers for the Java way, you can start here: http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html. Then do an Internet search. (Here’s a little hint: you would have to encapsulate the parameters in a class that can be de-referenced in the swap method.)

If you want to learn more about the nuances of C#, a good way to do so is to read some interview questions. Take a look at these here.