Click here to Skip to main content
15,860,861 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

Can the C# ‘var’ keyword be misused?

Rate me:
Please Sign up or sign in to vote.
4.00/5 (3 votes)
12 Jul 2011CPOL1 min read 15.4K   2   10
The original question was whether this practice can lead to misuse. Of course it can - almost everytime it is used! In the "real world" (business), most programmers spend their days working amongst reasonably old code with several bolt-on fixes. It becomes increasingly hard to maintain because...
The original question was whether this practice can lead to misuse. Of course it can - almost everytime it is used! In the "real world" (business), most programmers spend their days working amongst reasonably old code with several bolt-on fixes. It becomes increasingly hard to maintain because other programmers have their own coding style. Some companies may have coding standards to allow maintainability to remain at a reasonable level - but most do not. And now we have var...

It's all very well stating that the variable "xyz" is badly named, but in my experience, programmers do not name their variables exactly what it's functionality and type represents. Maybe it is the lack of time, the increasing pressure, laziness or just a product of spending too much time in their own little world - but declaring a variable using the data type in a strongly typed language is of great help to work colleagues.

The idea of scanning through a 1000 line function filled with var is abhorrent. Notice the word "scanning" - we don't want to have to reduce productivity and waste our time actually reading the entire function, the likelihood of which increases when var is used.

As a conclusion, my opinion of it is that the use of var is not only practised by lazy programmers, but also by ignorant and selfish programmers in the business world. It is used by those not thinking about other programmers - in their team (not everyone is a fresh-faced graduate who knows these "new fangled techniques"!), or those who will take their place when they leave that job.

License

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


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: I myself write 1000 line functions? Perhaps you need to read... Pin
Quirkafleeg22-Jul-11 0:31
Quirkafleeg22-Jul-11 0:31 
GeneralRe: Quirkafleeg, given that you come from a company without codi... Pin
Dave The Brave21-Jul-11 21:36
Dave The Brave21-Jul-11 21:36 
GeneralRe: Did you write those debunks? :) Pin
Quirkafleeg21-Jul-11 5:10
Quirkafleeg21-Jul-11 5:10 
GeneralI agree that var is not a problem, by another reason: In one... Pin
Paulo Zemek1-Aug-11 9:00
mvaPaulo Zemek1-Aug-11 9:00 
GeneralReason for my vote of 1 garbage Pin
Dave The Brave21-Jul-11 4:17
Dave The Brave21-Jul-11 4:17 
Generalsergueis63, I work all the time on such projects. And I pref... Pin
Dave The Brave21-Jul-11 4:01
Dave The Brave21-Jul-11 4:01 
GeneralRe: Man... you still don't get it. The point is that you can't c... Pin
sergueis6325-Jul-11 18:06
sergueis6325-Jul-11 18:06 
GeneralDave, you are completely off track. The point is the code _m... Pin
sergueis6318-Jul-11 13:27
sergueis6318-Jul-11 13:27 
GeneralRe: See the stackoverflow thread to debunk your myths: http://st... Pin
Dave The Brave21-Jul-11 4:04
Dave The Brave21-Jul-11 4:04 
GeneralReason for my vote of 1 I completely disagree. The var keywo... Pin
Dave The Brave15-Jul-11 0:45
Dave The Brave15-Jul-11 0:45 

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

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