{"id":134,"date":"2016-07-29T17:10:12","date_gmt":"2016-07-29T17:10:12","guid":{"rendered":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/?page_id=134"},"modified":"2020-09-04T10:23:57","modified_gmt":"2020-09-04T14:23:57","slug":"dos-and-donts-of-style","status":"publish","type":"page","link":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/dos-and-donts-of-style\/","title":{"rendered":"Do&#8217;s and Don&#8217;ts of Style"},"content":{"rendered":"<!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD HTML 4.0 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/REC-html40\/loose.dtd\">\n<html><body><p>A great programmer makes their code <strong>clear<\/strong>, <strong>robust<\/strong>, <strong>efficient<\/strong>, and <strong>user-friendly<\/strong>.<br>\nCode needs to be clear so others can read and understand your code.<br>\nCode needs to be <a href=\"#robust-defn\">robust<\/a>* to reduce the number of errors in your program.<br>\nCode needs to be efficient to reduce the cost of your code.<br>\nCode needs to be user-friendly so that people will want to use your program.<\/p>\n<p>Here are some Do&rsquo;s and Don&rsquo;ts that will guide you to becoming a better programmer. <strong>Bad style will cost your grade<\/strong> so keep these in mind and think of your own as well.<\/p>\n<p><strong>DO:<\/strong><\/p>\n<ul><li>Comment your code concisely<\/li>\n<li>Give your variables and functions good names for clarity<\/li>\n<li>End your phrases with semi-colons<\/li>\n<li>Indent your code properly<\/li>\n<li>Include your name, andrew ID, and section on the top of your code<\/li>\n<\/ul><p><strong>DON&rsquo;T:<br><\/strong><\/p>\n<ul><li>Have more than 80 characters in a line.<\/li>\n<li>Have dead code (code that does nothing, or commented out code)<\/li>\n<li>Use meaningless variable names<\/li>\n<li>Use magic numbers (numbers that come out of nowhere)<\/li>\n<li>Add too many comments, or redundant comments.<\/li>\n<li>Use tab characters &ndash; <a href=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/developing-p5js-programs#configure\">fix your editor to insert spaces instead of tabs<\/a>.<\/li>\n<\/ul><p>Here is a comparison of good and terrible code. If you can&rsquo;t differentiate the two, ask for assistance as soon as possible.<\/p>\n<pre class=\"lang:js decode:true \" title=\"Good style example\">\/*\n * Andrew Carnegie\n * andrew@andrew.cmu.edu\n * Section Z\n *\n * This program draws gray concentric circles.\n *\/\n\nfunction setup() {\n    createCanvas(600, 600); \/\/ Sets up the canvas size\n    background(50); \/\/ background is dark gray\n}\n\nfunction draw() {\n    \/\/ draw a 4-ringed circle in the center of the screen\n    drawConcentric(width \/ 2, height \/ 2, width \/ 3, 4);\n}\n\nfunction drawConcentric(xLoc, yLoc, radius, numRings) {\n    \/\/ Interate through numRings and draw circles of decreasing\n    \/\/ size.\n    for (i = 1; i &lt;= numRings; i++) {\n        ellipse(xLoc, yLoc, radius \/ i, radius \/ i);\n    }\n}<\/pre>\n<pre class=\"lang:js decode:true \" title=\"Bad style example\">\/\/ Lol comments\n\nfunction setup()\n{\n    createCanvas(600, 600);\n background(50);\n background(2);\n    background(7);\n}\n\nfunction draw() {\n    \/\/ draw a 4-ringed circle in the center of the screen\n    Draw2(300, 300, 100, 4);\n}\n\nfunction Draw2(v1, v2, v3, v4) {\nfor (i=1; i&lt;=numRings; i++) {\n    ellipse(v1, v2, v3\/ i, v3 \/ i)\n}\n}\n\nfunction DRAWSOMETHING() {\nrect(100, 100, 100, 100) \/\/ Lol rekt\n}<\/pre>\n<p id=\"robust-defn\"><em>*<\/em>&ldquo;Robust&rdquo; is a great adjective that, when talking about software, has a fairly specific and technical meaning. Robust software is software that works without depending on many assumptions or conditions. A program that crashes when an expected file is not found is not robust. A program that reports the missing file, prompts the user to find the file, or presses on using reasonable default values is more robust.<\/p><\/body><\/html>\n","protected":false},"excerpt":{"rendered":"<p>A great programmer makes their code clear, robust, efficient, and user-friendly. Code needs to be clear so others can read and understand your code. Code needs to be robust* to reduce the number of errors in your program. Code needs to be efficient to reduce the cost of your code. Code needs to be user-friendly &hellip; <a href=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/dos-and-donts-of-style\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Do&#8217;s and Don&#8217;ts of Style&#8221;<\/span><\/a><\/p>\n","protected":false},"author":535,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/pages\/134"}],"collection":[{"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/users\/535"}],"replies":[{"embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/comments?post=134"}],"version-history":[{"count":10,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/pages\/134\/revisions"}],"predecessor-version":[{"id":55252,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/pages\/134\/revisions\/55252"}],"wp:attachment":[{"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/media?parent=134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}