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.
  • 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
                        • P Pete OHanlon

                          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 Offline
                          B Offline
                          BobJanova
                          wrote on last edited by
                          #40

                          Honestly that all looks horrible and hacky ... and particularly if it is rewriting the IL underneath me I want no part of it. An 'abbreviator' can be written in normal code just fine, it's simply a validation method! Interfaces that are exposed to external APIs to implement are about the only place where I can see that postcondition validation would be useful. But since you can't force external providers to use contracts, it doesn't help you there anyway! Postconditions in general (and invariants are simply a postcondition applied to everything) shouldn't be necessary in your code because you should already know what your code is doing, and each particular operation can be tested. If you need a global postcondition then you can have a method in your test class that checks that part of the state, which you can call in each relevant test.

                          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#

                            R Offline
                            R Offline
                            Robert Ranck
                            wrote on last edited by
                            #41

                            1. Yes. 2. Yes for interface members and abstract members, where the actual logic is going to be implemented in an implementation. Occasionally on a concrete member if the logic is not straightforward and there are specific conditions that are reasonably verifiable. 3. Yes, I use the Contract class. 4. No.

                            1 Reply Last reply
                            0
                            • B BobJanova

                              Honestly that all looks horrible and hacky ... and particularly if it is rewriting the IL underneath me I want no part of it. An 'abbreviator' can be written in normal code just fine, it's simply a validation method! Interfaces that are exposed to external APIs to implement are about the only place where I can see that postcondition validation would be useful. But since you can't force external providers to use contracts, it doesn't help you there anyway! Postconditions in general (and invariants are simply a postcondition applied to everything) shouldn't be necessary in your code because you should already know what your code is doing, and each particular operation can be tested. If you need a global postcondition then you can have a method in your test class that checks that part of the state, which you can call in each relevant test.

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

                              I'm not trying to force you to use them. If you want to remain doing if/then checking then that's fine. And if you use the interface technique, you do force them to use the contracts.

                              BobJanova wrote:

                              Postconditions in general (and invariants are simply a postcondition applied to everything) shouldn't be necessary in your code because you should already know what your code is doing,

                              Indeed you should, but what happens three years down the line when you've left the company and young Harry Intern takes a shot at your code? Oh, and he doesn't run unit tests.

                              *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
                              • 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#

                                S Offline
                                S Offline
                                stefan seeland
                                wrote on last edited by
                                #43

                                I am quite sure I missed somthing about the concept of contracts: Violation of contracts cause exceptions, exactly like violation of using code which is not designed to work with values without the corresponding contract with the drawback of splitting location of need of assumption. What are your reasons for using contracts?

                                M 1 Reply Last reply
                                0
                                • S stefan seeland

                                  I am quite sure I missed somthing about the concept of contracts: Violation of contracts cause exceptions, exactly like violation of using code which is not designed to work with values without the corresponding contract with the drawback of splitting location of need of assumption. What are your reasons for using contracts?

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

                                  stefan seeland wrote:

                                  Violation of contracts cause exceptions, exactly like violation of using code which is not designed to work with values without the corresponding contract with the drawback of splitting location of need of assumption.
                                   
                                  What are your reasons for using contracts?

                                  Well, let's say you have a function that takes two numbers, persists them somewhere (maybe updating its own class' field values) and then returns the division result: double Divider(double n, double d) { Persist(n, d); return n/d; } The difference, with testing the parameter values first, is that you avoid the issue that something in the object's state (or some other system's state) has changed. Another example - if you could check every SQL transaction before executing it, then there wouldn't be any need for transactions and their accompanying rollbacks. 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
                                  • L lewax00

                                    Thanks, the article looks pretty in depth (only skimmed it for now, but it's bookmarked for later) :thumbsup:

                                    S Offline
                                    S Offline
                                    Sentenryu
                                    wrote on last edited by
                                    #45

                                    lewax00 wrote:

                                    bookmarked for later

                                    And, again, "later" will never come :-O

                                    I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p)

                                    L 1 Reply Last reply
                                    0
                                    • S Sentenryu

                                      lewax00 wrote:

                                      bookmarked for later

                                      And, again, "later" will never come :-O

                                      I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p)

                                      L Offline
                                      L Offline
                                      lewax00
                                      wrote on last edited by
                                      #46

                                      It will, next time I work on a specific product, I'm always looking for ways to make the code for it cleaner since I've written all of it so far and it all reflects on my ability. (But normally you'd be right ;P )

                                      S 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

                                        S Offline
                                        S Offline
                                        Sentenryu
                                        wrote on last edited by
                                        #47

                                        Judah Himango wrote:

                                        guaranteed non-null value and cannot be reassigned due to the readonly modifier

                                        FALSE. http://stackoverflow.com/questions/7876333/modify-private-readonly-member-variable[^] and as i'm a CPian: Internals of Constants and Readonly[^]

                                        Quote:

                                        Please note that you can't declare it in any of the methods or ctors because, throughout your class instance life time, the readonly variable should be known and so its value. Hence it is declared in class scope and defined there itself or at object construction AKA ctor. But you can bypass this rule via reflection.

                                        What if someone is crazy enogh to do this?

                                        I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p)

                                        J 1 Reply Last reply
                                        0
                                        • S Sentenryu

                                          Judah Himango wrote:

                                          guaranteed non-null value and cannot be reassigned due to the readonly modifier

                                          FALSE. http://stackoverflow.com/questions/7876333/modify-private-readonly-member-variable[^] and as i'm a CPian: Internals of Constants and Readonly[^]

                                          Quote:

                                          Please note that you can't declare it in any of the methods or ctors because, throughout your class instance life time, the readonly variable should be known and so its value. Hence it is declared in class scope and defined there itself or at object construction AKA ctor. But you can bypass this rule via reflection.

                                          What if someone is crazy enogh to do this?

                                          I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p)

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

                                          You're being pedantic. Of course you can crack open the hood and fiddle with the members via reflection, but you could break all sorts of contracts that way, particularly invariants. In the same vein, you can use reflection to modify strings, which are supposedly immutable! Imagine all the havoc you could wreak... But that's not really helpful. In practice, readonlys are readonly. :)

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

                                          S 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