Space-proof coding

Came across this and felt the need to write the “Power of 10” rules…

#1 – Simple Control Flow

Don’t use goto, setjmp, longjmp, or recursion.

#2 – Limit All Loops

All loops are bound by a hard upper limit, so instead of:

while (!e) {
  e = derp();

You limit your loop:

int x = 0;
while (!e && (x < LOOP_MAX)) {
  e = derp();

#3 – Don’t use the Heap

Don’t use malloc() or free(). Using memory can easily lead to leaks, heap overflows, exhaustion, and garbage collection. Instead, exclusively use the stack.

#4 – Limit Function Size

A function should do 1 thing. It might need multiple steps, but it should do a single action. Functions, per NASA, should not be longer than 60 lines, or fit on a piece of paper.

#5 – Practice Data Hiding

Data hiding is a technique of hiding internal object details. Data hiding restricts the data access to class members, and this maintains data integrity.

Also known as “limit use of Globals”

#6 – Check Return Values

Any functions that return non-void values should be checked.

If you really don’t care about the return value, in order to validate that you have “checked all return values” you should be able to cast the function as (void) (if doable)

#7 – Limit the Preprocessor

Only file inclusions and very simple conditional macros.

The C preprocessor is a powerful obfuscation tool that can destroy code clarify and befuddle many text-based checkers.

this youtube video, 4:28

This is specific to conditional compilation, or flags that change the code at compile time. All the flags present create exponential build targets which makes the code hard to scale and had to test.

#8 – Restrict Pointers Use

Pointers should not be able to be deferenced more than 1 layer at a time. By limiting the pointers to 1 dereference at a time it forces you to create structures that property track your pointers.

Also, no function pointers. Makes control graphs bad.

#9 – Be Pedanticc

Compile with all warnings and Pendantic.

gcc -Wall -Werror -Wpedantic

Leave a Reply