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#

    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
              • L lewax00

                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 Offline
                S Offline
                Sentenryu
                wrote on last edited by
                #49

                i'll look at it now, i liked the idea, but this post came literally 5 minutes before i leave the work :laugh:

                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)

                1 Reply Last reply
                0
                • J Judah Gabriel Himango

                  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 Offline
                  S Offline
                  Sentenryu
                  wrote on last edited by
                  #50

                  sorry if i sounded offensive, i posted that while trying to shut off the pc to get home :laugh: i was just pointing out that you should never trust the integrity of your data, even the internal readonly one. unless you are the only developer and the software will be used only internally, you can't assume no one will try to modify your internal data. BUT, for library code, you should never use private readonly anyway, i've had to much trouble with the MVC 3 model binder error messages on unobtrusive validation to know that ;P

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

                    F Offline
                    F Offline
                    Fabio Franco
                    wrote on last edited by
                    #51

                    Unit testing is for the weak. I prefer Chuck Norris style :rolleyes:

                    To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson ---- Our heads are round so our thoughts can change direction - Francis Picabia

                    1 Reply Last reply
                    0
                    • S Sentenryu

                      sorry if i sounded offensive, i posted that while trying to shut off the pc to get home :laugh: i was just pointing out that you should never trust the integrity of your data, even the internal readonly one. unless you are the only developer and the software will be used only internally, you can't assume no one will try to modify your internal data. BUT, for library code, you should never use private readonly anyway, i've had to much trouble with the MVC 3 model binder error messages on unobtrusive validation to know that ;P

                      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
                      #52

                      You didn't sound offensive. Just pedantic.

                      Sentenryu wrote:

                      you should never trust the integrity of your data

                      Do you use strings? Do you sprinkle checks in your string code to verify no one used reflection to modify the strings? Of course not. Neither should we be concerned that readonlys are being modified through reflection.

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

                      S 1 Reply Last reply
                      0
                      • J Judah Gabriel Himango

                        You didn't sound offensive. Just pedantic.

                        Sentenryu wrote:

                        you should never trust the integrity of your data

                        Do you use strings? Do you sprinkle checks in your string code to verify no one used reflection to modify the strings? Of course not. Neither should we be concerned that readonlys are being modified through reflection.

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

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

                        Judah Himango wrote:

                        Do you use strings? Do you sprinkle checks in your string code to verify no one used reflection to modify the strings?

                        there's a way to modify strings parameters passed to a method? i don't know so much about reflection, so i'm not aware if you can get a local variable of a method using reflection, but i think there's no way (and i'm happy thinking this way ;) ) but i do check to se if the data i'm using meet the criterias of the method in question, unless there's no criteria. that includes data gathered through members of the class. sometimes i assume people will be kind enough to use the elephanting properties exposed by the class, but some people insist to remember me of horrors that appear when you don't do a sanity check on the data passed to you, like entire server going down because someone decided that would be fun to mess up with our authentication codes that were defined as private readonly string, better yet, you should see what the sunshine he put in place of the codes:

                        Quote:

                        My baby, baby, baby noo, My baby, baby, baby oo

                        the sunshine also deleted the TFS logs of the commit, and published this to production :mad: well, in a side note, sorry if i was pedantic, i have serious communication problems, and by text is even harder to say what i want in the tone i want :sigh:

                        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)

                        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