views:

159

answers:

6

What's the main problem if I don't declare the type of a variable? Like, Dim var1 versus Dim var1 as Integer.

It can be anything so it should be good!

A: 

Superficially, if you don't declare the type, Intellisense can't help you with it because it doesn't know what type it is.

Welbog
A: 

This page has some points.

Shoban
+1  A: 
Joel Coehoorn
+6  A: 

The main reason to declare variable types in languages which allow you to use variant types is as a check on yourself. If you have a variable that you're using to hold a string, and then by accident you pass it to a function that expects an integer, the compiler can't tell you that you messed up unless you told it that that variable was supposed to always be a string. Instead, you're stuck with your string being reinterpreted as an integer, which is pretty much never going to give you what you want and the results will likely be confusing, and it will be hard to track down the bug.

In pretty much all languages there are a lot of constructs where you could leave it out and your program would work, but exist as a check on the programmer. The first job of a compiler is to make your code into an executable. But the second job of the compiler is to try as much as possible to make sure that the programmer didn't make a mistake. Especially when your program gets large, it's easier to let the compiler find mistakes like this as opposed to trusting that you typed everything exactly right.

Additionally, there is usually some processing overhead associated with variants, but this is a more minor concern.

Tyler McHenry
Thanks, nice reply you posted. I got the idea !
Daniel
+3  A: 

There are a few reasons:

  • Vastly improved type safety.
  • Lower cognitive overhead; the compiler and Intellisense can help you.
  • Lower performance overhead; transforming things to and from Variant types has a small but nontrivial cost.
  • Eliminates need for naming warts (e.g., lblTitle to tell you that something is supposed to hold a Label).
  • Moves some kinds of runtime errors to compile-time errors, which is a big productivity win.
John Feminella
A: 

Contrast this to Python's typing system. Python allows a developer to use a variable without declaring type in advance but once a variable is used, the type is fixed. A variant, by contrast, can be assigned any type of value initially and a different type can be stored later on without any warning or complaint. So you can put a string into a variable that previously held a numeric.

Dim myvar1
myvar1 = 1

'A whole lot more code 

myvar1 = "this string"

If you ever have to maintain someone else's code, you'll start to understand why this sort of thing (changing a variable type silently) can be extra tough to maintain. Especially if you're using a module level variable this could lead to some really interesting problems. This is along the same lines as using Option Explicit in VB code. Without Option Explicit you can do this sort of thing without realizing it:

myvar1 = 1

'A whole lot more code here too

myvarl = 2

In some fonts those two variable names would be impossible to distinguish and this could lead to some tough to find bugs.

Onorio Catenacci