Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Can the C# ‘var’ keyword be misused?

0.00/5 (No votes)
12 Jul 2011 2  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here