Categories
Senior Software Project

Coding Habits: Clean Code

Prompt: What is one thing from the articles that you would like to start doing (more often), and why? What is one thing you want to avoid doing and why?

Welcome back to my blog Sean’s Syntax. This week I will be researching and covering the principles of clean code and code smells. Both of these concepts are important for writing maintainable and scalable software. After reading the key ideas from Robert C. Martin’s Clean Code and Martin Fowler’s Refactoring, I have found some practices that i want to implement in my code more often and one I need to avoid.

One of the clean coding practices from Robert Martins Clean Code that I want to implement more is using descriptive and unambiguous names. As I am working with a team for my capstone project it’s important to create code that is easily readable and understandable to others. Martins talks about how these practices make the code self-documenting and help reduce excessive comments.

A good example is when creating this function below. I should have named it check_palindrome() instead of check_pal(). This would make it easier for others to understand the function’s use and search for it easily when needed. It also removes the need for the comment.

A coding practice I want to avoid is shotgun surgery. This bad code smell is identified by Martin Fowler in his book Refactoring is shotgun surgery. Fowler explains this as a single change that requires modifications across multiple classes or methods. This happens when responsibilities are poorly organized or too widely dispersed, making updates labor-intensive and error-prone (Fowler). I want to avoid this practice because shotgun surgery increases the odds of missing important updates and can make the codebase fragile.

Here is an example of the code smell: “if vault_name in vaults”. To fix this all i would need to do is refactors this to include a method vault_valid().

Refrences:

Martin, Robert C. Clean Code: A Handbook of Agile Software Craftsmanship. 1st edition, Pearson, 2008.

Fowler, Martin. Refactoring: Improving the Design of Existing Code. 2nd edition, Addison-Wesley Professional, 2018.