{"id":113,"date":"2024-01-18T05:22:12","date_gmt":"2024-01-18T05:22:12","guid":{"rendered":"https:\/\/blogs.oregonstate.edu\/ayleeshomalicapstone\/?p=113"},"modified":"2024-01-18T05:22:12","modified_gmt":"2024-01-18T05:22:12","slug":"clean-code-a-brief-investigation","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/ayleeshomalicapstone\/2024\/01\/18\/clean-code-a-brief-investigation\/","title":{"rendered":"Clean Code: A Brief investigation"},"content":{"rendered":"\n<p>One of the most important aspects of coding is making sure that others can easily read and understand your code. That way, they can use what you have created and make changes to it in the future. In a team setting, such as our capstone course, it is especially critical to encourage and enforce clean code for all developers and establish clear conventions to follow as a team. <\/p>\n\n\n\n<p>I&#8217;ve reviewed two articles on clean code and code smells and put together two examples: one clean code practice that I would like to implement more often, and one not-so-good coding practice I would like to avoid.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Clean Code Practice<\/h2>\n\n\n\n<p>Reduce number of parameters\/arguments in methods [1]<\/p>\n\n\n\n<p>This can be accomplished by creating a new class of related parameters, if applicable, and sending the object itself as a parameter instead.<\/p>\n\n\n\n<p>For example, let&#8217;s consider a method that automatically fills form fields in a new user form as part of a testing script:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void FillUserForm (\n      string username, \n      string address,\n      datetime birthDate, \n      string email, \n      string password)\n{\n         FilllUsernameField(username);\n\n         FilllAddressField(address);\n\n         \/\/ Etc.\n}<\/code><\/pre>\n\n\n\n<p><strong>This can be updated to:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Create class definition for UserFormValues with all parameters as properties.\npublic class UserFormValues()\n{\n   \/\/ Add each property as part of the class.\n}\n\npublic void FillUserForm (\n      UserFormValues userFormValues)\n{\n         FilllUsernameField(userFormValues.Username);\n\n         FilllAddressField(userFormValues.Address);\n\n         \/\/ Etc.\n}<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Why do this?<\/h5>\n\n\n\n<p>Methods are easier to read when there aren&#8217;t so many parameters and are less complex. This further modularizes the code and simplifies it for easier reading, which is why I would like to do this more often.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Coding Practice to Avoid<\/h2>\n\n\n\n<p>Don\u2019t implement super long functions [2]<\/p>\n\n\n\n<p>Really long functions can be harder to reuse and take a much longer time to understand for someone who isn&#8217;t familiar with the code. In addition, long functions may also mean that a function is performing too many different goals or certain steps can be grouped into additional methods.<\/p>\n\n\n\n<p>For example, consider this function for a potential daily routine: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void DoDailyRoutine ()\n{\n       \/\/ Morning Routine.\n       GetOutOfBed();\n       TakeShower();\n       FeedCat();\n       HaveBreakfast();\n\n       \/\/ Do regular work.\n       LogInToWorkComputer();\n       ReviewTestScriptFailures();\n       FixScripts();\n       ReviewPullRequests();\n       LogOutOfWorkComputer();\n\n       \/\/ Do homework.\n       CompleteReading();\n       TakeQuiz();\n       WriteBlogPost(); \n       WriteDiscussionReply();         \n}<\/code><\/pre>\n\n\n\n<p>This can be updated to:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void DoDailyRoutine ()\n{\n       DoMorningRoutine();\n\n       DoWork();\n\n       DoHomework();       \n}\n\n\/\/ Create new methods.\npublic void DoMorningRoutine()\n{\n      GetOutOfBed();\n      TakeShower();\n      FeedCat();\n      HaveBreakfast();\n}\n\npublic void DoWork() \n{\n      LogInToWorkComputer();\n      ReviewTestScriptFailures();\n      FixScripts();\n      ReviewPullRequests();\n      LogOutOfWorkComputer();\n}\n\npublic void DoHomework()\n{\n      CompleteReading();\n      TakeQuiz();\n      WriteBlogPost(); \n      WriteDiscussionReply();\n}<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Why avoid this?<\/h5>\n\n\n\n<p>I want to avoid the coding practice of too long of functions (around 10 lines or more) to help keep code cleaner, more modular, and easier to understand. Another reason would be to make the three new methods easier to reuse outside of the main method, since writing out the group of methods they call every time would no longer be needed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>Clean code will always be a crucial part of programming and there are many interesting practices that have been documented to help reach this goal. Through researching these two articles, I will continue to work towards writing cleaner code that helps not only future me, but my capstone team, and my coworkers \u2764\ufe0f<\/p>\n\n\n\n<p><strong>References<\/strong><\/p>\n\n\n\n<p>[1] O. Olsson, \u201cMy favorite code smells \ufe0f\u2764\ufe0f,\u201d Medium, https:\/\/medium.com\/@happybits\/my-favorite-code-smells-%EF%B8%8F-%EF%B8%8F-a14942335c9b (accessed Jan. 17, 2024).<\/p>\n\n\n\n<p>[2] S. Hettiarachchi, \u201c10 clean coding practices,\u201d Medium, https:\/\/medium.com\/swlh\/10-clean-coding-practices-e37ac283184d (accessed Jan. 17, 2024).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the most important aspects of coding is making sure that others can easily read and understand your code. That way, they can use what you have created and make changes to it in the future. In a team setting, such as our capstone course, it is especially critical to encourage and enforce clean [&hellip;]<\/p>\n","protected":false},"author":13648,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,4],"tags":[],"class_list":["post-113","post","type-post","status-publish","format-standard","hentry","category-capstone-project","category-general"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/ayleeshomalicapstone\/wp-json\/wp\/v2\/posts\/113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/ayleeshomalicapstone\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/ayleeshomalicapstone\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/ayleeshomalicapstone\/wp-json\/wp\/v2\/users\/13648"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/ayleeshomalicapstone\/wp-json\/wp\/v2\/comments?post=113"}],"version-history":[{"count":15,"href":"https:\/\/blogs.oregonstate.edu\/ayleeshomalicapstone\/wp-json\/wp\/v2\/posts\/113\/revisions"}],"predecessor-version":[{"id":128,"href":"https:\/\/blogs.oregonstate.edu\/ayleeshomalicapstone\/wp-json\/wp\/v2\/posts\/113\/revisions\/128"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/ayleeshomalicapstone\/wp-json\/wp\/v2\/media?parent=113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/ayleeshomalicapstone\/wp-json\/wp\/v2\/categories?post=113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/ayleeshomalicapstone\/wp-json\/wp\/v2\/tags?post=113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}