Hi,
is it a nice idea to do assetion checks for method arguments ? I
am currently redesigning a piece of code. Now the problem is that there
are some assumptions for the method arguments that the code makes. It
is not checking the validity of the arguments everywhere because that
would lead to performance penalty. So I am think that at least
assertions will be able check the arguments at least for debug version
to verify the design. But now it means that I put the assertions
everywhere in the code, something like below:
Code with out checks
func1(Ptr1 * ptr1, Ptr2 * ptr2)
{
ptr2->do something...
ptr1->call some method(ptr2);
}
Code with assertions
func1(Ptr1 * ptr1, Ptr2 * ptr2)
{
assert( ptr1!= NULL && ptr2 != NULL); // And do this for all
functions with arguments everywhere
ptr2->do something...
ptr1->call some method(ptr2);
}
so my questions really is : Is it a good idea to put the assertions for
the function arguments like shown above? Why or why not?
Thanks,
Divick 14 1720
Divick wrote: Hi, is it a nice idea to do assetion checks for method arguments ? I
YES.
.... }
Code with assertions func1(Ptr1 * ptr1, Ptr2 * ptr2) { assert( ptr1!= NULL && ptr2 != NULL); // And do this for all
assert( ptr1 )
assert( ptr2 )
Validate *each* argument rather than have one long expression.
NULL *is* 0 is C++. I don't use NULL any more (personal preference).
functions with arguments everywhere ptr2->do something... ptr1->call some method(ptr2); }
so my questions really is : Is it a good idea to put the assertions for the function arguments like shown above? Why or why not?
It's a good idea because:
a) It documents your assumptions and enforces them (at least in debug).
b) It catches errors early (which is always desirable).
* Divick: Hi, is it a nice idea to do assetion checks for method arguments ? I am currently redesigning a piece of code. Now the problem is that there are some assumptions for the method arguments that the code makes. It is not checking the validity of the arguments everywhere because that would lead to performance penalty. So I am think that at least assertions will be able check the arguments at least for debug version to verify the design. But now it means that I put the assertions everywhere in the code, something like below:
Code with out checks
func1(Ptr1 * ptr1, Ptr2 * ptr2) { ptr2->do something... ptr1->call some method(ptr2); }
Code with assertions func1(Ptr1 * ptr1, Ptr2 * ptr2) { assert( ptr1!= NULL && ptr2 != NULL); // And do this for all functions with arguments everywhere ptr2->do something... ptr1->call some method(ptr2); }
so my questions really is : Is it a good idea to put the assertions for the function arguments like shown above?
No.
Why or why not?
Use reference arguments where you don't intend to support NULL-values.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Divick wrote: Hi, is it a nice idea to do assetion checks for method arguments ? I am currently redesigning a piece of code. Now the problem is that there are some assumptions for the method arguments that the code makes. It is not checking the validity of the arguments everywhere because that would lead to performance penalty.
1st priority: Correctness.
2nd priority: Correctness!
Much lower priority: Performance.
Always validate the arguments, if there IS a performance penalty convert
the ones that matter to assertions.
If the input comes from the user, or a file or something, then
assertions are not really a suitable option. Validate those things ASAP
to contain the problem.
Ben Pope
--
I'm not just a number. To many, I'm known as a string...
>>Use reference arguments where you don't intend to support NULL-values.
Could you please give an example?
Seems like people have different opinions about this. Confused...:(
Divick
Hi Divick Use reference arguments where you don't intend to support NULL-values.
Could you please give an example?
Seems like people have different opinions about this. Confused...:(
I do not think many people have different opinions on this. :-)
Those who said that you should use assertions to make sure
your pointer parameters are not 0 were assuming you had a
reason for using pointers in the first place. What Alf said was
that you the question of whether to use assertions for this should
not be an issue if you do not need pointers here but can use
references.
In other words....
1. use assertions where needed.
2. use references instead of pointers wherever possible.
Just my two cents worth... and I hope it helps...
Best regards,
Jurko Gospodnetic
>>Those who said that you should use assertions to make sure your pointer parameters are not 0 were assuming you had a reason for using pointers in the first place. What Alf said was that you the question of whether to use assertions for this should not be an issue if you do not need pointers here but can use references.
Ok, that helps. Now I understand what Alf was trying to say.
Thanks a lot,
Divick
Divick wrote: Hi, is it a nice idea to do assetion checks for method arguments ?
Yes! Better safe than sorry.
If you are worried about performance (better support this
with an actual time-test, rather than thinking "this could
be too slow"), you might have two styles of asserts, like
Assert(...)
and
CostlyAssert(.. .)
that would be turned on/off independently with preprocessor
stuff like
#define WANT_ASSERTS
#define WANT_COSTLYASSE RTS
Then you could leave Asserts on for both debug and release
versions and turn CostlyAsserts only for debugging.
HTH,
- J.
Jacek Dziedzic wrote: Gianni Mariani wrote:
NULL *is* 0 is C++. I don't use NULL any more (personal preference).
Yes, but what about problems like that which was discussed recently:
http://groups.google.com/group/comp....93d210e3c2b5cb
That's a gem.
Yes, function overloading with templates and the special meaning of
literal 0 gets "overloaded ".
It's too late now, but I think the language rules around the 0 literal
and null pointers to now be a bad idea. The NULL macro provides no help
here from a readability perspective, so I think that's a bad idea as
well (IMHO). This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: P. Chalin |
last post by:
If you make use of assertions (e.g. assert statements) in your code, I
would like to invite you to participate in a 15 min survey on program
assertions. In this survey we seek your opinion concerning the issue
of errors/exceptions raised during assertion evaluation. The
questionnaire is available here:
https://www.dsrg.org/ASN_Survey/wnd.html
Thank-you in advance,
P.Chalin
|
by: Angel Tsankov |
last post by:
I need to make verifications in release builds. If a verification
fails, an error message should be displayed and the program should be
aborted. I need this solution to be portable. Then I thought of the
assert macro - it provides the desired functionality but only if
NDEBUG is not defined. So, I could write smth like this:
#if defined NDEBUG
#undef NDEBUG
#include <cassert>
#define NDEBUG
|
by: Jefffff |
last post by:
Regarding the use of assertions:
According to Juval Lowy's excellent coding standards document (PDF download
available at www.idesign.net), every assumption in your code should have an
assertion... and on average, every 5th line is an assertion.
My question:
Do any of you include that many assertions? I agree in principle that we
should code defensively, but his recommendation just seems to go "overboard"
and would result in a lot of...
|
by: douglass_davis |
last post by:
Say I would like to use assertions to make sure correct inputs are
given to a procedure. But, I want to do this in testing only, not in
production.
I saw Debug.Assert, which is nice, but does VB.NET have a feature where
you can turn off assertions on production code?
--
http://www.douglass.com
|
by: Matthew Wilson |
last post by:
Lately, I've been writing functions like this:
def f(a, b):
assert a in
assert b in
The point is that I'm checking the type and the values of the
parameters.
| |
by: jeffc226 |
last post by:
This might be less of a design issue than a C++ language issue per se,
but I have a problem with assertions. I mean, they work, of course.
But there's something I'm uncomfortable with that's never been
explained to my satisfaction.
I recently read this explanation. "There is an effective litmus test
to differentiate the cases in which you need to use assert and when
you need to use genuine error checking: you use error checking for...
|
by: Duncan Smith |
last post by:
Last week, I got reports back of errors in a patch I let someone try
out (release dll, but not from the build machine). Turns out it was
some Debug::Assert statements firing.
In umanaged code, I'd always understood that assertions were only
fired in debug builds and would compile down to NOPs in release builds
(for good reasons)
Now it looks as though I have to be careful to code assertions
conditionally?
|
by: Hugh Oxford |
last post by:
I am told that I should use assertions in my testing. The problem is,
because I test using a cut of live data, the data always changes. Has
anyone come up against this or have they abandoned assertions generally?
On a more general note, how does one reconcile a changing data set with
testing driven development?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |