views:

1205

answers:

9

Hi,

I'm preparing a class on Visual Basic 2005 targeting Visual Basic 6 programmers migrating to the .NET platform.

I would like a word of advice about whether to recommend them to always enable Option Strict or not.

I've worked exclusively with C-style programming languages, mostly Java and C#, so for me explicit casting is something I always expect I have to do, since it's never been an option.
However I recognize the value of working with a language that has built-in support for late-binding, because not having to be excessively explicit about types in the code indeed saves time. This is further proved by the popular diffusion of dynamic typed languages, even on the .NET platform with the Dynamic Language Runtime.

With this in mind, should someone who is approaching .NET for the first time using VB.NET and with a VB6 background be encouraged to get into the mindset of having to work with compile-time type checking because that's the "best practice" in the CLR? Or is it "OK" to continue enjoying the benefits of late-binding?

+12  A: 

Time spent developing with Option Strict enable will save you tremendous amount of debugging time later on.

Ilya Kochetov
I agree. But couldn't that risk be mitigated by make good use of Unit Testing, like for dynamic languages?
Enrico Campidoglio
Possibly, but these people are migrating from VB6 - do you really want to throw unit testing at the same time?
Carl
+5  A: 

YES!!!!

In my opinion, both as a developer, and as a college instructor YES.

It is best to get into the good habits from the start, it makes the whole process much easier, and Option Strict is one of those items that in my opinion is a NEEDED element.

added

There are literally tons of reasons that could be listed as to why, but the key is that it is a best practice, and when teaching a new language, it is key to teach those best practices from the start.

Mitchel Sellers
A: 

If you're used to having your types checked, then you probably want option strict on. turning it off can have advantages, but if your brain isn't tuned to spotting errors where your compiler would usually complain, then I would say to leave it on. I've worked in VB.Net a lot, and I have to say, that even though I work with options strict turned off most of the time, I've seen plenty of situations where having it turned on would have prevented quite a few bugs.

Kibbee
Hmmm - maybe you should consider turning Option Strict on?!?
MarkJ
Yes, I would, but in my current project, fixing all the errors by turning options strict on would probably take months.
Kibbee
A: 

I agree with all of your answers. But by looking at how dynamic languages have become increasingly popular, I wonder whether the risk of removing the compile-time type checking can be mitigated by making good use of testing practices, like Unit Testing.

However my primary concern is to teach the best practices when developing on the CLR. Is Option Strict On a best practice in .NET? If so, why did Microsoft choose to disable it by default in Visual Studio?

Enrico Campidoglio
MS bowed to the pressure of classic VB users. Just because the majority likes it doesn't make it right.
Bob King
Something to remember: VB.NET is not a dynamic language, even with Option Strict turned off. You're still working on top of a strongly-typed framework, and you still have the option of declaring typed variables... but the compiler won't warn you when the automatic conversions screw up your data.
Shog9
...in short, working with Option Strict off is like working with a lazy C++ programmer who "fixes" every compilation warning/error with a C-cast.
Shog9
Yes, all languages running on top of the CLR are indeed strongly typed. I should have phrased it better. What I meant is that late-binding is a "dynamic-like" feature of VB.
Enrico Campidoglio
However, late-binding in VB.NET is implemented by generating reflection code. Try it sometime and see what the compiled result looks like in Reflector. Ugly.
Joel B Fant
Option Explicit does most of what strict does. VB, QuickBASIC, etc has handled type casting more easily and safely than other languages for a number of years. So to a veteran VB programming the extra restrictions of Option Strict is irritating.
RS Conley
@Joel - it is ugly, and that hints at the one valid reason for turning off Option Strict (preferably only in one module). You can use it to write complicated reflection code automatically, rather than doing it yourself. But 99% of the time Option Strict should be turned on IMHO.
MarkJ
@Joel +1 for bringing this up. I didn't know that, but it surely makes sense since everything has to run on the CLR in the end, and Reflection is the only way to run dynamic code in VB.NET (up until now at least :-))
Enrico Campidoglio
+6  A: 

Option Strict obviously can't replace good unit testing – but neither the other way round. While unit testing can detect the same errors as Option Strict, this implies that there's no error in the unit tests, that unit testing is done often and early, etc ….

Writing good unit tests isn't always trivial and takes time. However, the compiler already implements some of the tests – in the form of type checking. At the very least, this saves time. More likely, this saves a lot of time and money (at least occasionally) because your tests were erroneous / didn't cover all cases / forgot to account for changes in the code.

To sum it up, there's no guarantee that your unit tests are correct. On the other hand, there's a strong guarantee that the type checking performed by the compiler is correct or at least that its glitches (unchecked array covariance, bugs with circular references …) are well-known and well-documented.

Another sum-up: Yes, Option Strict On is definitely best practice. In fact, I've worked for years in online communities like this one. Whenever someone needed help on code that obviously didn't have Option Strict enabled, we'd politely point this out and refuse to give any further help until this was fixed. It saves so much time. Often, the problem was gone after this. This is somewhat analogous to using correct HTML when asking help in a HTML support forum: invalid HTML might work, but then again, it might not and be the cause of the problems. Therefore, many professionals refuse to help.

