Do Not Assume

Programming, for all ages and all languages.
Post Reply
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Do Not Assume

Post by Solar »

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.
Craze Frog
Member
Member
Posts: 368
Joined: Sun Sep 23, 2007 4:52 am

Post by Craze Frog »

Do Not Assume
That everyone is using C.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post by Solar »

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.
User avatar
mystran
Member
Member
Posts: 670
Joined: Thu Mar 08, 2007 11:08 am

Post by mystran »

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.
The real problem with goto is not with the control transfer, but with environments. Properly tail-recursive closures get both right.
Pyrofan1
Member
Member
Posts: 234
Joined: Sun Apr 29, 2007 1:13 am

Post by Pyrofan1 »

Assume makes an @$$ out of u and me
EDIT: i didn't know about the word filter thing, but you get the idea
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post by Solar »

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.
User avatar
babernat
Member
Member
Posts: 42
Joined: Tue Jul 03, 2007 6:53 am
Location: Colorado USA

Post by babernat »

... that a Byte equals 8 bits ...

I ran into some old C code last year that had you define how many bits were in a Byte. Kind of uncommon these days, but I guess it can be different.
Thanks for all the fish.
Post Reply