{"id":18,"date":"2025-01-13T20:19:31","date_gmt":"2025-01-13T20:19:31","guid":{"rendered":"https:\/\/blogs.oregonstate.edu\/home\/?p=18"},"modified":"2025-01-13T20:19:31","modified_gmt":"2025-01-13T20:19:31","slug":"the-hidden-implications-of-clean-code","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/home\/2025\/01\/13\/the-hidden-implications-of-clean-code\/","title":{"rendered":"The Hidden Implications of Clean Code"},"content":{"rendered":"\n<p>Since the start of my programming career, I have always found it satisfying to rewrite my code in a way that does the same thing but is easier to understand. So, one might assume I am an avid supporter of \u201cclean code,\u201d which is actually not the case. There are quite a few benefits to writing clean code, but there are also some downsides that people forget to talk about. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Good Practices of Clean Code<\/strong><\/h4>\n\n\n\n<p>Starting with the benefits of clean code, there are a few tactics one can use to make their code easier to read without much effort:<\/p>\n\n\n\n<p><strong>Avoid Deep Nesting <\/strong><\/p>\n\n\n\n<p>Every time you nest something that is one extra step the person reading your code has to keep in their mind. If possible, try to collapse nested structures into one level. If the code has already been executed beyond a certain point, then the previous code no longer needs to be taken into consideration. <\/p>\n\n\n\n<p><span style=\"text-decoration: underline\">Bad Example:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">java<code>function calculateTaxes(user, cart) {\n    if (!isMaintenancePeriod) {\n        if (user.isAuthenticated) {\n            if (user.isAuthorized) {\n                if (cart.total &gt; 0) {\n                    <em>\/\/ Deep nested logic<\/em>\n                }\n            }\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><span style=\"text-decoration: underline\">Better Approach:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">java<code>function calculateTaxes(user, cart) {\n    if (isMaintenancePeriod) return null;\n    if (!isValidUser(user)) return null;\n    if (!isValidCart(cart)) return null;\n    \n    return calculateRegionalTax(user, cart);\n}<\/code><\/pre>\n\n\n\n<p><strong>Eliminate Code Duplication<\/strong><\/p>\n\n\n\n<p>While I do not think you have to extract everything, especially if you are only using it once, you should try to extract logic into reusable functions. By centralizing repeated logic, you can make code more maintainable. <\/p>\n\n\n\n<p><strong>Use Meaningful Names<\/strong><\/p>\n\n\n\n<p>Some programmers find coming up with good names challenging, but this does not mean we should give up entirely. When naming variables, you should at least use descriptive names that clearly convey purpose and intent. This makes code more self-documenting and can eliminate the need for some comments. <\/p>\n\n\n\n<p><span style=\"text-decoration: underline\">Bad Example:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">python<code>def calc(a, b):\n    res = 0\n    tmp = a\n    for i in range(b):\n        res += tmp\n    return res\n<\/code><\/pre>\n\n\n\n<p><span style=\"text-decoration: underline\">Better Example:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">python<code>def multiply(multiplicand, multiplier):\n    product = 0\n    currentValue = multiplicand\n    for count in range(multiplier):\n        product += currentValue\n    return product\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Where the Problems Start<\/strong><\/h4>\n\n\n\n<p>I do not think clean code is a bad idea, but I have a few issues with it. <\/p>\n\n\n\n<p>First, not everyone agrees on what \u201cclean code\u201d means. Does it mean having very short functions? Does it require continuously extracting functions to make them more abstract? Do functions really have to only do one thing? The answer to these questions, and many more, will change depending on who you ask. <\/p>\n\n\n\n<p>Now, there are a few things I do not like that are sometimes considered \u201cclean code.\u201d First, the idea to keep functions extremely short (2-4 lines) is impractical and can lead to fragmented, hard-to-follow code. Excessive function granularity makes code harder to follow by forcing readers to jump between tiny functions.<\/p>\n\n\n\n<p>Another issue is that premature optimization of code flexibility can lead to it becoming unnecessarily complex. I have noticed that writing clean code sometimes leads to over-engineering and feature overload. If you follow something like the DRY principle 100% of the time, it can lead to over-abstraction and hard-to-maintain code. People spend so much time turning a simple program into a framework-like program when they could have used the time better somewhere else. <\/p>\n\n\n\n<p>I recently <a href=\"https:\/\/qntm.org\/clean\">read an article<\/a> that discussed some of the issues with \u201cclean code,\u201d specifically Robert C. Martin\u2019s Clean Code book, more in-depth. The book itself is already pretty outdated, but it is an interesting article. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>When Clean Code Is Worse<\/strong><\/h4>\n\n\n\n<p>Here is a video that talks about how clean code principles can significantly hurt performance: <a href=\"https:\/\/www.youtube.com\/watch?v=tD5NrevFtbU\">\u201cClean\u201d Code, Horrible Performance<\/a><\/p>\n\n\n\n<p>In short, this video talks about and gives examples of how common \u201cclean code\u201d principles can impact software performance. One of the examples the YouTuber gave was using polymorphism over switch statements and hiding internal implementations, which can make code 15-24 times slower since it requires more cycles. Now, there are languages like Rust that let you do zero-cost abstractions, but that is not the case with many languages. The video goes into more advanced options, which significantly improved performance but broke even more \u201cclean code\u201d rules. <\/p>\n\n\n\n<p>Now, clean code is not necessarily a bad thing, but one should not blindly follow these rules without considering their significant performance implications.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since the start of my programming career, I have always found it satisfying to rewrite my code in a way that does the same thing but is easier to understand. So, one might assume I am an avid supporter of \u201cclean code,\u201d which is actually not the case. There are quite a few benefits to [&hellip;]<\/p>\n","protected":false},"author":14549,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-18","post","type-post","status-publish","format-standard","hentry","category-cs-462"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/home\/wp-json\/wp\/v2\/posts\/18","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/home\/wp-json\/wp\/v2\/users\/14549"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/home\/wp-json\/wp\/v2\/comments?post=18"}],"version-history":[{"count":1,"href":"https:\/\/blogs.oregonstate.edu\/home\/wp-json\/wp\/v2\/posts\/18\/revisions"}],"predecessor-version":[{"id":19,"href":"https:\/\/blogs.oregonstate.edu\/home\/wp-json\/wp\/v2\/posts\/18\/revisions\/19"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/home\/wp-json\/wp\/v2\/media?parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/home\/wp-json\/wp\/v2\/categories?post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/home\/wp-json\/wp\/v2\/tags?post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}