Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can someone explain to me exactly what the ASSERT is all about?

C#
Example
 // First example demonstrating
// CString::Find ( TCHAR ch )
CString s( "abcdef" );
ASSERT( s.Find( 'c' ) == 2 );
ASSERT( s.Find( "de" ) == 3 );



Thanks
-DrB
Posted
Comments
Albert Holguin 23-Dec-11 5:40am    
First thing you need to understand, ASSERT is not a CString member, it's a macro. So if you want to understand what it's doing, you probably need to understand macros first.

the ASSERT will test the argument and report if the argument is false.

In your particular example (the msdn example of CString::Find), the ASSERT will be triggered if the result of CString::Find is not what is expected.

in the first instance, the result is and must be 2 ( c is at position 2 (zero based) in the string; if the result is something else, then the ASSERT will be triggered and a warning will be posted to the user.

It should be noted that ASSERTs will only be executed in DEBUG mode, meaning that the code in the argument will not be executed; most of the time it is not a big issue because we test the RESULT of the expression, not the expression itself :

C++
CString s( "abcdef" );
bool result = s.Find( 'c' ) == 2;
ASSERT(result);
 
Share this answer
 
Comments
LaxmikantYadav 22-Dec-11 23:59pm    
My +5
DrBones69 23-Dec-11 0:00am    
Why would I or anyone ever need this? I am researching how to find substrings in a CString and came across this.
Sergey Alexandrovich Kryukov 23-Dec-11 0:26am    
Not obvious? Well, I tried to explain it in my answer, please see.
--SA
Sergey Alexandrovich Kryukov 23-Dec-11 0:27am    
My 5, but as OP did not get it all, I added my answer, please see.
--SA
DrBones69 23-Dec-11 19:31pm    
Who is OP?
Thanks, I'll read your answer below.
DrBones69 wrote:

Why would I or anyone ever need this? I am researching how to find substrings in a CString and came across this.


Assertion is a widely used fundamental concept of computer science and practical development methods.

See:
http://en.wikipedia.org/wiki/Assertion_(computing)[^]
http://en.wikipedia.org/wiki/Assertion_%28computing%29#Usage[^], as well as the articles references at the end of this page.

I clearly remember the time when I first came across this concept in C++ and immediately saw the importance of this little tools. If someone doubt its value, this looks so unusual, so it looks difficult to explain "why". Even though it is explained in detail in the article referenced above, please pay attention at the section "Assertions during the development cycle". It is important to understand that the usual use of assertions is related to the support of the project, not to the functionality of its release. Most common practice is to make assertions compiled conditionally, so the are skipped in release configuration. In this case, the role of assertions is to fail run-time test in debug configuration, when the condition of the assertion is not held.

—SA
 
Share this answer
 
Comments
Chuck O'Toole 23-Dec-11 7:55am    
I read the article and it does make it clear that ASSERT is useful in debugging but it also states that ASSERT is not a substitute for good error handling. Too often, people write ASSERT statements *instead* of handling the error properly. For example, doing an "ASSERT (ptr != NULL)" after a malloc() statement will allow you to proceed with a null pointer in RELEASE mode so you will still need an intelligent error process to deal with the fact that you ran out of memory. Too many (bad) programmers rely on DEBUGGING tools to find things and leave out the RELEASE mode checks for conditions. For this reason, I do not like ASSERT. I use my own Macro that is an ASSERT in DEBUG and a call to Throw an Exception in RELEASE. That way, the logic is always tested and the code does not plod onwards with bad values in RELEASE mode.
Sergey Alexandrovich Kryukov 23-Dec-11 19:49pm    
Very good points. It might be good if you could write at least short Tips & Tricks article on your method and your macro.
Thank you.
--SA
DrBones69 23-Dec-11 19:37pm    
Thanks for the informative articles.
My 5!
Sergey Alexandrovich Kryukov 23-Dec-11 19:49pm    
You are welcome.
Good luck,
--SA
 
Share this answer
 
v2
Comments
DrBones69 23-Dec-11 0:01am    
Thanks, I read this, but I don't see any need for it. What is a good use for it?
Sergey Alexandrovich Kryukov 23-Dec-11 0:26am    
Not obvious? Well, I tried to explain it in my answer, please see.
--SA
Sergey Alexandrovich Kryukov 23-Dec-11 0:30am    
I cannot agree with the vote of 3. What OP needs is to read this help page. So I counter-voted with my 5.
Also, I've replaced your link by a later one, would you mind?
I also added the explanation in my answer in response to OP's question, please see.
--SA
It is quite common in documentation that includes examples of a functions usage to use ASSERT in the example in leiu of using words.

For example, your post:

C#
Example
 // First example demonstrating
// CString::Find ( TCHAR ch )
CString s( "abcdef" );
ASSERT( s.Find( 'c' ) == 2 );
ASSERT( s.Find( "de" ) == 3 );


Is really saying:

C#
Example
 // First example demonstrating
// CString::Find ( TCHAR ch )
CString s( "abcdef" );
//
// s.Find( 'c' ); will return the value 2 
// s.Find( "de" ); will return the value 3


Microsoft does this all the time. It's just showing the specific results of a specific usage with known inputs.
 
Share this answer
 
Comments
RaviRanjanKr 23-Dec-11 15:37pm    
5+

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900