Hello,
I have a public function that needs to check the validity of a few parameters, but this function is also used internally in a few others places in which I know that the given parameters will be valid. Thus, I'm wondering, which is more costly: 1) Leave the validity checks in every public function, or 2) Do the validity checks in the public functions, but create private/internal functions that do not do that checks (use the public functions as wrappers for the internal functions).
Does the overhead of the function call out-weigh the benefit of not having to perform the redundant checks?
Basically, my current code would be along the following lines:
boolean foo(int* a, int* b, int* c)
{
if (!a || !b || !c) {
return FALSE;
}
<do the whatever foo does here>
}
boolean bar(int* a, int* b, int* c)
{
if (!a || !b || !c) {
return FALSE;
}
<do the whatever bar does here>
foo(a, b, c);
}
Thus, bar() does checks on a, b, and c because it is an exposed/public function, but then foo repeates the same checks. Would it be more efficient to allow this to occur? or to create an internal function along the lines of the following:
static boolean foo_internal(int* a, int* b, int* c)
{
<do the whatever foo does here>
}
boolean foo(int* a, int* b, int* c)
{
if (!a || !b || !c) {
return FALSE;
}
foo_internal();
}
boolean bar(int* a, int* b, int* c)
{
if (!a || !b || !c) {
return FALSE;
}
<do the whatever bar does here>
foo_internal(a, b, c);
}
I'm primarily concerned with the efficiency in regards to C code, but general answers/answers specific to other languages are helpful as well.
Thanks!