Do Not Assume
Posted: Wed Dec 12, 2007 9:23 am
Do you know "computer archaeology"? I sometimes get a funny feeling when I stumble over a text I've written ages ago and forgotten. It's like a different version of myself is talking to me, like a time capsule.
I came across the coding style guide I wrote for Pro-POS back then... I still hold to most of those rules, some others have changed / evolved with time and experience. But I especially liked the paragraph on "Do Not Assume".
I decided to post it here, for nostalgic reasons as much as pragmatic ones.
I came across the coding style guide I wrote for Pro-POS back then... I still hold to most of those rules, some others have changed / evolved with time and experience. But I especially liked the paragraph on "Do Not Assume".
I decided to post it here, for nostalgic reasons as much as pragmatic ones.
Take care!8. DNA (Do Not Assume)
The following rules are stated as "Murphy info" instead of negative "don't"s, since people seem to feel a certain negative attitude towards negative rules. Read them carefully and ponder their implications a bit.
8.1 Cross-Platform DNA
- There are no native datatypes. The only datatypes you might use are those declared in global/types.h and the standard library.
- char, short, int and long are of different size each, just like float, double, and long double.
- int is not 32 bits.
- char is neither signed nor unsigned.
- char cannot hold a number, only characters.
- Casting a short type into a longer one (int -> long) breaks alignment rules of the CPU.
- int and int* are of different size.
- int* and long* are of different size (as are pointers to any other datatype).
- You do remember the native datatypes do not even exist?
- 'a' - 'A' does not yield the same result as 'z' - 'Z'.
- 'Z' - 'A' does not yield the same result as 'z' - 'a', and is not equal to 25.
- You cannot do anything with a NULL pointer except test its value; dereferencing it will crash the system.
- Arithmetics involving both signed and unsigned types do not work.
- Alignment rules for datatypes change randomly.
- Internal layout of datatypes changes randomly.
- Specific behaviour of over- and underflows changes randomly.
- Function-call ABIs change randomly.
- Operands are evaluated in random order.
- Only the compiler can work around this randomness. The randomness will change with the next release of the CPU / OS / compiler.
- For pointers, == and != only work for pointers to the exact same datatype.
- <, >, <, > work only for pointers into the same array. They work only for char=s explicitly declared =unsigned.
- You still remember the native datatypes do not exist?
- size_t (the type of the return value of sizeof) can not be cast into any other datatype.
- ptrdiff_t (the type of the return value of substracting one pointer from the other) can not be cast into any other datatype.
- wchar_t (the type of a multibyte character) can not be cast into any other datatype.
- Any ..._t datatype cannot be cast into any other datatype