Scale Awareness

Integers and floats are not sufficient representations of measurements. If an engineer has an int to count bytes and an int to count sectors, adding the two yields useless gibberish. A C compiler is not able to catch such mistakes. An engineer can discover such blunders during a review, but developer time is expensive. Therefore, the compiler needs to be made aware of scales. There are two ways to accomplish this.

int size = 123; // bytes
int location = 4; // sectors
int new_location = location + size;

This is legal C, but a human reader sees the mistake of adding two variables of different scale.

The first method for scale awareness is to define types using the already existing tools. In the bytes and sectors example, an engineer can create a struct for sectors and a struct for bytes with accompanying arithmetic and conversion functions.

typedef struct {
      int value : 0;
} sector;
sector add(sector, sector);
sector sub(sector, sector);
byte to_bytes(sector);
class Sector {
    int value = 0;
    void add(Sector);
    void sub(Sector);
    Byte toBytes();
}

Scale aware software can prevent conversion errors.

The second method for scale awareness is a built-in compiler construct for defining scale relationships. Using such tool, an engineer can define a sector to be equal to 512 bytes. The compiler can then evaluate the addition of bytes and sectors as an expression of type byte, converting sectors to bytes at run-time.

typedef int sector;
typedef int byte;
scaledef sector = 512 byte;
byte size = 123;
sector location = 4;
byte new_location = location + size;

A modified C compiler can verify existence of scale conversions at compile time and perform the conversions at run time.

There are arguments in favor of both methods. Types can be placed into a standalone software library. This ensures modularity by separating the standard library, the scales library, and the controlling module that utilizes these libraries. However, because it is hard to account for all possible types of scales and conversions, the scales library must be constantly modified and updated, defeating one of the main advantages of modularity – code reuse.

The argument in favor of scaledef lies in its inherent simplicity from an engineer’s perspective. Having only one line of scaling definition increases readability of software source. The guarantee of automatic conversions by the compiler allows an engineer to concentrate on the higher level of logic rather than dwelling upon the details of ubiquitous conversions.

The software engineering community can benefit from a study in the quality of software produced by engineers who use the scale aware paradigm as compared to those who perform manual conversions. An other benefit of such study is the collection of first prototypes of the software libraries for scaling.

Leave a Reply

Captcha
Enter the letters you see above.