{"id":18,"date":"2024-01-18T19:27:11","date_gmt":"2024-01-18T19:27:11","guid":{"rendered":"https:\/\/blogs.oregonstate.edu\/jfcapstone\/?p=18"},"modified":"2024-01-18T19:33:25","modified_gmt":"2024-01-18T19:33:25","slug":"clean-code-and-code-smells","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/jfcapstone\/2024\/01\/18\/clean-code-and-code-smells\/","title":{"rendered":"Clean Code and Code Smells"},"content":{"rendered":"\n<p style=\"font-size:20px\">Winter 2024, the start of CS 462!<\/p>\n\n<p style=\"font-size:20px\">I chose to read <a href=\"https:\/\/reflectoring.io\/clean-code\/\"> this article on clean code<\/a><br>and <a href=\"https:\/\/pragmaticways.com\/31-code-smells-you-must-know\/\">this article on code smells.<\/a><\/p>\n\n<p style=\"font-size:20px\">What is one thing from the articles that you would like to start doing (more often), and why? What is one thing you want to avoid doing and why?<\/p>\n\n<p style=\"font-size:20px\">An area I always strive to improve in is applying comments strategically. I have vascillated (but, I think, improved overall) in my commenting style since I started going to school for Computer Science. In my lower-division classes, I developed really poor coding practices in that I rarely, if ever, actually commented. In part, this habit developed because the initial programs I was coding were so simple that they could easily be deciphered without comments, but because I didn&#8217;t correct this early on, it continued to my more complicated code. The code snippet below is an example from one of my early programming classes that could sorely use some commenting:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (rr + rw &gt;= 1)\n{\n   for (int i=0; i&lt;set.size(); i++)\n   {\n      string temp = set&#091;i];\n\n      if (temp&#091;0] != n1000 &amp;&amp; temp&#091;1] != n1000 &amp;&amp; temp&#091;2] != n1000 &amp;&amp; temp&#091;3] != n1000)\n         set.erase(set.begin()+i);\n\n      if (temp&#091;0] != n100 &amp;&amp; temp&#091;1] != n100 &amp;&amp; temp&#091;2] != n100 &amp;&amp; temp&#091;3] != n100)\n         set.erase(set.begin()+i);\n\n      if (temp&#091;0] != n10 &amp;&amp; temp&#091;1] != n10 &amp;&amp; temp&#091;2] != n10 &amp;&amp; temp&#091;3] != n10)\n         set.erase(set.begin()+i);\n\n      if (temp&#091;0] != n1 &amp;&amp; temp&#091;1] != n1 &amp;&amp; temp&#091;2] != n1 &amp;&amp; temp&#091;3] != n1)\n         set.erase(set.begin()+i);\n   }\n}<\/code><\/pre>\n\n\n\n<p style=\"font-size:20px\">When I realized that this was an issue that really needed correction, I ended up overcorrecting and used comments a little excessively (a better problem to have, but a problem nonetheless). The following code snippet is an example from a later course with some truly unnecessary comments:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class MonthDays\n{\n...\n  \/\/constructor for class\n  public MonthDays(int month, int year)\n  {\n    setMonth(month);\n    setYear(year);\n  }\n...\n  \/\/retrieves the month\n  public int getMonth() \n  {\n    return month;\n  }\n...\n  \/\/returns the year\n  public int getYear() \n  {\n    return year;\n  } \n...\n}<\/code><\/pre>\n\n\n\n<p style=\"font-size:20px\">I feel like I&#8217;ve improved a lot in my commenting practices at this point. Functions and blocks of logic generally have a comment quickly explaining their purpose, non-obvious bug fixes have comments explaining why something needs to be a certain way to avoid repeat mistakes, and blocks of code with similar functionality will have one accompanying explanation comment. I know though, that I still tend to overcommenting and it&#8217;s something I actively try to be aware of and will continue to try to improve going forward.<\/p>\n\n<p style=\"font-size:20px\">A practice I would really like to avoid is long parameter lists (#9 on the code smells article for reference). I chose this because it&#8217;s honestly something I&#8217;ve never thought about before, but is something I&#8217;ve noticed in code I&#8217;ve used (like imported classes or skeleton code). I do think I&#8217;ve done a pretty good job of staying away from this in my own code naturally, but I&#8217;d like to be more cognizant in avoiding it intentionally going forward.<br><br>The article makes a great point that if your parameter list is too long, your function is probably doing too much. But for me, the biggest issue comes with usability. Long parameter lists make it challenging to use a function. The programmer has to consider too many things &#8211; &#8220;What order do these go in?&#8221; &#8220;Which arguments are needed for this call?&#8221; &#8220;What types are used for which variables (especially when looking at number types &#8211; is it a decimal, float, int, double)?&#8221; If I have to look at a function header 3+ times to get all the parameters in, it&#8217;s most likely too long to be practical.<br><br>As an example, the crosstab function in the pandas library is, in my opinion, challenging from a usability perspective because of the number of parameters used. Below is the function header, but <a href=\"https:\/\/pandas.pydata.org\/docs\/reference\/api\/pandas.crosstab.html#pandas.crosstab\">here is the doc page for reference.<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pandas.crosstab(index, columns, values=None, rownames=None, colnames=None, aggfunc=None, margins=False, margins_name='All', dropna=True, normalize=False)<\/code><\/pre>\n\n\n\n<p style=\"font-size:20px\">These were some great examples of what to be more mindful of in my code, but reading through all of the other best practices on clean code and code smells were incredibly helpful as well. I think just having taken the time to think through all of these will make me more mindful in writing and reviewing my work.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Winter 2024, the start of CS 462! I chose to read this article on clean codeand this article on code smells. What is one thing from the articles that you would like to start doing (more often), and why? What is one thing you want to avoid doing and why? An area I always strive [&hellip;]<\/p>\n","protected":false},"author":13826,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-18","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/jfcapstone\/wp-json\/wp\/v2\/posts\/18","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/jfcapstone\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/jfcapstone\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/jfcapstone\/wp-json\/wp\/v2\/users\/13826"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/jfcapstone\/wp-json\/wp\/v2\/comments?post=18"}],"version-history":[{"count":7,"href":"https:\/\/blogs.oregonstate.edu\/jfcapstone\/wp-json\/wp\/v2\/posts\/18\/revisions"}],"predecessor-version":[{"id":26,"href":"https:\/\/blogs.oregonstate.edu\/jfcapstone\/wp-json\/wp\/v2\/posts\/18\/revisions\/26"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/jfcapstone\/wp-json\/wp\/v2\/media?parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/jfcapstone\/wp-json\/wp\/v2\/categories?post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/jfcapstone\/wp-json\/wp\/v2\/tags?post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}