Say I have this function:
void doThings(uint8_t index) {
if (an index is given) { ... }
}
Usually, an invalid index is -1, so that if statement would be if (index != -1)
. What if I'm using an unsigned integer to represent the index? Would it be silly to change the function definition to a signed int, just so I can test for -1
? Is there a universally accepted number representing 'no index' for unsigned ints?
If you must take into account the two situations in the same function, a better option may be to just provide a second parameter.
void doThings(uint8_t index, bool indexGiven) {
if (indexGiven) { ... }
}
However, using two entirely different functions, one for when the index is given and one for when it is not, may lead to a cleaner design.