Tuesday, August 19, 2008

Michael Phelps and his 12,000 Calorie Diet

So today I read an article that Michael Phelps eats 12,000 calories a day, which generally consists of the following:

Breakfast: Three fried-egg sandwiches loaded with cheese, lettuce, tomatoes, fried onions and mayonnaise. Two cups of coffee. One five-egg omelet. One bowl of grits. Three slices of French toast topped with powdered sugar. Three chocolate-chip pancakes.

Lunch: One pound of enriched pasta. Two large ham and cheese sandwiches with mayo on white bread. Energy drinks packing 1,000 calories.

Dinner: One pound of pasta. An entire pizza. More energy drinks.

I am totally jealous. Don't get me wrong... I have eaten a hot n' ready pizza all by myself for dinner many-a-time (always... always feel like garbage afterwards by the way), but if I did it every day I would look like this:

image

After he wins a ridiculous amount of medals in the 2012 games in London he should retire and take up competitive eating, which should become an Olympic event in the 2016 games. Imagine being the Gold medal winner for most hot dogs, grilled cheeses, ice cream, butter... whatever, you name it. How American is that? Kobayashi is going down!

phelpseating

My MS Paint skills are unbelievably awesome.

Sunday, August 17, 2008

Dependency Injection and Inversion of Control Explained

Dependency injection (also called inversion of control) is an interesting design pattern that I am now utilizing in the code I write.  The purpose of this pattern is to achieve a loose-coupling across relationships within classes, which also leads to much cleaner code and eases the ability to write unit tests.  The name may sound rather intimidating but the concept is fairly simple. Say you have a class 'walrus' that needs a 'walrusService'  instance in order to do its work.  Traditionally, I would code up the class like this:

public class Walrus
{
private readonly WalrusService walrusService =
new WalrusService();

public void DoSomeWork()
{
walrusService.DoSomeWork();
}

public void DoLotsOfWork()
{
walrusService.DoLotsOfWork();
}

public int SomeIntProperty
{
get;
set;
}
}


public class WalrusService
{
public void DoSomeWork()
{
Console.WriteLine("Doing some work...");
}

public void DoLotsOfWork()
{
Console.WriteLine("Doing lots of work...");
}
}

Notice how the Walrus class above manages its dependency on the WalrusService  to do some work. The class is tightly-coupled to the service class and there is no easy way to swap the service out for a different one.  The Walrus class also needs to know how to create an instance of the Walrus service internally.


Using dependency injection combined with interface-based programming methodologies will allow us to loosely-couple this dependency and will make the Walrus class no longer care about managing the dependency on the Walrus service.


The first thing I am going to do is make the WalrusService implement an interface. ReSharper is awesome for this, just right click on the Walrus class, go to the Refactor section and click Extract interface...


public interface IWalrusService
{
void DoSomeWork();
void DoLotsOfWork();
}
public class WalrusService : IWalrusService
{
public void DoSomeWork()
{
Console.WriteLine("Doing some work...");
}
public void DoLotsOfWork()
{
Console.WriteLine("Doing lots of work...");
}
}

Now I will create a constructor on the Walrus class that takes an instance of the IWalrusService class. This is where the injection/inversion concepts are taking place. Here is the new Walrus class:

public class Walrus
{
private readonly IWalrusService walrusService;

public Walrus(IWalrusService walrusService)
{
this.walrusService = walrusService;
}

public void DoSomeWork()
{
walrusService.DoSomeWork();
}

public void DoLotsOfWork()
{
walrusService.DoLotsOfWork();
}

public int SomeIntProperty
{
get;
set;
}
}

Performing this small change allows the Walrus class to no longer care about what service is being handed to it... as long as it is an IWalrusService then life is good. This is a good way to refactor code that is calling a service that hits the database because now that service can be mocked in a unit test scenario. If all classes are written to take its dependencies in the constructor, then relationships are always very clear and become easier to manage. I will write about Inversion of Control containers soon. These containers (such as the Castle Windsor IoC container) make these relationships even easier to manage from the caller's standpoint.

Sunday, August 10, 2008

Weekend Ramblings (some rants too...)

