Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Code contracts, do you use them?

Code contracts, do you use them?

Scheduled Pinned Locked Moved The Lounge
csharpphpcomdebuggingtutorial
53 Posts 14 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Marc Clifton

    In particular, I was just perusing the Code Contracts[^] class in .NET 4 / 4.5, so I thought I'd take a quick survey of the community: 1. Do you routinely verify the expected parameter values that your method receives? 2. Do you verify post-conditions (you're method is returning something correct)? 3. Do you use the Contract class, or are you happy with Debug.Assert... and its variants? 4. Do you use your own variant, something like the Contract class? Just curious. :) Marc

    Reverse Engineering Legacy Applications
    How To Think Like a Functional Programmer
    My Blog
    Computational Types in C# and F#

    J Offline
    J Offline
    Judah Gabriel Himango
    wrote on last edited by
    #20

    I've tried it, and stopped using it. The problem with Code Contracts is three-fold:

    • It's a boil-the-ocean proposition. Everything must use Code Contracts (including 3rd party libs!) or you'll drown in false positives.

    • It lacks language support. This code will give false positives, warning you that list might be a null reference:

      private readonly List<int> list = new List<int>();
      ...
      void DoSomething()
      {
      Console.Write(list.Length);
      }

      Another example of the pain of no language support is when it comes to declaring contracts on interfaces. You actually have to create a dummy class that implements the interface, then put contracts on that. YUCK!

    • Poor library support. Most libraries, even the .NET framework itself or its subsets, don't do Code Contracts well, or at all. I remember doing some Silverlight code a year or two ago, and many of the methods were missing obvious contracts.

    These things combined result in many false positives. To get rid of those false positives, you must write code to reassure the contract checker that everything's gonna be OK. Not fun. Not worth it. Bottom line: contracts could be great. But it needs broad support from libraries and languages, and that doesn't exist today, and probably won't tomorrow.

    My Messianic Jewish blog: Kineti L'Tziyon My software blog: Debugger.Break() Judah Himango

    M P 2 Replies Last reply
    0
    • J Judah Gabriel Himango

      I've tried it, and stopped using it. The problem with Code Contracts is three-fold:

      • It's a boil-the-ocean proposition. Everything must use Code Contracts (including 3rd party libs!) or you'll drown in false positives.

      • It lacks language support. This code will give false positives, warning you that list might be a null reference:

        private readonly List<int> list = new List<int>();
        ...
        void DoSomething()
        {
        Console.Write(list.Length);
        }

        Another example of the pain of no language support is when it comes to declaring contracts on interfaces. You actually have to create a dummy class that implements the interface, then put contracts on that. YUCK!

      • Poor library support. Most libraries, even the .NET framework itself or its subsets, don't do Code Contracts well, or at all. I remember doing some Silverlight code a year or two ago, and many of the methods were missing obvious contracts.

      These things combined result in many false positives. To get rid of those false positives, you must write code to reassure the contract checker that everything's gonna be OK. Not fun. Not worth it. Bottom line: contracts could be great. But it needs broad support from libraries and languages, and that doesn't exist today, and probably won't tomorrow.

      My Messianic Jewish blog: Kineti L'Tziyon My software blog: Debugger.Break() Judah Himango

      M Offline
      M Offline
      Marc Clifton
      wrote on last edited by
      #21

      Judah Himango wrote:

      The problem with Code Contracts is three-fold:

      Wow. Thank you for the detailed explanation! That provides some really valuable counterpoint. Marc

      Reverse Engineering Legacy Applications
      How To Think Like a Functional Programmer
      My Blog
      Computational Types in C# and F#

      1 Reply Last reply
      0
      • P Pete OHanlon

        Gosh shucks. :-O Who am I kidding? I lap up the attention.

        *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

        "Mind bleach! Send me mind bleach!" - Nagy Vilmos

        CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

        M Offline
        M Offline
        Marc Clifton
        wrote on last edited by
        #22

        Pete O'Hanlon wrote:

        I lap up the attention.

        Well, here's[^] your opportunity for more attention. :) Seriously though, I was wondering what your thoughts were on Judah's post - it seems like he brings up some significant issues. Marc

        Reverse Engineering Legacy Applications
        How To Think Like a Functional Programmer
        My Blog
        Computational Types in C# and F#

        P 1 Reply Last reply
        0
        • J Judah Gabriel Himango

          I've tried it, and stopped using it. The problem with Code Contracts is three-fold:

          • It's a boil-the-ocean proposition. Everything must use Code Contracts (including 3rd party libs!) or you'll drown in false positives.

          • It lacks language support. This code will give false positives, warning you that list might be a null reference:

            private readonly List<int> list = new List<int>();
            ...
            void DoSomething()
            {
            Console.Write(list.Length);
            }

            Another example of the pain of no language support is when it comes to declaring contracts on interfaces. You actually have to create a dummy class that implements the interface, then put contracts on that. YUCK!

          • Poor library support. Most libraries, even the .NET framework itself or its subsets, don't do Code Contracts well, or at all. I remember doing some Silverlight code a year or two ago, and many of the methods were missing obvious contracts.

          These things combined result in many false positives. To get rid of those false positives, you must write code to reassure the contract checker that everything's gonna be OK. Not fun. Not worth it. Bottom line: contracts could be great. But it needs broad support from libraries and languages, and that doesn't exist today, and probably won't tomorrow.

          My Messianic Jewish blog: Kineti L'Tziyon My software blog: Debugger.Break() Judah Himango

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #23

          Judah Himango wrote:

          • It's a boil-the-ocean proposition. Everything must use Code Contracts (including 3rd party libs!) or you'll drown in false positives.
          • It lacks language support. This code will give false positives, warning you that list might be a null reference:

          This should only happen if you're doing static checking. If you do runtime checking, you only see the contract checks, so the example you quote won't trigger anything. What I would expect to see here is (assuming that we can only call DoSomething after it's had some data added into it).

          *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

          "Mind bleach! Send me mind bleach!" - Nagy Vilmos

          CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

          J 1 Reply Last reply
          0
          • M Marc Clifton

            Pete O'Hanlon wrote:

            I lap up the attention.

            Well, here's[^] your opportunity for more attention. :) Seriously though, I was wondering what your thoughts were on Judah's post - it seems like he brings up some significant issues. Marc

            Reverse Engineering Legacy Applications
            How To Think Like a Functional Programmer
            My Blog
            Computational Types in C# and F#

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #24

            I've added my thoughts to that post - and introduced the caveat of thread safety with contracts.

            *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

            "Mind bleach! Send me mind bleach!" - Nagy Vilmos

            CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

            M 1 Reply Last reply
            0
            • P Pete OHanlon

              Judah Himango wrote:

              • It's a boil-the-ocean proposition. Everything must use Code Contracts (including 3rd party libs!) or you'll drown in false positives.
              • It lacks language support. This code will give false positives, warning you that list might be a null reference:

              This should only happen if you're doing static checking. If you do runtime checking, you only see the contract checks, so the example you quote won't trigger anything. What I would expect to see here is (assuming that we can only call DoSomething after it's had some data added into it).

              *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

              "Mind bleach! Send me mind bleach!" - Nagy Vilmos

              CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

              J Offline
              J Offline
              Judah Gabriel Himango
              wrote on last edited by
              #25

              Sure, if you turn off static checking, you don't get any compiler warnings. In that sense, it's hardly better than if (foo == null) throw new...

              What I would expect to see is:

              public void DoSomething()
              {
              Contract.Requires(list != null);
              }

              Why would you expect to see a list null check? List is initialized at declaration to a guaranteed non-null value and cannot be reassigned due to the readonly modifier.

              My Messianic Jewish blog: Kineti L'Tziyon My software blog: Debugger.Break() Judah Himango

              P S 2 Replies Last reply
              0
              • M Marc Clifton

                In particular, I was just perusing the Code Contracts[^] class in .NET 4 / 4.5, so I thought I'd take a quick survey of the community: 1. Do you routinely verify the expected parameter values that your method receives? 2. Do you verify post-conditions (you're method is returning something correct)? 3. Do you use the Contract class, or are you happy with Debug.Assert... and its variants? 4. Do you use your own variant, something like the Contract class? Just curious. :) Marc

                Reverse Engineering Legacy Applications
                How To Think Like a Functional Programmer
                My Blog
                Computational Types in C# and F#

                J Offline
                J Offline
                jschell
                wrote on last edited by
                #26

                Marc Clifton wrote:

                1. Do you routinely verify the expected parameter values that your method receives?

                Only at layer boundaries via the classes that are exposed that way.

                Marc Clifton wrote:

                2. Do you verify post-conditions (you're method is returning something correct)?

                No. I do however verify results from other layers.

                Marc Clifton wrote:

                3. Do you use the Contract class, or are you happy with Debug.Assert... and its variants?

                No and no. If I verify something then it remains part of the code.

                Marc Clifton wrote:

                4. Do you use your own variant, something like the Contract class?

                Not any more. I wrote one one time but it just isn't worthwhile.

                1 Reply Last reply
                0
                • J Judah Gabriel Himango

                  Sure, if you turn off static checking, you don't get any compiler warnings. In that sense, it's hardly better than if (foo == null) throw new...

                  What I would expect to see is:

                  public void DoSomething()
                  {
                  Contract.Requires(list != null);
                  }

                  Why would you expect to see a list null check? List is initialized at declaration to a guaranteed non-null value and cannot be reassigned due to the readonly modifier.

                  My Messianic Jewish blog: Kineti L'Tziyon My software blog: Debugger.Break() Judah Himango

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #27

                  Judah Himango wrote:

                  Why would you expect to see a list null check?

                  And what happens if someone modifies the code and removes the initialisation of the list? As far as turning off the static checking - contracts allow you to do so much more that they do add value beyond simple if (...) checking, even if that ultimately is what they produce. For example, object invariant contracts are useful if your class has state that must conform to a particular requirement - invariants are automatically rewritten into your code to make sure you don't break the state. If you have checks that you do regularly in a class, then use an abbreviator. And yes, you've pointed out that you have to write a dummy class for interface checking, but it has saved our bacon quite a few times with our interfaces by allowing us to supply APIs to the client that do the checking for them when they implement one of our interfaces - that's a real timesaver.

                  *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                  "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                  CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                  J 1 Reply Last reply
                  0
                  • P Pete OHanlon

                    Judah Himango wrote:

                    Why would you expect to see a list null check?

                    And what happens if someone modifies the code and removes the initialisation of the list? As far as turning off the static checking - contracts allow you to do so much more that they do add value beyond simple if (...) checking, even if that ultimately is what they produce. For example, object invariant contracts are useful if your class has state that must conform to a particular requirement - invariants are automatically rewritten into your code to make sure you don't break the state. If you have checks that you do regularly in a class, then use an abbreviator. And yes, you've pointed out that you have to write a dummy class for interface checking, but it has saved our bacon quite a few times with our interfaces by allowing us to supply APIs to the client that do the checking for them when they implement one of our interfaces - that's a real timesaver.

                    *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                    "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                    CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                    J Offline
                    J Offline
                    Judah Gabriel Himango
                    wrote on last edited by
                    #28

                    Pete O'Hanlon wrote:

                    And what happens if someone modifies the code and removes the initialisation of the list?

                    Then I would expect the contract checker to tell me it's busted. A boon of this tool should be: "tell me when my code is busted." The current state of this tool is: "your code is busted, even if it isn't!"

                    My Messianic Jewish blog: Kineti L'Tziyon My software blog: Debugger.Break() Judah Himango

                    P 1 Reply Last reply
                    0
                    • J Judah Gabriel Himango

                      Pete O'Hanlon wrote:

                      And what happens if someone modifies the code and removes the initialisation of the list?

                      Then I would expect the contract checker to tell me it's busted. A boon of this tool should be: "tell me when my code is busted." The current state of this tool is: "your code is busted, even if it isn't!"

                      My Messianic Jewish blog: Kineti L'Tziyon My software blog: Debugger.Break() Judah Himango

                      P Offline
                      P Offline
                      Pete OHanlon
                      wrote on last edited by
                      #29

                      Judah Himango wrote:

                      Then I would expect the contract checker to tell me it's busted.

                      And that's exactly what my example does. It tells you when you have broken the contract.

                      *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                      "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                      CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                      J B 2 Replies Last reply
                      0
                      • P Pete OHanlon

                        I've added my thoughts to that post - and introduced the caveat of thread safety with contracts.

                        *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                        "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                        CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                        M Offline
                        M Offline
                        Marc Clifton
                        wrote on last edited by
                        #30

                        Pete O'Hanlon wrote:

                        I've added my thoughts to that post - and introduced the caveat of thread safety with contracts.

                        Fascinating! And thank you! Marc

                        Reverse Engineering Legacy Applications
                        How To Think Like a Functional Programmer
                        My Blog
                        Computational Types in C# and F#

                        P 1 Reply Last reply
                        0
                        • M Marc Clifton

                          Pete O'Hanlon wrote:

                          I've added my thoughts to that post - and introduced the caveat of thread safety with contracts.

                          Fascinating! And thank you! Marc

                          Reverse Engineering Legacy Applications
                          How To Think Like a Functional Programmer
                          My Blog
                          Computational Types in C# and F#

                          P Offline
                          P Offline
                          Pete OHanlon
                          wrote on last edited by
                          #31

                          You're welcome mate. Glad to be of service.

                          *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                          "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                          CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                          1 Reply Last reply
                          0
                          • P Pete OHanlon

                            Judah Himango wrote:

                            Then I would expect the contract checker to tell me it's busted.

                            And that's exactly what my example does. It tells you when you have broken the contract.

                            *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                            "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                            CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                            J Offline
                            J Offline
                            Judah Gabriel Himango
                            wrote on last edited by
                            #32

                            Quote:

                            It tells you when you have broken the contract.

                            But it also tells you it's busted, when in fact it's not busted, right? (Don't you get the error even if you've initialized a readonly variable to a guaranteed non-null value?) I know for certain this wasn't working last year, but maybe they've fixed it. If so, cool

                            My Messianic Jewish blog: Kineti L'Tziyon My software blog: Debugger.Break() Judah Himango

                            P 1 Reply Last reply
                            0
                            • J Judah Gabriel Himango

                              Quote:

                              It tells you when you have broken the contract.

                              But it also tells you it's busted, when in fact it's not busted, right? (Don't you get the error even if you've initialized a readonly variable to a guaranteed non-null value?) I know for certain this wasn't working last year, but maybe they've fixed it. If so, cool

                              My Messianic Jewish blog: Kineti L'Tziyon My software blog: Debugger.Break() Judah Himango

                              P Offline
                              P Offline
                              Pete OHanlon
                              wrote on last edited by
                              #33

                              Judah Himango wrote:

                              But it also tells you it's busted, when in fact it's not busted, right?

                              Not with a dynamic check. The static check will tell you it's busted, but the dynamic check actually checks the condition at runtime.

                              *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                              "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                              CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                              J 1 Reply Last reply
                              0
                              • P Pete OHanlon

                                Judah Himango wrote:

                                But it also tells you it's busted, when in fact it's not busted, right?

                                Not with a dynamic check. The static check will tell you it's busted, but the dynamic check actually checks the condition at runtime.

                                *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                                "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                                CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                                J Offline
                                J Offline
                                Judah Gabriel Himango
                                wrote on last edited by
                                #34

                                Right. That's what I'm saying. If you use the tool, it gives you too many false positives. You solved that by turning off half the tool: turning off static analysis.

                                My Messianic Jewish blog: Kineti L'Tziyon My software blog: Debugger.Break() Judah Himango

                                P 1 Reply Last reply
                                0
                                • J Judah Gabriel Himango

                                  Right. That's what I'm saying. If you use the tool, it gives you too many false positives. You solved that by turning off half the tool: turning off static analysis.

                                  My Messianic Jewish blog: Kineti L'Tziyon My software blog: Debugger.Break() Judah Himango

                                  P Offline
                                  P Offline
                                  Pete OHanlon
                                  wrote on last edited by
                                  #35

                                  Well, I have used the static analysis in the past, and combined this with Pex and Moles. It was certainly enlightening. But yes, I turn static analysis off - I've been saying that right from the start here.

                                  *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                                  "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                                  CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                                  J 1 Reply Last reply
                                  0
                                  • P Pete OHanlon

                                    Well, I have used the static analysis in the past, and combined this with Pex and Moles. It was certainly enlightening. But yes, I turn static analysis off - I've been saying that right from the start here.

                                    *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                                    "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                                    CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                                    J Offline
                                    J Offline
                                    Judah Gabriel Himango
                                    wrote on last edited by
                                    #36

                                    Got it. We must have been talking past each other.

                                    My Messianic Jewish blog: Kineti L'Tziyon My software blog: Debugger.Break() Judah Himango

                                    1 Reply Last reply
                                    0
                                    • P Pete OHanlon

                                      Judah Himango wrote:

                                      Then I would expect the contract checker to tell me it's busted.

                                      And that's exactly what my example does. It tells you when you have broken the contract.

                                      *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                                      "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                                      CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                                      B Offline
                                      B Offline
                                      BobJanova
                                      wrote on last edited by
                                      #37

                                      It tells you at runtime that you called a method with an invalid argument. That is really no different from "if(x == null) throw new ArgumentException("...")", except that anyone who sees that code will go 'huh? What is Contract.*?' and it's an extra piece of learning required for very little benefit.

                                      P 1 Reply Last reply
                                      0
                                      • M Marc Clifton

                                        In particular, I was just perusing the Code Contracts[^] class in .NET 4 / 4.5, so I thought I'd take a quick survey of the community: 1. Do you routinely verify the expected parameter values that your method receives? 2. Do you verify post-conditions (you're method is returning something correct)? 3. Do you use the Contract class, or are you happy with Debug.Assert... and its variants? 4. Do you use your own variant, something like the Contract class? Just curious. :) Marc

                                        Reverse Engineering Legacy Applications
                                        How To Think Like a Functional Programmer
                                        My Blog
                                        Computational Types in C# and F#

                                        G Offline
                                        G Offline
                                        Gary Wheeler
                                        wrote on last edited by
                                        #38

                                        I take contracts out on my coworkers when they fail to meet the contracts specified by the interfaces between our respective parts of the product. Does this count?

                                        Software Zen: delete this;

                                        1 Reply Last reply
                                        0
                                        • B BobJanova

                                          It tells you at runtime that you called a method with an invalid argument. That is really no different from "if(x == null) throw new ArgumentException("...")", except that anyone who sees that code will go 'huh? What is Contract.*?' and it's an extra piece of learning required for very little benefit.

                                          P Offline
                                          P Offline
                                          Pete OHanlon
                                          wrote on last edited by
                                          #39

                                          BobJanova wrote:

                                          That is really no different from "if(x == null) throw new ArgumentException("...")",

                                          Superficially, you're right. If that's all that contracts did, I wouldn't bother with them. However, they provide some really handy ways to do this as I specify here[^] and here[^]. Invariants and Abbreviators are very handy, but the ability to provide contracts for interfaces is the cherry on the cake as far as I'm concerned.

                                          *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                                          "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                                          CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                                          B 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups