Anti-Patterns: Mistakes with Names

Teams keep making the same mistakes, as they keep finding the same fixes, and the mistakes have names too: the god object, the golden hammer, copy-paste programming and spaghetti code are anti-patterns, answers that look like they work, cause more trouble than they solve, and have a known better answer.

Journeyman · Software Development · OOP · Updated

By

After this lesson you can

  • Name the anti-pattern in code or a team habit: god object, golden hammer, copy-paste programming or spaghetti code
  • Explain how an anti-pattern differs from a code smell and from a pattern, and give its better answer

Key takeaways

  • An anti-pattern is a common answer to a problem that seems to work, causes more trouble than it solves, and has a known better answer.
  • A smell is a symptom you can see in code; an anti-pattern is the habit or design that produces it; a pattern is a fix that holds up.
  • The four to know: the god object (one class doing everything), the golden hammer (one favorite tool for every job), copy-paste programming and spaghetti code.
  • Naming one in a review turns "I don't like this" into a shared problem with a known way out.

Before this: Smells of Size, which met the class that does everything, and Pattern Fever: When a Pattern Makes Code Worse.

Your first change to Summit Gym's code is in review, and the senior developer's comments are short: "Careful, this is turning into a god object." "Copy-paste here: pull it out?" "Golden hammer: not everything needs to be a Strategy." Each comment is two or three words, and each one carries a whole explanation, because the mistake it points at has a name. Patterns are names for fixes that hold up. These are names for "fixes" that don't.

The problem: the same fix, in two places

Here is the mistake the reviewer meant by "copy-paste". The member app and the front desk each show how many places are left in a class. In March, someone fixed the app so canceled bookings don't take up a place. Sam canceled this morning:

