How Visual Studio 2008 made me even lazier
|
It's true. I'm lazy by nature. Don't try to prove otherwise by pointing out the fact that I work about 80 hours a week. That's merely evidence of stupidity. But Visual Studio 2008 has made me even lazier thanks to implicitly typed local variables.Here's a C# doc on them and here's a VB doc about them. I use them everywhere. It's so bad that I've been asked to explicitly declare variables in the code samples in my conference presentations so that attendees don't get confused. While it makes my life easier and my coding more productive, is it making my code less readable? I don't remember the last time I typed AS in a line of Visual Basic code, or found myself typing out the name of a type more than once in a line of code whether it is VB or C#. |


Comments (6)
The first step is admitting that you have a problem... :-)
Posted by Mike Pizzo | May 6, 2008 4:34 PM
The trick is convincing people they don't actually need the "missing" information to understand your intent.
We are practically hard-wired to derive our understanding of a variable from its type, not its name. The name is merely a placeholder for "an instance of the declared type".
I see "var" as making variable declarations more useful and less redundant, emphasizing intent.
Also, the mind implicitly parses keywords - you don't have to actually read "if" to recognize a conditional block, "else" as the alternative, "for" as a loop, etc. I realize it is a personal opinion, but standardizing variable declarations goes a long way towards improving readability.
A favorite litmus test: Which of the following sounds more natural to say out loud?
----------------------
List customers = GetCustomers();
foreach(Customer customer in customers)
{
...
}
----------------------
var customers = GetCustomers();
foreach(var customer in customers)
{
...
}
----------------------
Let's thrown in interfaces for extra syllables!
----------------------
List customers = GetCustomers();
foreach(ICustomer customer in customers)
{
...
}
Posted by Bryan Watts | May 6, 2008 7:22 PM
That should be List obviously...
Posted by Bryan Watts | May 6, 2008 7:41 PM
This is all well and fine for developers with a good couple years of dev experience, but try and teach this to a graduate developer or junior.
Everything we've been trying to teach them about using the correct type for the intended purpose of a line of code is lost on them when it comes to implicit variable declaration.
While I have no doubt the compiler will choose the best fitting type for the data the variable must contain, the concept of choosing the correct type has little value to developers who are starting afresh.
I think this is a double edged sword.
Posted by Gareth Stephenson | May 7, 2008 2:11 AM
I agree with Bryan's first comment. Code should be "self-describing" so there is no ambiguity on the meaning or intent of any Class, Interface, Property, Method, Field, or local variable. I may be lazy by using anonymous types for variables but I make up for it by using longer and more descriptive variable names.
Posted by Dave Black | May 7, 2008 12:16 PM
I don't think you're being lazy by using var.
For another strong case in favor of var - one that goes beyond the obvious expediency of var - consider Ilya Ryzhenkov's post here.
Posted by Ward Bell | May 22, 2008 5:18 AM