{"id":47,"date":"2025-01-15T22:56:39","date_gmt":"2025-01-15T22:56:39","guid":{"rendered":"https:\/\/blogs.oregonstate.edu\/bytebybyte\/?p=47"},"modified":"2025-01-15T22:56:39","modified_gmt":"2025-01-15T22:56:39","slug":"sniffing-out-code-smells-a-clean-code-guide","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/bytebybyte\/2025\/01\/15\/sniffing-out-code-smells-a-clean-code-guide\/","title":{"rendered":"Sniffing Out Code Smells: A Clean Code Guide"},"content":{"rendered":"\n<p>After reading Chapter 1, <em>Clean Code<\/em> by Robert C. Martin, and Chapter 3, <em>Bad Code Smells<\/em> by Martin Fowler, I gained valuable insights into writing and maintaining high-quality software. Both chapters emphasize the importance of clean, readable, and maintainable code, while highlighting the consequences of poor practices. Martin\u2019s emphasis on crafting code that communicates clearly and Fowler\u2019s detailed exploration of common &#8220;code smells&#8221; provide actionable guidance for writing better software. Inspired by these readings, I reflected on practices I want to adopt and avoid to improve my coding habits.<\/p>\n\n\n\n<h2 class=\"wp-block-heading has-medium-font-size\">One Thing to <s>Start<\/s>Continue Doing: Writing Self-Explanatory Code<\/h2>\n\n\n\n<p>As Robert Martin states in <em>Clean Code: <\/em><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>&#8220;<em>Indeed, the ratio of time spent reading versus writing is well over 10 to 1. We are constantly reading old code as part of the effort to write new code.&#8221;<\/em><\/p>\n<\/blockquote>\n\n\n\n<p>Fortunately, writing clear, self-explanatory code reduces maintenance costs and avoids unnecessary confusion. Below, I will share some examples of code, one that doesn&#8217;t communicate its intent well, and one that does. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function getValue(a) {\n    if (a &gt; 50) {\n        return a * 2;\n    }\n    return a \/ 2;\n}\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>function calculateAdjustedValue(inputValue) {\n    const adjustmentThreshold = 50;\n    return inputValue &gt; adjustmentThreshold \n        ? inputValue * 2 \n        : inputValue \/ 2;\n}<\/code><\/pre>\n\n\n\n<p>The second example does a far superior job at communicating intent, primarily by using variables that clearly explain the use of the function. <\/p>\n\n\n\n<h2 class=\"wp-block-heading has-medium-font-size\">One Thing to Avoid: Hidden Dependencies &amp; Code Duplication<\/h2>\n\n\n\n<p>As Martin Fowler notes in <em>Refactoring: <\/em><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>&#8220;Any fool can write code that a computer can understand. Good programmers write code that humans can understand.&#8221;<\/p>\n<\/blockquote>\n\n\n\n<p>Hidden dependencies and duplicated code make the system fragile and harder to extend or debug. Examples of hidden dependencies and explicit dependences will be shared below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Hidden Dependencies\ndef calculate_total_price(quantity):\n    tax_rate = 0.08  # Dependency hidden inside the function\n    return quantity * 100 * (1 + tax_rate)\n\n\n# Explicit Dependencies\ndef calculate_total_price(quantity, tax_rate):\n    return quantity * 100 * (1 + tax_rate)\n\n# Usage:\ntotal = calculate_total_price(quantity=5, tax_rate=0.08)\n<\/code><\/pre>\n\n\n\n<p>Now that tax_rate is being passed explicitly, the dependency is clear, making the function reusable and easier to test. Furthermore, an important principle of clean code is <a href=\"https:\/\/www.getdbt.com\/blog\/guide-to-dry\">DRY<\/a> (Don&#8217;t Repeat Yourself). Consequently, it is crucial to avoid creating duplicate code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Duplicated Logic\n\npublic int calculateRectangleArea(int width, int height) {\n    return width * height;\n}\n\npublic int calculateSquareArea(int side) {\n    return side * side;\n}\n\n# Abstracted Logic\npublic int calculateArea(int length, int width) {\n    return length * width;\n}<\/code><\/pre>\n\n\n\n<p>With an example like this, duplicate code seems awfully avoidable, but many more complex examples can quickly arise, and are important to avoid. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>After reading Chapter 1, Clean Code by Robert C. Martin, and Chapter 3, Bad Code Smells by Martin Fowler, I gained valuable insights into writing and maintaining high-quality software. Both chapters emphasize the importance of clean, readable, and maintainable code, while highlighting the consequences of poor practices. Martin\u2019s emphasis on crafting code that communicates clearly &hellip; <a href=\"https:\/\/blogs.oregonstate.edu\/bytebybyte\/2025\/01\/15\/sniffing-out-code-smells-a-clean-code-guide\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Sniffing Out Code Smells: A Clean Code Guide&#8221;<\/span><\/a><\/p>\n","protected":false},"author":14491,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-47","post","type-post","status-publish","format-standard","hentry","category-cs-462"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/bytebybyte\/wp-json\/wp\/v2\/posts\/47","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/bytebybyte\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/bytebybyte\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/bytebybyte\/wp-json\/wp\/v2\/users\/14491"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/bytebybyte\/wp-json\/wp\/v2\/comments?post=47"}],"version-history":[{"count":3,"href":"https:\/\/blogs.oregonstate.edu\/bytebybyte\/wp-json\/wp\/v2\/posts\/47\/revisions"}],"predecessor-version":[{"id":50,"href":"https:\/\/blogs.oregonstate.edu\/bytebybyte\/wp-json\/wp\/v2\/posts\/47\/revisions\/50"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/bytebybyte\/wp-json\/wp\/v2\/media?parent=47"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/bytebybyte\/wp-json\/wp\/v2\/categories?post=47"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/bytebybyte\/wp-json\/wp\/v2\/tags?post=47"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}