Option Strict

S

Simon Harris

Hi All,

I've been advised to use option strict. I've tried to read up on this, all i
can find is that it...

"disallows implicit narrowing conversions"

This kinda makes sense - Means I have to explicitly cast or convert data
when comparing/setting two different data types right?

Is there any more to it than this? What are the benefits of using option
strict?

Regards,
Simon.
 
S

Sahil Malik

Option Strict generally leads to better code.

How is that?

Well, for one, it prevents programmers from taking shortcuts and declaring
everything as object. Not only is the final code more intuitive to the end
user, you might prevent instances of boxing/unboxing not to happen.

Secondly, it allows you to catch more errors at compile time, i.e. type
mismatches, undeclared types etc. And an error caught at compile time is an
error saved at runtime.

- Sahil Malik
You can reach me thru my blog at
http://www.dotnetjunkies.com/weblog/sahilmalik
 
S

Simon Harris

Thank you for your reply Sahil, would you mind explaining what
boxing/unboxing is?
 
S

Simon Harris

Also, I've been switching this on in VS.Net by right clicking the project >
Properties > Build

Are you aware of any way of switching this on permently in VS.Net?

Thanks again!
Simon.
 
S

Scott M.

Option Strict also "disallows late binding".

Late binding is when the application doesn't know until run time what an
object's true type is. This causes more overhead to your application and
opens the door to errors.

Not allowing late binding (and therefore enforcing early binding) means that
all objects must know their type at design time, because of this, the
IntelliSense in VS.NET tells you what class members are allowed on the
object.
 
S

Steve C. Orr [MVP, MCSD]

The most important thing is that it turns potential runtime errors into
compile time errors.
Compile time errors are much easier and less expensive to fix, and they'll
never reach your users.
 
K

Kevin Spencer

Great point. Let me give an example, from my real-life experience. I came to
work for this company a year and a half ago, and the guy that was directly
underneath me had taught himself VB.Net coming from a VB/ASP background. He
didn't use Option Strict. He had private fields that were integers exposed
via public properties that were strings. Imagine the difficulty tracing down
an issue with a non-integer string value being used to set the value of the
Integer field. Data types are important, moreso as the bigger and more
complex the app becomes. Making sure that your data types are correct not
only speeds up runtime performance, but can prevent some hard-to-identify
errors from happening as well.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living
 
S

Sahil Malik

So all of us agree that Option Strict/Option Explicit are good things ..
then why the f*** doesn't everyone use it?

Atleast in Whidbey, they should make the default behavior to enforce those,
and overridable somehow. Overridable because there is still plenty of bad
code we need to live with.

- Sahil Malik
You can reach me thru my blog at
http://www.dotnetjunkies.com/weblog/sahilmalik
 
K

Kevin Spencer

Atleast in Whidbey, they should make the default behavior to enforce
those,
and overridable somehow. Overridable because there is still plenty of bad
code we need to live with.

You're preaching to the choir here, Sahil. I've been telling MS to turn it
ON by default for several years now.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top