{"id":16,"date":"2025-01-14T20:29:32","date_gmt":"2025-01-14T20:29:32","guid":{"rendered":"https:\/\/blogs.oregonstate.edu\/flowe\/?p=16"},"modified":"2025-01-14T20:47:18","modified_gmt":"2025-01-14T20:47:18","slug":"getting-rid-of-stinky-code","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/flowe\/2025\/01\/14\/getting-rid-of-stinky-code\/","title":{"rendered":"Getting Rid of Stinky Code"},"content":{"rendered":"\n<p>If you are a programmer, and have been for even a year, chances are you have seen what you might consider to be messy code; I for sure have. It&#8217;s the code that looks like someone&#8217;s mind threw up what they were trying to do, but yet it doesn&#8217;t quite do it; or if it does, the methods it uses are so ridiculous you just close the page because you can&#8217;t stand the sight of it. Luckily, from early on in my learning, I worked in a group with someone who embodies clean code. What he wrote was beautiful. I learned so much from him and, over the last year, have tried to adapt it to my own development workflow.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What to Begin<\/h2>\n\n\n\n<p>With that being said, one major takeaway from what I\u2019ve learned from him and from the article about clean code is that writing clean code starts at the very beginning of developing any piece of software [1]. Bjarne Stroustrup put it best: \u201cClean code does one thing well.\u201d [1]. While a piece of software must accomplish multiple tasks, each component of that software, such as a function, should do one thing\u2014and do it exceptionally well. For example, in web development, a React component should focus on a single responsibility. If it\u2019s handling form input, it shouldn\u2019t also concern itself with data fetching or state management.<\/p>\n\n\n\n<p>Here is an example of a React component that could be considered &#8220;Messy Code&#8221;:<\/p>\n\n\n\n<p><strong>RecipeSearch.js<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"682\" height=\"1024\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/carbon-1-682x1024.png\" alt=\"\" class=\"wp-image-27\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/carbon-1-682x1024.png 682w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/carbon-1-200x300.png 200w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/carbon-1-768x1154.png 768w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/carbon-1-1022x1536.png 1022w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/carbon-1-1363x2048.png 1363w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/carbon-1-1200x1803.png 1200w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/carbon-1-1980x2975.png 1980w\" sizes=\"auto, (max-width: 682px) 100vw, 682px\" \/><\/figure>\n\n\n\n<p>Here is an example of the &#8220;Messy Code&#8221; above becoming &#8220;Clean Code&#8221;:<\/p>\n\n\n\n<p><strong>SearchForm.js<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"629\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/searchform-1024x629.png\" alt=\"\" class=\"wp-image-28\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/searchform-1024x629.png 1024w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/searchform-300x184.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/searchform-768x471.png 768w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/searchform-1536x943.png 1536w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/searchform-2048x1257.png 2048w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/searchform-1200x737.png 1200w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/searchform-1980x1215.png 1980w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><strong>SearchResults.js<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/results-1024x683.png\" alt=\"\" class=\"wp-image-29\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/results-1024x683.png 1024w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/results-300x200.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/results-768x513.png 768w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/results-1536x1025.png 1536w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/results-1200x801.png 1200w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/results.png 1672w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><strong>RecipeSearch.js<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"799\" height=\"1024\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/carbon-2-799x1024.png\" alt=\"\" class=\"wp-image-30\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/carbon-2-799x1024.png 799w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/carbon-2-234x300.png 234w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/carbon-2-768x985.png 768w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/carbon-2-1198x1536.png 1198w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/carbon-2-1597x2048.png 1597w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/carbon-2-1200x1539.png 1200w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/carbon-2-1980x2539.png 1980w\" sizes=\"auto, (max-width: 799px) 100vw, 799px\" \/><\/figure>\n\n\n\n<p>In the &#8220;Messy Code&#8221; example, the <code>RecipeSearch<\/code> React component is doing multiple things at once. It handles state management, user input, API fetching, and rendering the results\u2014all within a single component. While it might perform what it needs to do, reading and understanding it can be challenging, and maintaining or extending it could lead to errors in the future.<\/p>\n\n\n\n<p>The &#8220;Clean Code&#8221; example separates all the functions of the <code>RecipeSearch<\/code> into three distinct components, each with a single responsibility:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>SearchForm<\/code><\/strong>: Responsible solely for rendering the search form and handling user input. This makes it easy to understand and reuse if the same form structure is needed elsewhere in the app.<\/li>\n\n\n\n<li><strong><code>SearchResults<\/code><\/strong>: Handles the display of search results, ensuring the rendering logic for the results is isolated from the state management and data fetching.<\/li>\n\n\n\n<li><strong><code>RecipeSearch<\/code><\/strong>: Manages the overall state, orchestrates the API calls, and connects the <code>SearchForm<\/code> and <code>SearchResults<\/code> components. It serves as a higher-level controller, keeping the logic centralized but the presentation decoupled.<\/li>\n<\/ul>\n\n\n\n<p>By following this approach, the code becomes easier to read, test, and maintain. Each component is focused, making it less likely that changes in one area will accidentally introduce bugs elsewhere. This is something I already do, but need to do from the beginning of writing code, instead of just going back through the &#8220;messy&#8221; code and refactoring it to become &#8220;clean&#8221; code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What to Avoid<\/h2>\n\n\n\n<p>On the other hand, from the article about bad code smells, one habit I need to work on avoiding is the Long Method. Long methods are difficult to understand, maintain, and extend. They kind of just happen if you&#8217;re not careful. They often try to do too much at once, leading to clutter and confusion [2]. The idea is simple: shorter methods are easier to read and manage because they focus on a single task. Modern object-oriented languages such as TypeScript eliminate much of the overhead of subroutine calls, making it practical to use many small methods instead of one long one.<\/p>\n\n\n\n<p>This thing to avoid ties in very well to the concept of clean code. Long methods often indicate that a function is trying to handle multiple responsibilities, which goes against the principle of \u201cdoing one thing well.\u201d Breaking down a long method into smaller, more focused methods not only makes the code easier to read but also improves reusability and testing.<\/p>\n\n\n\n<p>For example, let\u2019s revisit the <code>handleSearch<\/code> function from the messy <code>RecipeSearch<\/code> component. In its current form, it does everything: preventing the default form submission behavior, updating the loading state, fetching data from the API, and handling both success and error states. This approach makes the method long and harder to maintain.<\/p>\n\n\n\n<p>Instead, by applying the principle of breaking down long methods, we could refactor it into smaller, more focused functions:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"964\" height=\"1024\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/carbon-3-964x1024.png\" alt=\"\" class=\"wp-image-31\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/carbon-3-964x1024.png 964w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/carbon-3-282x300.png 282w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/carbon-3-768x816.png 768w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/carbon-3-1446x1536.png 1446w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/carbon-3-1928x2048.png 1928w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/carbon-3-1200x1275.png 1200w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8168\/files\/2025\/01\/carbon-3-1980x2104.png 1980w\" sizes=\"auto, (max-width: 964px) 100vw, 964px\" \/><\/figure>\n\n\n\n<p>Here, <code>handleSearch<\/code> delegates specific tasks to smaller, dedicated functions like <code>fetchRecipes<\/code>, <code>updateResults<\/code>, and <code>logError<\/code>. Each function has a single responsibility, making the code easier to follow and less error-prone.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Final Thoughts<\/h2>\n\n\n\n<p>The road to writing clean, maintainable code starts with awareness and deliberate practice. By focusing on principles like separation of concerns, avoiding long methods, and prioritizing readability, we can significantly improve the quality of our codebases. While refactoring messy code into clean code is an essential skill, the ultimate goal should be to write clean code from the very beginning. This not only saves time and effort but also creates a foundation for scalable and robust applications.<\/p>\n\n\n\n<p>As developers, it\u2019s our responsibility to strive for clarity and simplicity in our work. After all, clean code doesn\u2019t just make our lives easier\u2014it makes our codebases a joy to work with for everyone.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Sources<\/h2>\n\n\n\n<p>[1] R. C. Martin,&nbsp;<em>Clean code a handbook of agile software craftmanship.<\/em>&nbsp;Upper Saddle River [Etc.] Prentice Hall, 2010.<\/p>\n\n\n\n<p>[2] M. Fowler,&nbsp;<em>Refactoring: Improving the design of existing code<\/em>. Boston: Addison-Wesley, 2019.<\/p>\n\n\n\n<p>\u200c<\/p>\n\n\n\n<p>\u200c<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are a programmer, and have been for even a year, chances are you have seen what you might consider to be messy code; I for sure have. It&#8217;s the code that looks like someone&#8217;s mind threw up what they were trying to do, but yet it doesn&#8217;t quite do it; or if it [&hellip;]<\/p>\n","protected":false},"author":14559,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-16","post","type-post","status-publish","format-standard","hentry","category-coding-topics"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/flowe\/wp-json\/wp\/v2\/posts\/16","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/flowe\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/flowe\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/flowe\/wp-json\/wp\/v2\/users\/14559"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/flowe\/wp-json\/wp\/v2\/comments?post=16"}],"version-history":[{"count":6,"href":"https:\/\/blogs.oregonstate.edu\/flowe\/wp-json\/wp\/v2\/posts\/16\/revisions"}],"predecessor-version":[{"id":32,"href":"https:\/\/blogs.oregonstate.edu\/flowe\/wp-json\/wp\/v2\/posts\/16\/revisions\/32"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/flowe\/wp-json\/wp\/v2\/media?parent=16"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/flowe\/wp-json\/wp\/v2\/categories?post=16"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/flowe\/wp-json\/wp\/v2\/tags?post=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}