<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Joshua Speelman (Joachim)</title>
    <description>The latest articles on DEV Community by Joshua Speelman (Joachim) (@joshuaspeelman).</description>
    <link>https://dev.to/joshuaspeelman</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1166758%2F183e8bd9-d1f2-4a1f-ad4f-aebbb4ae2970.PNG</url>
      <title>DEV Community: Joshua Speelman (Joachim)</title>
      <link>https://dev.to/joshuaspeelman</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/joshuaspeelman"/>
    <language>en</language>
    <item>
      <title>Tidying Up: Key Insights from "Clean Code"</title>
      <dc:creator>Joshua Speelman (Joachim)</dc:creator>
      <pubDate>Fri, 22 Sep 2023 23:09:52 +0000</pubDate>
      <link>https://dev.to/joshuaspeelman/tidying-up-key-insights-from-clean-code-4n7f</link>
      <guid>https://dev.to/joshuaspeelman/tidying-up-key-insights-from-clean-code-4n7f</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Let's not mess around. Change is the only constant in the realm of software development. And in this ever-changing world, it's crucial to have a solid foundation of principles and practices to guide our decisions. ESPECIALLY when it comes to ouputting code. "Clean Code: A Handbook of Agile Software Craftsmanship” by Robert C. Martin (affectionately referred to as "Uncle Bob" throughout the book) provides just that—thoughtful and insightful perspective into the fact that producing code that is not only functional, but also MAINTAINABLE is an ever-present challenge.&lt;/p&gt;

&lt;p&gt;Published in 2008, Uncle Bob's "Clean Code" quickly became one of the quintessential classics among programmers and developers alike, offering universal principles and guidelines that can be applied to any codebase. While some of the later examples tend to get a bit heavy (a product of the time, perhaps?), the core principles throughout the book remain true today, and once I internalised the words, I've found myself coming back again and again to the following core principles when I sit down to build something. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KeERVf08--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rasjwjqxo6r7cqgrgzlx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KeERVf08--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rasjwjqxo6r7cqgrgzlx.png" alt="Image description" width="496" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Some of this might sound basic, but it's surprising how the deeper you go trying to make something work, the less you can pay attention to those fundamentals of programming that won't look like ancient hieroglyphics to your coworkers. Here's what I took from the book, in massively condensed format!:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Clean code always looks like it was written by someone who cares.&lt;/em&gt; - Michael Feathers&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The following post aims to share key insights I gathered from reading this almost-timeless classic.&lt;/p&gt;

&lt;h2&gt;
  
  
  Meaningful Function Names
&lt;/h2&gt;

&lt;p&gt;The first fundamental principle discussed in the book is the importance of choosing descriptive and meaningful names for all variables, functions and classes. Uncle Bob advocates heavily that code should be "off the cuff self-explanatory", and well-chose names can serve as effective documentation in their own right.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Bad variable name example
x = 42

# Good variable name example
totalScore = 42
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The idea is that, according to Uncle Bob, when someone reads your code, they should be able to follow the logic effortlessly, understand its purpose, and grasp the intent of the programmer. By adhering to descriptive variable and function names, following the Single Responsibility Principle (SRP) (Which will be extrapolated on further down in this article!), and reducing complexity, you can ensure your code tells a clear and compelling narrative.&lt;/p&gt;

&lt;h2&gt;
  
  
  Small Functions (or, the Single Responsibility Principle)
&lt;/h2&gt;

&lt;p&gt;The second principle the book explores is the important of keeping functions small, concise and focused on doing one single thing well. The SRP, as outlined in Clean Code, states that a function or class should only have ONE reason to change. This principle aims to promote modularisation and maintainability by ensuring that each component of your codebase is responsible for a single, well-defined task. Following SRP results in cleaner, more maintainable code that is easier to test and extend.&lt;/p&gt;

&lt;p&gt;The following function ignores the SRP:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def process_data():
    # This function is vague and doesn't indicate its purpose.
    # It does multiple things, making it hard to understand.
    data = fetch_data_from_database()
    sanitized_data = sanitize_data(data)
    result = compute_result(sanitized_data)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The following function abides by the SRP, resulting in smaller chunks of much clearer, managable functions. Each function's purpose is evident from its name, and makes the code more readable, maintainable and easier to understand:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def fetch_data_from_database():
    # This function specifically fetches data from the database.
    data = # code to fetch data
    return data

def sanitize_data(data):
    # This function specifically sanitizes the data.
    sanitized_data = # code to sanitize data
    return sanitized_data

def compute_result(data):
    # This function specifically computes a result from the data.
    result = # code to compute result
    return result
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Not only this, but with a single responsibility, classes become far more testable! Writing unit tests for each responsibility, independently, ensures each part of the code works as expected.&lt;/p&gt;

&lt;h2&gt;
  
  
  The DRY Principle
&lt;/h2&gt;

&lt;p&gt;DRY, or "Don't Repeat Yourself" is another cornerstone of maintaining clean code. Repeated code not only introduces redundancies, but also creates nightmares when it comes to maintenance. By encapsulating common functionality into reusable functions, classes or modules, you adhere to the DRY principle and reduce risk of errors and inconsistencies.&lt;/p&gt;

&lt;p&gt;Suppose you have a Python program in which you want to calculate the sum of elements in a list, and you need to do it in multiple places. Instead of repeating the code, a function can do this for you:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Ignoring the DRY principle
numbers = [1, 2, 3, 4, 5]
sum1 = 0
for num in numbers:
    sum1 += num

numbers = [10, 20, 30, 40, 50]
sum2 = 0
for num in numbers:
    sum2 += num

# Keeping the DRY principle in mind
def calculate_sum(number_list):
    total = 0
    for num in number_list:
        total += num

numbers1 = [1, 2, 3, 4, 5]
sum1 = calculate_sum(numbers1)

numbers2 = [10, 20, 30, 40, 50]
sum2 = calculate_sum(numbers2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By encapsulating repetitive tasks in functions, you're following the DRY principle! Making code more concise, easier to maintain, and less prone to errors.&lt;/p&gt;

&lt;h2&gt;
  
  
  Code Comments Should Be a Last Resort
&lt;/h2&gt;

&lt;p&gt;In the tech world, comments are often seen (and used!) as a necessary means to explain complex logic, document code, or provide context for future reads. While comments are valuable, Uncle Bob argues they should be used judiciously and sparingly. Here's why!&lt;/p&gt;

&lt;p&gt;Clean code should be self-explanatory. This means the structure and naming within the code should convey clear intent and functionality without requiring additional comments. Code that follows naming conventions and is clear becomes almost a form of documentation itself.&lt;/p&gt;

&lt;p&gt;Ironically, as Uncle Bob sardonically points out, comments can sometimes also mislead developers if they are not kept in sync with the code they describe. When code is modified, developers might forget to update accompanying comments, leaving a disparity between what the code does, and what the comments claim it does. This is the precursor to entering into needless bugs and misunderstandings.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;"Clean Code: A Handbook of Agile Software Craftsmanship” is a timeless resource for software developers, beginner and experienced alike, who aspire to be true craftsmen. By following these principles and adopting the mindsets presented in the book, devs can create software that isn't only functional, but also maintainable and adaptable to changing environments. &lt;/p&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>agile</category>
      <category>python</category>
    </item>
  </channel>
</rss>