List<(string Name, string Status)> bookings = new List<(string, string)> { ("Maya", "booked"), ("Sam", "canceled"), ("Leo", "booked") };
Console.WriteLine(quot;App: Yoga has {AppPlacesLeft(bookings)} places left");
Console.WriteLine(quot;Front desk: Yoga has {FrontDeskPlacesLeft(bookings)} places left");

int AppPlacesLeft(List<(string Name, string Status)> bookings) => 12 - bookings.Count(booking => booking.Status != "canceled"); // fixed in March

int FrontDeskPlacesLeft(List<(string Name, string Status)> bookings) => 12 - bookings.Count; // the copy that missed the fix
App: Yoga has 10 places left
Front desk: Yoga has 9 places left
record Booking(String name, String status) {}

int appPlacesLeft(List<Booking> bookings) {
    return 12 - (int) bookings.stream().filter(booking -> !booking.status().equals("canceled")).count(); // fixed in March
}

int frontDeskPlacesLeft(List<Booking> bookings) {
    return 12 - bookings.size(); // the copy that missed the fix
}

void main() {
    List<Booking> bookings = List.of(new Booking("Maya", "booked"), new Booking("Sam", "canceled"), new Booking("Leo", "booked"));
    IO.println("App: Yoga has " + appPlacesLeft(bookings) + " places left");
    IO.println("Front desk: Yoga has " + frontDeskPlacesLeft(bookings) + " places left");
}
App: Yoga has 10 places left
Front desk: Yoga has 9 places left
def app_places_left(bookings):
    return 12 - sum(1 for name, status in bookings if status != "canceled")  # fixed in March


def front_desk_places_left(bookings):
    return 12 - len(bookings)  # the copy that missed the fix


bookings = [("Maya", "booked"), ("Sam", "canceled"), ("Leo", "booked")]
print(f"App: Yoga has {app_places_left(bookings)} places left")
print(f"Front desk: Yoga has {front_desk_places_left(bookings)} places left")
App: Yoga has 10 places left
Front desk: Yoga has 9 places left
function appPlacesLeft(bookings) {
  return 12 - bookings.filter((booking) => booking.status !== "canceled").length; // fixed in March
}

function frontDeskPlacesLeft(bookings) {
  return 12 - bookings.length; // the copy that missed the fix
}

const bookings = [{ name: "Maya", status: "booked" }, { name: "Sam", status: "canceled" }, { name: "Leo", status: "booked" }];
console.log(`App: Yoga has ${appPlacesLeft(bookings)} places left`);
console.log(`Front desk: Yoga has ${frontDeskPlacesLeft(bookings)} places left`);
App: Yoga has 10 places left
Front desk: Yoga has 9 places left
interface Booking { name: string; status: string; }

function appPlacesLeft(bookings: Booking[]): number {
  return 12 - bookings.filter((booking) => booking.status !== "canceled").length; // fixed in March
}

function frontDeskPlacesLeft(bookings: Booking[]): number {
  return 12 - bookings.length; // the copy that missed the fix
}

const bookings: Booking[] = [{ name: "Maya", status: "booked" }, { name: "Sam", status: "canceled" }, { name: "Leo", status: "booked" }];
console.log(`App: Yoga has ${appPlacesLeft(bookings)} places left`);
console.log(`Front desk: Yoga has ${frontDeskPlacesLeft(bookings)} places left`);
App: Yoga has 10 places left
Front desk: Yoga has 9 places left

A member sees 10 places in the app, walks in, and the front desk says 9. Nobody made a strange mistake: the front desk's code was copied from the app's before March, and a fix reaches only the copy it was made in.

The cure is one you know: one function, placesLeft, that both screens call, so the next fix reaches both:

List<(string Name, string Status)> bookings = new List<(string, string)> { ("Maya", "booked"), ("Sam", "canceled"), ("Leo", "booked") };
Console.WriteLine(quot;App: Yoga has {PlacesLeft(bookings)} places left");
Console.WriteLine(quot;Front desk: Yoga has {PlacesLeft(bookings)} places left");

int PlacesLeft(List<(string Name, string Status)> bookings) => 12 - bookings.Count(booking => booking.Status != "canceled");
App: Yoga has 10 places left
Front desk: Yoga has 10 places left
record Booking(String name, String status) {}

int placesLeft(List<Booking> bookings) {
    return 12 - (int) bookings.stream().filter(booking -> !booking.status().equals("canceled")).count();
}

void main() {
    List<Booking> bookings = List.of(new Booking("Maya", "booked"), new Booking("Sam", "canceled"), new Booking("Leo", "booked"));
    IO.println("App: Yoga has " + placesLeft(bookings) + " places left");
    IO.println("Front desk: Yoga has " + placesLeft(bookings) + " places left");
}
App: Yoga has 10 places left
Front desk: Yoga has 10 places left
def places_left(bookings):
    return 12 - sum(1 for name, status in bookings if status != "canceled")


bookings = [("Maya", "booked"), ("Sam", "canceled"), ("Leo", "booked")]
print(f"App: Yoga has {places_left(bookings)} places left")
print(f"Front desk: Yoga has {places_left(bookings)} places left")
App: Yoga has 10 places left
Front desk: Yoga has 10 places left
function placesLeft(bookings) {
  return 12 - bookings.filter((booking) => booking.status !== "canceled").length;
}

const bookings = [{ name: "Maya", status: "booked" }, { name: "Sam", status: "canceled" }, { name: "Leo", status: "booked" }];
console.log(`App: Yoga has ${placesLeft(bookings)} places left`);
console.log(`Front desk: Yoga has ${placesLeft(bookings)} places left`);
App: Yoga has 10 places left
Front desk: Yoga has 10 places left
interface Booking { name: string; status: string; }

function placesLeft(bookings: Booking[]): number {
  return 12 - bookings.filter((booking) => booking.status !== "canceled").length;
}

const bookings: Booking[] = [{ name: "Maya", status: "booked" }, { name: "Sam", status: "canceled" }, { name: "Leo", status: "booked" }];
console.log(`App: Yoga has ${placesLeft(bookings)} places left`);
console.log(`Front desk: Yoga has ${placesLeft(bookings)} places left`);
App: Yoga has 10 places left
Front desk: Yoga has 10 places left

Copying the function felt like a fix the day it was done: the front desk needed places left, and here was code that worked. That's what makes it an anti-pattern rather than a slip.

Mistakes with names

An anti-pattern is a common answer to a problem that seems to work, causes more trouble than it solves, and has a known better answer. Teams kept making the same mistakes, the same way they kept finding the same fixes, so in the late 1990s people started naming the mistakes too, as the mirror image of patterns.

How it fits with the words you already have:

  • A code smell is a symptom you can see in the code: the same lines in two places.
  • An anti-pattern is the habit or design that produces it: copying code instead of naming it.
  • A pattern is a fix that holds up: one function both screens call, or, when the copies are many, a shared shape.

The four you'll hear most:

Anti-pattern What it looks like Why it hurts The better answer
God object One class that knows and does everything: GymManager, with sixty methods Every change goes through it, and nobody can change it safely Pull out a class per job (Smells of Size)
Golden hammer One favorite tool used for every job: every rule a Strategy, every shared thing a Singleton The tool fits some problems and gets forced onto the rest Choose the tool from the problem: the smell map, and Pattern Fever's question
Copy-paste programming Copying a block that works, instead of naming it and calling it A fix reaches one copy, and the copies drift apart Pull out a method; when the copies are many, the next chapter's rule of three
Spaghetti code Code whose flow jumps everywhere: flags set in one place and read far away, methods calling back and forth, shared variables changed from anywhere Nobody can follow one path through it, so every change is a guess Pin it down, then untangle it in small steps into named methods with clear inputs and outputs

Four anti-patterns, what each looks like and the better answer: a god object (one class does everything; a class per job), a golden hammer (one tool for every job; choose from the problem), copy-paste programming (pull out a method) and spaghetti code (pin it down, then untangle it)

The golden hammer gets its name from an old saying: when all you have is a hammer, everything looks like a nail. A golden one is a hammer you're proud of, which makes it harder to put down. Right after a course like this one, the golden hammer is usually a pattern.

Using the names kindly

The names are for problems, not for people. "This is turning into a god object" is about the class, and it comes with a way out. "You wrote spaghetti" is about the person, and comes with nothing. Name the anti-pattern, point at the lines, and suggest the better answer: that's how the names save a team time instead of starting an argument. Everyone writes all four at some point; most code with a god object in it was written by someone in a hurry, doing their best.

How often you'll meet them: every week, in reviews and in old code (our estimate).

Check yourself

2 questions to try on the lesson page.

So what

Patterns give fixes names; anti-patterns give mistakes names. When you can say "copy-paste" or "god object" and point at the lines, the whole team knows the problem and its better answer in two words.

Key terms

Cheat sheets