Favor Format Over Concatenation
| class Mission { |
| |
| Logbook logbook; |
| LocalDate start; |
| |
| void update(String author, String message) { |
| LocalDate today = LocalDate.now(); |
| String month = String.valueOf(today.getMonthValue()); |
| String formattedMonth = month.length() < 2 ? "0" + month : month; |
» | String entry = author.toUpperCase() + ": [" + formattedMonth + "-" + |
| today.getDayOfMonth() + "-" + today.getYear() + "](Day " + |
| (ChronoUnit.DAYS.between(start, today) + 1) + ")> " + |
| message + System.lineSeparator(); |
| logbook.write(entry); |
| } |
| } |
Understandability and readability don’t just matter in your code—they’re also important for the output your code produces.
If you have to build large strings, you can use format strings ...
Get Java By Comparison now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.