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.
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
Take care!
Every good solution is obvious once you've found it.
Well yes, but this is a chapter taken from a coding style guide for C. And most of the points should be taken into account for most other languages you could use (perhaps excluding assembler).
Every good solution is obvious once you've found it.
mystran wrote:My favourite is a piece from wisdom I originally learned from some on IRC years ago:
- when trying to do something 'undefined' it's perfectly valid for the program to order pizza instead.
...or format your hard drive (you have been warned).
Related quote (taken from the Amiga Guru Book):
The '#pragma' command is specified in the ANSI standard to have an arbitrary implementation-defined effect. In the GNU C preprocessor, '#pragma' first attempts to run the game 'rogue'; if that fails, it tries to run the game 'hack'; if that fails, it tries to run GNU Emacs displaying the Tower of Hanoi; if that fails, it reports a fatal error. In any case, preprocessing does not continue.
-- Richard M. Stallman, The GNU C Preprocessor, version 1.34
Every good solution is obvious once you've found it.