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. Assert

Assert

Scheduled Pinned Locked Moved The Lounge
question
31 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.
  • V Offline
    V Offline
    VAIO Blue
    wrote on last edited by
    #1

    I often see code like this. void foo(int i = -1) { Assert(i != -1); if(i == -1) throw; .... work with i ... } What is the point of using assert in this case?

    L C R T C 6 Replies Last reply
    0
    • V VAIO Blue

      I often see code like this. void foo(int i = -1) { Assert(i != -1); if(i == -1) throw; .... work with i ... } What is the point of using assert in this case?

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      VAIO Blue wrote:

      What is the point of using assert in this case?

      Stupid code, the Assert checks an invalid condition, yet the 'invalid' condition is handled. Find the author, ask him to find his mistake, if he does not, shoot him.

      **

      xacc.ide-0.2.0.57 - now with C# 2.0 parser and seamless VS2005 solution support!

      **

      N J R 3 Replies Last reply
      0
      • L leppie

        VAIO Blue wrote:

        What is the point of using assert in this case?

        Stupid code, the Assert checks an invalid condition, yet the 'invalid' condition is handled. Find the author, ask him to find his mistake, if he does not, shoot him.

        **

        xacc.ide-0.2.0.57 - now with C# 2.0 parser and seamless VS2005 solution support!

        **

        N Offline
        N Offline
        Nish Nishant
        wrote on last edited by
        #3

        leppie wrote:

        Stupid code, the Assert checks an invalid condition, yet the 'invalid' condition is handled. Find the author, ask him to find his mistake, if he does not, shoot him.

        In a release build, since the Assert gets ignored, the exception acts as a backup error-check perhaps?

        Regards, Nish


        Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
        Currently working on C++/CLI in Action for Manning Publications. Also visit the Ultimate Toolbox blog (New)

        L V 2 Replies Last reply
        0
        • N Nish Nishant

          leppie wrote:

          Stupid code, the Assert checks an invalid condition, yet the 'invalid' condition is handled. Find the author, ask him to find his mistake, if he does not, shoot him.

          In a release build, since the Assert gets ignored, the exception acts as a backup error-check perhaps?

          Regards, Nish


          Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
          Currently working on C++/CLI in Action for Manning Publications. Also visit the Ultimate Toolbox blog (New)

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          How can it be an exception (to the rule) if it's an invalid state. Assert = never, Exception = in exceptional circumstances. OT: The server gives an 500 error everytime I try to submit the 1st reply. Going back and resumitting works though. :~

          **

          xacc.ide-0.2.0.57 - now with C# 2.0 parser and seamless VS2005 solution support!

          **

          N K 2 Replies Last reply
          0
          • N Nish Nishant

            leppie wrote:

            Stupid code, the Assert checks an invalid condition, yet the 'invalid' condition is handled. Find the author, ask him to find his mistake, if he does not, shoot him.

            In a release build, since the Assert gets ignored, the exception acts as a backup error-check perhaps?

            Regards, Nish


            Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
            Currently working on C++/CLI in Action for Manning Publications. Also visit the Ultimate Toolbox blog (New)

            V Offline
            V Offline
            VAIO Blue
            wrote on last edited by
            #5

            >> In a release build, since the Assert gets ignored, the exception acts as a backup error-check perhaps? This is what I don't understand, if a condition must be met or else the program will fail, then why use Assert instead of throwing exceptions? Generally, error checking should be included in both debug and release build. When you assert something, you know there is a possibility that the condition might fail, but then if you assert the condition, it is only checked in debug build, which implies that such checking should be ignored in release build. But if such check can be ignored in release build, why checking it in a debug build then?

            K 1 Reply Last reply
            0
            • L leppie

              How can it be an exception (to the rule) if it's an invalid state. Assert = never, Exception = in exceptional circumstances. OT: The server gives an 500 error everytime I try to submit the 1st reply. Going back and resumitting works though. :~

              **

              xacc.ide-0.2.0.57 - now with C# 2.0 parser and seamless VS2005 solution support!

              **

              N Offline
              N Offline
              Nish Nishant
              wrote on last edited by
              #6

              leppie wrote:

              How can it be an exception (to the rule) if it's an invalid state. Assert = never, Exception = in exceptional circumstances.

              I am not justifying the code, I was merely trying to figure out the coder's line of thinking. I think, what happened is this. There was this method that didn't take any args. Later, an overload was added that took an arg. Now they wanted to trace out all calls to the method that used the arg-less version. So they set the default value to -1, and assert on -1. The coder (who was probably inexperienced) may not have been sure if that was good enough, and meaninglessly added an exception block too. That's my inference on what may have happened.

              Regards, Nish


              Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
              Currently working on C++/CLI in Action for Manning Publications. Also visit the Ultimate Toolbox blog (New)

              R 1 Reply Last reply
              0
              • V VAIO Blue

                I often see code like this. void foo(int i = -1) { Assert(i != -1); if(i == -1) throw; .... work with i ... } What is the point of using assert in this case?

                C Offline
                C Offline
                Chris Maunder
                wrote on last edited by
                #7

                It depends on what exceptions are thrown. You could have code higher up the call stack that handles the explicitely thrown exception type gracefully, and have the exception generated by the Assert statement left to propogate to the top. That way while debugging you're assured of seeing each and every Assert, but in release mode the code will continue to run fine. Then you get into the whole "exceptions vs return values" argument.

                cheers, Chris Maunder

                CodeProject.com : C++ MVP

                FIX: A MFC program created in Visual Studio .NET 2003 unexpectedly quits when you try to close it[^]

                P C 2 Replies Last reply
                0
                • V VAIO Blue

                  I often see code like this. void foo(int i = -1) { Assert(i != -1); if(i == -1) throw; .... work with i ... } What is the point of using assert in this case?

                  R Offline
                  R Offline
                  Rama Krishna Vavilala
                  wrote on last edited by
                  #8

                  This is for making the code robust. It is an extreely good practice for large scale software development. If you look at ATL and also MFC source code this pattern is used everywhere. You will also find code like this in rotor and as far as I think in the .Net framework BCL also. Steve McConnell author of Code Complete suggests the use of both assert and error handling (exceptions in this case). He cites the example of Microsoft Word.

                  In the source code for Microsoft Word, for example, conditions that should always be true are asserted, but such errors are also handled by error-handling code in case the assertion fails. For extremely, large complex applications like Word, assertions are valuable because they help to flush out as many development-time errors as possible. But the application is so complex (millions of lines of code) and has gone so many generations of modification that it isn't realistic that every conceivable error will be detected and corrected before the software ships, and so errors must be handled in the production version of the system as well.

                  The other advantage of Assert is that you can directly stop in the debugger. Assert -> Figure problems in design time. Exception -> Handle any errors that may have been missed. In this case if i is -1 it can cause problems in the code. So you have to throw an exception.


                  Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan

                  N S C 3 Replies Last reply
                  0
                  • C Chris Maunder

                    It depends on what exceptions are thrown. You could have code higher up the call stack that handles the explicitely thrown exception type gracefully, and have the exception generated by the Assert statement left to propogate to the top. That way while debugging you're assured of seeing each and every Assert, but in release mode the code will continue to run fine. Then you get into the whole "exceptions vs return values" argument.

                    cheers, Chris Maunder

                    CodeProject.com : C++ MVP

                    FIX: A MFC program created in Visual Studio .NET 2003 unexpectedly quits when you try to close it[^]

                    P Offline
                    P Offline
                    Paul Watson
                    wrote on last edited by
                    #9

                    Chris Maunder wrote:

                    Then you get into the whole "exceptions vs return values" argument.

                    At which point I'd rather start discussing the existential meaning of Santa Claus and the Easter Bunny.

                    regards, Paul Watson Ireland FeedHenry needs you

                    Shog9 wrote:

                    eh, stop bugging me about it, give it a couple of days, see what happens.

                    1 Reply Last reply
                    0
                    • N Nish Nishant

                      leppie wrote:

                      How can it be an exception (to the rule) if it's an invalid state. Assert = never, Exception = in exceptional circumstances.

                      I am not justifying the code, I was merely trying to figure out the coder's line of thinking. I think, what happened is this. There was this method that didn't take any args. Later, an overload was added that took an arg. Now they wanted to trace out all calls to the method that used the arg-less version. So they set the default value to -1, and assert on -1. The coder (who was probably inexperienced) may not have been sure if that was good enough, and meaninglessly added an exception block too. That's my inference on what may have happened.

                      Regards, Nish


                      Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
                      Currently working on C++/CLI in Action for Manning Publications. Also visit the Ultimate Toolbox blog (New)

                      R Offline
                      R Offline
                      Rama Krishna Vavilala
                      wrote on last edited by
                      #10

                      Nishant Sivakumar wrote:

                      The coder (who was probably inexperienced)

                      In my opinion the coder is quite experienced to write such robust code. Ever looked at source code of MFC/ATL you will find such pattern everywhere. Assert and then Error Handling (in this case using exceptions and in case of MFC/ATL using return values).


                      Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan

                      N 1 Reply Last reply
                      0
                      • V VAIO Blue

                        I often see code like this. void foo(int i = -1) { Assert(i != -1); if(i == -1) throw; .... work with i ... } What is the point of using assert in this case?

                        T Offline
                        T Offline
                        Taka Muraoka
                        wrote on last edited by
                        #11

                        I often do things like this. Firstly, the assert will only fire for debug builds i.e. usually when I'm in my IDE. The program will stop at where the problem was detected, not just keep on going. I use VC6 and I don't think it has a "break on exception" feature like VS.NET but even if it did, I wouldn't want to use it. By the time the exception has been caught, you've lost your stack frame and it's generally too late to know what caused the problem. Secondly, I use my own souped-up assert macro that logs a lot of additional information that won't be available in a catch handler (e.g. local variables).


                        0 bottles of beer on the wall, 0 bottles of beer, you take 1 down, pass it around, 4294967295 bottles of beer on the wall. Awasu 2.2.3 [^]: A free RSS/Atom feed reader with support for Code Project.

                        1 Reply Last reply
                        0
                        • R Rama Krishna Vavilala

                          This is for making the code robust. It is an extreely good practice for large scale software development. If you look at ATL and also MFC source code this pattern is used everywhere. You will also find code like this in rotor and as far as I think in the .Net framework BCL also. Steve McConnell author of Code Complete suggests the use of both assert and error handling (exceptions in this case). He cites the example of Microsoft Word.

                          In the source code for Microsoft Word, for example, conditions that should always be true are asserted, but such errors are also handled by error-handling code in case the assertion fails. For extremely, large complex applications like Word, assertions are valuable because they help to flush out as many development-time errors as possible. But the application is so complex (millions of lines of code) and has gone so many generations of modification that it isn't realistic that every conceivable error will be detected and corrected before the software ships, and so errors must be handled in the production version of the system as well.

                          The other advantage of Assert is that you can directly stop in the debugger. Assert -> Figure problems in design time. Exception -> Handle any errors that may have been missed. In this case if i is -1 it can cause problems in the code. So you have to throw an exception.


                          Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan

                          N Offline
                          N Offline
                          Nish Nishant
                          wrote on last edited by
                          #12

                          Good post - 5 :-)

                          Regards, Nish


                          Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
                          Currently working on C++/CLI in Action for Manning Publications. Also visit the Ultimate Toolbox blog (New)

                          C 1 Reply Last reply
                          0
                          • R Rama Krishna Vavilala

                            Nishant Sivakumar wrote:

                            The coder (who was probably inexperienced)

                            In my opinion the coder is quite experienced to write such robust code. Ever looked at source code of MFC/ATL you will find such pattern everywhere. Assert and then Error Handling (in this case using exceptions and in case of MFC/ATL using return values).


                            Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan

                            N Offline
                            N Offline
                            Nish Nishant
                            wrote on last edited by
                            #13

                            Rama Krishna Vavilala wrote:

                            Ever looked at source code of MFC/ATL you will find such pattern everywhere. Assert and then Error Handling (in this case using exceptions and in case of MFC/ATL using return values).

                            Yep, I've seen that pattern before.

                            Regards, Nish


                            Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
                            Currently working on C++/CLI in Action for Manning Publications. Also visit the Ultimate Toolbox blog (New)

                            1 Reply Last reply
                            0
                            • R Rama Krishna Vavilala

                              This is for making the code robust. It is an extreely good practice for large scale software development. If you look at ATL and also MFC source code this pattern is used everywhere. You will also find code like this in rotor and as far as I think in the .Net framework BCL also. Steve McConnell author of Code Complete suggests the use of both assert and error handling (exceptions in this case). He cites the example of Microsoft Word.

                              In the source code for Microsoft Word, for example, conditions that should always be true are asserted, but such errors are also handled by error-handling code in case the assertion fails. For extremely, large complex applications like Word, assertions are valuable because they help to flush out as many development-time errors as possible. But the application is so complex (millions of lines of code) and has gone so many generations of modification that it isn't realistic that every conceivable error will be detected and corrected before the software ships, and so errors must be handled in the production version of the system as well.

                              The other advantage of Assert is that you can directly stop in the debugger. Assert -> Figure problems in design time. Exception -> Handle any errors that may have been missed. In this case if i is -1 it can cause problems in the code. So you have to throw an exception.


                              Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan

                              S Offline
                              S Offline
                              Shog9 0
                              wrote on last edited by
                              #14

                              Good post, Rama. :)

                              ---- Scripts i’ve known... CPhog 1.8.2 - make CP better. Forum Bookmark 0.2.5 - bookmark forum posts on Pensieve Print forum 0.1.2 - printer-friendly forums Expand all 1.0 - Expand all messages In-place Delete 1.0 - AJAX-style post delete Syntax 0.1 - Syntax highlighting for code blocks in the forums

                              N 1 Reply Last reply
                              0
                              • S Shog9 0

                                Good post, Rama. :)

                                ---- Scripts i’ve known... CPhog 1.8.2 - make CP better. Forum Bookmark 0.2.5 - bookmark forum posts on Pensieve Print forum 0.1.2 - printer-friendly forums Expand all 1.0 - Expand all messages In-place Delete 1.0 - AJAX-style post delete Syntax 0.1 - Syntax highlighting for code blocks in the forums

                                N Offline
                                N Offline
                                Nish Nishant
                                wrote on last edited by
                                #15

                                Shog9 wrote:

                                Good post, Rama.

                                Damnit Shog, I said that first!

                                Regards, Nish


                                Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
                                Currently working on C++/CLI in Action for Manning Publications. Also visit the Ultimate Toolbox blog (New)

                                S C 2 Replies Last reply
                                0
                                • L leppie

                                  How can it be an exception (to the rule) if it's an invalid state. Assert = never, Exception = in exceptional circumstances. OT: The server gives an 500 error everytime I try to submit the 1st reply. Going back and resumitting works though. :~

                                  **

                                  xacc.ide-0.2.0.57 - now with C# 2.0 parser and seamless VS2005 solution support!

                                  **

                                  K Offline
                                  K Offline
                                  Kevin McFarlane
                                  wrote on last edited by
                                  #16

                                  Assertions can validly be configured to generate exceptions. The exception is then an indication of a bug. For example, see Standard C++'s logic_error class. http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/classstd_1_1logic__error.html[^] It is still "exceptional" because bugs should be exceptional!

                                  Kevin

                                  1 Reply Last reply
                                  0
                                  • N Nish Nishant

                                    Shog9 wrote:

                                    Good post, Rama.

                                    Damnit Shog, I said that first!

                                    Regards, Nish


                                    Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
                                    Currently working on C++/CLI in Action for Manning Publications. Also visit the Ultimate Toolbox blog (New)

                                    S Offline
                                    S Offline
                                    Shog9 0
                                    wrote on last edited by
                                    #17

                                    Yeah, but you called him "5". 5's cool and all, but i figured Rama should get credit for the post.

                                    ---- Scripts i’ve known... CPhog 1.8.2 - make CP better. Forum Bookmark 0.2.5 - bookmark forum posts on Pensieve Print forum 0.1.2 - printer-friendly forums Expand all 1.0 - Expand all messages In-place Delete 1.0 - AJAX-style post delete Syntax 0.1 - Syntax highlighting for code blocks in the forums

                                    N 1 Reply Last reply
                                    0
                                    • S Shog9 0

                                      Yeah, but you called him "5". 5's cool and all, but i figured Rama should get credit for the post.

                                      ---- Scripts i’ve known... CPhog 1.8.2 - make CP better. Forum Bookmark 0.2.5 - bookmark forum posts on Pensieve Print forum 0.1.2 - printer-friendly forums Expand all 1.0 - Expand all messages In-place Delete 1.0 - AJAX-style post delete Syntax 0.1 - Syntax highlighting for code blocks in the forums

                                      N Offline
                                      N Offline
                                      Nish Nishant
                                      wrote on last edited by
                                      #18

                                      Shog9 wrote:

                                      Yeah, but you called him "5". 5's cool and all, but i figured Rama should get credit for the post.

                                      Blast!!!

                                      Regards, Nish


                                      Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
                                      Currently working on C++/CLI in Action for Manning Publications. Also visit the Ultimate Toolbox blog (New)

                                      1 Reply Last reply
                                      0
                                      • V VAIO Blue

                                        >> In a release build, since the Assert gets ignored, the exception acts as a backup error-check perhaps? This is what I don't understand, if a condition must be met or else the program will fail, then why use Assert instead of throwing exceptions? Generally, error checking should be included in both debug and release build. When you assert something, you know there is a possibility that the condition might fail, but then if you assert the condition, it is only checked in debug build, which implies that such checking should be ignored in release build. But if such check can be ignored in release build, why checking it in a debug build then?

                                        K Offline
                                        K Offline
                                        Kevin McFarlane
                                        wrote on last edited by
                                        #19

                                        VAIO Blue wrote:

                                        if a condition must be met or else the program will fail, then why use Assert instead of throwing exceptions?

                                        An assertion represents a violation of a method's contract or is indicative of a logic error. By using an assertion we convey our intent. We can then distinguish between what is a logic error and what is a more general exception.

                                        VAIO Blue wrote:

                                        Generally, error checking should be included in both debug and release build.

                                        Correct.

                                        VAIO Blue wrote:

                                        When you assert something, you know there is a possibility that the condition might fail, but then if you assert the condition, it is only checked in debug build, which implies that such checking should be ignored in release build.

                                        We often set up things this way but it need not be so. Try looking up the theory of Design by Contract. The reason we often set up things this way is that we aim to have fully debugged the software during testing, in which case the assertions are valuable. When we ae confident we then remove them during release to improve performance (but leave in error handling that is not to do with logic violations, i.e., external or user errors). But in real applications there can be a degree of selectivity in how we do this.

                                        VAIO Blue wrote:

                                        But if such check can be ignored in release build, why checking it in a debug build then?

                                        Because they help us to debug the application as we're developing it. They also aid the maintenance programmer in producing only valid consumptions of existing code.

                                        Kevin

                                        1 Reply Last reply
                                        0
                                        • L leppie

                                          VAIO Blue wrote:

                                          What is the point of using assert in this case?

                                          Stupid code, the Assert checks an invalid condition, yet the 'invalid' condition is handled. Find the author, ask him to find his mistake, if he does not, shoot him.

                                          **

                                          xacc.ide-0.2.0.57 - now with C# 2.0 parser and seamless VS2005 solution support!

                                          **

                                          J Offline
                                          J Offline
                                          Joe Woodbury
                                          wrote on last edited by
                                          #20

                                          Quite to the contrary, this is extremely good practice. An assert will rarely fire. In debug builds, you want to know when they happen and resolve the issue. In the rare chance they happen in production code, you want some sort of graceful recovery rather than a fault.

                                          Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

                                          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