{"id":37,"date":"2023-01-29T03:01:59","date_gmt":"2023-01-29T03:01:59","guid":{"rendered":"https:\/\/blogs.oregonstate.edu\/maxdiebold\/?p=37"},"modified":"2023-01-29T03:01:59","modified_gmt":"2023-01-29T03:01:59","slug":"smells-like-clean-spirit","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/maxdiebold\/2023\/01\/29\/smells-like-clean-spirit\/","title":{"rendered":"Smells Like Clean Spirit"},"content":{"rendered":"\n<p><strong>Clean Code<\/strong> and <strong>Code Smells<\/strong> are tightly intertwined: unclean leads to code smells more often than not, but they differ in their consequences and subjectivity. While some code smells relate to stylistic preferences, others detract from the long-term maintainability of a project.<\/p>\n\n\n\n<p>In a survey of 85 professional developers, Yamashita and Moonen discovered that a large percentage of developers cared little about smells in their code, and a significant portion of these developers were unaware of the concept<sup><a href=\"#footnotes\">1<\/a><\/sup>. Those that were aware of the concept were asked to rank the significance of each smell\/anti-pattern, with the following results:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6231\/files\/2023\/01\/ranking-code-smells.png\" alt=\"\" class=\"wp-image-39\" width=\"323\" height=\"293\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6231\/files\/2023\/01\/ranking-code-smells.png 566w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6231\/files\/2023\/01\/ranking-code-smells-300x272.png 300w\" sizes=\"auto, (max-width: 323px) 100vw, 323px\" \/><figcaption class=\"wp-element-caption\">Source: <em>Do Developers Care about Code Smells? An Exploratory Survey<\/em><\/figcaption><\/figure>\n\n\n\n<p>The survey results illustrated that developers almost never intentionally refactored to remove bad code smells. In response, the &#8220;Clean Code&#8221; movement took up arms, drafting an approach to prevent such odors from coming into existence. Although the capitalized phrase evokes the sense of a rigid framework adopted by snobbish elites with large bumper stickers, clean code is actually a fluid concept that encompasses a wide range of practices and choices. A core belief is that, early on, projects tend to focus on platform technologies and state-of-the-art patterns instead of code quality, and that this leads to ever-increasing <a href=\"https:\/\/en.wikipedia.org\/wiki\/Technical_debt\">technical debt<\/a>. This increases the chances of a <em>bad fix<\/em> being employed in the case of a bug, which may propagate more code smells.<\/p>\n\n\n\n<p>Latte, Henning, &amp; Wojcieszak propose that tool-chains and practices should be employed from the start to strictly enforce clena code. Their approach is based on four factors<sup><a href=\"#footnotes\">2<\/a><\/sup>:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Clean code and code review\n<ul class=\"wp-block-list\">\n<li>Code style guides should be drafted, including, but not limited to, formatting, consistent use of language features, naming conventions, and use of meaningful names.<\/li>\n\n\n\n<li>In cases of non-compliance, code reviewers should openly discuss the issues, and refactor immediately.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Test-Driven Development (TDD)\n<ul class=\"wp-block-list\">\n<li>This facilitates later refactoring, and keeps a short development loop.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Static code analysis\n<ul class=\"wp-block-list\">\n<li>This includes tools for checking syntax, coding mistakes, and security  vulnerabilities, which may be run externally or added to an Integrated Development Environment.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Continuous Integration \/ Continuous Delivery (CI\/CD)\n<ul class=\"wp-block-list\">\n<li>This automated quality enforcement forces developers to immediately refactor if a step in the pipeline fails, and provides various levels of feedback.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">My Takeaways<\/h4>\n\n\n\n<p>As far as my personal coding habits go, I already incorporate a lot of static analysis tools into my editor, including automatic formatting, linters, style\/naming suggestions, etc. In addition, My project teams utilize other tools in our build pipelines: <a href=\"https:\/\/en.wikipedia.org\/wiki\/CircleCI\">CircleCI<\/a>, <a href=\"https:\/\/github.com\/features\/actions\">GitHub Actions<\/a>, and <a href=\"https:\/\/www.sonarsource.com\/products\/sonarcloud\/?gads_campaign=SC-USA-Brand&amp;gads_ad_group=SonarCloud&amp;gads_keyword=sonarcloud\">SonarCloud<\/a>, to be specific. We also use <a href=\"https:\/\/www.zaproxy.org\/\">OWASP ZAP<\/a> to scan for security vulnerabilities before each deployment.<\/p>\n\n\n\n<p>For my own sanity, I tend to refactor very often, since I&#8217;m a big fan of <a href=\"https:\/\/en.wikipedia.org\/wiki\/Self-documenting_code\">self-documenting code<\/a>. This keeps my functions and classes quite short, but in the future I&#8217;d like adopt Test-Driven Development, instead of testing after the fact. This would <em>force<\/em> functions and classes to be short and poignant, and would likely reduce the total number of refactorizations.<\/p>\n\n\n\n<p>I&#8217;d also like to focus more on avoiding complex conditionals. SonarCloud does good job of pointing these out, but I could prevent them from arising in the first place by focusing on small functions with single responsibilities..<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"footnotes\">References<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Yamashita, A., &amp; Moonen, L. (2013). Do Developers Care about Code Smells? An Exploratory Survey.<\/li>\n\n\n\n<li>Latte, B., Henning, S., &amp; Wojcieszak, M. (2019). Clean Code: On the Use of Practices and Tools to Produce Maintainable Code for Long-Living Software.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Clean Code and Code Smells are tightly intertwined: unclean leads to code smells more often than not, but they differ in their consequences and subjectivity. While some code smells relate to stylistic preferences, others detract from the long-term maintainability of a project. In a survey of 85 professional developers, Yamashita and Moonen discovered that a [&hellip;]<\/p>\n","protected":false},"author":12993,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-37","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/maxdiebold\/wp-json\/wp\/v2\/posts\/37","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/maxdiebold\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/maxdiebold\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/maxdiebold\/wp-json\/wp\/v2\/users\/12993"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/maxdiebold\/wp-json\/wp\/v2\/comments?post=37"}],"version-history":[{"count":2,"href":"https:\/\/blogs.oregonstate.edu\/maxdiebold\/wp-json\/wp\/v2\/posts\/37\/revisions"}],"predecessor-version":[{"id":40,"href":"https:\/\/blogs.oregonstate.edu\/maxdiebold\/wp-json\/wp\/v2\/posts\/37\/revisions\/40"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/maxdiebold\/wp-json\/wp\/v2\/media?parent=37"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/maxdiebold\/wp-json\/wp\/v2\/categories?post=37"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/maxdiebold\/wp-json\/wp\/v2\/tags?post=37"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}