Source:
<code>public static DateTime getDateTime(final String dateString) throws Exception { DateTime retval; final String pattern = "([a-z]*)[ ]*([+-]?)[ ]*([0-9]*)[ ]*([a-z]*)"; final Pattern r = Pattern.compile(pattern); final Matcher m = r.matcher(dateString); if (m.groupCount() != 4) { throw new Exception("Incorrect dateString [" + dateString + "]"); } m.find(); final String when = m.group(1).toLowerCase(); final String op = m.group(2); final Integer numberToAddOrSubstract = Integer.parseInt(m.group(3)); final String what = m.group(4); switch (when) { case "tomorrow": retval = DateTime.now().plusDays(1); break; case "yesterday": retval = DateTime.now().minusDays(1); break; case "now": case "today": retval = DateTime.now(); break; default: throw new Exception("Incorrect dateString [" + dateString + "]"); } <START> switch (what) { <END> case "days": if (op.equals("+")) { retval = retval.plusDays(numberToAddOrSubstract); } else { retval = retval.minusDays(numberToAddOrSubstract); } case "hours": if (op.equals("+")) { retval = retval.plusHours(numberToAddOrSubstract); } else { retval = retval.minusHours(numberToAddOrSubstract); } case "weeks": if (op.equals("+")) { retval = retval.plusWeeks(numberToAddOrSubstract); } else { retval = retval.minusWeeks(numberToAddOrSubstract); } case "months": if (op.equals("+")) { retval = retval.plusMonths(numberToAddOrSubstract); } else { retval = retval.minusMonths(numberToAddOrSubstract); } case "years": if (op.equals("+")) { retval = retval.plusYears(numberToAddOrSubstract); } else { retval = retval.minusYears(numberToAddOrSubstract); } } return retval; }</code><technical_language>Reduce complexity. If op.equals("+") is checked first, methods (for minus) handling specific cases. case is simple</technical_language>

Target:
public static DateTime getDateTime(final String dateString) throws Exception { DateTime retval; final String pattern = "([a-z]*)[ ]*([+-]?)[ ]*([0-9]*)[ ]*([a-z]*)"; final Pattern r = Pattern.compile(pattern); final Matcher m = r.matcher(dateString); if (m.groupCount() != 4) { throw new Exception("Incorrect dateString [" + dateString + "]"); } m.find(); final String when = m.group(1).toLowerCase(); final String op = m.group(2); final Integer numberToAddOrSubstract = Integer.parseInt(m.group(3)); final String what = m.group(4); switch (when) { case "tomorrow": retval = DateTime.now().plusDays(1); break; case "yesterday": retval = DateTime.now().minusDays(1); break; case "now": case "today": retval = DateTime.now(); break; default: throw new Exception("Incorrect dateString [" + dateString + "]"); } if (op.equals("+")) { switch (what) { case "days": retval = retval.plusDays(numberToAddOrSubstract); case "hours": retval = retval.plusHours(numberToAddOrSubstract); case "weeks": retval = retval.plusWeeks(numberToAddOrSubstract); case "months": retval = retval.plusMonths(numberToAddOrSubstract); case "years": retval = retval.plusYears(numberToAddOrSubstract); } } else { switch (what) { case "days": retval = retval.minusDays(numberToAddOrSubstract); case "hours": retval = retval.minusHours(numberToAddOrSubstract); case "weeks": retval = retval.minusWeeks(numberToAddOrSubstract); case "months": retval = retval.minusMonths(numberToAddOrSubstract); case "years": retval = retval.minusYears(numberToAddOrSubstract); } } return retval; }