Last active
December 7, 2016 16:10
-
-
Save gaplo917/a80a23bf3acf277ef37d9e993d0caf63 to your computer and use it in GitHub Desktop.
Java Utils Pattern is not Human friendly or IDE friendly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Java | |
// please focus on the function name & declaration | |
// As we all know, String is a final class and we can't extend | |
class StringUtils{ | |
public static TypeA toTypeA(String str) | |
} | |
// Assume TypeA is from third party library that you can't extend | |
class TypeAUtils { | |
public static Double calculateResult(TypeA a) | |
} | |
// Similar to String, you can't extend Double | |
class DoubleUtils { | |
public static Double toTwoDecimalPlace(Double d) | |
} | |
String someString = "someString"; | |
// The execution order is not as same as to our reading direction(top-to-bottom) | |
// it is difficult to read | |
final Double typeAResultWithRounding = | |
DoubleUtils.toTwoDecimalPlace( | |
TypeAUtils.calculateResult( | |
StringUtils.toTypeA(someString) | |
) | |
); | |
// flatten it? better to read but more unnessecary variables are created | |
final TypeA typeA = StringUtils.toTypeA(someString); | |
final Double typeAResult = TypeAUtils.calculateResult(typeA); | |
final Double typeAResultWithRounding = DoubleUtils.toTwoDecimalPlace(typeArResult); | |
// Question: | |
// How do a new comer would know to use exactly these three Utils? | |
// Normally, they would ask someone or implement a duplicate one for their use case. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment