{"id":38,"date":"2005-06-22T16:39:38","date_gmt":"2005-06-22T07:39:38","guid":{"rendered":"https:\/\/regex.info\/blog\/2005-06-22\/38"},"modified":"2005-06-22T16:41:15","modified_gmt":"2005-06-22T07:41:15","slug":"the-coding-commands-according-to-dr-bergeron","status":"publish","type":"post","link":"https:\/\/regex.info\/blog\/2005-06-22\/38","title":{"rendered":"The (Coding) Commands According to Dr. Bergeron"},"content":{"rendered":"<p>In my first semester at <a href=\"http:\/\/www.cs.unh.edu\/\">grad school<\/a>\n(wow, now 18 years ago... I'm old), all CS master's students were required\nto take two intensive &#8220;<i>this is what you should have learned during undergrad<\/i> classes&#8221;, <b>CS801<\/b> (theory) and <b>CS802<\/b>\n(practice). <span class='nobr'>The latter<\/span> was taught by <a\nhref=\"http:\/\/www.cs.unh.edu\/~rdb\/rdb.html\">Dr. Dan Bergeron<\/a>, who was\n&mdash; and probably still is &mdash; an excellent teacher, but whom <span class='nobr'>I remember<\/span> most\nas <span class='nobr'>a man<\/span> of stellar integrity and fairness &mdash; <span class='nobr'>a good<\/span> father-figure that\nmost collegiate environments lack.\n\n<\/p><p>Anyway, while rummaging around some old papers the other day, <span class='nobr'>I found<\/span> this handout on &#8220;coding commandments&#8221; from the class. (It\nseems that programming in the class was done in <a\nhref=\"http:\/\/en.wikipedia.org\/wiki\/Pascal_programming_language\">Pascal<\/a>\n&mdash; ugh.) The commandments are still as relevant today as then (well,\nperhaps the last one is more &#8220;advice&#8221; than\n&#8220;commandment&#8221;, and about some others, <span class='nobr'>I do remember<\/span> bristling\nat some of them, but <span class='nobr'>I no<\/span> longer remember which.)\n\n<\/p><p>I thought some of you might enjoy this, so pass it along...\n\n<\/p><table style=\"background-color:#999977; border:solid black 1px; color:black; margin: 20px; padding: 20px 1.5em\" cellpadding=\"10\"><tr><td><table> <tr valign='top'><td colspan=\"2\">\n <p align=\"center\"> Coding Commandments <br \/>CS802 - Fall 1987 <br\n\/><span class='nobr'>October 30,<\/span> 1987 <\/p><p>\n\nThe programming style conventions listed below <b>shall<\/b> be followed for\nprograms submitted in this course. <span class='nobr'>I firmly<\/span> believe programs that follow\nthese guidelines are more readable, more comprehensible and more\nmaintainable than programs that do not follow the conventions. Furthermore,\nthese conventions are consistent with well-established philosophies that\ndefine what constitutes good software engineering and are not just my\nidiosyncrasies (in spite of the fact that lots of existing software does\nnot follow these guidelines). Finally, <span class='nobr'>I will be<\/span> the last to claim that\nthere are no circumstances in which these guidelines should be violated.\n(In this course, however, there will be no exceptions!)<\/p><\/td><\/tr>\n\n<tr valign=\"top\"><td><p>(1)&nbsp;&nbsp;<\/p><\/td><td><p>Every\nprocedure\/function shall have exactly 1 <b>return<\/b> statement and that\nstatement will appear at the bottom of the procedure as the last executable\nstatement of the procedure. <\/p><\/td><\/tr><tr\nvalign=\"top\"><td><p>(2)&nbsp;&nbsp;<\/p><\/td><td><p> Every identifier used\nin the program shall have an associated comment that describes what the\nfunction of the identifier is. This includes symbolic constants, types,\nfields in records, parameters, variables, etc.\n\n<\/p><\/td><\/tr><tr valign=\"top\"><td><p>(3)&nbsp;&nbsp;<\/p><\/td><td><p> Every\nprocedure\/function shall be preceded by <span class='nobr'>a clear<\/span> separator (such as <span class='nobr'>a row<\/span> of\nasterisks or dashes or whatever) with <span class='nobr'>a clear<\/span> indication of the procedure\nname and <span class='nobr'>a brief<\/span> comment describing the essential function provided by that\nfunction.\n\n<\/p><\/td><\/tr><tr valign=\"top\"><td><p>(4)&nbsp;&nbsp;<\/p><\/td><td><p>A long\nprocedure is a bad procedure. <span class='nobr'>All the code<\/span> for <span class='nobr'>a procedure<\/span> should fit on no\nmore than one page, so that it can be readily comprehended by <span class='nobr'>a reader.<\/span>\n\n<\/p><\/td><\/tr><tr\nvalign=\"top\"><td><p>(5)&nbsp;&nbsp;<\/p><\/td><td><p>Indentation of code is a\nvery important tool in producing understandable code in which the control\nflow is readily discernible. <span class='nobr'>Use good indentation<\/span> that clearly shows the\ncontrol flow. Don't let comments or debugging code obscure the indentation.\nDon't indent more than 2-4 blanks per nesting level.\n\n<\/p><\/td><\/tr><tr valign=\"top\"><td><p>(6)&nbsp;&nbsp;<\/p><\/td><td><p>Use a\nblank line to clearly identify major loop or conditional statements. <span class='nobr'>A brief<\/span> comment at the end of the line with the statement keyword is also\nvery helpful if it tells at <span class='nobr'>a high<\/span> level what the statement does. <span class='nobr'>If you have<\/span> <span class='nobr'>a loop<\/span> or conditional that covers more than 8 lines or so, make sure\nthat the <b>end<\/b> statement includes <span class='nobr'>a comment<\/span> that identifies which\nstatement is being ended. <span class='nobr'>If there<\/span> are nested statements of the same type,\nbe sure to include enough information to clearly identify the right one.\n(For example, <b>end { while i <> 0 }<\/b>)\n\n\n<\/p><\/td><\/tr><tr valign=\"top\"><td><p>(7)&nbsp;&nbsp;<\/p><\/td><td><p>If you\nhave an <b>if-then-else<\/b> statement with 1 short alternative and one long\nalternative, arrange the condition so that the short alternative is the\n<b>then<\/b> clause, and the long one is the <b>else<\/b>.\n\n<\/p><\/td><\/tr><tr valign=\"top\"><td><p>(8)&nbsp;&nbsp;<\/p><\/td><td><p>Use\nnested <b>else-if<\/b> in preference to nested <b>then-if<\/b>.\n\n<\/p><\/td><\/tr><tr valign=\"top\"><td><p>(9)&nbsp;&nbsp;<\/p><\/td><td><p>Every\nloop should have exactly one exit. <span class='nobr'>Do not use<\/span> a <b>loop<\/b> statement if a\n<b>while<\/b> or <b>repeat<\/b> is what you mean.\n\n<\/p><\/td><\/tr><tr valign=\"top\"><td><p>(10)&nbsp;&nbsp;<\/p><\/td><td><p>Use\nmeaningful variable names.\n\n<\/p><\/td><\/tr><tr valign=\"top\"><td><p>(11)&nbsp;&nbsp;<\/p><\/td><td><p>Use\nsymbolic constants excessively.\n\n<\/p><\/td><\/tr><tr valign=\"top\"><td><p>(12)&nbsp;&nbsp;<\/p><\/td><td><p>If\nyou use arrays, you must always check the index for exceeding the bounds\nbefore trying to access the array element indicated by the index.\n\n<\/p><\/td><\/tr><tr valign=\"top\"><td><p>(13)&nbsp;&nbsp;<\/p><\/td><td><p>If\nyou use lists, use a dummy <i>sentinel<\/i>, so that no list is ever empty.\nGenerally, it is also wise to always have <span class='nobr'>a tail<\/span> pointer for efficiency of\nsearch and addition to the list. For searching, store the key you wish to\nsearch for in the tail (which is the sentinel), so that you are guaranteed\nto find it. Finally, if you are bothering to implement <span class='nobr'>a list<\/span> at all, you\nmight as well implement two-way lists. <span class='nobr'>The extra<\/span> overhead of having the\n<i>back<\/i> pointer is insignificant and it makes deletion much easier.\n\n<\/p><\/td><\/tr><\/table><\/td><\/tr><\/table>\n","protected":false},"excerpt":{"rendered":"<p>In my first semester at grad school (wow, now 18 years ago... I'm old), all CS master's students were required to take two intensive \"this is what you should have learned during undergrad classes\", <b>CS801<\/b> (theory) and <b>CS802<\/b> (practice). The latter was taught by Dr. Dan Bergeron, who was -- and probably still is -- an excellent teacher, but whom I remember most as a man of stellar integrity and fairness -- a good father-figure that most collegiate environments lack. <\/p><p>Anyway, while rummaging around some old papers the other day, I found this handout on \"coding commandments\" from the class. [...]","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/regex.info\/blog\/wp-json\/wp\/v2\/posts\/38"}],"collection":[{"href":"https:\/\/regex.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/regex.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/regex.info\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/regex.info\/blog\/wp-json\/wp\/v2\/comments?post=38"}],"version-history":[{"count":0,"href":"https:\/\/regex.info\/blog\/wp-json\/wp\/v2\/posts\/38\/revisions"}],"wp:attachment":[{"href":"https:\/\/regex.info\/blog\/wp-json\/wp\/v2\/media?parent=38"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/regex.info\/blog\/wp-json\/wp\/v2\/categories?post=38"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/regex.info\/blog\/wp-json\/wp\/v2\/tags?post=38"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}