Konrad Rudolph
Great answer. The only thing I would add is to set warnings to be treated as errors at compile time. Also - when developing web sites, I set my compatibility to the most strict level (currently doctype XHTML 1.1) for the same reason.
Rob Allen
Agreed. The extra verbosity in code due to explicit casting of variables does not compare to the great amount of time saved by letting the compiler do its job. Thanks for a great answer!
Enrico Campidoglio
+15  A: 

Yes! Option Strict is definitely a best practice with .Net. Emphasize that .Net is at it's core a strongly typed platform, and will be until the DLR is more completely supported. With few exceptions, every Dim and Function should have an explicit type declared to go with it. Things like LINQ or Boo and JScript are the exceptions that prove the rule.

Here are some other things to point out. I'm sure you're well aware of all this, but I've had to work with and maintain a lot of VB.Net code written by former VB6ers, and so this is something of a sore spot for me:

  • Don't use the old string functions: LEN(), REPLACE(), TRIM(), etc
  • Hungarian warts are no longer recommended. oMyObject and sMyString are not kosher. Show them the reference in Microsoft's Design Guidelines if they don't believe you.
  • Make sure they learn about the new AndAlso/OrElse logical operators
  • PARAMETERIZED QUERIES and modern ADO.Net. Can't emphasize that enough. They should never need to call CreateObject() again.
  • Scope works differently (and is more important) in .Net than it was in VB6. VB.Net still has modules, but they're now more analogous to a static class. It's important to understand how developing in a real object oriented environment be different, as opposed to the partial OOP support provided by VB6. There's no good reason anymore to allow methods to run to ungodly lengths.
  • Make sure they get an introduction to Generics and Interfaces (including IEnumeralbe(Of T)), and learn why they should never use an ArrayList again.

I could keep going, but I'll just point you to the Hidden Features of VB.Net Question to close out this rant.

Joel Coehoorn
“Every Dim and Function should have an explicit type declared to go with it.” - No! `Option Infer On` to the rescue (only for `Dim`s though).
Konrad Rudolph
Good point: I'm still stuck in .Net 2.0. You are allowed to break this rule for things like LINQ expressions in later versions.
Joel Coehoorn
Also, your first point's justification is wrong: No VB6 runtime is called! The calls may be redundant and bad style (inconsistent) but certainly not evil. Some even have tangible advantages (take `MsgBox` for example).
Konrad Rudolph
I went looking trying to find where I had read about MS.VB.dll calling out to the old runtime and I coudln't find it, so I just removed the reference completely.
Joel Coehoorn
Good points! Yes, my biggest challenge with this class is indeed to teach them the concepts of Object-Oriented programming first and foremost. The fact that they get to keep the same language syntax they are used to is a minor luxury.
Enrico Campidoglio
Instead of telling them not to use the compatibility functions, tell them to remove the global import of Microsoft.VisualBasic. They can still use the old functions by typing out the full namespace, but that's time consuming and ugly. A good way to make it costly to use the old stuff.
Strilanc
+3  A: 

Remember there are two levels here.

Option Explicit Option Strict

The main difference between the two is that Option Strict disable VB's automatic conversion of different data types. You have to explicitly use CType or another data conversion function to assign a variable to another type.

I been using VB since 1.0 and while I can see the point of this I think that Strict is over zealous paritcularily when dealing with Objects that have implemented or inherited different interfaces and classes.

I would start with Strict at first and if it starts getting in your way then drop down to Explicit. But don't ever both turn off, that way lies madness and excessive debugging time.

Over the years with VB I pretty much use Double for all floating point variables. This way you avoid many problems with rounding and loss of accuracy. In VB6 I used long as it was a 32-bit integer, but Integer work just as well in .NET as it is a Int32. I also recommend using Int32, Int16, etc instead of Integer,Long, etc in case Microsoft ever decides to redefine those keywords.

RS Conley
+2  A: 

I'm going to disagree with RS Conley (very unusual). My favourite VB6 gurus - Francesco Balena, Dan Appleman - all disliked VB6's automatic conversion, and are in favour of Option Strict in .NET. Many experienced VB6 programmers know automatic conversion as "evil type coercion" (pdf), and will be delighted to switch on Option Strict.

It's occasionally better to use one small module without Option Strict, to avoid lots of complicated reflection code. But that's the exception that proves the rule.

MarkJ
+1  A: 

Given Boehm's notion that fixing a problem earlier in the development cycle consumes the least resources, I'm a fan of every tool that helps developers "get it right" as early as possible. For this reason, I'm an advocate of things like IntelliSense that is both an efficiency boost as well as a tool that helps you implement working code earlier in the cycle. (Working, but not necessarily correct.)

For this reason, I also support the use of Option Strict as a way to help keep missteps and the consequent corrections deep into "design time."

adengle