{"id":83,"date":"2025-01-16T17:33:07","date_gmt":"2025-01-16T17:33:07","guid":{"rendered":"https:\/\/blogs.oregonstate.edu\/codedcat\/?p=83"},"modified":"2025-01-16T17:33:07","modified_gmt":"2025-01-16T17:33:07","slug":"writing-clean-code-a-developers-commitment-to-excellence","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/codedcat\/2025\/01\/16\/writing-clean-code-a-developers-commitment-to-excellence\/","title":{"rendered":"Writing Clean Code: A Developer\u2019s Commitment to Excellence"},"content":{"rendered":"\n<p>Have you ever found yourself staring at old code and wondering, &#8220;What was I even thinking?&#8221; As developers, we strive for clean code\u2014code that is readable, maintainable, and efficient. However, achieving this ideal requires a conscious effort to adopt good practices and avoid common pitfalls, often referred to as &#8220;code smells.&#8221;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Clean Code: Practices Worth Embracing<\/h3>\n\n\n\n<p>Chapter 1 of Robert Martin\u2019s <em>Clean Code: A Handbook of Agile Software Craftsmanship<\/em> dives into meaningful naming, a habit that can transform messy scripts into something even your future self will thank you for. Martin argues that good names reveal intent, making code easier to understand and maintain. Inspired by this, I aim to adopt the habit of choosing descriptive, unambiguous names for variables, functions, and classes. For example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n# Clean Code Example:\ndef calculate_total_price(items):\n    &quot;&quot;&quot;Calculate the total price of available items in the cart.&quot;&quot;&quot;\n    return sum(item&#x5B;&#039;price&#039;] for item in items if item&#x5B;&#039;available&#039;])\n\n# Bad Code Example:\ndef calc(items):\n    total = 0\n    for i in items:\n        if i&#x5B;2]:\n            total += i&#x5B;1]\n    return total\n\n<\/pre><\/div>\n\n\n<p>The difference is huge\u2014the first example practically explains itself, while the second feels like trying to read a foreign language without a dictionary. As <a href=\"https:\/\/dev.to\/favourmark05\/writing-clean-code-best-practices-and-principles-3amh\">MFONIDO <\/a><a href=\"https:\/\/dev.to\/favourmark05\/writing-clean-code-best-practices-and-principles-3amh\">Mark<\/a> writes in their DEV Community article, clean code should read like well-written prose. Meaningful naming is a cornerstone of this principle.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Code Smells: Pitfalls to Avoid<\/h3>\n\n\n\n<p>Martin Fowler\u2019s Chapter 3 of <em>Refactoring: Improving the Design of Existing Code<\/em> provides an extensive discussion of code smells\u2014symptoms of poor design that can lead to bugs or excessive technical debt. One smell that particularly resonates with me is &#8220;duplicated code.&#8221;<\/p>\n\n\n\n<p>Duplicated code not only bloats a codebase but also creates maintenance challenges. Fowler advocates for refactoring duplicated logic into reusable functions or classes. <a href=\"https:\/\/dev.to\/hamzakhan\/code-smells-and-how-to-fix-them-a-practical-guide-for-developers-m3h\">Hamza Khan\u2019s DEV Community article<\/a> provides practical advice for identifying and resolving such smells. For example, consider this:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n# Code Smell: Duplicated Code\nif user_role == &#039;admin&#039;:\n    print(&quot;Access granted to admin dashboard&quot;)\nif user_role == &#039;editor&#039;:\n    print(&quot;Access granted to editor dashboard&quot;)\n\n# Refactored Code:\ndef grant_access(role):\n    access_messages = {\n        &#039;admin&#039;: &quot;Access granted to admin dashboard&quot;,\n        &#039;editor&#039;: &quot;Access granted to editor dashboard&quot;,\n    }\n    print(access_messages.get(role, &quot;Access denied&quot;))\n\ngrant_access(user_role)\n\n<\/pre><\/div>\n\n\n<p>By consolidating logic, we reduce redundancy and simplify future updates.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Reflection and Commitment<\/h3>\n\n\n\n<p>For me, writing clean code goes beyond just improving my own workflow\u2014it\u2019s about making life easier for anyone who might work with my code later. By adopting meaningful naming and avoiding duplication, I hope to make my code more readable and efficient.<\/p>\n\n\n\n<p>Though it takes some effort, the payoff\u2014a cleaner codebase and fewer headaches for everyone involved\u2014is absolutely worth it. As I continue my journey, I\u2019ll strive to apply these principles and explore others outlined by experts like Martin and Fowler.<\/p>\n\n\n\n<h3 class=\"wp-block-heading has-text-align-center\">A Picture for Purr-spective<\/h3>\n\n\n\n<p>No blog post of mine would be complete without a little feline flair! Meet <strong>Meatball Sandwich<\/strong>, my loyal coding companion and the undisputed king of car rides. Whether he\u2019s perched on the passenger seat or giving me judgmental looks from the dashboard, Meatball is always there to keep me inspired and on my toes.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"608\" height=\"808\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8085\/files\/2025\/01\/image.png\" alt=\"\" class=\"wp-image-84\" style=\"width:331px;height:auto\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8085\/files\/2025\/01\/image.png 608w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8085\/files\/2025\/01\/image-226x300.png 226w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/figure>\n<\/div>\n\n\n<p>Because let\u2019s be honest: even the cleanest code can\u2019t compete with this level of cuteness.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever found yourself staring at old code and wondering, &#8220;What was I even thinking?&#8221; As developers, we strive for clean code\u2014code that is readable, maintainable, and efficient. However, achieving this ideal requires a conscious effort to adopt good practices and avoid common pitfalls, often referred to as &#8220;code smells.&#8221; Clean Code: Practices Worth &hellip; <a href=\"https:\/\/blogs.oregonstate.edu\/codedcat\/2025\/01\/16\/writing-clean-code-a-developers-commitment-to-excellence\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Writing Clean Code: A Developer\u2019s Commitment to Excellence&#8221;<\/span><\/a><\/p>\n","protected":false},"author":14468,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-83","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/codedcat\/wp-json\/wp\/v2\/posts\/83","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/codedcat\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/codedcat\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/codedcat\/wp-json\/wp\/v2\/users\/14468"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/codedcat\/wp-json\/wp\/v2\/comments?post=83"}],"version-history":[{"count":1,"href":"https:\/\/blogs.oregonstate.edu\/codedcat\/wp-json\/wp\/v2\/posts\/83\/revisions"}],"predecessor-version":[{"id":85,"href":"https:\/\/blogs.oregonstate.edu\/codedcat\/wp-json\/wp\/v2\/posts\/83\/revisions\/85"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/codedcat\/wp-json\/wp\/v2\/media?parent=83"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/codedcat\/wp-json\/wp\/v2\/categories?post=83"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/codedcat\/wp-json\/wp\/v2\/tags?post=83"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}