© COPYRIGHT 2019 COVEROS, INC. ALL RIGHTS RESERVED. 1@CoverosGene #CodeMash
Creative Solutions to
Already Solved Problems
Gene Gotimer
© COPYRIGHT 2019 COVEROS, INC. ALL RIGHTS RESERVED. 2@CoverosGene #CodeMash
Let us help you
Please think before you code
Your IDE will warn you
Analysis will point out problems
It’s dangerous to go alone. Ask for help
© COPYRIGHT 2019 COVEROS, INC. ALL RIGHTS RESERVED. 3@CoverosGene #CodeMash
Make sure it is imported
import java.util.Date;
import java.lang.Boolean;
import java.lang.Boolean;
import java.sql.Timestamp;
import java.lang.Boolean;
import java.lang.Boolean;
import java.lang.Boolean;
import java.util.Date;
1
2
1
2
3
4
5
© COPYRIGHT 2019 COVEROS, INC. ALL RIGHTS RESERVED. 4@CoverosGene #CodeMash
Make sure it is really a String
ArrayList<String> xqueries = new ArrayList<String>();
xqueries.add("/EnrollReq/SenderID/text()");
xqueries.add("/EnrollReq/TransInfo/CurrentTime/text()");
List<String> forMap = execXPath(xqueries.get(0).toString());
if(forMap.size() > 0){
retMap.put("SenderID", forMap.get(0).toString());
}else{
retMap.put("SenderID", "");
}
© COPYRIGHT 2019 COVEROS, INC. ALL RIGHTS RESERVED. 5@CoverosGene #CodeMash
Trust issues with version control
LinkUsers.java
LinkUsers.javaold
LoginAccountImpl.java
ManageAcctImpl.java
ManageAcctImpl.java_OLD
AppDaoImpl.javanew
© COPYRIGHT 2019 COVEROS, INC. ALL RIGHTS RESERVED. 6@CoverosGene #CodeMash
Concrete Strategy Factory Pattern
public class EnrollStrategyFactory {
private static EnrollStrategyFactory strtgyFctry = null;
private EnrollStrategy strtgy = new EnrollStrategy();
…
public EnrollStrategy getStrategy() {
return strtgy;
}
}
public class EnrollStrategy {…}
© COPYRIGHT 2019 COVEROS, INC. ALL RIGHTS RESERVED. 7@CoverosGene #CodeMash
That darned ternary operator
boolean srcReuseData;
srcReuseData = (true ? this.isReuseData():false);
So when true is true,
srcReuseData = this.isReuseData().
If true is not true,
srcReuseData = false and maybe chaos?
© COPYRIGHT 2019 COVEROS, INC. ALL RIGHTS RESERVED. 8@CoverosGene #CodeMash
Spell it all out
boolean boolResult = false;
if (hashResult > 0)
boolResult = true;
else
boolResult = false;
assertEquals("HashCode returned was not returned",
true, boolResult);
as opposed to
assertTrue("HashCode should be a positive integer",
hashResult > 0);
© COPYRIGHT 2019 COVEROS, INC. ALL RIGHTS RESERVED. 9@CoverosGene #CodeMash
Never don’t be not clear
fail("Did not throw a configuration exception when MyParser
was not configured with mappings");
fail("Did not throw a configuration exception when the
configuration property for MyParser had an
invalid value");
fail("Did not throw a configuration exception when MyParser
configuration referenced a role that does
not exist");
© COPYRIGHT 2019 COVEROS, INC. ALL RIGHTS RESERVED. 10@CoverosGene #CodeMash
Always check for blank. Always.
if (!(retrievedList == null
|| "".equals(retrievedList))) {
if (person.getBirthDate() != null
&& !person.getBirthDate().equals("")) {
if (person.getGender() != null
&& !person.getGender().equals("")) {
if (org.getPeriodEndDate() != null
&& !org.getPeriodEndDate().equals("")) {
String[]
java.util.Date
pkg.Gender
java.util.Date
© COPYRIGHT 2019 COVEROS, INC. ALL RIGHTS RESERVED. 11@CoverosGene #CodeMash
Just snuck over the limit
© COPYRIGHT 2019 COVEROS, INC. ALL RIGHTS RESERVED. 12@CoverosGene #CodeMash
Strive to be #1
3004 public static MemberEvent memberEventMapper(
3005 MemberEventDTO memberEventDto) {
3006 MemberEvent memberEvent = new MemberEvent();
…
4336 return memberEvent;
4337 }
Method lines of code: 1,334 (top is 1,793)
Method complexity: 256 #1
© COPYRIGHT 2019 COVEROS, INC. ALL RIGHTS RESERVED. 13@CoverosGene #CodeMash
Just a simple DTO
1 …
comments and annotations
1346 public class PersonResultsDTO
1347 {
…
43734 }
43735
© COPYRIGHT 2019 COVEROS, INC. ALL RIGHTS RESERVED. 14@CoverosGene #CodeMash
Test all the things, literally
for (Method method : object.getClass().getMethods()) {
if (method.getName.startsWith("set")) {
if (method.getParameterTypes().length > 0) {
Class paramClass = method.getParameterTypes()[0];
if (paramClass.getName.equals("java.lang.String")) {
method.invoke(object, "JUNIT TEST");
} else if (paramClass.getName.equals("java.lang.Object")) {
method.invoke(object, new Object());
} else if (paramClass.getName.equals("java.util.Date")) {
method.invoke(object, new java.util.Date());
} else if (paramClass.getName.equals("java.math.BigDecimal")) {
method.invoke(object, new BigDecimal(100));
© COPYRIGHT 2019 COVEROS, INC. ALL RIGHTS RESERVED. 15@CoverosGene #CodeMash
Well, I can’t just leave it empty
try {
…
} catch (Exception e) {
e.getMessage();
}
© COPYRIGHT 2019 COVEROS, INC. ALL RIGHTS RESERVED. 16@CoverosGene #CodeMash
Exception inception
try {
…
} catch (Exception e) {
try {
throw e;
} catch (Exception e1) {
e1.printStackTrace();
}
}
© COPYRIGHT 2019 COVEROS, INC. ALL RIGHTS RESERVED. 17@CoverosGene #CodeMash
Tests have exceptions, too
public Member generateTestMember(){
Member member = new Member();
…
return null;
}
@Test(expected = RuntimeException.class)
public void testAddTaxCreditSuccess() throws Exception {
Member person = generateTestMember();
String spouseId = person.findSpouseId();
assertNotNull(service.addTaxCredit(person, spouseId));
}
2
1
3
© COPYRIGHT 2019 COVEROS, INC. ALL RIGHTS RESERVED. 18@CoverosGene #CodeMash
I’d like to buy a constant
public static final String DASH = "-";
public static final String MANAGE_ACTION_0 = "0";
public static final String MANAGE_ACTION_1 = "1";
public static final String MANAGE_ACTION_2 = "2";
public static final String MANAGE_ACTION_3 = "3";
public static final String MANAGE_ACTION_4 = "4";
…
public static final String MANAGE_ACTION_17 = "17";
© COPYRIGHT 2019 COVEROS, INC. ALL RIGHTS RESERVED. 19@CoverosGene #CodeMash
Just give them descriptive names
// BigInteger ONE
public static final BigInteger BIGINTEGER_ONE = BigInteger.ONE;
// BigInteger ZERO
public static final BigInteger BIGINTEGER_ZERO = BigInteger.ZERO;
// BIGDECIMAL ZERO
public static final BigDecimal BIGDECIMAL_ZERO = BigDecimal.ZERO;
/** The Constant Y_INDICATOR. */
protected static final String Y_INDICATOR = "Y";
/** The Constant N_INDICATOR. */
protected static final String N_INDICATOR = "N";
© COPYRIGHT 2019 COVEROS, INC. ALL RIGHTS RESERVED. 20@CoverosGene #CodeMash
What if the values change?
public static final boolean BOOL_TRUE = true;
public static final boolean BOOL_FALSE = false;

Creative Solutions to Already Solved Problems

  • 1.
    © COPYRIGHT 2019COVEROS, INC. ALL RIGHTS RESERVED. 1@CoverosGene #CodeMash Creative Solutions to Already Solved Problems Gene Gotimer
  • 2.
    © COPYRIGHT 2019COVEROS, INC. ALL RIGHTS RESERVED. 2@CoverosGene #CodeMash Let us help you Please think before you code Your IDE will warn you Analysis will point out problems It’s dangerous to go alone. Ask for help
  • 3.
    © COPYRIGHT 2019COVEROS, INC. ALL RIGHTS RESERVED. 3@CoverosGene #CodeMash Make sure it is imported import java.util.Date; import java.lang.Boolean; import java.lang.Boolean; import java.sql.Timestamp; import java.lang.Boolean; import java.lang.Boolean; import java.lang.Boolean; import java.util.Date; 1 2 1 2 3 4 5
  • 4.
    © COPYRIGHT 2019COVEROS, INC. ALL RIGHTS RESERVED. 4@CoverosGene #CodeMash Make sure it is really a String ArrayList<String> xqueries = new ArrayList<String>(); xqueries.add("/EnrollReq/SenderID/text()"); xqueries.add("/EnrollReq/TransInfo/CurrentTime/text()"); List<String> forMap = execXPath(xqueries.get(0).toString()); if(forMap.size() > 0){ retMap.put("SenderID", forMap.get(0).toString()); }else{ retMap.put("SenderID", ""); }
  • 5.
    © COPYRIGHT 2019COVEROS, INC. ALL RIGHTS RESERVED. 5@CoverosGene #CodeMash Trust issues with version control LinkUsers.java LinkUsers.javaold LoginAccountImpl.java ManageAcctImpl.java ManageAcctImpl.java_OLD AppDaoImpl.javanew
  • 6.
    © COPYRIGHT 2019COVEROS, INC. ALL RIGHTS RESERVED. 6@CoverosGene #CodeMash Concrete Strategy Factory Pattern public class EnrollStrategyFactory { private static EnrollStrategyFactory strtgyFctry = null; private EnrollStrategy strtgy = new EnrollStrategy(); … public EnrollStrategy getStrategy() { return strtgy; } } public class EnrollStrategy {…}
  • 7.
    © COPYRIGHT 2019COVEROS, INC. ALL RIGHTS RESERVED. 7@CoverosGene #CodeMash That darned ternary operator boolean srcReuseData; srcReuseData = (true ? this.isReuseData():false); So when true is true, srcReuseData = this.isReuseData(). If true is not true, srcReuseData = false and maybe chaos?
  • 8.
    © COPYRIGHT 2019COVEROS, INC. ALL RIGHTS RESERVED. 8@CoverosGene #CodeMash Spell it all out boolean boolResult = false; if (hashResult > 0) boolResult = true; else boolResult = false; assertEquals("HashCode returned was not returned", true, boolResult); as opposed to assertTrue("HashCode should be a positive integer", hashResult > 0);
  • 9.
    © COPYRIGHT 2019COVEROS, INC. ALL RIGHTS RESERVED. 9@CoverosGene #CodeMash Never don’t be not clear fail("Did not throw a configuration exception when MyParser was not configured with mappings"); fail("Did not throw a configuration exception when the configuration property for MyParser had an invalid value"); fail("Did not throw a configuration exception when MyParser configuration referenced a role that does not exist");
  • 10.
    © COPYRIGHT 2019COVEROS, INC. ALL RIGHTS RESERVED. 10@CoverosGene #CodeMash Always check for blank. Always. if (!(retrievedList == null || "".equals(retrievedList))) { if (person.getBirthDate() != null && !person.getBirthDate().equals("")) { if (person.getGender() != null && !person.getGender().equals("")) { if (org.getPeriodEndDate() != null && !org.getPeriodEndDate().equals("")) { String[] java.util.Date pkg.Gender java.util.Date
  • 11.
    © COPYRIGHT 2019COVEROS, INC. ALL RIGHTS RESERVED. 11@CoverosGene #CodeMash Just snuck over the limit
  • 12.
    © COPYRIGHT 2019COVEROS, INC. ALL RIGHTS RESERVED. 12@CoverosGene #CodeMash Strive to be #1 3004 public static MemberEvent memberEventMapper( 3005 MemberEventDTO memberEventDto) { 3006 MemberEvent memberEvent = new MemberEvent(); … 4336 return memberEvent; 4337 } Method lines of code: 1,334 (top is 1,793) Method complexity: 256 #1
  • 13.
    © COPYRIGHT 2019COVEROS, INC. ALL RIGHTS RESERVED. 13@CoverosGene #CodeMash Just a simple DTO 1 … comments and annotations 1346 public class PersonResultsDTO 1347 { … 43734 } 43735
  • 14.
    © COPYRIGHT 2019COVEROS, INC. ALL RIGHTS RESERVED. 14@CoverosGene #CodeMash Test all the things, literally for (Method method : object.getClass().getMethods()) { if (method.getName.startsWith("set")) { if (method.getParameterTypes().length > 0) { Class paramClass = method.getParameterTypes()[0]; if (paramClass.getName.equals("java.lang.String")) { method.invoke(object, "JUNIT TEST"); } else if (paramClass.getName.equals("java.lang.Object")) { method.invoke(object, new Object()); } else if (paramClass.getName.equals("java.util.Date")) { method.invoke(object, new java.util.Date()); } else if (paramClass.getName.equals("java.math.BigDecimal")) { method.invoke(object, new BigDecimal(100));
  • 15.
    © COPYRIGHT 2019COVEROS, INC. ALL RIGHTS RESERVED. 15@CoverosGene #CodeMash Well, I can’t just leave it empty try { … } catch (Exception e) { e.getMessage(); }
  • 16.
    © COPYRIGHT 2019COVEROS, INC. ALL RIGHTS RESERVED. 16@CoverosGene #CodeMash Exception inception try { … } catch (Exception e) { try { throw e; } catch (Exception e1) { e1.printStackTrace(); } }
  • 17.
    © COPYRIGHT 2019COVEROS, INC. ALL RIGHTS RESERVED. 17@CoverosGene #CodeMash Tests have exceptions, too public Member generateTestMember(){ Member member = new Member(); … return null; } @Test(expected = RuntimeException.class) public void testAddTaxCreditSuccess() throws Exception { Member person = generateTestMember(); String spouseId = person.findSpouseId(); assertNotNull(service.addTaxCredit(person, spouseId)); } 2 1 3
  • 18.
    © COPYRIGHT 2019COVEROS, INC. ALL RIGHTS RESERVED. 18@CoverosGene #CodeMash I’d like to buy a constant public static final String DASH = "-"; public static final String MANAGE_ACTION_0 = "0"; public static final String MANAGE_ACTION_1 = "1"; public static final String MANAGE_ACTION_2 = "2"; public static final String MANAGE_ACTION_3 = "3"; public static final String MANAGE_ACTION_4 = "4"; … public static final String MANAGE_ACTION_17 = "17";
  • 19.
    © COPYRIGHT 2019COVEROS, INC. ALL RIGHTS RESERVED. 19@CoverosGene #CodeMash Just give them descriptive names // BigInteger ONE public static final BigInteger BIGINTEGER_ONE = BigInteger.ONE; // BigInteger ZERO public static final BigInteger BIGINTEGER_ZERO = BigInteger.ZERO; // BIGDECIMAL ZERO public static final BigDecimal BIGDECIMAL_ZERO = BigDecimal.ZERO; /** The Constant Y_INDICATOR. */ protected static final String Y_INDICATOR = "Y"; /** The Constant N_INDICATOR. */ protected static final String N_INDICATOR = "N";
  • 20.
    © COPYRIGHT 2019COVEROS, INC. ALL RIGHTS RESERVED. 20@CoverosGene #CodeMash What if the values change? public static final boolean BOOL_TRUE = true; public static final boolean BOOL_FALSE = false;