3 ms·
What I'm missing from static analyzers are English awareness: static void get_foobar() ... Get is a verb that means "to retrive" so "get_foobar" shoul
by bjourne 3y ago
What I'm missing from static analyzers are English awareness:
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.
- cozzyd 3y agoget_foobar() could update some thread_local static value, for example.... or print something to screen. 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 :)
- bjourne 3y agoNah, those are type errors. Some languages use case so that FOO can never have the same type as foo. Nothing prevents a type system from similarly distinguishing between get_foo() and set_foo().
- neutrono 3y agoIn C23 you can use the attribute [[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.
- flohofwoe 3y ago> A negative number of bytes is nonsense. 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.
- bjourne 3y agoIt's not about the compiler, it's about clearly communicating the intent of the code to the reader.