Stand with Clean Code
You will see two code versions side by side on the projector for the same idea. Each pair shows one cleanliness principle. For every pair, silently decide which version you find clearer/easier to read, then vote/stand for that side. After the vote, we’ll briefly say why we chose it and what principle it shows. Focus on explaining your choice, not on being “right”.
Conjunction in names
|
Left⬅️
| def save_user_and_role(user, role):
...
|
|
➡️Right
| def save_user(user):
...
def save_role(role):
...
|
|
Conjunction in names (and/or/then)
|
Left⬅️
| def validate_and_save(user):
if not user.is_valid():
raise ValueError("Invalid user")
database.save(user)
|
|
➡️Right
| def update_user_profile(user):
_validate(user)
_save(user)
def _validate(user):
if not user.is_valid():
raise ValueError("Invalid user")
def _save(user):
database.save(user)
|
|
Common prefix/suffix in parameters
|
Left⬅️
| def move_rectangle(rect, center_x, center_y):
rect.x = center_x - rect.width / 2
rect.y = center_y - rect.height / 2
|
|
➡️Right
| from dataclasses import dataclass
@dataclass
def Point:
x: float
y: float
def move_rectangle(rect, center: Point):
rect.x = center.x - rect.width / 2
rect.y = center.y - rect.height / 2
|
|
Empty lines vs. real extraction
|
Left⬅️
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | def register_user(data):
# validate
if "email" not in data or "name" not in data:
raise ValueError("missing fields")
# normalize
data["email"] = data["email"].lower()
# persist
user_id = database.insert("users", data)
# notify
mailer.send_welcome(data["email"])
return user_id
|
|
➡️Right
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 | def register_user(data):
_validate(data)
_normalize(data)
user_id = _save(data)
_notify(data)
return user_id
def _validate(data):
if "email" not in data or "name" not in data:
raise ValueError("missing fields")
def _normalize(data):
data["email"] = data["email"].lower()
def _save(data):
return database.insert("users", data)
def _notify(data):
mailer.send_welcome(data["email"])
|
|
Logical cohesion (group data + operations)
|
Left⬅️
| # data is flat, operations scattered
def get_full_name(user):
return f'{user["first_name"]} {user["last_name"]}'
def get_full_address(user):
return f'{user["street"]}, {user["city"]} {user["zip"]}'
def format_shipping_label(user):
return f'{get_full_name(user)}\n{get_full_address(user)}'
|
|
➡️Right
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 | class Address:
def __init__(self, street, city, zip_code):
self.street = street
self.city = city
self.zip_code = zip_code
def formatted(self):
return f"{self.street}, {self.city} {self.zip_code}"
class Person:
def __init__(self, first_name, last_name, address: Address):
self.first_name = first_name
self.last_name = last_name
self.address = address
def full_name(self):
return f"{self.first_name} {self.last_name}"
def shipping_label(self):
return f"{self.full_name()}\n{self.address.formatted()}"
|
|
Magic strings / numbers → Enums
|
Left⬅️
| def calculate_discount(user_type, total):
if user_type == "student":
return total * 0.85
if user_type == "employee":
return total * 0.80
return total
|
|
➡️Right
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | from enum import Enum
class UserType(Enum):
STUDENT = "student"
EMPLOYEE = "employee"
OTHER = "other"
DISCOUNTS = {
UserType.STUDENT: 0.85,
UserType.EMPLOYEE: 0.80,
UserType.OTHER: 1.00,
}
def calculate_discount(user_type: UserType, total):
return total * DISCOUNTS.get(user_type, 1.00)
|
|