{"id":122,"date":"2021-12-10T17:28:31","date_gmt":"2021-12-10T17:28:31","guid":{"rendered":"https:\/\/thorshov.net\/?p=122"},"modified":"2021-12-10T18:42:46","modified_gmt":"2021-12-10T18:42:46","slug":"elegent-hook","status":"publish","type":"post","link":"https:\/\/thorshov.net\/index.php\/2021\/12\/10\/elegent-hook\/","title":{"rendered":"Elegant Hook"},"content":{"rendered":"\n<p>I attended Hoover Middle School during the mid 80s and remember my first experience instructing a computer to do something.   My school was located in a fairly affluent area of Albuquerque, New Mexico.   Albuquerque is the home of Sandia National Laboratories and the public education system there seemed to benefit by having quality technology and science programs.  In 7th grade I had a brief chance to learn an introduction to programming on an Apple II with a color monitor.  This was an amazing opportunity for the time considering desktop computing was just becoming available to the public!   The API was a heavily wrapped version of BASIC that allowed students to write lines of code to control the color of pixels on what was probably a 50&#215;50 grid with a 16 color palette.  There were very few commands at the disposal of the students and I imagine the code would have looked something like this:<\/p>\n\n\n\n<div class=\"wp-block-columns\">\n<div class=\"wp-block-column is-vertically-aligned-top\">\n<pre class=\"wp-block-code\"><code lang=\"basic\" class=\"language-basic\">10 clear white\n20 set 4,6 blue\n30 set 6,6 blue\n40 set 3,4 green\n50 set 4,3 green\n60 set 5,3 green\n70 set 6,3 green\n80 set 6,4 green<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-top\">\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"342\" height=\"342\" src=\"https:\/\/thorshov.net\/wp-content\/uploads\/2021\/12\/applecode.png\" alt=\"\" class=\"wp-image-123\" srcset=\"https:\/\/thorshov.net\/wp-content\/uploads\/2021\/12\/applecode.png 342w, https:\/\/thorshov.net\/wp-content\/uploads\/2021\/12\/applecode-300x300.png 300w, https:\/\/thorshov.net\/wp-content\/uploads\/2021\/12\/applecode-150x150.png 150w\" sizes=\"(max-width: 342px) 100vw, 342px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p>I remember it was fun to write instructions and create something visual on the screen, but the simplicity of the learning experience did not leave me feeling inspired.   As an Eldorado High School freshman, my next programming experience was undertaken to impress Rhonda, a beautiful junior cheerleader.  We shared a biology class and I had acquired some useful information about an upcoming multiple choice test.    My family owned an MSDOS (pre windows) Leading Edge PC with a proprietary word processor and floppy disk drive.  I wanted to print a very small cheat sheet for the test that one could literally digest if compelled to do so, but the options available were too limited.  Around this time my mother had just purchased a new printer and asked if I could figure out how to configure it.  I remembered a page in the manual showcasing the font sizes available and some associated BASIC code to manipulate the printer from DOS.  This would be perfect for my test guide, so I learned a great deal about making my way around DOS, directory structures, file editing and programming in BASIC.   My first independently conceived program worked;  I shared my tiny test guide with Rhonda and was rewarded with a hug, smile, and a little respect.  I had seen some potential for the value of programming and began to tinker more with writing BASIC programs.   <\/p>\n\n\n\n<p>Another interest I had at the time was in amateur electronics.   I grew up designing and building model train sets which gave me an early introduction into how electricity works.   All of these technical hobbies provided a fruitful connection to my father who has EE undergraduate degrees and a PHD in computer science.  One weekend, he was teaching me about resistors and how to decode their color bands to identify their value.   Each color relates to a whole number from 0-9, with gold and silver indicating the quality or tolerance of the resistor.  The first two color bands represent the base value and the third indicates the power of 10 to multiply by.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img src=\"https:\/\/1.bp.blogspot.com\/-ymp6oVGpE1g\/XxRY23aZQOI\/AAAAAAAAC4k\/oYGSWn2SAMcQlSf0-260IzPHMmKv45iOwCLcBGAsYHQ\/w640-h308\/Resistor-color-code-table.webp\" alt=\"Resistor color code\"\/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-columns\">\n<div class=\"wp-block-column\">\n<p>For example, the top resistor pictured on the right has a gold band indicating a 5% tolerance, with three color bands Green(<strong>5<\/strong>), Black(<strong>0<\/strong>) and Red(<strong>2<\/strong>).<br><br>This relates to <strong>50<\/strong>*10^<strong>2<\/strong> = 5000 Ohms.<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column\">\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"655\" height=\"455\" src=\"https:\/\/thorshov.net\/wp-content\/uploads\/2021\/12\/image.png\" alt=\"\" class=\"wp-image-126\" srcset=\"https:\/\/thorshov.net\/wp-content\/uploads\/2021\/12\/image.png 655w, https:\/\/thorshov.net\/wp-content\/uploads\/2021\/12\/image-300x208.png 300w\" sizes=\"(max-width: 655px) 100vw, 655px\" \/><figcaption>5000, 10000, 1000 and Ohm resistors<\/figcaption><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p>Selecting a resistor for your project can be a bit trickier because you will not always find a perfect match for the exact value you would need.  At times selecting the resistor with the closest value would have to do.   After the weekend with my dad, I returned home and wrote a program called &#8220;Resistance Assistance&#8221; to aid in choosing a resistor.  The input of the program was a theoretical resistance value and the output was the closest physical resistor and it&#8217;s color code.<\/p>\n\n\n\n<p>I had my program on a 720 KB floppy disc the next time I visited my dad and showed him how it worked.   He was proud of the way the program performed and was immediately interested in the code I had written.   We spent the following couple of hours examining and refining the code to a more professional standard.  It was an experience that impacted my personality as a programmer immensely.  I will illustrate the progression of refinement to my code with four examples.  <\/p>\n\n\n\n<p class=\"has-large-font-size\">#1 Minimizing Logical Statements<\/p>\n\n\n\n<p>The following lines assign the variable C$ variable to the color that corresponds to the correct color.   The code is logically accurate but poorly written.  At the time I was unaware of the ELSE statement.   Even though the correct color will be assigned in line 40, every following comparison statement after will be run resulting in 7 unnecessary calls.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"basic\" class=\"language-basic\">10  NUMBER=2                                                                    \n20  IF NUMBER=0 THEN C$=\"BLACK\"                                                 \n30  IF NUMBER=1 THEN C$=\"BROWN\"                                                 \n40  IF NUMBER=2 THEN C$=\"RED\"                                                   \n50  IF NUMBER=3 THEN C$=\"ORANGE\"                                                \n60  IF NUMBER=4 THEN C$=\"YELLOW\"                                                \n70  IF NUMBER=5 THEN C$=\"GREEN\"                                                 \n80  IF NUMBER=6 THEN C$=\"BROWN\"                                                 \n90  IF NUMBER=7 THEN C$=\"VIOLET\"                                                \n100 IF NUMBER=8 THEN C$=\"GRAY\"                                                  \n110 IF NUMBER=9 THEN C$=\"WHITE\"                                                 \n120 PRINT \"COLOR \" C$<\/code><\/pre>\n\n\n\n<p>My dad described this problem to me and helped me to imagine this part of the program running hundreds, thousands, or even millions of times.  If the number variable was evenly distributed between 0 and 9, the average number of calls wasted would be 5 per run.  This would cause noticeable slowdown.  We refined the code to include ELSE statements so that once a color was assigned the following IF statements would be ignored.   Instead of 10 comparisons being made in the code above, the following only requires 3. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"basic\" class=\"language-basic\">10  NUMBER=2                                                                    \n20  IF NUMBER=0 THEN C$=\"BLACK\" ELSE                                                \n30  IF NUMBER=1 THEN C$=\"BROWN\" ELSE                                               \n40  IF NUMBER=2 THEN C$=\"RED\" ELSE                                                  \n50  IF NUMBER=3 THEN C$=\"ORANGE\" ELSE                                               \n60  IF NUMBER=4 THEN C$=\"YELLOW\" ELSE                                                \n70  IF NUMBER=5 THEN C$=\"GREEN\" ELSE                                                \n80  IF NUMBER=6 THEN C$=\"BROWN\" ELSE                                                \n90  IF NUMBER=7 THEN C$=\"VIOLET\" ELSE                                               \n100 IF NUMBER=8 THEN C$=\"GRAY\" ELSE                                                 \n110    NUMBER=9 THEN C$=\"WHITE\"                                                \n120 PRINT \"COLOR \" C$<\/code><\/pre>\n\n\n\n<p class=\"has-large-font-size\">#2 Using Data Structures<\/p>\n\n\n\n<p>Next, he showed me how the logic structure could be removed entirely and the same task of assigning a color could be accomplished in one line.  He introduced me to the method of using an indexed array for the task.  First, store the colors in a list (A$) and then simply access the needed color by location (line 50).  There would be some overhead to loading the data in lines 10-30, but this would only need be done once in the program.  From that point forward, a million color assignments could be made with a minimal million calls as in line 50.  <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"basic\" class=\"language-basic\">10 DIM A$(10)                                                                   \n20 FOR I=1 TO 10: READ A$(I):NEXT                                               \n30   DATA \"BLACK\",\"BROWN\",\"RED\",\"ORANGE\",\"YELLOW\",\n          \"GREEN\",\"BROWN\",\"VIOLET\",\"GRAY\",\"WHITE\"                                                                       \n40 NUMBER=2                                                                     \n50 C$ = A$(NUMBER)<\/code><\/pre>\n\n\n\n<p class=\"has-large-font-size\">#3 User Friendliness<\/p>\n\n\n\n<p>The original code I had written worked so long as the resistance values entered were strictly positive integers between one and a billion.  What if a user entered a negative value or a non-numeric like the word &#8220;dog&#8221;?  The program would crash.  My dad explained that even though we programs new not to do this, we should catch this input error and give the user information and another chance.  Also, there should be a way to gracefully exit the program.  So, we added a few more lines.  Line 120 let the user know how to exit the program and we catch that request on line 140 by exiting back to DOS if entered.   Line 150 ensures that the resistance values entered for conversion are in a specified range.  If not, the user will be reminded of the limitation and get another shot.  These changes made the program more user friendly.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"basic\" class=\"language-basic\">120 PRINT \"              PRESS F1+ENTER TO QUIT RESISTENCE ASSISTENCE\"          \n130 INPUT;\"                                                                                   \u2590\u2590\u2590\u2590\u2590\u2590 ENTER DESIRED RESISTENCE 9  \",N                            \n140 IF N=-1 THEN SYSTEM\n150 IF N=0 OR N&lt;.1 OR N&gt;=1E+11 THEN PRINT\" OOPS! BETWEEN .1 AND 9.54E+10 PLEASE.\":GOTO 130<\/code><\/pre>\n\n\n\n<p class=\"has-large-font-size\">#4 Math can often reduce code<\/p>\n\n\n\n<p>The final step we took with the &#8220;Resistance Assistance&#8221; program was to make it an actaul tool by employing some mathematics.   My assumption upon writing the original code was that a resistor could be found for any value needed.  I didn&#8217;t stop to think that manufacturers probably couldn&#8217;t effectively make billions of different sizes. Prior to my birth, the IEC (International Electrotechnical Commission) created a limited set of resistor values to span the predicted need. (<a href=\"https:\/\/eepower.com\/resistor-guide\/resistor-standards-and-codes\/resistor-values\/#\" data-type=\"URL\" data-id=\"https:\/\/eepower.com\/resistor-guide\/resistor-standards-and-codes\/resistor-values\/#\">&#8230;more<\/a>).    The available resistor values are based on a logarithmic scale, which at the time, I didn&#8217;t really understand.  So, my dad gave me some quality explanation to help me understand and then integrated the mathematics to speed up and reduce the size of the program once again.  This was neat.  I had always liked math and really had my eyes opened to how interesting programming could be. <\/p>\n\n\n\n<p>It may have been that day, or during some later conversations, when I heard my dad describe his appreciation for &#8220;Elegant Code.&#8221;   With the interest and patience of a great teacher, he helped refine my rough attempt at solving a problem into something more beautiful.  I have come to truly enjoy the process of algorithm refinement. Like a sculptor working to reveal a form that awaits under the stone, I chip away at my rough code until the elegant solution remains.  I have been hooked since. Thanks Dad!<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"RESISTANCE ASSISTANCE\" width=\"640\" height=\"480\" src=\"https:\/\/www.youtube.com\/embed\/5Jc9_nmTumc?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Below is the final GWBasic Code<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"basic\" class=\"language-basic\">10 KEY OFF                                                                      \n20 CLS                                                                          \n30 KEY 1, \"-1\"                                                                  \n40 OPTION BASE 1                                                                \n50 DIM B(25),R(24),L$(12)                                                       \n60 FOR I=1 TO 24:READ B(I):NEXT                                                 \n70 DATA 1.05,1.15,1.25,1.4,1.55,1.7,1.9,2.1,2.3,2.55,2.85,3.15,3.45,3.75,4.05,4.45,4.9,5.35,5.9,6.5,7.15,7.85,8.65,9.55                                         \n80 FOR I=1 TO 24:READ R(I):NEXT                                                 \n90 DATA 1,1.1,1.2,1.3,1.5,1.6,1.8,2,2.2,2.4,2.7,3,3.3,3.6,3.9,4.2,4.7,5.1,5.6,6.2,6.8,7.5,8.2,9.1                                                               \n100 FOR I=1 TO 12:READ L$(I):NEXT                                               \n110 DATA \"SILVER\",\"GOLD\",\"BLACK\",\"BROWN\",\"RED\",\"ORANGE\",\"YELLOW\",\"GREEN\",\"BLUE\",\"VIOLET\",\"GRAY\",\"WHITE\"                                                         \n120 PRINT \"              PRESS F1+ENTER TO QUIT RESISTANCE ASSISTANCE\"          \n130 INPUT;\"                                                                                   \u2590\u2590\u2590\u2590\u2590\u2590 ENTER DESIRED RESISTANCE 9  \",N                            \n140 IF N=-1 THEN SYSTEM\n150 IF N=0 OR N&lt;.1 OR N&gt;=1E+11 THEN PRINT\" OOPS! BETWEEN .1 AND 9.54E+10 PLEASE.\":GOTO 130                                                                      \n160 P=INT(LOG(N)\/LOG(10))                                                       \n170 X=N\/(10^P)                                                                  \n180 FOR I=1 TO 24                                                               \n190 IF X&lt;B(I) THEN A=R(I):GOTO 220                                              \n200 NEXT                                                                        \n210 X=X\/10:P=P+1:GOTO 180                                                       \n220 IF P&lt;3 THEN J$=\"\":T=P:GOTO 250                                              \n230 IF P&gt;=3 AND P&lt;6 THEN T=P-3:J$=\"k\":GOTO 250                                  \n240 IF P&gt;=6 THEN T=P-6:J$=\"M\"                                                   \n250 N=A*(10^T)                                                                  \n260 BEEP                                                                        \n270 H$=L$(INT(A)+3)+\" \u2551 \"+L$(10*(A-INT(A))+3)+\" \u2551 \"+L$(P+2)                     \n280 Z$=\"\u2590\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\"                                                   \n290 Q=LEN(H$)                                                                   \n300 K$=LEFT$(\"\u2584\u2584\u2584\u2584\u2584\u2584\u2584\u2584\u2584\u2584\u2584\u2584\u2584\u2584\u2584\u2584\u2584\u2584\u2584\u2584\u2584\u2584\u2584\u2584\u2584\u2584\u2584\u2584\",Q+4)                                \n310 D$=LEFT$(\"\u2580\u2580\u2580\u2580\u2580\u2580\u2580\u2580\u2580\u2580\u2580\u2580\u2580\u2580\u2580\u2580\u2580\u2580\u2580\u2580\u2580\u2580\u2580\u2580\u2580\u2580\u2580\u2580\",Q+4)                                \n320 PRINT\"                                    \u263a USE A\";N J$\" \u03a9 RESISTOR         \n330 PRINT\"                    \"K$                                               \n340 PRINT\" \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u258c \"H$\" \"Z$\"                                       \n350 PRINT\"                    \"D$:GOTO 130<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I attended Hoover Middle School during the mid 80s and remember my first experience instructing a computer to do something. My school was located in a fairly affluent area of Albuquerque, New Mexico. Albuquerque is the home of Sandia National Laboratories and the public education system there seemed to benefit by having quality technology and &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/thorshov.net\/index.php\/2021\/12\/10\/elegent-hook\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Elegant Hook&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":139,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7,4,8],"tags":[],"_links":{"self":[{"href":"https:\/\/thorshov.net\/index.php\/wp-json\/wp\/v2\/posts\/122"}],"collection":[{"href":"https:\/\/thorshov.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thorshov.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thorshov.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thorshov.net\/index.php\/wp-json\/wp\/v2\/comments?post=122"}],"version-history":[{"count":11,"href":"https:\/\/thorshov.net\/index.php\/wp-json\/wp\/v2\/posts\/122\/revisions"}],"predecessor-version":[{"id":146,"href":"https:\/\/thorshov.net\/index.php\/wp-json\/wp\/v2\/posts\/122\/revisions\/146"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/thorshov.net\/index.php\/wp-json\/wp\/v2\/media\/139"}],"wp:attachment":[{"href":"https:\/\/thorshov.net\/index.php\/wp-json\/wp\/v2\/media?parent=122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thorshov.net\/index.php\/wp-json\/wp\/v2\/categories?post=122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thorshov.net\/index.php\/wp-json\/wp\/v2\/tags?post=122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}