This article was originally published as a visual guide by Herkie Jerkie on the Soargram platform.
You write code. It works. You feel good.
But deep down, you know it's messy. You just don't know why.
Every beginner writes anti-patterns. It's not because you're stupid - it's because nobody told you the right way.
Here are 5 you probably have right now.
1. Too Many Parameters
def create_user(name, age, email, phone, address, city, country, zip_code): ...
Eight parameters is a nightmare. You forget the order, you mix them up, your IDE screams.
Fix: Use a dictionary or a dataclass.
def create_user(user_data): ...
One object. Clean. Maintainable.
2. type(x) == str
if type(x) == str:
print("It's a string")
type(x) == str is fragile. Subclasses break it. Use isinstance() instead - it handles inheritance.
if isinstance(x, str):
print("It's a string")
One simple change. Works every time.
3. The else After return
def is_even(n):
if n % 2 == 0:
return True
else:
return False
else after return is useless. Remove it.
def is_even(n):
if n % 2 == 0:
return True
return False
Cleaner. Simpler. Done.
4. Deep Nesting
if user:
if user.is_active:
if user.has_paid:
if user.has_access:
print("Welcome")
Stop. You're building a pyramid. It's slow to read and hard to debug.
Fix: Use early returns.
if not user:
return
if not user.is_active:
return
if not user.has_paid:
return
if not user.has_access:
return
print("Welcome")
Flat. Readable. This is how seniors write code.
5. Mutating Lists While Iterating
for item in items:
if item < 0:
items.remove(item)
This skips elements. Python moves the index, but you move the list.
Fix: Use list comprehension.
items = [x for x in items if x >= 0]
One line. No bugs. Clean.
Or use a copy if you really need to mutate:
for item in items.copy():
if item < 0:
items.remove(item)
The Bottom Line
5 anti-patterns. Fix them - and your code instantly goes from junior to senior level.
These are the little things that separate messy code from clean code.
This article was originally published as a visual guide by Herkie Jerkie on Soargram - a social network for visual guides. Read, save, and discuss it there.
Top comments (0)