← All videos

Engineering video

10:27 · 4 September 2024

why NASA doesn't use recursive functions

Chapters 10 sections

  1. 0:00Introduction and Rule 1: Simple Control Flow
  2. 2:00Rule 2: Fixed Limits on Loops
  3. 2:51Rule 3: Do Not Use Heap Memory
  4. 4:00Rule 4: Restrict Functions to a Single Page

Key concepts in this video:

1. Simple Control Flow and Recursion

NASA rules state that complex constructs like recursion should be avoided because deep recursive calls can exceed stack memory and cause stack overflows.

Watch section from 0:00 →

2. Fixed Limits on Loops

All loops must include hard limits, such as array lengths or max iteration constants, to prevent infinite loops and runaway code.

Watch section from 0:00 →

3. Avoiding Heap Memory

Manual memory allocation on the heap should be avoided to prevent memory leaks; stack memory and function arguments should be used instead.

Watch section from 2:51 →

4. Function Length Restrictions

Functions should be restricted to a single printed page (around 60 lines of code) to improve maintainability and testability.

Watch section from 4:00 →

5. Runtime Assertions

The video describes a rule of at least two runtime assertions per function to check arguments and handle failures in a controlled manner, including restarting when appropriate.

Watch section from 4:00 →

6. Smallest Variable Scope

Variables should be declared in the smallest scope possible to avoid the unpredictable behaviors associated with global state.

Watch section from 7:01 →

7. Checking Return Values

Developers should check return values of non-void functions and explicitly indicate when functions return void.

Watch section from 7:41 →

8. Sparse Use of Preprocessors

Tooling and preprocessors like polyfills, compilers, and bundlers should be used sparingly to avoid debugging complexity and production performance issues.

Watch section from 8:07 →

9. Restricting Reference Pointers

Object reference pointers should be limited to one level to prevent accidental mutations and hard-to-track bugs.

Watch section from 9:11 →

10. Compiling with All Warnings Active

Code should be compiled and checked with all possible warnings, linter rules, and static analysis tools active rather than ignoring warnings under delivery pressure.

Watch section from 9:49 →

Transcript

The shuttle used around 400,000 lines of code. The International Space Station uses 1.4 million lines of code for inflight operations and 3 million lines of code for on ground support. The curiosity Rover that's exploring Mars uses 2.5 million lines of embedded code. Not only that in space, the margin for error, it's zero. To avoid bugs in these scenarios, NASA developed 10 rules to write better code. In this video, we will apply them to a JavaScript code base and see what can we learn from the space agency that can make us better engineers. Rule number one, simple control flow, basically avoiding complex constructs like recursion. NASA says we should not use recursive functions like this one. As we see, factorial will end up calling factorial again. Imagine we would have a very, very big number, this function can really get out of control. And we would end up with a stack overflow error because we have way too many functions calling each other, all being handled by the limited stack memory. Instead, write every recursive behavior as a loop. In this case, we can write it as a for loop, and also be done with a while loop. Very important for you as a JavaScript developer or a developer interviewing right now, make sure you can translate from recursive function to an iterative approach with a for or a while loop. Rule number two, all loops must have fixed limits. We've seen that we shouldn't be using recursion. But NASA goes even further and says, whenever we have a loop, like in this case, I have a while loop. We will add a hard limit to it. In this case, we look at the while loop, it's basically saying while the element in the array is not equal to a specific target, keep increasing i. In the case in which the target, it's never in the array, this while loop will be infinite and basically the program will collapse. It will fail, it will end up in unpredictable state. Instead of this, NASA says we should add high