Hamza has some friends in the theater business. These friends had an in-house developed Java application to manage seating arrangements, and they had some problems with it. They had lots of problems with it. So Hamza cut them a deal and agreed to take a look.

There were the usual litany of problems: performance was garbage, features bugged out if you didn’t precisely follow a certain path, it crashed all the time, etc. There was also an important missing feature.

Seats in a theater section lay out roughly in a grid. Roughly- not every row has exactly the same number of seats, sometimes there are gaps in the middle of the row to make way for the requirements of the theater itself, and the application could handle that- for one arrangement. Seats could be removed to create standing-room sections, seats could be added to rows, seats could be reserved for various purposes, and so on. None of this was supported by the application.

Hamza dug into the code which rendered the seating arrangements. Did it read from a database? Did it read from a config file? Did it have a hard-coded array?

for (int r = 0; r < NUMROWS; r++) {
  for (int c = 0; c < NUMCOLS; c++) {
    Rectangle rec = new Rectangle(24, 24, Color.rgb(204, 0, 0));
    fat[r][c] = new Seat();
    rec.setStroke(Color.BLACK);
    changeRecColor(rec, r, c);
    StackPane s = new StackPane();
    s.getChildren().add(rec);
    gridPane.add(s, c, r);
    if (
        r == 14 ||
            c == 0 ||
            c == 1 && r != 23 ||
            c == 2 && r != 23 ||
            c == 3 && (r != 22 && r != 23) ||
            c == 4 && (r != 20 && r != 21 && r != 22 && r != 23) ||
            c == 5 && (r != 12 && r != 13 && r != 18 && r != 19 && r != 20 && r != 21 && r != 22 && r != 23) ||
            c == 6 && (r != 10 && r != 11 && r != 12 && r != 13 && r != 16 && r != 17 && r != 18 && r != 19 && r != 20 && r != 21 && r != 22 && r != 23) ||
            c == 7 && (r != 8 && r != 9 && r != 10 && r != 11 && r != 12 && r != 13 && r != 16 && r != 17 && r != 18 && r != 19 && r != 20 && r != 21 && r != 22 && r != 23) ||
            c == 8 && (r == 0 || r == 1 || r == 2 || r == 3 || r == 4 || r == 5 || r == 24) ||
            c == 9 && (r == 0 || r == 1 || r == 2 || r == 3 || r == 24) ||
            c == 10 && (r == 1 || r == 24) ||
            (c == 11 || c == 12 || c == 13) && r == 24 ||
            (c == 14 || c == 15 || c == 16) && r == 24 ||
            c == 17 ||
            c == 18 ||
            c == 19 && (r != 0) ||
            c == 20 && (r == 15 || r == 23 || r == 24) ||
            c == 21 && (r == 23 || r == 24) ||
            (c == 22 || c == 23) && (r == 23 || r == 24) ||
            (c == 24 || c == 25 || c == 26 || c == 27 || c == 28 || c == 29 || c == 30) && (r == 23 || r == 24) ||
            (c == 31 || c == 32 || c == 33 || c == 34 || c == 35) && (r == 23 || r == 24) ||
            c == 36 && (r != 0 && r != 16 && r != 17 && r != 18 && r != 19 && r != 20 && r != 21 && r != 22) ||
            c == 37 && (r != 23) ||
            c == 38 && (r != 23) ||
            c == 39 && (r == 15 || r == 16 || r == 17 || r == 18 || r == 19 || r == 20 || r == 21 || r == 22 || r == 24) ||
            c == 40 && r == 24 ||
            (c == 41 || c == 42 || c == 43 || c == 44) && r == 24 ||
            c == 45 && (r == 1 || r == 24) ||
            c == 46 && (r < 14 && (r == 0 || r == 1 || r == 2 || r == 3) || r == 24) ||
            c == 47 && (r < 14 && (r == 0 || r == 1 || r == 2 || r == 3 || r == 4 || r == 5) || r == 24) ||
            c == 48 && (r != 8 && r != 9 && r != 10 && r != 11 && r != 12 && r != 13 && r != 15 && r != 16 && r != 17 && r != 18 && r != 19 && r != 20 && r != 21 && r != 22 && r != 23) ||
            c == 49 && (r != 10 && r != 11 && r != 12 && r != 13 && r != 16 && r != 17 && r != 18 && r != 19 && r != 20 && r != 21 && r != 22 && r != 23) ||
            c == 50 && (r != 12 && r != 13 && r != 16 && r != 17 && r != 18 && r != 19 && r != 20 && r != 21 && r != 22 && r != 23) ||
            c == 51 && (r < 14 || r > 14 && (r == 15 || r == 16 || r == 17 || r == 24)) ||
            c == 52 && (r != 20 && r != 21 && r != 22 && r != 23) ||
            c == 53 && (r != 22 && r != 23) ||
            c == 54 ||
            r > 24
    ) {
      rec.setFill(Color.WHITE);
      fat[r][c] = null;
    }
  }
}

No, it had a 37 line if condition.

Hamza writes: “Refactoring this application was fun. Not.”

[Advertisement] Otter - Provision your servers automatically without ever needing to log-in to a command prompt. Get started today!