Member-only story

Write Better Codes: Identify 100 Smelly Python Codes & Eliminate Them for Better Code Quality

btd
46 min readDec 19, 2023

--

“Smelly code” is a term used in software development to describe code that exhibits characteristics that may indicate issues or potential problems. These issues can make the code harder to read, maintain, or extend. Smelly code doesn’t necessarily mean that the code is incorrect or doesn’t work; rather, it suggests that there are opportunities for improvement in terms of readability, maintainability, and overall software design.

Here are 100 examples of smelly codes and how to fix them:

1. Long Functions:

# Smelly code
def complex_function(arg1, arg2, arg3, arg4, arg5):
# ... many lines of code ...

# Cleaned-up code
def helper_function(arg):
# ... smaller, focused code ...

def complex_function(arg1, arg2, arg3, arg4, arg5):
result = helper_function(arg1)
result += helper_function(arg2)
result += helper_function(arg3)
result += helper_function(arg4)
result += helper_function(arg5)
return result
  • Long functions can be challenging to understand, maintain, and debug. They often violate the single responsibility principle and make code less readable.
  • The “Cleaned-up Code” example demonstrates a common refactoring technique: breaking down a long…

--

--

btd
btd

No responses yet