Thursday, January 31, 2008

JP Boodhoo, You Magnificent Bastard!

For a little while now, my co-workers and I have been watching videos from dnrTV over lunch once a week. Recently we have been watching JP Boodhoo's series on demystifying design patterns, which has been nothing short of excellent. But one of the neatest things I learned while watching was something he and the host mostly glossed over: A strongly typed Constants class.

In the past when I've done a Constants class, it has looked something like this:

22 public class MyConstants

23 {

24 public const string MyFirstValue = "VALUE1";

25 public const string MySecondValue = "VALUE2";

26 }


Then to use them it goes sort of like this:

17 public class MyClass

18 {

19 public string MyValue { get; set; }

20 }


8 class Program

9 {

10 static void Main(string[] args)

11 {

12 MyClass myObj = new MyClass { MyValue = MyConstants.MyFirstValue };

13 Console.Write(myObj.MyValue);

14 }

15 }


Which would print out "VALUE1".

This works out fine, but has an inherent flaw: you are still just passing around a string, and there is nothing stopping someone from forgetting there is a class of constants and simply assigning their own random string value. This is where JP comes in. In this episode, somewhere near the end, he whips up a quick strongly-typed Constants class, and it works really, really well. Here's what my example above would look like if I had a quarter of Mr. Boodhoo's intelligence:

22 public class MyConstants

23 {

24 public static readonly MyConstants MyFirstValue = new MyConstants();

25 public static readonly MyConstants MySecondValue = new MyConstants();

26

27 private MyConstants() { }

28 }


Now when you use it, it can be typed:

17 public class MyClass

18 {

19 public MyConstants MyValue { get; set; }

20 }


8 class Program

9 {

10 static void Main(string[] args)

11 {

12 MyClass myObj = new MyClass { MyValue = MyConstants.MyFirstValue };

13 Console.Write(myObj.MyValue);

14 }

15 }


Which would print out... ConsoleApplication1.MyConstants? Oh crap, we're only part way there. But JP didn't leave us much to do, we just need to find a way to get a string value out of that object. Here's what our class could look like now:

22 public class MyConstants

23 {

24 public static readonly MyConstants MyFirstValue =

25 new MyConstants("VALUE1");

26 public static readonly MyConstants MySecondValue =

27 new MyConstants("VALUE2");

28 private readonly string value;

29

30 private MyConstants(string value)

31 {

32 this.value = value;

33 }

34

35 public override string ToString()

36 {

37 return value;

38 }

39 }


Now let's use it again:

17 public class MyClass

18 {

19 public MyConstants MyValue { get; set; }

20 }


8 class Program

9 {

10 static void Main(string[] args)

11 {

12 MyClass myObj = new MyClass { MyValue = MyConstants.MyFirstValue };

13 Console.Write(myObj.MyValue.ToString());

14 }

15 }


Pretty sweet eh?

The Latest Christmas Post Evar

When last I wrote, I promised a picture of this year's Christmas tree, in all it's glory. Six weeks later, I've gotten around to it. Here it is, check out the girth of this thing:



I'm gonna miss that tree, what a monster. Compare it to last year's tree from my previous post they both have the same tree skirt!

Coming soon: A useful post!

Monday, December 17, 2007

Christmas Reflections

We just brought home our Christmas '07 tree, which is so awesomely huge that, not only did it mark up the ceiling, but it also caused us to run out of lights! So I don't have any completed decorating shots yet, but I decided it might be fun to post the before and after shots of Christmas '05, when Molly, our goofy sheepdog (and at that time about 9 months old), decided to play with the tree one day while we were out.

Here is a look at our proud (yet admittedly small) tree from '05 in all it's glory:



And here is what it looked like after Molly had her way with it (I hurriedly stood it up before I thought to take a picture):



Finally, while there is no visual evidence of the culprit from that day, here is a picture from this afternoon. Do not be fooled by the innocent expression, this dog is 100% rascal...

Monday, December 10, 2007

How Christmas Is Supposed To Work

My buddy D'Arcy is apparently new to the real Christmas tree thing. I'm a little shocked, as I always believed he came from somewhere up north, a place where I assumed fake trees would be properly mocked for the craptastic little plastic devils that they are. For anyone out there curious about my take on artificial Christmas trees, here it is:
Fake trees are for housewives and little girls. Men slowly kill something beautiful by removing it from nature, erecting it in their dwelling, and carefully dressing it up before spending 2-3 weeks watching it slowly whither and die. When finally it has succumbed, men haul the carcass into the back yard and hack it to pieces in preparation for it's final resting place: the fire pit. Forget the Baby Jesus, this is what Christmas is all about. -- Dave Harris

