Writing Space-Proof Code

Meta

I learned of this from the following youtube video: How NASA Writes Space-Proof Code

Principles

  1. Simple Control Flow - Excluding even recursion
  2. Limit the maximum times a loop can run
  3. Don't use the heap - use only the stack and the max amt of memory the stack can use
  4. Limit Function Size - no more than 60 lines or what can fit on a piece of paper
  5. Declare variables at the lowest scope possible
  6. Check return values
  7. Limit preprocessor to file inclusions and very simple conditional macros (different platforms)
  8. Restrict pointer usage - one derefernce at a time and no function pointers at all
  9. Compile with all warnings enabled and in pedantic mode
  10. Use multiple static code analyzers and write unit tests

Reflections

  • obviously written for C but general concepts like KISS and Testing can be applied to everywhere
  • I'd love to see exceptions they made to excluding recursions if there are any and techniques one can use to avoid recursion in places you'd normally see them
  • Wonder what their code does if a loop reaches its upper limit
  • For other languages, I'd imagine don't use the heap can translate to prefer simple data structures and built-in operations as much as possible
  • I'd like to see a better metric for limiting function size
  • My translation: Atomic/Purity/Immutability + good testing and analyzing + fault-tolerant exception handling + Simple structures

Author: Keshav Italia (keshavitalia0@gmail.com)

Date:

Emacs 29.1 (Org mode 9.6.6)

Validate