Performance: Returning different status codes
This affects the server response time and perceived page load speed by the client, impacting how quickly the browser can proceed after receiving the status code.
public ResponseEntity<String> getData() {
Optional<String> data = service.getDataOptional();
return data.map(d -> ResponseEntity.ok(d))
.orElseGet(() -> ResponseEntity.status(HttpStatus.NOT_FOUND).body("Not Found"));
}public ResponseEntity<String> getData() {
try {
String data = service.getData();
return new ResponseEntity<>(data, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>("Error", HttpStatus.INTERNAL_SERVER_ERROR);
}
}| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Generic 500 on all errors | N/A (server-side) | N/A | N/A | [X] Bad |
| Specific status codes (404, 200) | N/A (server-side) | N/A | N/A | [OK] Good |
public ResponseEntity<String> getUser(boolean found) {
if (found) {
return ResponseEntity.ok("User found");
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("User not found");
}
}public ResponseEntity<String> createItem() {
return new ResponseEntity<>("Item created", 201);
}Pass the test and this pattern moves unknown