Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

What is the dis advantage in using the var type variable?

user-image
Question ajoutée par Yameen Mohammed Khan , IT Manager , Al Faisal Medical Center
Date de publication: 2013/10/30
Daanish Rumani
par Daanish Rumani , Product Manager , Publicis Sapient

In my opinion using var forces me to use good names for the variables and ultimately lead to more readable code. I always use var for temporary variables. On the other hand I strive to keep temporary variables to minimum in accordance with good coding practices.

 

The obvious disadvantage is that you cannot write bad code when using var or the reader would invariably be confused.

Azmat Jadoon
par Azmat Jadoon , Software Engineer , NADRA

It reduces the readability of the code, when someone else is reading your code especially when some person is new to an existing project.

Noor Khan
par Noor Khan , Technical Team Leader , Insightz360

There has been a lot of dispute over the use of var. My general rules are the following.

  • When the type is obvious such as when the right hand of the assignment is a constructor, use var.
  • When the type is complex to write, such as a LINQ query (the reason for var in the first place) use var.
  • For ambivalent types (your Decimal being an example) where you want to make sure that your variable is correctly typed, spell it out.
  • Anonymous types have to use var.
  • In all other cases spell out the type.

Basically, the goal is to make it easier to read the code. If you feel that var suffices because the assignment is obvious, use var. Use the full type name as a hint for the reader when you feel it's necessary.

More Questions Like This

Avez-vous besoin d'aide pour créer un CV ayant les mots-clés recherchés par les employeurs?