Common Anti-Patterns and How to Fix Them
An anti-pattern is a design that looks reasonable when you write it and hurts later. It is not a bug: the code runs, the tests pass, and the first demo goes fine. The pain shows up on the next change, when one small feature means editing six places, or when a test needs half the system to run.
Interviewers know these shapes well, because they have fixed them in real code. When they see a 400-line
LibrarySystem class or a method called send(msg, True, False, True), they don't need to read further to
know how your design will handle the follow-up questions. The good news is that each anti-pattern has a standard
fix, and most fixes are small. This post covers the five you are most likely to write under time pressure, and
for each one shows the bad version, exactly what it costs you, the fix, and when the "bad" version is actually
fine.
π‘ Why this matters in an interview: you rarely lose points for missing a clever pattern. You lose points for a design that can't absorb the follow-up. Every anti-pattern below is a reason a follow-up becomes hard.
The God Class
A god class is one class that knows everything and does everything. It usually gets a name like System,
Manager or Service, and every new requirement lands in it because that is where everything else already is.
Here is a library system written as one class.
Bad:
class LibrarySystem:
def __init__(self) -> None:
self.books: dict[str, dict] = {} # isbn -> {"title", "copies", "available"}
self.members: dict[str, dict] = {} # member_id -> {"name", "loans", "fines"}
self.loans: list[dict] = []
def add_book(self, isbn: str, title: str, copies: int) -> None: ...
def register_member(self, member_id: str, name: str) -> None: ...
def checkout(self, member_id: str, isbn: str, day: int) -> bool: ...
def return_book(self, member_id: str, isbn: str, day: int) -> int: ...
def compute_fine(self, loan: dict, day: int) -> int: ...
def send_overdue_email(self, member_id: str) -> None: ...
def search_by_title(self, text: str) -> list[str]: ...
def monthly_report(self) -> str: ...
def save_to_disk(self, path: str) -> None: ...
What the bad version costs you. Every method can touch every dictionary, so no rule has an owner. The rule
"a member with unpaid fines can't borrow" might live in checkout, in register_member, or in both. Changing
the fine policy means reading the whole class to find who else computes fines. Testing compute_fine requires
building a LibrarySystem with books and members it doesn't care about. And in an interview, a god class means
you skipped the entities step: there is no Book, Member or Loan for the interviewer to discuss.
The fix: give each rule a home. Find the nouns that hold state and enforce rules, and move each rule to the class that owns the data it checks. The orchestrator stays, but it only coordinates.
Good:
class Member:
def __init__(self, member_id: str) -> None:
self.member_id = member_id
self.unpaid_fines = 0
self.active_loans: list["Loan"] = []
def can_borrow(self) -> bool:
return self.unpaid_fines == 0 and len(self.active_loans) < 5
class Loan:
def __init__(self, copy_id: str, member: Member, due_day: int) -> None:
self.copy_id, self.member, self.due_day = copy_id, member, due_day
def fine_on(self, day: int) -> int:
return max(0, day - self.due_day) * 25 # cents per late day
class Library:
def __init__(self, catalog: "Catalog", notifier: "Notifier") -> None:
self.catalog = catalog
self.notifier = notifier
def checkout(self, member: Member, isbn: str, day: int) -> Loan | None:
if not member.can_borrow():
return None
copy = self.catalog.take_available_copy(isbn)
if copy is None:
return None
loan = Loan(copy.copy_id, member, due_day=day + 14)
member.active_loans.append(loan)
return loan
Now each question has one answer. Borrowing limits live in Member. Fines live in Loan. Search lives in
Catalog. Emails go through a Notifier you can replace with a fake in tests. Library.checkout reads like the
requirement it implements.
How to spot it. A class with more than about seven public methods that serve different requirements, fields that only some methods use, or a name you can't describe without "and." A useful test: list who would ask for a change to this class. If the answer is "the fines team, the search team and the email team," it has three jobs.
When it's fine. A small orchestrator with many short methods that each delegate one line is not a god class.
Library above may grow checkout, return_copy, place_hold and pay_fine; that is fine as long as each
method calls into the class that owns the rule. What makes a god class is owning the rules, not having many
entry points.