- Use underscore
_
as digit separator in long numeric literals [Java7]
// 👎 non-compliant
long creditCardNumber = 1234567890123456L;
long hexBytes = 0xFFECDE5E;
long bytes = 0b11010010011010011001010010010010;
// 👍 preference
long creditCardNumber = 1234_5678_9012_3456L;
long hexBytes = 0xFF_EC_DE_5E;
long bytes = 0b11010010_01101001_10010100_10010010;
- Use
&
to check oddity
Reason: cover negative cases, high performance
// 👎 non-compliant
public boolean isOdd(int num) {
return num % 2 == 1;
}
// 👍 preference
public boolean isOdd(int num) {
return (num & 1) != 0;
}
- Use
double-brace initialization/stream=> convenience factory method to initialize collections [Java9]
// 👎 traddictional: verbose
Set<String> set = new HashSet<>();
set.add("one");
set.add("two");
set.add("three");
set = Collections.unmodifiableSet(set);
// 👎 double-brace initialization: reduces the readability, anti-pattern
Set<String> set = Collections.unmodifiableSet(new HashSet<String>() {{
add("one"); add("two"); add("three");
}});
// 👎 stream: not obvious and intuitive, creates of unnecessary objects, can't create Map
Stream.of("one", "two", "three").collect(collectingAndThen(toSet(), Collections::unmodifiableSet));
// 👍 convenience factory
Set<String> set = Set.of("one", "two", "three");