Performance: @Size for length constraints
LOW IMPACT
@Size affects form validation speed and user experience by checking input length before processing.
import jakarta.validation.constraints.Size; public class User { @Size(min = 3, max = 20) private String username; // getters and setters }
public class User { private String username; public void setUsername(String username) { if(username == null || username.length() < 3 || username.length() > 20) { throw new IllegalArgumentException("Invalid length"); } this.username = username; } }
| Pattern | Validation Timing | Code Complexity | Server Load | Verdict |
|---|---|---|---|---|
| Manual length check in setter | Late (during setter call) | High (repeated code) | Higher (extra processing) | [X] Bad |
| @Size annotation on field | Early (before logic) | Low (declarative) | Lower (early rejection) | [OK] Good |