Friday, March 16, 2012

Option Strict

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.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

"Simon Harris" <too-much-spam@.makes-you-fat.com> wrote in message
news:uxdp6OWsEHA.2808@.TK2MSFTNGP14.phx.gbl...
> 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.
Thank you for your reply Sahil, would you mind explaining what
boxing/unboxing is?

"Sahil Malik" <contactmethrumyblog@.nospam.com> wrote in message
news:OefR7SWsEHA.636@.TK2MSFTNGP09.phx.gbl...
> 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
>
> "Simon Harris" <too-much-spam@.makes-you-fat.com> wrote in message
> news:uxdp6OWsEHA.2808@.TK2MSFTNGP14.phx.gbl...
>> 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.
>>
>>
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.

"Sahil Malik" <contactmethrumyblog@.nospam.com> wrote in message
news:OefR7SWsEHA.636@.TK2MSFTNGP09.phx.gbl...
> 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
>
> "Simon Harris" <too-much-spam@.makes-you-fat.com> wrote in message
> news:uxdp6OWsEHA.2808@.TK2MSFTNGP14.phx.gbl...
>> 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.
>>
>>
Boxing and Unboxing - http://www.dotnetextreme.com/articles/cSharpBoxing.asp
:)

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

"Simon Harris" <too-much-spam@.makes-you-fat.com> wrote in message
news:efkKtVWsEHA.1156@.TK2MSFTNGP14.phx.gbl...
> Thank you for your reply Sahil, would you mind explaining what
> boxing/unboxing is?
> "Sahil Malik" <contactmethrumyblog@.nospam.com> wrote in message
> news:OefR7SWsEHA.636@.TK2MSFTNGP09.phx.gbl...
> > 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
> > "Simon Harris" <too-much-spam@.makes-you-fat.com> wrote in message
> > news:uxdp6OWsEHA.2808@.TK2MSFTNGP14.phx.gbl...
> >> 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.
> >>
> >>
Yes there is .. go to Tools -> Options -> Projects -> VB -> .. whoaa u see
option strict/ option compare and option explicit .. more reading to do :)

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

"Simon Harris" <too-much-spam@.makes-you-fat.com> wrote in message
news:OpcEFXWsEHA.1156@.TK2MSFTNGP14.phx.gbl...
> 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.
> "Sahil Malik" <contactmethrumyblog@.nospam.com> wrote in message
> news:OefR7SWsEHA.636@.TK2MSFTNGP09.phx.gbl...
> > 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
> > "Simon Harris" <too-much-spam@.makes-you-fat.com> wrote in message
> > news:uxdp6OWsEHA.2808@.TK2MSFTNGP14.phx.gbl...
> >> 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.
> >>
> >>
Thank you for your replies Sahil - Very helpful :)

"Sahil Malik" <contactmethrumyblog@.nospam.com> wrote in message
news:uAKcAdWsEHA.3872@.TK2MSFTNGP15.phx.gbl...
> Yes there is .. go to Tools -> Options -> Projects -> VB -> .. whoaa u see
> option strict/ option compare and option explicit .. more reading to do :)
> - Sahil Malik
> You can reach me thru my blog at
> http://www.dotnetjunkies.com/weblog/sahilmalik
>
> "Simon Harris" <too-much-spam@.makes-you-fat.com> wrote in message
> news:OpcEFXWsEHA.1156@.TK2MSFTNGP14.phx.gbl...
>> 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.
>>
>> "Sahil Malik" <contactmethrumyblog@.nospam.com> wrote in message
>> news:OefR7SWsEHA.636@.TK2MSFTNGP09.phx.gbl...
>> > 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
>>>>> > "Simon Harris" <too-much-spam@.makes-you-fat.com> wrote in message
>> > news:uxdp6OWsEHA.2808@.TK2MSFTNGP14.phx.gbl...
>> >> 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.
>> >>
>> >>
>>>>
>>
You're welcome :)

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

"Simon Harris" <too-much-spam@.makes-you-fat.com> wrote in message
news:uu$breWsEHA.3556@.TK2MSFTNGP10.phx.gbl...
> Thank you for your replies Sahil - Very helpful :)
> "Sahil Malik" <contactmethrumyblog@.nospam.com> wrote in message
> news:uAKcAdWsEHA.3872@.TK2MSFTNGP15.phx.gbl...
> > Yes there is .. go to Tools -> Options -> Projects -> VB -> .. whoaa u
see
> > option strict/ option compare and option explicit .. more reading to do
:)
> > - Sahil Malik
> > You can reach me thru my blog at
> > http://www.dotnetjunkies.com/weblog/sahilmalik
> > "Simon Harris" <too-much-spam@.makes-you-fat.com> wrote in message
> > news:OpcEFXWsEHA.1156@.TK2MSFTNGP14.phx.gbl...
> >> 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.
> >>
> >> "Sahil Malik" <contactmethrumyblog@.nospam.com> wrote in message
> >> news:OefR7SWsEHA.636@.TK2MSFTNGP09.phx.gbl...
> >> > 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
> >> >> >> >> > "Simon Harris" <too-much-spam@.makes-you-fat.com> wrote in message
> >> > news:uxdp6OWsEHA.2808@.TK2MSFTNGP14.phx.gbl...
> >> >> 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.
> >> >>
> >> >>
> >> >> >>
> >>
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.

"Simon Harris" <too-much-spam@.makes-you-fat.com> wrote in message
news:uxdp6OWsEHA.2808@.TK2MSFTNGP14.phx.gbl...
> 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.
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.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net

"Simon Harris" <too-much-spam@.makes-you-fat.com> wrote in message
news:uxdp6OWsEHA.2808@.TK2MSFTNGP14.phx.gbl...
> 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.
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

"Scott M." <s-mar@.nospam.nospam> wrote in message
news:urgzotWsEHA.1404@.TK2MSFTNGP11.phx.gbl...
> 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.
>
> "Simon Harris" <too-much-spam@.makes-you-fat.com> wrote in message
> news:uxdp6OWsEHA.2808@.TK2MSFTNGP14.phx.gbl...
> > 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.
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

"Kevin Spencer" <kspencer@.takempis.com> wrote in message
news:OjdMmZgsEHA.2516@.TK2MSFTNGP11.phx.gbl...
> 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
> "Scott M." <s-mar@.nospam.nospam> wrote in message
> news:urgzotWsEHA.1404@.TK2MSFTNGP11.phx.gbl...
> > 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.
> > "Simon Harris" <too-much-spam@.makes-you-fat.com> wrote in message
> > news:uxdp6OWsEHA.2808@.TK2MSFTNGP14.phx.gbl...
> > > 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.
>
> 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

"Sahil Malik" <contactmethrumyblog@.nospam.com> wrote in message
news:O1ABHYhsEHA.532@.TK2MSFTNGP10.phx.gbl...
> 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
>
> "Kevin Spencer" <kspencer@.takempis.com> wrote in message
> news:OjdMmZgsEHA.2516@.TK2MSFTNGP11.phx.gbl...
> > 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
> > "Scott M." <s-mar@.nospam.nospam> wrote in message
> > news:urgzotWsEHA.1404@.TK2MSFTNGP11.phx.gbl...
> > > 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.
> > > > > "Simon Harris" <too-much-spam@.makes-you-fat.com> wrote in message
> > > news:uxdp6OWsEHA.2808@.TK2MSFTNGP14.phx.gbl...
> > > > 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.
> > > >

0 comments:

Post a Comment