Winter 2025 Term – Scott Lindsay – CS462
Introduction
Looking back at my older code, I’ve often found myself struggling to understand what I wrote. Writing clean, maintainable code is a crucial skill for any developer, but achieving it consistently can be difficult. What exactly makes code “clean”? How can we identify and eliminate bad coding practices, commonly known as “code smells”? In this post, I’ll share some basic insights from my research on clean code and code smells which I believe will be beneficial to incorporate into my workflow and also what I plan to avoid.
Clean Code: Focusing On The Human Element
One of the core principles from Robert Martin’s Clean Code: A Handbook of Agile Software Craftsmanship is that code should be written for humans, not just computers. This means using meaningful variable and function names, writing small and single-purpose functions, and avoiding unnecessary complexity.
Example of Bad Practice: Unclear variable names, no comments
def calc(x, y):
return x * y
Improved Version: Descriptive names and clear functionality
def multiply_numbers(factor1, factor2):
“””
Multiplies two numbers and returns the result.
“””
return factor1 * factor2
Code Smells: Recognizing and Avoiding Bad Practices
Martin Fowler’s Refactoring: Improving the Design of Existing Code describes various code smells as reliable indicators of bad coding practices that lead to maintainability issues. One of the most common code smells is caused by functions getting too long and becoming bloated with too much responsibility.
Bad Practice Ex: Long method doing too much
public void processUserRegistration(User user) {
Validate user
if (user == null || user.getEmail().isEmpty()) {
throw new IllegalArgumentException(“Invalid user”);
}
Save user to database
UserRepository repository = new UserRepository();
repository.save(user);
Send welcome email
EmailService emailService = new EmailService();
emailService.sendWelcomeEmail(user);
}
What I Will Start Doing: Writing Smaller Functions
From my research, I’ve realized the importance of writing small, single-purpose functions. This makes code easier to read, test, and maintain. Going forward, I will be more intentional about breaking down large methods into smaller, more modular functions and keep an eye on the length as I code each function.
What I Will Avoid: Ignoring Code Smells
In the past, I’ve sometimes ignored minor inefficiencies, thinking they wouldn’t matter much. However, I now understand that bad practices accumulate over time, making future modifications harder. I will actively look for and eliminate code smells, especially long methods and poor naming conventions.
Conclusion
I’ve realized that clean code is not just about making things look nice; it significantly improves maintainability and reduces technical debt. By writing meaningful function names, breaking down long methods, and avoiding common code smells, we can make our codebases more efficient and understandable.
I hope you found my post helpful and appreciate any comments or feedback.
– Scott Lindsay