4 ms·
If you know your variable is between 0 and 15 and care deeply about this, I kinda think you should get explicitly-ranged integer types added to your language ra
by astrange 3y ago
If you know your variable is between 0 and 15 and care deeply about this, I kinda think you should get explicitly-ranged integer types added to your language rather than use a type that's defined for -32767 - 32767 but doesn't enforce it.
Also, IME it obscures bugs in your code if you're forced to overspecify things, because now someone reading it has to determine if all those explicitly specified details are actually correct or not.
- IshKebab 3y agoDefinitely! But MISRA is about writing robust code with an error-prone language, not picking a different language that is better designed. Your second point doesn't make any sense. Someone reading `int` has to determine if that is actually correct or not too.
- astrange 3y ago> Your second point doesn't make any sense. Someone reading `int` has to determine if that is actually correct or not too. If you assign an int32_t to an int16_t because you know the actual range of values fits at that point, later readers don't necessarily know that. (And it can change.) If they're both int, there's no possible problem.
- IshKebab 3y agoYou're suggesting using the same integer type everywhere. If so you can use `uint64_t`. To put it another way: > If they're both int32_t, there's no possible problem.
- archi42 3y agoI agree, and there is tooling to do this kind of thing with C/C++. If this was embedded into the language, you'll quickly learn that static checking has its limits; dynamic checking introduces a new class of failure mode into your embedded, safety critical (as in kills or destroys) system, which might also not be what you want. It's a conundrum :( The second statement contradicts the first - int16_t is too specific and hides errors, but an arbitrary range is not?
- astrange 3y ago> The second statement contradicts the first - int16_t is too specific and hides errors, but an arbitrary range is not? The arbitrary range is actually capable of being correct. int16_t is almost never "correct", since it's larger than the actual range of values. It can also be said to be an over-optimization, since on some platforms like x86 it's easier to deal with 32-bit values than 16-bit ones.
- archi42 3y agoThe compiler takes care of this kind of optimization via static analysis. And who said x86? You're right, but what about Power9 register windows vs the three-and-a-half brain cells of x86? I'm not in the mood of discussing this or convincing you otherwise. If you made up your mind to use int over intWhatever_t then so be it. We're not working on the same project and I suppose you're not building anything that might kill me.