- Forward


Boolean Methods
A Programming Pattern


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Boolean Methods
Back SMYC Forward
  • Definition:
    • A method that returns true or false
  • Common Uses:
    • Indicate success or failure of a process
    • Conduct a test
Indicating Success/Failure
Back SMYC Forward
  • The Idea:
    • The method is called to change the state of the system (e.g., generate output, modify a collection)
    • The method might or might not succeed
  • Using a Boolean Method:
    • Instead of making the method void, have it return true on success and false on failure
Indicating Success/Failure (cont.)
Back SMYC Forward
public static boolean printSquareRoot(double x) { boolean ok; double squareRoot; if (x >= 0.) { squareRoot = Math.sqrt(x); JMUConsole.printf("The root of %f is %f.\n", x, squareRoot); ok = true; } else { ok = false; } return ok; }
Test Methods
Back SMYC Forward
  • The Idea:
    • Replace an expression that conducts a test with a method call that conducts the test
  • Advantages/Benefits:
    • Increase readability
    • Reduce code duplication
An Example of a Test Method
Back SMYC Forward
  • The Original Code:
    • if (((dieOne + dieTwo) == 7) || ((dieOne + dieTwo) == 11)) { // Make Payout }
  • Using a Boolean Method:
      if (wins(dieOne, dieTwo)) { // Make Payout }
An Example of a Test Method (cont.)
Back SMYC Forward
  • An Inelegant Implementation:
    • public static boolean wins(int dieOne, int dieTwo) { boolean result; int total; total = dieOne + dieTwo; if ((total == 7) || (total == 11)) result = true; else result = false; return result; }
  • An Elegant Implementation:
      public static boolean wins(int dieOne, int dieTwo) { int total; total = dieOne + dieTwo; return ((total == 7) || (total == 11)); }
The Benefits of Test Methods
Back SMYC Forward
  • Readability:
    • isOdd(x) is much easier to read and understand than ((x % 2) == 1)
  • Reduced Code Duplication:
    • It is difficult to modify duplicate code consistently (which we can see with a simple example)
The Benefit of Reducing Code Duplication
Back SMYC Forward
  • The Original Code:
    • if (height >= 62) { if (sex == 'M') { // Case 1 } else { // Case 2 } } else { if (sex == 'M') { // Case 3 } else { // Case 4 } }
  • The Necessary Modification:
    • Account for both 'M' and 'm'
The Benefit of Reducing Code Duplication (cont.)
Back SMYC Forward
  • The New Code:
    • if (height >= 62) { if ((sex == 'M') || (sex == 'm')) { // Case 1 } else { // Case 2 } } else { if ((sex == 'M') || (sex == 'm')) { // Case 3 } else { // Case 4 } }
  • The Important Observation:
    • Because of code duplication, the change had to be made in multiple places
The Benefit of Reducing Code Duplication (cont.)
Back SMYC Forward
  • Using a Test Method:
    • if (height >= 62) { if (isMale(sex)) // Case 1 else // Case 2 } else { if (isMale(sex)) // Case 3 else // Case 4 }
  • The Original and Modified Test Methods:
    • public static boolean isMale(char sex) { return (sex == 'M'); }
    • public static boolean isMale(char sex) { return ((sex == 'M') || (sex == 'm')); }
Related/Similar Test Methods
Back SMYC Forward
  • A Common Situation:
    • There are related test methods (e.g., isOdd() and isEven())
  • A Design that is Difficult to Modify:
    • Making the test methods independent (e.g., use (x%2) == 1 in isOdd() and (x%2) == 0 in isEven())
  • A Better Design:
    • Have one test method invoke another (e.g., have isEven() return !isOdd())
Related/Similar Test Methods (cont.)
Back SMYC Forward
  • An Implementation that is Error-Prone:
    • public static boolean isLowIncome(double income) { return (income < 20000.0); } public static boolean isMiddleIncome(double income) { return ((income >= 20000.0) && (income < 75000.0)); } public static boolean isHighIncome(double income) { return (income >= 75000.0); }
  • A Better Implementation:
    • public static boolean isLowIncome(double income) { return (income < 20000.0); } public static boolean isMiddleIncome(double income) { return ((!isLowIncome()) && (income < 75000.0)); } public static boolean isHighIncome(double income) { return (!isLowIncome(income) && !isMiddleIncome(income)); }
Boolean Methods are Not Always Appropriate
Back SMYC Forward
  • Recall:
    • In Java a Boolean method returns a boolean (i.e., true or false)
  • An Observation:
    • Sometimes it is more convenient to return 0/1 (because numeric values can be used in other calculations)
Boolean Methods are Not Always Appropriate (cont.)
Back SMYC Forward
One Implementation
public static boolean isPassing(int grade) { return grade>=60; } public static int creditsEarned(int grade, int creditsPossible) { if (isPassing(grade)) return creditsPossible; else return 0; }
An Alternative Implementation
public static int isPassing(int grade) { return grade/60; } public static int creditsEarned(double grade, int creditsPossible) { return isPassing(grade)*creditsPossible; }
There's Always More to Learn
Back -