Code Smells — Examples and Fixes in Python & JavaScript
Code smells are patterns in code that suggest deeper problems — they don't break functionality immediately but signal design issues that make code harder to maintain and more prone to bugs over time. Here are the most common ones with real before/after examples.
What is code smells?
The term "code smell" was coined by Kent Beck and popularized by Martin Fowler's "Refactoring" (1999). A code smell is a surface-level indicator of a potential deeper problem. Like a smell in a house, it doesn't prove there's a problem — but it's worth investigating.
Long Method / Long Function
A function longer than 30-50 lines almost certainly does more than one thing. The fix is Extract Function.
❌ Doing 5 things in one function
def create_order(user_id, items, promo_code):
# 15 lines: validate user and items
# 10 lines: calculate subtotal
# 15 lines: apply promo code
# 20 lines: charge payment
# 10 lines: send email
# 5 lines: return result
pass # Total: 75 lines, 5 responsibilities✅ Extracted by responsibility
def create_order(user_id, items, promo_code):
user, validated_items = validate_order_data(user_id, items)
price = calculate_total(validated_items, promo_code)
payment = charge_user(user, price)
notify_user(user.email, payment.receipt)
return Order(user, validated_items, payment)Duplicate Code
The same logic in two places means bug fixes must be applied twice — and one is always forgotten.
❌ Same validation in two routes
@app.route('/register')
def register():
if len(request.json['password']) < 8:
return error("Password too short")
if not re.match(r'[A-Z]', request.json['password']):
return error("Need uppercase")
# ... 10 more validation lines
@app.route('/change-password')
def change_password():
if len(request.json['password']) < 8: # Duplicated!
return error("Password too short")
# ... same 10 lines again✅ Single source of truth
def validate_password(password):
if len(password) < 8:
raise ValueError("Password too short")
if not re.match(r'[A-Z]', password):
raise ValueError("Need uppercase letter")
return True
@app.route('/register')
def register():
validate_password(request.json['password']) # One line
@app.route('/change-password')
def change_password():
validate_password(request.json['password']) # Same lineFeature Envy
A function that uses more data from another class than its own class probably belongs in that other class.
❌ UserReport reaches into User too much
class UserReport:
def get_display_name(self, user):
# Uses user data, not UserReport data
if user.first_name and user.last_name:
return f"{user.first_name} {user.last_name}"
return user.email.split('@')[0]✅ Method belongs in User class
class User:
@property
def display_name(self): # Belongs here — uses own data
if self.first_name and self.last_name:
return f"{self.first_name} {self.last_name}"
return self.email.split('@')[0]
class UserReport:
def render(self, user):
return f"Report for: {user.display_name}" # CleanData Clumps
Groups of data that always appear together should become a class or named tuple.
❌ lat/lng always travel together
def get_distance(lat1, lng1, lat2, lng2):
pass
def find_nearby(lat, lng, radius):
pass
def save_location(user_id, lat, lng, timestamp):
pass✅ Coordinates class
from dataclasses import dataclass
@dataclass
class Coordinates:
lat: float
lng: float
def get_distance(point1: Coordinates, point2: Coordinates):
pass
def find_nearby(point: Coordinates, radius: float):
pass
def save_location(user_id: int, point: Coordinates, timestamp):
passPro tip: LearnCodeGuide's Code Quality analysis specifically detects: Long Function (> 50 lines), Duplicate Code (>5 lines of identical logic), Dead Code (unreachable or unused), and Magic Numbers. Run it on your files and sort findings by impact.
Detect Code Smells in Your Code
Paste your code — LearnCodeGuide detects all these issues automatically using GPT-4o + Claude Sonnet. Free to start.
Analyze Your Code →Related Guides
Published by LearnCodeGuide Team · Last reviewed: November 2025