A7 postmortem — what a reader needs

A file laid out to be read, a value named once, a switch Java can check, and why null has no place in this course’s programs.

Published

October 25, 2026

A7 is marked, and your own feedback is on Brightspace, on your submission. This is the other half: four things that came up while marking and are worth saying once, to everyone.

The code below is reconstructed: the shape a file had, with the names changed. Nobody’s file is quoted.

One statement to a line, and spaces

int fare(int km){return km<=2?5:5+km*2;}

A file crammed onto a few long lines creates an uncalled-for challenge for the instructor and won’t be marked. What to do? There are several ways to deal with formatting in general.

First, use IDEA’s built-in tool to format your code properly: Code → Reformat Code (Ctrl+Alt+L) lays a file out one statement to a line in one keystroke. But here are two reasons against it.

  1. This tool admits a workflow when you work with code that is ill-formatted, and you only format it before handing in. This is a wrong workflow: if your code has an issue, finding it will be more challenging than necessary. Compounding with that, sometimes the reformat tool won’t work if your code is broken, closing the door for a workflow when you format only when you need to read it (another popular among newcomers but a wrong idea).
  2. Auto-formatting tools are meant for a different scenario than what this course does. Such tools are good at removing friction in teams working on a shared code base where every person may have their own preferences around “reasonable formatting”, and the tool ensures uniformity (no one wants to work on code written in 10 different styles, however good each style is).

Second, maintain reasonable formatting manually at all times. It’s easier than it sounds (you only need to hit Enter once in a while, and rarely Tab or Backspace) and is the advisable way to go about formatting:

// the fare: 5 up to 2 km, 5 plus 2 a km beyond
int fare(int km) {
    return km <= 2 ? 5 : 5 + km * 2;
}

And a space on each side of an operator, km <= 2 ? 5 : 5 + km * 2 rather than km<=2?5:5+km*2, so that each part can be read on its own.

From A8 on, a crammed file gets 0; A8’s page already says so.

Name a value you need more than once

Several prices asked for the same value twice, once in the question and again in the answer:

int full = ticket(age);
return full < discount(s) ? 0 : full - discount(s);

Give it a name once and use the name. The value is worked out once, and “never below 0” reads the way the page says it:

int full = ticket(age) - discount(s);
return full < 0 ? 0 : full;

The same goes for ticket(age) written out several times. From now on it costs a little when the name is missing.

Why an enum’s switch has no default

return switch (r) {
    case G, PG, PG13 -> true;
    case R -> age >= 17 || withAdult;
    case NC17 -> age >= 18;
};

This arrow form is a switch expression: it has a value, and Java refuses the program unless every value of the enum has a case. Add a sixth rating to Rating, and Java points at this switch — the switch expression does not cover all possible input values — until someone decides what the new rating means. A default would answer for the new rating without anyone deciding, and Java would say nothing.

The older form, a switch statement with case R: return ...; and a return false; after it, falls into the same trap:

switch (r) {
    case G:
    case PG:
    case PG13:
        return true;
    case R:
        return age >= 17 || withAdult;
    case NC17:
        return age >= 18;
}
return false;

Java does not hold a statement to every value — without the last line it says missing return statement — and the return after it answers for whatever is missing, exactly as a default would. Use the arrow form, with no default when deciding about enums. For a set that can never grow, like the days of the week, a default for “every other day” is defensible; the rule is for enums a program might extend.

null checks

int total(Order o) {
    if (o == null) {
        return 0;
    }
    return o.count() * o.price();
}

Checking for null came up in some submissions. It is a complicated topic and not a part of this course. Hence, null must not be mentioned in your programs for this course.

One thing about null checks is worth knowing already, though. When a method hands back some default value (e.g. "No ticket", 0, false) after seeing null, it hides a problem somewhere else in the program — the place that passed the null in. The program carries on as if nothing was wrong. This is one of the worst modes of failure — silent failures, and it is similar to the default problem from the previous section.

The next course in the Java sequence, CMSC 130, will introduce the idea of exceptions — a dedicated Java mechanism to deal with errors like receiving null (and many others).