Answer the question
In order to leave comments, you need to log in
Why is it bad to check the function argument for valid values inside the function?
I'm writing something like a homemade driver to work with some device. I pass the frequency divider as an argument to the function. The divisor must be even. Therefore, inside the function, I check that the argument is divisible by two, and if not, I throw out the assert.
The boss says it's wrong. What you need to either pass a divisor multiplied by two into the function (then you will have to divide once again), or create a separate enum for the allowable divisor values (and there are a lot of them, these values).
What is wrong with asserting inside such a function?
Answer the question
In order to leave comments, you need to log in
assert in the driver is somehow overkill.
Just reset the least significant bit forcibly and you will always have an even number.
And in the instructions, describe that the value is reduced to the nearest smaller even.
For a 1 byte value it would be:
val & 0xfe
and check if it works properly
> As a function argument, I pass the frequency divider.
Functions.
You check the parameters at the input, if you don’t like something, return -1, without producing anything in the function.
int function foo (int bar) {
if (bar & 1) return -1;
New Freq = bar;
return 0;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question