
Stop Writing if-checks: Refinement Types in Java 8+
The Code That Lives in Every Java Codebase Every Java developer has written this code. You have written this code. Probably this week. A method starts with five lines of null checks, blank checks, and range checks before a single line of business logic executes. The validation is scattered, duplicated, and — worst of all — invisible at the API boundary. public void createUser ( String name , int age , List < String > roles ) { if ( name == null || name . trim (). isEmpty ()) { throw new IllegalArgumentException ( "name must not be blank" ); } if ( age <= 0 ) { throw new IllegalArgumentException ( "age must be positive" ); } if ( roles == null || roles . isEmpty ()) { throw new IllegalArgumentException ( "roles must not be empty" ); } // finally, the actual logic... } You know the problems. The checks get copy-pasted across service layers. Someone forgets one in a new method. A bug slips through because the validation was in the controller but not in the domain service. The types li
Continue reading on Dev.to
Opens in a new tab


