Option Strict

Discussion in 'ASP .Net' started by Simon Harris, Oct 13, 2004.

  1. Simon Harris

    Simon Harris Guest

    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.
     
    Simon Harris, Oct 13, 2004
    #1
    1. Advertisements

  2. Simon Harris

    Sahil Malik Guest

    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
     
    Sahil Malik, Oct 13, 2004
    #2
    1. Advertisements

  3. Simon Harris

    Simon Harris Guest

    Thank you for your reply Sahil, would you mind explaining what
    boxing/unboxing is?
     
    Simon Harris, Oct 13, 2004
    #3
  4. Simon Harris

    Simon Harris Guest

    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.
     
    Simon Harris, Oct 13, 2004
    #4
  5. Simon Harris

    Sahil Malik Guest

    Sahil Malik, Oct 13, 2004
    #5
  6. Simon Harris

    Sahil Malik Guest

    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
     
    Sahil Malik, Oct 13, 2004
    #6
  7. Simon Harris

    Simon Harris Guest

    Thank you for your replies Sahil - Very helpful :)
     
    Simon Harris, Oct 13, 2004
    #7
  8. Simon Harris

    Sahil Malik Guest

    Sahil Malik, Oct 13, 2004
    #8
  9. Simon Harris

    Scott M. Guest

    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.
     
    Scott M., Oct 13, 2004
    #9
  10. 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.
     
    Steve C. Orr [MVP, MCSD], Oct 14, 2004
    #10
  11. 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
     
    Kevin Spencer, Oct 14, 2004
    #11
  12. Simon Harris

    Sahil Malik Guest

    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
     
    Sahil Malik, Oct 14, 2004
    #12
  13. Atleast in Whidbey, they should make the default behavior to enforce
    those,
    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
     
    Kevin Spencer, Oct 14, 2004
    #13
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.