Non-technical ramblings:
  • I thought Pineapple Express was a mediocre movie (literally just got back from seeing it)
  • Got my scale I ordered from Amazon... I am trying to lose weight and am recording the daily results on a dry erase board in my nerd cave. Hopefully the cat won't erase it... stupid cat. If I can create a trend where I sustain a loss of a pound a week then that is perfect.
  • The book 'When You Are Engulfed in Flames' isn't very good. I guess that is why I should maybe check out this thing called the library... because now I am stuck with a crappy book from which I've only read the first few chapters.
  • I am almost caught up on watching the show Weeds. (What is it with all these pot-themed movies and shows that I have seen recently???) I was streaming them online through my Netflix account and then realized they didn't have season 3 available to stream... so I rented season 3, disc one from the video store down the road and watched it late Friday night only to want to see more episodes on Saturday afternoon. I went back to the video store and they did not have the 2nd disc in stock (crap!). I go to another video store  (I was not a member there and would have had to sign up...) and it is out there as well (double crap!). Then I remember this wonderful thing called the Internet. It is where I was watching the first 2 seasons after all... so I Google Weeds streaming episodes and sure enough I find all of them all the way up to the current episode (like season 4, episode 8). Needless to say, I watched way too much TV this weekend.
  • The new Ours cd (an indie rock band from Chicago) is not very good. Unfortunately, it seems they tried real hard to make most of the songs radio-friendly and they just end up sounding forced. A couple songs are good, but they are no Sigur Ros :-) (Sept 23rd can't come soon enough!)
  • The Olympics have started and have been fun to watch. It was good to see the USA beat China in basketball this morning.

Technical ramblings:

  • Learned some good things about how to implement a LINQ data access layer on the program at work. It is just a start, but patterns are beginning to take shape... especially the fact that timestamp columns are a great was to handle concurrency and that we need to get on board with it. Timestamp columns also give you a way of moving entities over the fence to attach to a separate LINQ DataContext. Having a way to create real foreign-keys in the system now is also extremely useful for generating automatic relationships within the dbml file and across the entities.
  • I am also finally beginning to understand domain-driven design concepts. I am still only through the first couple chapters in the book I mentioned before, but I revisited those chapters and compared the patterns to the ones used on the LINQ data access layer above and was pleasantly surprised to see that many of the practices tried this week were also in the book. (Google 'LINQ Repository Pattern') Unfortunately, the book does not deal with LINQ at all despite saying it is a .NET 3.5 book, which sucks honestly because as I am viewing the code the author wrote I can already see better ways to do the job with much less code. For instance, he still uses dynamic SQL strings built up in code to perform updates and inserts. Also there are several cases where auto-implemented properties and lambda expressions would have yielded much less code. Most of the concepts are still good, but it is hard to ignore the LINQ DataContext and generated entities when reading through the book.
  • Here is a good rant... why the heck can't I alter a column in SQL server that is numeric to set it as an Identity column. I swear I had to jump through way too many hoops at work last week to switch all of my Id fields over within my data model. At least give some way to do an update against an Identity column as well. It sucks that I had to temporarily move the data around in order to drop the table and re-create is as I wanted. I first went through creating this huge dynamic SQL script that handled updating all of my tables in one shot, which ended up working. But it was annoying because it placed all of the Id columns at the end of the table because I was forced to drop the original Id column. I know column order should never matter, but querying those tables in management studio (with select * syntax) and scrolling over to see the primary key would have drove me insane pretty quickly.
  • The 'vote of no confidence' against the still unreleased entity framework is now big news. Listen to the podcast about it from the ALT.NET guys below (really, really interesting concepts come from this website by the way). I found it funny how one of the guys wants to invent a time machine to go back and force Anders (the lead developer of C#) to create all methods as virtual by default. http://altnetpodcast.com/episodes/8-vote-of-no-confidence
  • I emailed John-Paul Boodhoo last week to see if he plans on instructing one of his 'Nothing But .NET' bootcamps sometime soon in Chicago or Detroit. He is one of the developers that quickly gained notoriety a year or two ago for pressing the subjects of TDD, IoC, DDD, and all that good stuff on .NET rocks and DNR TV. He does these bootcamps where he has a very small class, like less than 20 people, and does a hands on project with you for 5 days straight, with most days lasting up to 14 hours. The material is very, very good... and he is really good at presenting the concepts. He replied back to me today and says he has on his schedule to present in Chicago during the 2nd quarter of next year. I really want to go, but this stuff is not cheap because of the very small class sizes ($3,000 total, which includes the 5 days, food for the week, a ReSharper license, $70 amazon gift card, and a 'I survived .NET bootcamp' shirt)  Check out his webpage at http://jpboodhoo.com/Home.oo

Sunday, August 3, 2008

Nerd Cave and New Books

I spent most of the week setting up my new computer and rearranging the basement.

Here are some pictures of how I decided to lay out a study area/exercise/media room. My basement is unfinished for the most part but does have a rather large old dining room table in it, which makes a really good computer desk that gives me space for books and whatnot.

IMG_0178

IMG_0179

IMG_0180

IMG_0181

The new computer has been working really well.  I know all the techies will want the specs on it so here they are:

HP PAVILION M9200T ELITE SERIES

PROCESSOR
Intel Core 2 Quad Q6600 (2.40GHz/64KB L1/8MB L2 Cache)
MEMORY
4GB (4x1024) DDR2 SDRAM (PC2 6400 / 800 MHz)
Total memory slots: 4 DIMM
HARD DRIVE
750GB SATA-3G 7200RPM Drive with 8MB Cache
PRIMARY MULTIMEDIA DRIVE
Lightscribe 16x/8x -DL DVD+/-RW 12x RAM SATA
SECONDARY MULTIMEDIA DRIVE
N/A
GRAPHICS CARD
512MB NVIDIA GeForce 8600GT, TV-out, DVI-I, HDMI
TV TUNER
ViXS PureTVU 48B0 (NTSC/ATSC Combo)
SOUND CARD
Sound Blaster XFi Xtreme Audio (Vista) Sound Card
NETWORKING
Intel 82566DC 10/100/1000 Mb/s

I have an extra 250GB hard drive laying around that I would like to install in it when I have some ambition. I like the fact that this would bring up the total space available to a terabyte... pretty ridiculous. 10 years from now if I look back on this post I will laugh because they will have 10 Terabyte micro-SD cards and cell phones with 80 processors :-)

So far the media center aspects of Vista are pretty good. The computer came with a spiffy remote that lets me record and watch TV and control DVD menus and go through an included media guide for my cable service. All in all I was impressed with how seamless it just worked right out of the box... all I had to do was hook up the coax cable and go through a really simple wizard. It is nice to ride the exercise bike and be able to watch some TV or DVD.

Right now I am watching the show Weeds based on a recommendation. It is pretty good so far, but yeah it is a strange concept for a show to have a pot dealing mom living in a "stepford wives" kind of neighborhood (McMansion Communism, anyone?) where everyone seems to need to get a fix in order to deal with their incredibly boring lives.

Oh and speaking of fixes... I just my latest shipment from Amazon this week, which consisted of 3 books and 2 boxes of protein bars. The protein bars are really good, I guess I could talk more on those later... but here are the 3 books I ordered:

So far I have only delved into the first 2 chapters of the .NET book. I like the concepts so far, but I think the author did a pretty poor job at writing the book in a manner suited to the reader (me) who is trying to follow the code and type is out as it is presented. He never explicitly states to add files to your solution in location "X"... fortunately he does use consistent namespace conventions. Sometimes the concepts are barely glossed over too and I don't fully understand what he is talking about. But overall, so far I do like the code concepts as a whole. All of the classes and interfaces are definitely geared towards being concise by existing to do one job and to do it well.  The author is a strong believer in  established patterns and principals like these:

  • Factory
  • Decorator
  • Dependency Inversion through Interface-based programming
  • Separation of Concern and the DRY principal (Don't Repeat Yourself)
  • Model-View-ViewModel (apparently a spin-off of the classic Model-View-Presenter where I don't know the differences yet...)
  • Layered Supertype
  • Many more I am sure, I just finished chapter 2!

I would like to do a series of posts about most of these concepts in detail down the line as I get further along into this book. I am very interested in the whole "Inversion of Control" concept. It is a very unique approach to breaking down dependencies... 

For now, Walrus out!

Friday, August 1, 2008

Walrusize your LINQ!

This is a very short blog post... If selecting the same result data type as my element type within a collection I like to shorthand my code by using the Where() function instead of the more verbose LINQ syntax, which turns this:
var evenNumbers = from number in numbers
where number%2 == 0
select number;

Into this:
var evenNumbers = numbers.Where(
number => number%2 == 0 );

Am I being picky here?