How to Avoid Static Helper Classes

Static helper classes are classes which contain only static methods, often having names that end in Helper or Utils. The existence of such a class usually indicates a sub-optimal design. In this post I’ll talk about why these classes are a poor design choice in an object oriented language such as Java and how to refactor to a better design.

How to avoid static helpers…

Simplify Code by Encapsulating Collections

A quick survey: Below are two methods that return school district ratings given a list of houses. Which method do you find more to the point?

Method A
public List<Rating> getDistrictRatings(List<House> houses, Price maxPrice) {
  Set<SchoolDistrict> districts = houses.stream()
      .filter(house -> house.price().isLessThan(maxPrice))
      .map(house -> house.getSchoolDistrict())
      .collect(Collectors.toSet());

  return ratingService.rateDistricts(districts);
}
Method B
public List<Rating> getDistrictRatings(Houses houses, Price maxPrice) {
  Set<SchoolDistrict>=> districts = houses.below(maxPrice)
      .getSchoolDistricts();
  return ratingService.rateDistricts(districts);
}

How to achieve the cleaner method…

How to avoid Object types in Java using the Adapter Pattern

Using the Object type for a method parameter or instance variable in Java application code is a choice that will later haunt you. The temptation to use Object strikes when common functionality needs to be extracted from otherwise unrelated classes and it is impossible for these classes to share a common interface. Seeing this problem can indicate that a larger refactoring is in order, however for now we’ll focus on this specific issue.

How to avoid using Object…

Most Useful IntelliJ Refactorings for Cleaner Code

IntelliJ’s refactorings are a quick way to improve your Java code. In this post I detail the refactoring actions I find most commonly useful. Even small refactorings can greatly improve readability and make larger refactorings easier and more obvious. Many of these actions have multiple options that are worth exploring. For a more in depth discussion of refactoring I recommend Martin Fowler’s Refactoring: Improving the Design of Existing Code [affiliate link].

See refactorings…