Blog Post #1 Winter term

Since I started my computer science journey, one of the biggest things I have been taught is to write clean code and make sure my code is legible. One of the crucial things I remember learning was making sure my code followed the standards of clean code so that not only the grader could read it but also me in the future.

Embracing Meaningful Variable Names

The importance of meaningful variable names resonates across various perspectives on clean code. The first chapter in Clean Code: A Handbook of Agile Software Craftsmanship by Robert C. Martin emphasizes the significance of choosing descriptive names for variables. This resonates with the broader notion that clean code should be readable and understandable by developers other than the original author.

One thing I would like to start doing (more often):

I want to enhance the communicative aspect of my code by embracing meaningful and expressive variable names. Big Dave Thomas, founder of OTI, stresses the importance of clean code that makes it easy for other developers to enhance it. This involves writing functional code and crafting a narrative that future readers (including myself) can follow effortlessly.

# Before
def calc_area(w, h):
    return w * h

# After
def calculate_rectangle_area(width, height):
    return width * height

By adopting more expressive names like width and height, I transform the code into a narrative that effectively communicates its purpose and functionality.

Avoiding Code Smell

The 3rd chapter from Refactoring: Improving the Design of Existing Code by Martin Fowler highlights the challenges posed by long parameter lists in methods. I commit to refraining from creating methods with excessive parameters to prevent this. Instead, I explore techniques like introducing parameter objects, preserving whole objects, or using other refactorings to keep the parameter lists concise.

One thing I want to avoid doing:

I want to avoid doing the “Duplicate Code” smell, where identical or similar codes exist in multiple places in the codebase. As per the reading, duplicated code can lead to maintenance challenges, as changes need to be applied in multiple locations. Refactoring to eliminate duplication can improve code maintainability.

## Code before
def calculate_area(radius):
    return 3.14 * radius * radius

def calculate_volume(radius, height):
    return 3.14 * radius * radius * height

##Code After
PI = 3.14

def calculate_area(radius):
    return PI * radius * radius

def calculate_volume(radius, height):
    return PI * radius * radius * height

In this example, the refactored code introduces a constant PI to replace the magic number 3.14 used for calculations. This makes the code more readable and maintainable and avoids the duplication of the magic number.

Conclusion

Incorporating meaningful variable names and avoiding long, convoluted methods aligns with industry leaders’ broader clean code principles. By embracing these practices, I can contribute to a codebase that is functional, readable, maintainable, and a testament to our commitment to code craftsmanship.

Print Friendly, PDF & Email

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *