Z
Z
Zellily2017-06-10 10:42:52
C++ / C#
Zellily, 2017-06-10 10:42:52

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

4 answer(s)
R
res2001, 2017-06-10
@res2001

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

C
chupasaurus, 2017-06-10
@chupasaurus

You replace the condition with what is done in two shifts.

K
Konstantin Tsvetkov, 2017-06-10
@tsklab

and check if it works properly

So you need to check the frequency during tuning so that it can be divided by an even number, that is, it was even . And it’s not clear with the divisor: if this is a setting, check it, if not, round it up.

A
Alexander, 2017-06-10
@AlanDrakes

> 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 question

Ask a Question

731 491 924 answers to any question