But alas, it appears that D'Arcy has never had the joy of a real tree before! It seems he is learning quickly, but I fear he is not properly appreciating the traditions and joy that come with a real Christmas. Yes, I leave 'tree' off that sentence for a reason: Christmas without a real tree is not a real Christmas. This is not open for debate.

Anyways, this is how a real Christmas works, and it should be celebrated and loved for all its glorious tribulations:
  1. Buy a perfectly imperfect Christmas tree. Or better yet, chop one down out of the woods in the middle of the night (it helps to scout out the tree you want during the day when it is light out, that way you can avoid needing a flashlight when it comes time for the Big Dirty).
  2. Drag the tree into your house and stand it up in the base. If you do this right, you should have snow, needles, and branches all over your house.
  3. If you picked your tree properly, it will be at least 2-3 feet too tall. You will need to cut the bottom off, but you want to make sure that you don't cut too much. The proper final height should leave the tree-topper bent over sideways up against the ceiling. Do the cutting in place on the living room floor, which should closely resemble the tree's original home by the time you are done.
  4. While you get all sappy holding the tree up straight, have your wife or another family member fight their way through the needles and screw it into place. This entire process should result in at least one good yelling fit by the involved parties.
  5. Circle the tree like a wolf circles it's prey. You are looking to find the 'good' side, the side that has the least number of gaping holes and broken limbs. Once you've found it, face that side towards the viewing area.
  6. Your tree, if properly selected, is now gorgeously engrossing a good portion of the room it is in, pressing out against the ceiling and walls on 2-3 sides. If you've wedged it in good you might be set, but just in case, it isn't a bad idea to tie it to a couple hooks or fixtures. These ropes add ambiance and 'festive spirit' to your decor.
  7. Time for the lights! Now, if you're doing this right, you should have a couple boxes of tangled up lights, preferably containing strands from as many different eras as possible. Don't take time planning anything out or testing the lights though; just start grabbing strands of lights out at random and spiraling them down the tree, from top to bottom.
  8. If you are lucky, you will have a few bubble lights on your tree. These can be hard to find, so don't be ashamed if you don't have any yet, but be sure to acquire them whenever you can, and hold on to them dearly! You can go years, if not decades, without seeing bubble lights in stores. It is imperative that you treat the ones you have like Smeagol treats his precioussssss.
  9. And now is the time on Sprockets when we dance!
  10. After the dancing, get out your boxes of decorations. These decorations should be a mishmash of things you made when you were 6 and that were handed down to you from your dead grandparents. Nothing should 'match'. I repeat: NOTHING. SHOULD. MATCH. 'Designer' Christmas trees make the Baby Jesus cry.
  11. Once the tree is littered with ornaments, some people think they are done! Those people are dumb. For now it is time for the coup de grace: tinsel! And not garland or whatever, but real tinsel. Old tinsel is even better, it used to be heavier and hung nicer than the stuff they make now. So just like bubble lights, horde your good tinsel. Do NOT throw it out with the tree! Not only is that bad for the environment, but you need that good stuff!
  12. Tinsel is extremely important; it can mean the difference between looking at a rotting bark-carcass strung up in your living room, or viewing a modern-day, pine-scented Van Gogh. So when it comes to applying the tinsel, technique is very important. You can't just take hunks of it and slap it all over the tree in 15 minutes and say you're done. A properly tinseled tree is done one strand at a time, hung across one individual needle. Each little 'finger' on each branch should have at least 2-3 pieces of tinsel. To avoid frustration, it's best to start at the top and work down. Also work from the inside of the branch outwards. For a good 10 foot tree this process should take a few hours, so it is acceptable to span the work across a couple of days.
There you have it, the 12 building blocks which are the foundation of a fantastic Christmas! The tree at the Harris household will be going up next weekend, so I will post a picture of it sometime next week in all of it's imperfect, half-dead, tinsel-laden glory.

Thursday, November 29, 2007

XSD.EXE, oh how you vex me

I ran into an interesting issue with xsd.exe today. I was generating some classes based on a schema, but when I went to use the classes, I became confused. For some reason when generating a section that simply contains an array of another complex type, it ends up creating a double array of the inner class, rather than a normal array of the parent type which would contain a single array of the child class. This had me pretty confused for awhile but luckily I was able to find a solution online, which saved me from hand-bombing the fix.

