Introduction
This guide defines conventions for Java code. Code reviewers, keep these conventions in mind when reviewing code.
Guidelines
try-with-resources
Close resources by defining them in the try block; avoid finally blocks for resource management.
Good:
try ( FileInputStream fis = new FileInputStream(…) ) { … some code here … } catch (IOException e) { LOG.error(…) } |
Avoid:
FileInputStream fis = null; try { fis = new FileInputStream(…); … some code here … } catch (IOException e) { LOG.error(…) } finally // don’t follow this pattern { if ( fis != null ) {…} } |
Parameterized Logging
Avoid if-checks to log output, and avoid string concatenation in the message.
Good:
LOG.debug(“user {} created object type {} with id {}”, auth.getName(), objectType, objectId); |
Avoid:
if ( LOG.isDebugEnabled() ) { LOG.debug(“user ” + auth.getName() + ” created object type ” + objectType + ” with id ” + objectId); } |