{"id":3678,"date":"2014-08-08T20:00:45","date_gmt":"2014-08-09T00:00:45","guid":{"rendered":"http:\/\/www.paleotechnologist.net\/?p=3678"},"modified":"2014-08-08T15:14:48","modified_gmt":"2014-08-08T19:14:48","slug":"how-not-to-code","status":"publish","type":"post","link":"https:\/\/www.paleotechnologist.net\/?p=3678","title":{"rendered":"How Not To Code"},"content":{"rendered":"<p>Recently, my friend Dan shared a thought-provoking picture of a quote by Martin Golding:<\/p>\n<p>&nbsp;<\/p>\n<div id=\"attachment_3679\" style=\"width: 617px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.paleotechnologist.net\/wp-content\/uploads\/2014\/08\/coding_advice.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3679\" class=\"size-full wp-image-3679\" src=\"https:\/\/www.paleotechnologist.net\/wp-content\/uploads\/2014\/08\/coding_advice.jpg\" alt=\"&quot;Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.&quot; --Martin Golding\" width=\"607\" height=\"809\" \/><\/a><p id=\"caption-attachment-3679\" class=\"wp-caption-text\">Words to live by.<\/p><\/div>\n<p>When I first started writing programs back in the Dark Ages (1980s), I wasn&#8217;t even aware that coding styles existed. Not only did I probably break every applicable rule in the <a href=\"http:\/\/www.barrgroup.com\/Coding-Standard-Muse\" target=\"_blank\">Embedded C Coding Standard<\/a> &#8212; my code was usually cryptic, terse, and would have been very very difficult to scale.<\/p>\n<p>Nowadays, looking through my code library, I can almost tell when I wrote a piece of code, based on how it looks. Here&#8217;s an example of some of my oldest BASIC code that I could find: (The file, helpfully, was called &#8220;T.BAS&#8221;)<\/p>\n<blockquote><p>5 PRINT TIME$<br \/>\n10 FOR X = 1 TO 1000000<br \/>\n20 P = 1 \/ X<br \/>\n30 NEXT X<br \/>\n40 PRINT TIME$<\/p><\/blockquote>\n<p>This is Dark Ages code for sure. No comments or other documentation whatsoever, single-letter variable names (the only reason TIME$ is there is that it&#8217;s built-in), and LINE NUMBERS. Fortunately, this program is very simple, so it&#8217;s an easy enough task to figure out what it does. (It&#8217;s a very simple benchmark for a division loop.)<\/p>\n<p>Here&#8217;s how I might write it these days:<\/p>\n<blockquote><p>&#8216;Division loop benchmark test program (FreeBASIC)<br \/>\n&#8216;Notes time to execute one million divisions of size double<br \/>\n&#8216;M. Eric Carr \/ eric(at)(email address)<\/p>\n<p>&#8216;Variable declarations<br \/>\n&#8216;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\ndim as double quotient<br \/>\ndim as ulongint curNum<\/p>\n<p>&#8216;Note the start time<br \/>\nprint &#8220;Doing 1,000,000 divisions&#8230;&#8221;<br \/>\nprint &#8220;Started at: &#8220;;time$<\/p>\n<p>&#8216;Run a loop of 1,000,000 divisions<br \/>\nfor curNum = 1 to 1000000<br \/>\nquotient = 1\/x<br \/>\nnext curNum<\/p>\n<p>&#8216;Note the end time<br \/>\nprint &#8220;Ended at: &#8220;;time$<\/p>\n<p>end<\/p><\/blockquote>\n<p>This is a little better. It&#8217;s still flat code (no subroutines or other hierarchical structure), but it&#8217;s simple enough that such structure isn&#8217;t really needed. The documentation and intuitive variable names make it much easier to maintain, the output now tells the poor clueless user what is going on, and the global variables are at least declared.<\/p>\n<p>Here are some other helpful tips to make your code more maintainable:<\/p>\n<ul>\n<li>Use comments liberally. If it isn&#8217;t immediately obvious what a line of code does, document it inline with comments. In fact, it can be helpful to write the entire program in pseudocode, as comments, and then go back and fill in the mechanics.<\/li>\n<li>Use intuitive, descriptive variable names. A variable named &#8220;x&#8221; can mean many things, but one named &#8220;currentOutdoorTemp&#8221; is a bit more descriptive. When using a compiler, this has zero runtime cost &#8212; the compiler will translate the variable names into memory locations automagically, anyway.<\/li>\n<li>Use structured programming. If you have several pieces of information that belong together, consider using custom data types. If you have a program that is more than a dozen lines of code or so, it has probably become large enough to be split into subroutines. Doing so modularizes the task of programming, making the program easier to understand and far easier to maintain, modify, and scale.<\/li>\n<li>Along with the &#8220;structured programming&#8221; theme, use as few global variables as possible. They can be modified by not only the main code, but by subroutines, making it potentially very difficult to diagnose what is happening. For multithreaded applications, this becomes crucial; any global or shared variables will require good, solid mutexes.<\/li>\n<li>Don&#8217;t use GOTO statements unless absolutely necessary. These can often be avoided even when programming in assembly. (My own goal is to never use GOTOs, with the exception of a single GOTO at the end of the main loop, when coding in assembly.) GOTO statements can make code execution very difficult to trace.<\/li>\n<\/ul>\n<p>&#8230;because all too often, the violent psychopath who will have to maintain your code &#8212; is YOU!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently, my friend Dan shared a thought-provoking picture of a quote by Martin Golding: &nbsp; When I first started writing programs back in the Dark Ages (1980s), I wasn&#8217;t even aware that coding styles existed. Not only did I probably &hellip; <a href=\"https:\/\/www.paleotechnologist.net\/?p=3678\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"footnotes":""},"categories":[236,216],"tags":[],"class_list":["post-3678","post","type-post","status-publish","format-standard","hentry","category-basic","category-coding"],"_links":{"self":[{"href":"https:\/\/www.paleotechnologist.net\/index.php?rest_route=\/wp\/v2\/posts\/3678","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.paleotechnologist.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.paleotechnologist.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.paleotechnologist.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.paleotechnologist.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3678"}],"version-history":[{"count":3,"href":"https:\/\/www.paleotechnologist.net\/index.php?rest_route=\/wp\/v2\/posts\/3678\/revisions"}],"predecessor-version":[{"id":3682,"href":"https:\/\/www.paleotechnologist.net\/index.php?rest_route=\/wp\/v2\/posts\/3678\/revisions\/3682"}],"wp:attachment":[{"href":"https:\/\/www.paleotechnologist.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.paleotechnologist.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.paleotechnologist.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}