# Saturday, April 04, 2009

I want you to look at the following code and try not to think too hard. There is really only one basic assumption even worth mentioning, which is that > 99.999% of the time, you look at source code in Visual Studio (specifically, Visual Studio 2008, not that it matters).

Here is the code we will be discussing for the moment - pretend you are one of my coworkers reading it for the first time, and you need to understand what is going on:

   1: // ...a handful of lines into a method...
   2: webGrid.Columns = WebGridDefinitions.QuarterlyReportColumns(linkUrl);

Now, of what type is the variable webGrid?
How about linkUrl?
I would be willing to bet you may be able to guess, even at this point.


Now, let's say you are still not 100% sure, so you scroll up the method definition a bit to find out, and you find this code:

   1: var linkUrl = htmlHelper.GetVirtualPath("MyAction", "MyController");
   2: var webGrid = new WebGrid(someObject, someData);

Now, of what type is the variable webGrid?
How about linkUrl?
If you don't know at this point, there is little hope for you.


Let's say that instead of the previous bit of code, you find this:

   1: string linkUrl = htmlHelper.GetVirtualPath("MyAction", "MyController");
   2: WebGrid webGrid = new WebGrid(someObject, someData);

Now of what type is the variable webGrid?
How about linkUrl
How is this code any more clear than the code which uses the implicitly-typed variable declaration? Is the redundancy really that useful? Really?!

The only argument I am even willing to consider here is that, somehow, you are not sure what type htmlHelper.GetVirtualPath() returns, and thus the variable linkUrl is a bit ambiguous. But, remember the assumption we began with? You have no excuse now, unless you just started using Visual Studio 5 minutes ago and are not yet familiar with the power of Intellisense.

I posit that using implicitly-typed variable declarations via the var keyword is generally easier to read and maintain than being explicit (and redundant) in declaring variables.

Now, one's personal taste can make this argument somewhat moot, since you can still get the job done either way, but I would argue that it isn't worth pushing your personal tastes on others if they are comfortable with either method. If you want to stay behind the times and turn into a dinosaur, that's fine by me, but don't force me into the same fate because you want to push your subjective tastes on others for your comfort.

smile_tongue

By the way, the WebGrid type here is not a standard type, but that isn't the point. Whatever a "WebGrid" is in this code, it's only as far away as "Go To Definition."

Kick It on DotNetKicks.com
All comments require the approval of the site owner before being displayed.
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, b, blockquote@cite, i, strike, u) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Live Comment Preview