This article provides some methods from Apache & Google libraries which can help to quickly refactor code without much impact & makes code more readable & well formed.
Libraries – Guava, Apache Commons Lang
Below code snippets provide the code which can be found in any project & then provides Apache/Google library way to refactor it.
Multiple objects null checks (If not null AND if not null …)
1 2 3 4 5 6 7 8 9 10 11 |
/** Look for code like this **/ if (someObject != null && anotherObject != null && yetAnotherObject != null) { doSomething(); } /** Refactor using Apache commons lang3 **/ import static org.apache.commons.lang3.ObjectUtils.allNotNull; if (allNotNull(someObject, anotherObject, yetAnotherObject)) { doSomething(); } |
Default value (If null then use default value)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
/** Look for code like this **/ if (name != null) { providedName = name; } else { providedName = "NOT_PROVIDED"; } /** Refactor using ternary operator **/ providedName = name != null ? name : "NOT_PROVIDED"; /** Refactor using Apache commons lang3 **/ import static org.apache.commons.lang3.ObjectUtils.defaultIfNull; providedName = defaultIfNull(name, "NOT_PROVIDED"); |
Multiple String equality checks (If equals OR if equals …)
1 2 3 4 5 6 7 8 9 10 11 |
/** Look for code like this **/ if("SOMETYPE".equals(type) || "OTHERTYPE".equals(type) || "YETOTHERTYPE".equals(type)) { doSomething(); } /** Refactor using apache commons lang3 **/ import static org.apache.commons.lang3.StringUtils.equalsAny; if(equalsAny(type, "SOMETYPE","OTHERTYPE","YETOTHERTYPE")) { doSomething(); } |
Create new List with some values (New list , add, add …)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/** Look for code like this **/ List<String> list = new ArrayList<>(); list.add("ONE"); list.add("TWO"); list.add("THREE"); /** Refactor using Guava, if list supposed to be modified later **/ import static com.google.common.collect.Lists.newArrayList; List<String> list1 = newArrayList("ONE", "TWO", "THREE"); /** If list is not supposed to be modified later **/ import com.google.common.collect.ImmutableList; List<String> list2 = ImmutableList.of("ONE", "TWO", "THREE"); |
Map of Constant values (New map, put, put …)
1 2 3 4 5 6 7 8 9 |
/** Look for code like this **/ Map<String, String> mapOfConstants = new HashMap<String, String>(); mapOfConstants.put("USA", "USD"); mapOfConstants.put("India", "INR"); /** Refactor using Guava **/ import com.google.common.collect.ImmutableMap; Map<String, String> mapOfConstants1 = new ImmutableMap.Builder().put("USA", "USD").put("India", "INR").build(); |
First non-null non-empty String (If not empty else if not empty …)
1 2 3 4 5 6 7 8 9 10 11 12 13 |
/** Look for code like this **/ if (name != null && !name.isEmpty()) { name = firstName; } else if (nickName != null && !nickName.isEmpty()) { name = nickName; } else if (shortName != null && !shortName.isEmpty()) { name = shortName; } /** Refactor using Apache commons lang 3 **/ import static org.apache.commons.lang3.StringUtils.firstNonEmpty; name = firstNonEmpty(firstName, nickName, shortName); |
Enum validity check
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
/** Look for code like this **/ SomeEnum someEnum = null; try { someEnum = SomeEnum.valueOf(someString); } catch (IllegalArgumentException e) { // Exception handling } if (someEnum != null) { doSomething(); } /** Refactor using Guava **/ Optional<SomeEnum> someEnum1 = getIfPresent(SomeEnum.class, someString); if (someEnum1.isPresent()) { doSomething(); } /** Refactor using Apache commons lang3 **/ boolean isValidSomeEnum = isValidEnum(SomeEnum.class, someString); if (isValidSomeEnum) { SomeEnum someEnum2 = someEnum.valueOf(someString); doSomething(); } |