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. Defensive programming - Assertions

Defensive programming - Assertions

Scheduled Pinned Locked Moved The Lounge
csharphtmlcsscomtesting
19 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.
  • N N a v a n e e t h

    I am just wondering, do you guys use Debug.Assert or some kind of assertions when developing in .NET? I have not seen any .NET projects using it so far. Many articles/books saying it as a good practice and part of defensive programming. I think, the code will be mixed with all these assertions and will become less readable. I prefer to keep the assertions separate from the original code (something like unit test cases). Any thoughts?

    Navaneeth How to use google | Ask smart questions

    R Offline
    R Offline
    R Giskard Reventlov
    wrote on last edited by
    #9

    I tend to use Design-By-Contract[^]. There are a couple of good articles here, such as this one. I do agree, however, that nothing much beats unit testing: eat your own dog food!

    me, me, me

    1 Reply Last reply
    0
    • M moon_stick

      I use both assertions and unit tests in my code; the units tests are there to test that the behaviour of the function is correct whereas the assertions are used to ensure that supposedly unreachable situations aren't reached, i.e. a non-zero integer passed into a function etc could indicate a problem in the calling routine etc. One of the developers at a company I used to work for liked the idea of using assertions but had the unfortunate habit of writing code like: #if debug Debug.Assert(LoadData()); #endif which then produced spurious results when the build was changed to 'release'. Because of this, I now tend to routinely remove assertions once I've finished writing a function / library before running my unit tests again.

      It definitely isn't definatley

      S Offline
      S Offline
      Simon Capewell
      wrote on last edited by
      #10

      Ah, a classic gotcha. Reminds me of a interview question where they asked when it was appropriate to use ASSERT as opposed to VERIFY. I used asserts lots in C++, it seemed to be the convention in MFC to assert on NULL hWnds and the like, but in .NET code they've tended to get left out. BTW, are you aware that the Assert method is marked with [Conditional("DEBUG")] so the #if/#endif is unnecessary?

      M 1 Reply Last reply
      0
      • S Simon Capewell

        Ah, a classic gotcha. Reminds me of a interview question where they asked when it was appropriate to use ASSERT as opposed to VERIFY. I used asserts lots in C++, it seemed to be the convention in MFC to assert on NULL hWnds and the like, but in .NET code they've tended to get left out. BTW, are you aware that the Assert method is marked with [Conditional("DEBUG")] so the #if/#endif is unnecessary?

        M Offline
        M Offline
        moon_stick
        wrote on last edited by
        #11

        Simon Capewell wrote:

        BTW, are you aware that the Assert method is marked with [Conditional("DEBUG")] so the #if/#endif is unnecessary?

        I was just being explicit about my intentions. Obviously.... ;)

        It definitely isn't definatley

        S 1 Reply Last reply
        0
        • M moon_stick

          Simon Capewell wrote:

          BTW, are you aware that the Assert method is marked with [Conditional("DEBUG")] so the #if/#endif is unnecessary?

          I was just being explicit about my intentions. Obviously.... ;)

          It definitely isn't definatley

          S Offline
          S Offline
          Simon Capewell
          wrote on last edited by
          #12

          Obviously. Double the conditional compilation, double the defensiveness ;)

          1 Reply Last reply
          0
          • S Simon P Stevens

            Thomas Weller wrote:

            if (this argument is crap) { throw new ArgumentException("no crap please!"); }

            I have a static 'ArgumentHelper' class in a library assembly that has methods like 'CheckForNull' and 'CheckMinValue', that do the checks and throw the exceptions. It improves the readableness of the checking code at the start of methods

            ArgumentHelper.CheckForNull(arguement1);
            ArgumentHelper.CheckMinValue(arguement2, 0);

            Simon

            S Offline
            S Offline
            Steve Hansen
            wrote on last edited by
            #13

            Don't forget a string parameter for the parameter name, that way you'll know which argument was incorrect ;) My helper is a:

            Argument.NotNull("parameter", parameter);

            S 1 Reply Last reply
            0
            • S Steve Hansen

              Don't forget a string parameter for the parameter name, that way you'll know which argument was incorrect ;) My helper is a:

              Argument.NotNull("parameter", parameter);

              S Offline
              S Offline
              Simon P Stevens
              wrote on last edited by
              #14

              Funny, I do actually pass the name of the parameter in as a string. I just left it out to simplify the example. This is obviously quite a common pattern. Mine is actually the other way round. Parameter first, then string name. I wrote snippets for the ones I use most commonly, so all I type is "exnull" and hit tab twice and it expands out. That way I only have to type the argument name once and it auto fills both parameters. I found that if I passed the string first I had to type it in full with no intellisense, but if I passed the argument object first I could type it and get intellisense help, and then the string value would be auto filled. (All to save a few extra keystrokes :laugh: )

              Simon

              S 1 Reply Last reply
              0
              • S Simon P Stevens

                Funny, I do actually pass the name of the parameter in as a string. I just left it out to simplify the example. This is obviously quite a common pattern. Mine is actually the other way round. Parameter first, then string name. I wrote snippets for the ones I use most commonly, so all I type is "exnull" and hit tab twice and it expands out. That way I only have to type the argument name once and it auto fills both parameters. I found that if I passed the string first I had to type it in full with no intellisense, but if I passed the argument object first I could type it and get intellisense help, and then the string value would be auto filled. (All to save a few extra keystrokes :laugh: )

                Simon

                S Offline
                S Offline
                Steve Hansen
                wrote on last edited by
                #15

                My shortcut is nn and resharper allows to fill out the second argument first so it still shows intellisense, plus its smart enough to use parameters so I have to type almost nothing :p

                1 Reply Last reply
                0
                • N N a v a n e e t h

                  I am just wondering, do you guys use Debug.Assert or some kind of assertions when developing in .NET? I have not seen any .NET projects using it so far. Many articles/books saying it as a good practice and part of defensive programming. I think, the code will be mixed with all these assertions and will become less readable. I prefer to keep the assertions separate from the original code (something like unit test cases). Any thoughts?

                  Navaneeth How to use google | Ask smart questions

                  D Offline
                  D Offline
                  Douglas Troy
                  wrote on last edited by
                  #16

                  Yes, I use them as well as Unit Tests, to help catch bugs/design problems.

                  1 Reply Last reply
                  0
                  • N N a v a n e e t h

                    I am just wondering, do you guys use Debug.Assert or some kind of assertions when developing in .NET? I have not seen any .NET projects using it so far. Many articles/books saying it as a good practice and part of defensive programming. I think, the code will be mixed with all these assertions and will become less readable. I prefer to keep the assertions separate from the original code (something like unit test cases). Any thoughts?

                    Navaneeth How to use google | Ask smart questions

                    S Offline
                    S Offline
                    Single Step Debugger
                    wrote on last edited by
                    #17

                    I’m using it in the IO operations.

                    The narrow specialist in the broad sense of the word is a complete idiot in the narrow sense of the word. Advertise here – minimum three posts per day are guaranteed.

                    1 Reply Last reply
                    0
                    • N N a v a n e e t h

                      I am just wondering, do you guys use Debug.Assert or some kind of assertions when developing in .NET? I have not seen any .NET projects using it so far. Many articles/books saying it as a good practice and part of defensive programming. I think, the code will be mixed with all these assertions and will become less readable. I prefer to keep the assertions separate from the original code (something like unit test cases). Any thoughts?

                      Navaneeth How to use google | Ask smart questions

                      H Offline
                      H Offline
                      Henry Minute
                      wrote on last edited by
                      #18

                      N a v a n e e t h wrote:

                      I am just wondering, do you guys use Debug.Assert

                      I may be being a little thick here but if you are suspicious enough of a section of code to put in a Debug.Assert, shouldn't you be putting it in a try/catch (or equivalent) block anyway? On this basis I have never been convinced of the need. Obviously a lot of you do use it and therefore I must have missed the point. Is there an article that any of you would recommend, which explains in words of one syllable, you know beginner level?

                      Henry Minute If you open a can of worms, any valid solution *MUST* involve a larger can!

                      1 Reply Last reply
                      0
                      • N N a v a n e e t h

                        I am just wondering, do you guys use Debug.Assert or some kind of assertions when developing in .NET? I have not seen any .NET projects using it so far. Many articles/books saying it as a good practice and part of defensive programming. I think, the code will be mixed with all these assertions and will become less readable. I prefer to keep the assertions separate from the original code (something like unit test cases). Any thoughts?

                        Navaneeth How to use google | Ask smart questions

                        M Offline
                        M Offline
                        Miszou
                        wrote on last edited by
                        #19

                        I used ASSERT and VERIFY all the time in C++, but since moving to C# I find that I hardly use them at all. It's probably due to the better exception handling and debugging abilities that have been added since Studio 6.0.

                        The StartPage Randomizer | The Windows Cheerleader | Twitter

                        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