Never Use "Var"...

Monday, November 9, 2020
By: Matthew Doucette

...unless you know what you are doing. And, even then, use it sparingly.

My thoughts on the use of "var", quoted from a quick internet post:

Overuse of "var" has cons: It only helps you, not your team. It leads to bugs. No one wants to mouse over your "vars" to see what you may have meant. In the context of debugging, is raises the thought you coded something wrong. Is it the type the coder meant? Not always possible to tell with var. Your code should communicate what you mean and empower you and your team. If you do not know what you are doing -- which includes moments where you think you know what you are doing -- then do not use "var". Code should be legible and simple, but not too simple. And, respect your team!

P.S. The use of "var" does have pros, too. Player player = new Player(); could become var player = new Player(); and it is obvious to all what is meant and the code could be said to be more legible. That said, the pros do not outweight the cons of confusing a single software developer on your team for even 5 minutes, let alone creating a bug that takes a team days to uncover.

 

That is all.

 

 

About the Author: I am Matthew Doucette of Xona Games, an award-winning indie game studio that I founded with my twin brother. We make intensified arcade-style retro games. Our business, our games, our technology, and we as competitive gamers have won prestigious awards and received worldwide press. Our business has won $190,000 in contests. Our games have ranked from #1 in Canada to #1 in Japan, have become #1 best sellers in multiple countries, have won game contests, and have held 3 of the top 5 rated spots in Japan of all Xbox LIVE indie games. Our game engines have been awarded for technical excellence. And we, the developers, have placed #1 in competitive gaming competitions -- relating to the games we make. Read about our story, our awards, our games, and view our blog.