by bjourne · 3y ago · 0 comments
static void
get_foobar() ...
Get is a verb that means "to retrive" so "get_foobar" should return something. int n_bytes = ...;
Why a signed type? A negative number of bytes is nonsense. In fact a whole language designed around assigning semantic meaning to names would be cool. f has to be a function, n has to be an integer >= 0, M has to be a square matrix of some type, has_xyz must be a boolean, and so on. [[nodiscard]]
on functions to mark that the result from function shouldn't be discarded.
I think there is also a [[reproducible]]
attribute for saying a function doesn't do side effects.That's true, but mixing signed and unsigned integers opens a much bigger can of worms (for instance it's not unusual to add a negative quantity to an unsigned integer). It's often better to just stick to signed integer types which are big enough to cover the required positive range (e.g. you only loose one bit, and 63 bits ought to be enough for anybody).
...also an optimizing compiler will usually turn something like:
assert((index >= 0) && (index < size));
...into a single comparison, since on the assembly level it's free to interpret that 'index' as unsigned.n_bytes could easily be negative if a byte offset, or as a sentinel.
This is why we have a type system, to express these things :)