Interestingly, while I'm currently using xsd.exe to gen the classes, I'm actually more a fan of the Skeleton Crew tools from BoneSoft. If you ever need to generate schema's from XML, or classes from schema's, or both, give it a try.

Tuesday, November 13, 2007

Back in the Whale's Vagina

I've been sent back to San Diego for some more time on-site with our favorite U.S. of American client, but if you're hoping for more embarrassing traveling tales you will be sorely disappointed by this post. On the other hand, I think we can all agree that the title alone would make this a solid 3.5 even if I just pasted the "Lorem Ipsum" placeholder text in for content. Lets hope I can do better than that, but I'm not making any promises.

This time around I'm here a little longer, and I've had a little more time to get off the hotel-office trolley tracks and enjoy the place. I got here early Sunday and took the opportunity to see the San Diego Super Chargers take on Peyton Manning and the Baltimore Colts of Indianapolis. This was my first NFL game, and although it cost me a pretty penny to buy my ticket from stubhub.com, and although I'm pretty sure that I broke a state law in doing so, it was worth it*.

I've never been to a sporting event by myself before. In fact, I've never even so much as attended a movie by myself, so it felt a little weird. But the awesome, drunken fans around me made me feel welcome, and I enjoyed myself in spite of the awkwardness. There was a true Super Fan right behind me and he screamed and hollered the entire game, even during TV timeouts and the space between quarters. I left my seat to wander the stadium at half-time, so he may have been quiet during that period, but I'm skeptical. He had our whole section in stitches the entire game, but I think my favorite part was when he got so excited he started stomping up and down in a puddle of his own spilled beer, at which point his extraordinarily patient significant other finally had to pull out the "please settle down honey, you're spraying beer everywhere" line. This caused him to "squeegee" the beer away from his literal stomping grounds with his shoes in case he got too excited later on.

The game itself had everything. The Chargers returned the opening kick-off for a touchdown. They returned a punt for a touchdown. They intercepted the golden boy every time he threw the ball in the first half. And yet they still almost found a way to snatch defeat from the jaws of victory. Thankfully Adam Vinatieri is in the early stages of his heel turn (in which he will slowly destroy the Colts from within before revealing that he is still a Patriot at heart) so he missed the game-winning field goal and sent everyone home happy.

The other thing that a little more time to wander around outside has allowed me to do is notice the giant Crate and Barrel store across the street from my hotel. What a wondrous land of well-priced linens and stately knick-knacks.

Just kidding, what I've actually begun to appreciate is the beauty that is San Diego and its weather. This place is simply amazing. I'm not sure I'd want to live here (for a bunch of reasons that I won't get into), but I simply cannot argue that perpetual summer doesn't have some serious appeal.

It certainly doesn't hurt that the cold spectre of winter is looming menacingly back in Winnipeg. In fact, if I get sent here in February there is a 37% chance that I will get caught making out with a palm tree. As with the sketchy ducat purchase noted above, however, this is a risk I'm willing to take.

*If the police read this and I get arrested before I'm back in Canada it was in no way worth it.

Tuesday, November 6, 2007

On Babies And Shelvesets

One of my co-workers is expecting his first baby any day now, and yet at the same time we've saddled him with some rather large changes that spread across all areas of our code base. The fall back, in case he cannot complete the changes to a suitable state for check-in by the time the baby arrives, has been for him to "shelve early and often". Basically, if he walks away from the computer we want him to shelve his code into TFS. This way, if he doesn't come back to the computer for a few days because a stork knocked on his door, I can just un-shelve his changes and finish up the leftover bits.

This seemed like a great plan; many backs were slapped, high-fives initiated (and returned!) and egos inflated. We are genius! So I don't know about the other guys, but I've been sleeping like a newborn baby knowing that all our bases were covered. I think you know where this is going. Except it's not. But it almost did.

Basically, papa-to-be was curious why some tests were failing and asked if I could unshelve his latest change and give it a look-see. When I went to do so, I got the following lovely error:

SupportTestData.xls is locked for check-out by INSERTCOWORKERHERE in workspace COWORKERSWORKSPACE.

Umm, that's not good. You see, xls is binary, so you can't share a check-out. And I can't download a shelveset unless I can check out every file. This is apparently a 'feature' of TFS, because hey, shelving isn't made for sharing, that's just a nice side-benefit, so don't expect it to always work the way you hoped it would.

Thankfully we discovered this pre-stork. Now we have the knowledge that he can simply undo his pending changes following his shelves, ensuring that there are no locks if he never comes back.