Quick revision
The short answer
We rename the method and variables so they reveal intent, for example findFlaggedCells, replace int[] with a record such as Cell(int status, int row, int col) so each value has a name, replace the magic number 4 with a named constant or an enum value such as FLAGGED, and either use a stream, cells.stream().filter(Cell::isFlagged).toList(), or a clear loop with braces around every body. The behaviour stays the same, but a reader can now understand the method without guessing.
What to remember
In this answer, we rewrote a cryptic five-line method step by step. We gave the method and variables names that reveal intent, replaced the int[] with a Cell record so each value has a name and a type, replaced the magic number 4 with an enum value and an isFlagged() method, and added braces or used a stream filter so the structure is explicit. The behaviour is the same, apart from the stream's unmodifiable result, but the code now explains itself.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We rename the method and variables so they reveal intent, for example findFlaggedCells, replace int[] with a record such as Cell(int status, int row, int col) so each value has a name, replace the magic number 4 with a named constant or an enum value such as FLAGGED, and either use a stream, cells.stream().filter(Cell::isFlagged).toList(), or a clear loop with braces around every body. The behaviour stays the same, but a reader can now understand the method without guessing.
Why this question is asked
Readability questions look easy, and that is why they are useful in interviews. Anyone can make code shorter; the question is whether we can make it obviously correct to the next person, and explain why each change helps rather than just applying rules. This snippet packs several classic problems into one line: names that say nothing, an array used as a record, a magic number, and a loop and an if without braces. In this answer, we will read the original code as a newcomer would, then improve it step by step, explaining each change, and finish with the complete rewritten version.
1Reading the Original as a Newcomer
Here is the code from the question, formatted so we can see its structure.
It compiles and works. But try to answer simple questions about it. What does it return? Them, which could be anything. What is l, or x? What does x[0] hold, and why does 4 matter? What are x[1] and x[2]? Nothing in the code tells us. The reader has to find the callers, or the original author, to understand a five-line method. The problem is not complexity but missing meaning.
2Names That Reveal Intent
A good name answers why something exists and what it does. The method returns the cells that are flagged, so we call it findFlaggedCells. The parameter is the board's cells, so cells. The result is the flagged ones, so flagged. The loop variable is one cell, so cell.
Notice that nothing about the logic changed, yet the method's purpose is now clear from its signature alone. A caller reading board.findFlaggedCells(cells) no longer needs to open the method. Short names like i are fine for tiny scopes such as an index in a two-line loop; l, r and x here carry real meaning that must be spelled out.
3A Record Instead of int[]
The int[] is being used as a little object with three fields: a status and, presumably, a row and a column. Arrays do not have field names, so the meaning is in the indexes, which the reader must remember. Since Java 16, a record gives us a named, immutable data class in one line, with a constructor, accessors, equals, hashCode and toString.
Now cell.status() says what it is, the compiler stops us from mixing up row and column positions, and printing a cell in a log shows readable values instead of something like [I@1b6d3586. The method signature becomes List<Cell> findFlaggedCells(List<Cell> cells).
4Replace the Magic Number
A magic number is a literal whose meaning is not stated, like 4 here. It forces every reader to know that 4 means flagged, and if the encoding ever changes, we must find every 4 that means flagged and none of the 4s that mean something else. The simplest fix is a named constant. A better fix, if we control the model, is an enum, because then an invalid status cannot exist at all.
With the enum, the question what does 4 mean disappears, and isFlagged() puts the rule next to the data it reads, so every caller uses the same check. If the numbers come from an external format, such as a file where 4 means flagged, we convert them to the enum once, at the boundary, and the rest of the code never sees the raw number.
5A Clear Loop With Braces, or a Stream
The original loop and if have no braces. That is legal Java, but it invites a well-known mistake: someone adds a second line, indents it to look like part of the if, and it runs for every element. Braces make the structure explicit and keep later edits safe.
The stream version reads almost like the method's name: take the cells, keep the flagged ones, make a list. Stream.toList(), available since Java 16, returns an unmodifiable list, so a caller that tries to add to it gets UnsupportedOperationException; if callers need a mutable list, use collect(Collectors.toCollection(ArrayList::new)) or keep the loop. Both versions are fine; the team's style decides, and the important part is that the intent is obvious.
6The Rewritten Version
Putting all the changes together gives code whose purpose is clear without any comment.
| Before | After | Why |
|---|---|---|
getThem, l, r, x | findFlaggedCells, cells, flagged, cell | names explain purpose |
| int[] with x[0], x[1], x[2] | record Cell(status, row, col) | fields have names and types |
| x[0] == 4 | cell.isFlagged(), CellStatus.FLAGGED | no magic number, one rule |
| for and if without braces | braces, or a stream filter | safe edits, intent visible |
Notice what we did not do: we did not add comments to explain x[0] == 4. A comment would have helped, but it can go out of date, while names and types are checked by the compiler and stay correct. The one behaviour difference to mention in an interview is that toList() returns an unmodifiable list where the original returned an ArrayList.
7Follow-up questions
Is the stream version always better than the loop?
No. For a simple filter like this, the stream reads very clearly. For logic with several conditions, early exits or checked exceptions, a well-named loop is often easier to read and debug. Readability, not fashion, should decide.
Why not just add a comment saying 4 means flagged?
A comment explains the problem instead of removing it. Every other place that checks a status still uses 4, the comment can become wrong when the code changes, and the compiler cannot help. A named constant or enum puts the meaning into the code itself.
What if the int[] format comes from an external library?
Then we convert at the boundary: one small method turns each int[] into a Cell, with the meaning of each index written in one place. Everything else works with Cell, and only the converter knows about the raw format.
8Conclusion
In this answer, we rewrote a cryptic five-line method step by step. We gave the method and variables names that reveal intent, replaced the int[] with a Cell record so each value has a name and a type, replaced the magic number 4 with an enum value and an isFlagged() method, and added braces or used a stream filter so the structure is explicit. The behaviour is the same, apart from the stream's unmodifiable result, but the code now explains itself.
Open with
I would make four changes. First, names. getThem, l, r and x tell the reader nothing, so I would call the method findFlaggedCells, the parameter cells, the result flagged and the loop variable cell.
Then, if they want more
Just that makes the purpose clear from the signature. Second, the int[] is really a small object with a status, a row and a column, but its fields only have index numbers, so I would replace it with a record, Cell with status, row and col.
Then cell.status() says what it is, the compiler stops mix-ups, and logging a cell prints readable values. Third, 4 is a magic number.
At minimum I would make it a constant called FLAGGED, but better is an enum CellStatus with FLAGGED, and an isFlagged method on the record, so the rule lives in one place and an invalid status cannot exist. If the numbers come from an external format, I would convert them once at the boundary.
Fourth, the loop and if have no braces, which invites the classic bug where someone adds a second indented line that runs for every element. I would add braces, or write it as cells.stream().filter(Cell::isFlagged).toList(), which reads like the method name.
I would mention that toList returns an unmodifiable list, so if callers modify the result I would keep the loop or collect into an ArrayList.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 16
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Rewrite this for readability and explain each change:": https://scrivio-interview-library.netlify.app/java/clean-code/54deb9d398/