{"id":1848,"date":"2023-06-11T19:35:43","date_gmt":"2023-06-11T23:35:43","guid":{"rendered":"https:\/\/www.unliterate.net\/?p=1848"},"modified":"2023-06-11T19:35:58","modified_gmt":"2023-06-11T23:35:58","slug":"space-proof-coding","status":"publish","type":"post","link":"https:\/\/www.unliterate.net\/index.php\/2023\/06\/11\/space-proof-coding\/","title":{"rendered":"Space-proof coding"},"content":{"rendered":"\n<p>Came across this and felt the need to write the &#8220;Power of 10&#8221; rules&#8230;<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"how NASA writes space-proof code\" width=\"648\" height=\"365\" src=\"https:\/\/www.youtube.com\/embed\/GWYhtksrmhE?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<!--more-->\n\n\n\n<p><strong>#1 &#8211; Simple Control Flow<\/strong><\/p>\n\n\n\n<p>Don&#8217;t use goto, setjmp, longjmp, or recursion. <\/p>\n\n\n\n<p><strong>#2 &#8211; Limit All Loops<\/strong><\/p>\n\n\n\n<p> All loops are bound by a hard upper limit, so instead of:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while (!e) {\n  e = derp();\n}<\/code><\/pre>\n\n\n\n<p>You limit your loop:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int x = 0;\nwhile (!e &amp;&amp; (x &lt; LOOP_MAX)) {\n  e = derp();\n  x++;\n}<\/code><\/pre>\n\n\n\n<p><strong>#3 &#8211; Don&#8217;t use the Heap<\/strong><\/p>\n\n\n\n<p>Don&#8217;t use malloc() or free(). Using memory can easily lead to leaks, heap overflows, exhaustion, and garbage collection. Instead, exclusively use the stack.<\/p>\n\n\n\n<p><strong>#4 &#8211; Limit Function Size<\/strong><\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p><strong>#5 &#8211; Practice Data Hiding<\/strong><\/p>\n\n\n\n<p>Data hiding is a technique of hiding internal object details. Data hiding restricts the data access to class members, and this maintains data integrity.<\/p>\n\n\n\n<p>Also known as &#8220;limit use of Globals&#8221;<\/p>\n\n\n\n<p><strong>#6 &#8211; Check Return Values<\/strong><\/p>\n\n\n\n<p>Any functions that return non-void values should be checked.<\/p>\n\n\n\n<p>If you really don&#8217;t care about the return value, in order to validate that you have &#8220;checked all return values&#8221; you should be able to cast the function as (void) (if doable)<\/p>\n\n\n\n<p><strong>#7 &#8211; Limit the Preprocessor<\/strong><\/p>\n\n\n\n<p>Only file inclusions and <em>very simple<\/em> conditional macros.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>The C preprocessor is a powerful obfuscation tool that can destroy code clarify and befuddle many text-based checkers.<\/p>\n<cite>this youtube video, 4:28<\/cite><\/blockquote>\n\n\n\n<p>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.<\/p>\n\n\n\n<p><strong>#8 &#8211; Restrict Pointers Use<\/strong><\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>Also, no function pointers. Makes control graphs bad.<\/p>\n\n\n\n<p><strong>#9 &#8211; Be Pedantic<\/strong>c<\/p>\n\n\n\n<p>Compile with all warnings and Pendantic.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gcc -Wall -Werror -Wpedantic<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Came across this and felt the need to write the &#8220;Power of 10&#8221; rules&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-1848","post","type-post","status-publish","format-standard","hentry","category-geek-instructions"],"_links":{"self":[{"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/posts\/1848","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/comments?post=1848"}],"version-history":[{"count":1,"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/posts\/1848\/revisions"}],"predecessor-version":[{"id":1849,"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/posts\/1848\/revisions\/1849"}],"wp:attachment":[{"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/media?parent=1848"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/categories?post=1848"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/tags?post=1848"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}