Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 45 additions & 13 deletions examples/real_world_bugs.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# HARMONIZER SCORE: ~0.85 (!! DISHARMONY)
# OTHER TOOLS: All pass ✓


def validate_email(email: str) -> bool:
"""
BUG: Function claims to validate, but actually sends emails!
Expand All @@ -39,15 +40,18 @@ def validate_email(email: str) -> bool:
return True
return False


def send_welcome_email(email):
"""Placeholder - in real code, this would send email"""
print(f"Sending email to {email}")


# FIX: Separate validation from action
def validate_email_fixed(email: str) -> bool:
"""Just validates, doesn't send"""
return "@" in email and "." in email


def process_new_user(email: str) -> bool:
"""Orchestrates validation AND sending"""
if validate_email_fixed(email):
Expand All @@ -62,6 +66,7 @@ def process_new_user(email: str) -> bool:
# HARMONIZER SCORE: ~0.95 (!! CRITICAL DISHARMONY)
# OTHER TOOLS: All pass ✓


def get_user_by_id(user_id: int):
"""
BUG: Function claims to GET, but actually DELETES!
Expand All @@ -83,20 +88,25 @@ def get_user_by_id(user_id: int):
db.execute(f"DELETE FROM users WHERE id = {user_id}")
return user_id


def get_database_connection():
"""Placeholder"""

class FakeDB:
def execute(self, query):
print(f"Executing: {query}")

return FakeDB()


# FIX: Name matches behavior
def delete_user_by_id(user_id: int):
"""Honestly named - clearly destructive"""
db = get_database_connection()
db.execute(f"DELETE FROM users WHERE id = {user_id}")
return user_id


def get_user_by_id_fixed(user_id: int):
"""Actually gets without modifying"""
db = get_database_connection()
Expand All @@ -111,6 +121,7 @@ def get_user_by_id_fixed(user_id: int):
# HARMONIZER SCORE: ~0.75 (!! DISHARMONY)
# OTHER TOOLS: Might pass


def check_file_exists(filepath: str) -> bool:
"""
BUG: Function claims to check, but creates if missing!
Expand All @@ -129,22 +140,26 @@ def check_file_exists(filepath: str) -> bool:

if not os.path.exists(filepath):
# VIOLATION: Check functions shouldn't create!
with open(filepath, 'w') as f:
with open(filepath, "w") as f:
f.write("")
return False
return True


# FIX: Separate check from creation
def check_file_exists_fixed(filepath: str) -> bool:
"""Pure check - no side effects"""
import os

return os.path.exists(filepath)


def ensure_file_exists(filepath: str) -> bool:
"""Honest name - creates if missing"""
import os

if not os.path.exists(filepath):
with open(filepath, 'w') as f:
with open(filepath, "w") as f:
f.write("")
return False
return True
Expand All @@ -156,6 +171,7 @@ def ensure_file_exists(filepath: str) -> bool:
# HARMONIZER SCORE: ~0.70 (!! DISHARMONY)
# OTHER TOOLS: Tests might pass if they expect side effects


def calculate_total_price(items: list) -> float:
"""
BUG: Function claims to calculate, but also saves to database!
Expand All @@ -171,26 +187,29 @@ def calculate_total_price(items: list) -> float:
- Hard to test
- Violates single responsibility
"""
total = sum(item['price'] for item in items)
total = sum(item["price"] for item in items)

# VIOLATION: Calculate functions shouldn't persist!
save_to_database('total_price', total)
save_to_database("total_price", total)

return total


def save_to_database(key, value):
"""Placeholder"""
print(f"Saving {key} = {value} to database")


# FIX: Separate calculation from persistence
def calculate_total_price_fixed(items: list) -> float:
"""Pure calculation - no side effects"""
return sum(item['price'] for item in items)
return sum(item["price"] for item in items)


def calculate_and_save_total_price(items: list) -> float:
"""Honest name - calculates AND saves"""
total = calculate_total_price_fixed(items)
save_to_database('total_price', total)
save_to_database("total_price", total)
return total


Expand All @@ -200,6 +219,7 @@ def calculate_and_save_total_price(items: list) -> float:
# HARMONIZER SCORE: ~0.80 (!! DISHARMONY)
# OTHER TOOLS: Hard to catch without semantic analysis


def read_configuration(config_file: str) -> dict:
"""
BUG: Function claims to read, but updates last_accessed timestamp!
Expand All @@ -217,26 +237,31 @@ def read_configuration(config_file: str) -> dict:
"""
import json

with open(config_file, 'r') as f:
with open(config_file, "r") as f:
config = json.load(f)

# VIOLATION: Read functions shouldn't modify!
update_last_accessed_timestamp(config_file)

return config


def update_last_accessed_timestamp(filepath):
"""Placeholder"""
import time

print(f"Updating timestamp for {filepath} to {time.time()}")


# FIX: Either truly read-only OR honest name
def read_configuration_fixed(config_file: str) -> dict:
"""Pure read - no side effects"""
import json
with open(config_file, 'r') as f:

with open(config_file, "r") as f:
return json.load(f)


def read_and_track_configuration(config_file: str) -> dict:
"""Honest name - reads AND tracks access"""
config = read_configuration_fixed(config_file)
Expand All @@ -250,6 +275,7 @@ def read_and_track_configuration(config_file: str) -> dict:
# HARMONIZER SCORE: ~0.90 (!! CRITICAL DISHARMONY)
# OTHER TOOLS: Might not catch unless tests verify original list unchanged


def filter_invalid_users(users: list) -> list:
"""
BUG: Function claims to filter (read), but deletes from database!
Expand All @@ -270,32 +296,35 @@ def filter_invalid_users(users: list) -> list:
valid_users = []

for user in users:
if user['email'] and user['name']:
if user["email"] and user["name"]:
valid_users.append(user)
else:
# VIOLATION: Filter shouldn't delete from DB!
delete_user_from_database(user['id'])
delete_user_from_database(user["id"])

return valid_users


def delete_user_from_database(user_id):
"""Placeholder"""
print(f"DELETING user {user_id} from database!")


# FIX: Separate filtering from deletion
def filter_invalid_users_fixed(users: list) -> list:
"""Pure filter - no side effects"""
return [u for u in users if u['email'] and u['name']]
return [u for u in users if u["email"] and u["name"]]


def remove_invalid_users(users: list) -> list:
"""Honest name - filters AND deletes from database"""
valid_users = []

for user in users:
if user['email'] and user['name']:
if user["email"] and user["name"]:
valid_users.append(user)
else:
delete_user_from_database(user['id'])
delete_user_from_database(user["id"])

return valid_users

Expand All @@ -306,6 +335,7 @@ def remove_invalid_users(users: list) -> list:
# HARMONIZER SCORE: ~0.65 (!! DISHARMONY)
# OTHER TOOLS: Tests might catch if they expect no exceptions


def log_error_message(message: str):
"""
BUG: Function claims to log, but raises exception!
Expand All @@ -326,11 +356,13 @@ def log_error_message(message: str):
if "critical" in message.lower():
raise RuntimeError(f"Critical error: {message}")


# FIX: Either log OR raise, not both under "log" name
def log_error_message_fixed(message: str):
"""Just logs - never raises"""
print(f"ERROR: {message}")


def handle_error_message(message: str):
"""Honest name - logs AND may raise"""
print(f"ERROR: {message}")
Expand Down
Loading