Monday, December 8, 2008

Variable types and sizes

Just a random thought on variable sizes:

There's an old design decision for programming languages as to whether the variable size for built-in numeric types be static or dynamic. For example, the .NET framework has static sizes: every type in the System namespace (which are all the built-in types) has a size associated with it, eg: Int32. Conversely, in C/C++, int is of dynamic size, dependent on the compiler and the target environment.

There are arguments for both. On the static size, you have deterministic size, so you can predict exactly what values will/won't fit. On the dynamic side, you can automatically use the size which is appropriate for the architecture, which gives you automatic adaptability to architectures with new intrinsic data sizes (eg: 32bit -> 64bit) without speed degradation from extra operations to adapt non-standard variable sizes.

With those trade-offs in mind, I'd propose a new? thought for variable declarations: a concept of "at least" x bits. This would give you the best of both worlds: you could say with certainty that values within a target range would fit in your variable, while allowing the compiler to allocate a larger type if that was more optimal for the target architecture. You would sacrifice predictability of variable size, but you could still use fixed-size constructs as a fallback if you needed them.

With that in mind, variable declarations might look like:
int32p i32bitOrLargerValue;
int64p i64bitOrLargerValue;

... where the 'p' is for 'plus'.

Just a thought.

1 comment:

Andrew33 said...
This comment has been removed by the author.