Split Method with Boolean Parameters

 class​ Logbook {
 
 static​ ​final​ Path CAPTAIN_LOG = Paths.get(​"/var/log/captain.log"​);
 static​ ​final​ Path CREW_LOG = Paths.get(​"/var/log/crew.log"​);
 
»void​ log(String message, ​boolean​ classified) ​throws​ IOException {
 if​ (classified) {
  writeMessage(message, CAPTAIN_LOG);
  } ​else​ {
  writeMessage(message, CREW_LOG);
  }
  }
 
 void​ writeMessage(String message, Path location) ​throws​ IOException {
  String entry = LocalDate.now() + ​" "​ + message;
  Files.write(location, Collections.singleton(entry),
  StandardCharsets.UTF_8, StandardOpenOption.APPEND);
  }
 }

In general, a method should specialize on a single task only. Boolean method parameters show that a method does at ...

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.