I
istillshine
I have a function foo, shown below. Is it a good idea to test each
argument against my assumption? I think it is safer. However, I
notice that people usually don't test the validity of arguments.
For example,
#define MAX_SIZE 100000
int foo(double *x, unsigned ling sz, int option)
{
int val;
if (x == NULL) { /* test against NULL */
printf("foo: x cannot be NULL\n");
exit(EXIT_FAILURE);
}
if (sz > MAX_SIZE) { /* test against too large sz */
printf("foo: sz=%lu too large\n", sz);
exit(EXIT_FAIULRE);
}
if (option != 0 && option != 1) { /* test against invalid option */
printf("foo: option=%d invalid\n", option);
exit(EXIT_FAILURE);
}
/* do main things */
return val;
}
argument against my assumption? I think it is safer. However, I
notice that people usually don't test the validity of arguments.
For example,
#define MAX_SIZE 100000
int foo(double *x, unsigned ling sz, int option)
{
int val;
if (x == NULL) { /* test against NULL */
printf("foo: x cannot be NULL\n");
exit(EXIT_FAILURE);
}
if (sz > MAX_SIZE) { /* test against too large sz */
printf("foo: sz=%lu too large\n", sz);
exit(EXIT_FAIULRE);
}
if (option != 0 && option != 1) { /* test against invalid option */
printf("foo: option=%d invalid\n", option);
exit(EXIT_FAILURE);
}
/* do main things */
return val;
}