Performance: Specification pattern for dynamic queries
MEDIUM IMPACT
This pattern affects database query performance and server response time by dynamically building queries.
Specification<Entity> spec = Specification.where(null); if (name != null) { spec = spec.and(EntitySpecifications.hasName(name)); } if (age != null) { spec = spec.and(EntitySpecifications.hasAge(age)); } if (city != null) { spec = spec.and(EntitySpecifications.hasCity(city)); } return repository.findAll(spec);
List<Entity> findByFilters(String name, Integer age, String city) {
if (name != null) {
// query with name filter
}
if (age != null) {
// query with age filter
}
if (city != null) {
// query with city filter
}
// multiple if-else with string concatenation for query
return null; // placeholder return
}| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual string query concatenation | 0 | 0 | 0 | [X] Bad |
| Specification pattern with composable predicates | 0 | 0 | 0 | [OK] Good |