 Tuesday, July 29, 2008
Is MS-Linux next???
Take a look at this little announcement that came out of OSCON last week... http://port25.technet.com/archive/2008/07/25/oscon2008.aspx Amazingly, Microsoft has agreed to join the Apache Software Foundation as a platinum sponsor. Oh, in case you weren't aware, Microsoft has this little product called "IIS" that kind of directly competes against the open-source Apache web server. Paradoxically, IIS would die to get the market share enjoyed by Apache, yet is neither free as in beer nor as in speech. A fact that I imagine probably makes for a tough sell for the Microsoft marketing department. The cynics are saying this is a bold move by Microsoft to help ease the sting caused by the PR fiasco known as "Windows Vista." "Hey, look at us, you might think we suck and are inherently evil, but suddenly we love open source (well, not anything that is GPL'ed - but I guess that's just common sense)." I honestly don't know what to make of it. I guess in the grand scheme of things, the $100,000 ponied up to join Apache can't even be considered a drop in the bucket for Microsoft. As they said on TWiT this last weekend, "That just gets them on the mailing list." Maybe it's like Sun-Tzu said (and later echoed by Michael Coreleone in Godfather II), "Keep your friends close, but your enemies even closer."
Tuesday, July 29, 2008 10:01:31 PM (Eastern Standard Time, UTC-05:00) .Net | Apache | IIS | Open Source
 Sunday, July 20, 2008
 Saturday, July 19, 2008
Don't call virtual methods in a constructor
I ran into a piece of code today similar to this: public class Base
{
public Base()
{
this.Initialize();
}
protected virtual void Initialize()
{
Debug.WriteLine("Base.Initialize()");
}
}
public class Derived : Base
{
public Derived() : base() {}
protected override void Initialize()
{
base.Initialize();
}
}
public class FurtherDerived : Derived
{
public FurtherDerived() : base() {}
protected override void Initialize()
{
Debug.WriteLine("FurtherDerived.Initialize()");
}
}
public class Tester
{
public static void Main(string[] args)
{
Base myBase = new Base();
Derived myDerived = new Derived();
FurtherDerived myFurtherDerived = new FurtherDerived();
Console.ReadLine();
}
}
See any problems here? Even though the compiler lets you call virtual methods from a base constructor, it's generally a "bad idea". That's because the constructor in the derived classes defers its construction up to its base classes before it executes its own constructor (even if we don't explicitly call the base constructor like in the example). Yet the call to a virtual function in the base class constructor is executed in the derived class implementation. This can cause subtle, unexpected problems.
In the example above, things "happen" to go well. It actually runs just fine. It outputs the following as you'd probably expect: Base.Initialize()
Base.Initialize()
FurtherDerived.Initialize()
But that's only because we didn't do anything significant in the derived constructors. What happens if we change the FurtherDerived class: public class FurtherDerived : Derived
{
StringBuilder _sb;
public FurtherDerived() : base()
{
_sb = new StringBuilder();
}
protected override void Initialize()
{
_sb.Append("FurtherDerived.Initialize()");
Debug.WriteLine(_sb.ToString());
}
}
Guess what happens when we run this code? FAIL!!! The dreaded, "Object reference not set to a reference of an object". Since the Initialize() method in the FurtherDerived class is called from the Base constructor, we are trying to access the _sb class level parameter before it's ever initialized. We are calling a method on an class we know has not been fully constructed. That ain't good.
The lesson is - never call a virtual method from a constructor. At best, you are introducing a bug waiting to happen. At worst, you've already done so.
Friday, July 18, 2008 11:57:17 PM (Eastern Standard Time, UTC-05:00) .Net | Back To Basics | C#
 Monday, July 14, 2008
Generating a random string in Groovy
A while ago, I wrote a post about using LINQ in C# to generate a random confirmation number. On my current gig we are using Groovy to write test scripts for SOAP UI acceptance test cases (incidentally, SOAP UI is a great web service testing tool that probably deserves its own blog post). In order to generate a dummy random string, I used an algorithm similar to the one I used in the LINQ post and wanted to show how it would be implemented in Groovy. // create the list of available characters
def availChars = []
('A'..'Z').each { availChars << it.toString() }
// even it out to about the same odds of getting a char or a number
3.times { (0..9).each { availChars << it.toString() } }
def generateRandomString = { length ->
def max = availChars.size
def rnd = new Random()
def sb = new StringBuilder()
length.times { sb.append(availChars[rnd.nextInt(max)]) }
sb.toString()
}
// print it out 10 times to see the randomness
10.times { println generateRandomString(8) }
If you're not familiar with Groovy, the -> symbol in the generateRandomString declaration marks a closure that takes one argument. In this case, it takes the desired length of the returned string. The last line invokes the closure n times. I really like (similar to Ruby) how you can pass function blocks as iterators over collections. It makes creating a loop both simple and intuitive (e.g. 10.times). And the syntantical sugar of creating a ranged list (e.g. 'A'..'Z') is another nice feature of the language.
Monday, July 14, 2008 2:21:50 PM (Eastern Standard Time, UTC-05:00) Groovy
 Tuesday, July 08, 2008
Windows Tips Using The Scroll Wheel
Like most developers, I'm a huge fan of the scroll wheel. It's a major part of my daily computing experience, so much so that I find I lose major productivity if I'm relegated to using just the scrollpad on my laptop. Now I've been using Windows for a while now. But I was amazed to learn something brand new today. Somebody posted a tip that you could simply "middle click" with the scroll wheel on a tab in Visual Studio to close out that document. So I fired up VS to test it out and sure enough, it works. That made me wonder ... is this a Visual Studio trick, or does it apply to any application with tabs? I fired up Firefox, IE, and Notepad++ and it works on all of them, too. I showed this little trick to a coworker who was also surprised to learn about it. He said, "I knew you could open new tabs by middle-clicking with the scroll wheel, but I didn't know you could close them, too." Huh??? "Sure. If you middle-click on a link in Firefox or IE, it will automatically open the link in a new tab." There you go - two tips for the price of none.
Tuesday, July 08, 2008 2:10:38 PM (Eastern Standard Time, UTC-05:00) Tips | Windows
 Monday, July 07, 2008
|