Java traps · Chapter 12 of 16

Optional's eager orElse

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Money
  • 3 tasks
  • Java
  • In the all labs plan

The first 2 chapters of Java traps are free. This one comes with the all labs plan.

Why you need this

The method returns the right value: the found id wins. Nothing throws and nothing in the return value is wrong. Only the side effect is, and a test that checks the returned id alone passes on the bug.

A hurried fix often reaches for get(), which has its own trap: on an empty Optional it throws (java Empty.java, a lookup of HWH):

Exception in thread "main" java.util.NoSuchElementException: No value present
	at java.base/java.util.Optional.get(Unknown Source)
	at Empty.main(Empty.java:11)

get() is only safe right after a check, and an Optional you looked up before you created something is still empty afterwards.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Repro.java: a regular who is signed up again

    Write reproduce() in Repro.java. Make a fake Loyalty.Customers that knows one regular (findByPhone gives their id for their phone and an empty Optional for any other phone) and counts how many times create is called (create gives a new id). Check the regular in with Loyalty.checkIn, and return how many times create ran.

    The check runs your reproduce() twice: beside the original Loyalty.java it must return 1 (a regular the till found was signed up again), and beside a fixed Loyalty.java it must return 0. So it must really check a known phone in through checkIn and count the calls. No real waiting and no reading input: reproduce() must finish within 3 seconds.

  2. Task 2

    Loyalty.java: a regular is found, never signed up again

    Fix checkIn(String phone, Customers customers) in Loyalty.java so that it calls create only when findByPhone has no one for that phone: for a phone the customers service knows, it returns that customer's id and never calls create; for a phone it does not know, it calls create once and returns the id create gave.

    Keep the Customers interface and the log line, check-in <phone>: customer <id>, as they are. The check uses its own fake customers service, which counts every call to create, and checks in regulars, new passengers and a new passenger who scans again. A fix that stops the duplicates but throws for a passenger the service has never seen is as wrong as the bug.

  3. Task 3

    LoyaltyTest.java: a test that would have caught it

    Write a JUnit test class, LoyaltyTest.java, that would have caught this bug before it reached the tills. The check runs your file three times: beside the original Loyalty.java, where at least one test must fail; beside the fixed Loyalty.java, where every test must pass; and beside a wrong fix that calls create when the phone is unknown and then get() on the empty Optional it found before, where at least one test must fail.

    So test both sides with a fake Customers that counts its create calls: a regular is checked in and create is never called, and a new passenger is signed up once and gets the new id back. No real waiting: each test must finish within 3 seconds.

What you will learn

  • Repro.java: a regular who is signed up again
  • Loyalty.java: a regular is found, never signed up again
  • LoyaltyTest.java: a test that would have caught it

More in Money

Build "Optional's eager orElse" yourself

Tapri Counter runs on Java, and Kabir, its senior developer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Java traps are free. This one comes with the all labs plan.