Click here to Skip to main content
11,640,599 members (61,342 online)
Click here to Skip to main content

Tagged as

Can the C# ‘var’ Keyword be Misused?

, 21 Jul 2011 CPOL 12.3K 1
Rate this:
Please Sign up or sign in to vote.
More and more often I've been seeing C# code like this: var Data = GetData(); What on earth does GetData() return? This code is not as maintainable as it could be and is not as maintainable as it should be. Doesn't explicitly declaring the variable type make the code more readable, understandabl

More and more often I've been seeing C# code like this:

var Data = GetData(); 

What on earth does GetData() return? This code is not as maintainable as it could be and is not as maintainable as it should be.

Doesn't explicitly declaring the variable type make the code more readable, understandable and ultimately more maintainable?

DataTable Data = GetData(); 

Ahhh, GetData() returns a DataTable.

I know that var has uses but I wish it would have been named something much longer because typing 'var' is too easy. Perhaps it should have been named AutomaticTypeVar or even AutoVar to reduce the lazy misuse.

Just my 2 cents.

Steve Wellens

[Update]

A user on the Asp.Net forums was kind enough to provide this quote and link:

"However, the use of var does have at least the potential to make your code more difficult to understand for other developers. For that reason, the C# documentation generally uses var only when it is required."

http://msdn.microsoft.com/en-us/library/bb384061.aspx 

 

[Update II]

There seem to be a lot of lazy developers who are trying to justify and rationalize their lazy habits. 

Let's try a heuristic approach to see if the concept of maintenance can be better communicated:

var x = FunctionA();              // this is unacceptable

var x = GetCustomerID();          // better

var CustID = GetCustomerID();     // better but what is CustID?

String CustID = GetCustomerID();  // best.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Steve Wellens
EndWell Software, Inc.
United States United States
I am an independent contractor/consultant working in the Twin Cities area in Minnesota. I work in .Net, Asp.Net, C#, C++, XML, SQL, Windows Forms, HTML, CSS, etc., etc., etc.

You may also be interested in...

Comments and Discussions

 
Question"Var" is good when it avoids redundancy; bad when it obscures needed information. Pin
supercat921-Jul-11 9:42
membersupercat921-Jul-11 9:42 
AnswerRe: "Var" is good when it avoids redundancy; bad when it obscures needed information. Pin
Jaime Olivares1-Aug-11 15:12
memberJaime Olivares1-Aug-11 15:12 
GeneralI agree -- I hate the trade-off of convenience vs. stability (or security) Pin
Bit-Smacker15-Jun-10 7:01
memberBit-Smacker15-Jun-10 7:01 
GeneralI just had to... Pin
Tom Janssens6-Apr-10 6:47
memberTom Janssens6-Apr-10 6:47 
Soz, couldn't resist Wink | ;)

[edit]
I did vote a 5 Big Grin | :-D

GeneralAbout 'var' name Pin
Jaime Olivares19-Nov-09 18:24
memberJaime Olivares19-Nov-09 18:24 
GeneralRe: About 'var' name [modified] Pin
Steve Wellens1-Aug-11 15:04
memberSteve Wellens1-Aug-11 15:04 
GeneralRe: About 'var' name Pin
Jaime Olivares1-Aug-11 15:08
memberJaime Olivares1-Aug-11 15:08 
GeneralRe: About 'var' name Pin
Steve Wellens1-Aug-11 15:10
memberSteve Wellens1-Aug-11 15:10 
GeneralRe: About 'var' name Pin
Jaime Olivares1-Aug-11 15:19
memberJaime Olivares1-Aug-11 15:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150731.1 | Last Updated 21 Jul 2011
Article Copyright 2009 by Steve Wellens
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid