{"id":21,"date":"2025-01-16T20:30:45","date_gmt":"2025-01-16T20:30:45","guid":{"rendered":"https:\/\/blogs.oregonstate.edu\/nicholasnelson\/?p=21"},"modified":"2025-01-16T20:30:45","modified_gmt":"2025-01-16T20:30:45","slug":"clean-code-vs-code-smells","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/nicholasnelson\/2025\/01\/16\/clean-code-vs-code-smells\/","title":{"rendered":"Clean Code vs. Code-Smells"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p>As an aspiring software engineer constantly learning new coding concepts and strategies, my coding practices have been improving both directly and subconsciously. Now that I am reaching the end of my journey as only a student of computer science, It&#8217;s important to recognize that mastering clean code principles and eliminating code smells are vital in building maintainable codebases . <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Clean Code<\/h2>\n\n\n\n<p>After reading an article from FreeCodeCamp.org on writing clean code, one thing I want to start doing more often is called the Single Responsibility Principle (SRP) which states that every class or module should only have one responsibility. <\/p>\n\n\n\n<p>Here are some examples found on FreeCodeCamp.org:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Example 1: Withouth SRP\nfunction processOrder(order) {\n  \/\/ validate order\n  if (order.items.length === 0) {\n    console.log(\"Error: Order has no items\");\n    return;\n  }\n\n  \/\/ calculate total\n  let total = 0;\n  order.items.forEach(item =&gt; {\n    total += item.price * item.quantity;\n  });\n\n  \/\/ apply discounts\n  if (order.customer === \"vip\") {\n    total *= 0.9;\n  }\n\n  \/\/ save order\n  const db = new Database();\n  db.connect();\n  db.saveOrder(order, total);\n}<\/code><\/pre>\n\n\n\n<p>The above code block shows processOrder which isn&#8217;t utilizing SRP. It handles several functions, which makes it hard to understand. Furthermore a change to one function may change the others which makes it hard to maintain. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Example 2: With SRP\nclass OrderProcessor {\n  constructor(order) {\n    this.order = order;\n  }\n\n  validate() {\n    if (this.order.items.length === 0) {\n      console.log(\"Error: Order has no items\");\n      return false;\n    }\n    return true;\n  }\n\n  calculateTotal() {\n    let total = 0;\n    this.order.items.forEach(item =&gt; {\n      total += item.price * item.quantity;\n    });\n    return total;\n  }\n\n  applyDiscounts(total) {\n    if (this.order.customer === \"vip\") {\n      total *= 0.9;\n    }\n    return total;\n  }\n}\n\nclass OrderSaver {\n  constructor(order, total) {\n    this.order = order;\n    this.total = total;\n  }\n\n  save() {\n    const db = new Database();\n    db.connect();\n    db.saveOrder(this.order, this.total);\n  }\n}\n\nconst order = new Order();\nconst processor = new OrderProcessor(order);\n\nif (processor.validate()) {\n  const total = processor.calculateTotal();\n  const totalWithDiscounts = processor.applyDiscounts(total);\n  const saver = new OrderSaver(order, totalWithDiscounts);\n  saver.save();\n}<\/code><\/pre>\n\n\n\n<p>This above code shows the same method but this time utilizing SRP. The processOrder has been split up into multiple classes, OrderProcessor and OrderSaver which gives each classes its own functionality. This makes the code easier to understand and maintain. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Code-Smells<\/h2>\n\n\n\n<p>A code smell, which are indicators in code that reveal that there may be a problem in the design of the code, is something I am unashamed to admit that I am guilty of. The first step in correcting a problem is to admit you have one, and there is one thing that I wish to avoid doing in the future to make my code less smelly. <\/p>\n\n\n\n<p>Code duplication an easy issue to create, as it can be so easy to just ignore it because its functional, but failure to correct these kinds of problems early can make the codebase unmaintainable. By performing refactoring, one can remove duplicated code.<\/p>\n\n\n\n<p>Here are some examples of what I mean that I found on Medium.com:<\/p>\n\n\n\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-ad2f72ca wp-block-group-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"273\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8109\/files\/2025\/01\/Screenshot-2025-01-16-120208-1-1024x273.png\" alt=\"\" class=\"wp-image-25\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8109\/files\/2025\/01\/Screenshot-2025-01-16-120208-1-1024x273.png 1024w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8109\/files\/2025\/01\/Screenshot-2025-01-16-120208-1-300x80.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8109\/files\/2025\/01\/Screenshot-2025-01-16-120208-1-768x205.png 768w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8109\/files\/2025\/01\/Screenshot-2025-01-16-120208-1-1536x410.png 1536w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8109\/files\/2025\/01\/Screenshot-2025-01-16-120208-1-624x167.png 624w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8109\/files\/2025\/01\/Screenshot-2025-01-16-120208-1.png 1678w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><br><\/p>\n<\/div>\n\n\n\n<p>The above code block shows two functions can contain duplicate code, one function to calculate the area of a rectangle, and the other to calculate the perimeter. While they do  serve to distinct purposes, the code is so incredibly similar they don&#8217;t need to be in two different functions. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"414\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8109\/files\/2025\/01\/Screenshot-2025-01-16-120120-1024x414.png\" alt=\"\" class=\"wp-image-26\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8109\/files\/2025\/01\/Screenshot-2025-01-16-120120-1024x414.png 1024w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8109\/files\/2025\/01\/Screenshot-2025-01-16-120120-300x121.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8109\/files\/2025\/01\/Screenshot-2025-01-16-120120-768x311.png 768w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8109\/files\/2025\/01\/Screenshot-2025-01-16-120120-1536x621.png 1536w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8109\/files\/2025\/01\/Screenshot-2025-01-16-120120-624x252.png 624w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8109\/files\/2025\/01\/Screenshot-2025-01-16-120120.png 1688w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>The code block above shows the refactored version, which creates a function that utilizes the logic of the area function, and simply reuses it for the perimeter. This solves the issue with duplicate logic and improves maintainability. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Sources<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.freecodecamp.org\/news\/how-to-write-clean-code\/?utm_source=chatgpt.com\">https:\/\/www.freecodecamp.org\/news\/how-to-write-clean-code\/?utm_source=chatgpt.com<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/leonardoherdy.medium.com\/clean-code-does-code-smell-part-1-7cf593f1e502\">https:\/\/leonardoherdy.medium.com\/clean-code-does-code-smell-part-1-7cf593f1e502<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction As an aspiring software engineer constantly learning new coding concepts and strategies, my coding practices have been improving both directly and subconsciously. Now that I am reaching the end of my journey as only a student of computer science, It&#8217;s important to recognize that mastering clean code principles and eliminating code smells are vital [&hellip;]<\/p>\n","protected":false},"author":14501,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-21","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/nicholasnelson\/wp-json\/wp\/v2\/posts\/21","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/nicholasnelson\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/nicholasnelson\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/nicholasnelson\/wp-json\/wp\/v2\/users\/14501"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/nicholasnelson\/wp-json\/wp\/v2\/comments?post=21"}],"version-history":[{"count":2,"href":"https:\/\/blogs.oregonstate.edu\/nicholasnelson\/wp-json\/wp\/v2\/posts\/21\/revisions"}],"predecessor-version":[{"id":27,"href":"https:\/\/blogs.oregonstate.edu\/nicholasnelson\/wp-json\/wp\/v2\/posts\/21\/revisions\/27"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/nicholasnelson\/wp-json\/wp\/v2\/media?parent=21"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/nicholasnelson\/wp-json\/wp\/v2\/categories?post=21"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/nicholasnelson\/wp-json\/wp\/v2\/tags?post=21"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}