- Forward


Checklists
A Programming Pattern


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Motivation
Back SMYC Forward
  • The Idea:
    • In many aspects of life, both personal and professional, it is necessary to determine if criteria have been satisfied
    • One common way to do this is to use a checklist
  • An Example:
    • A personal packing list for a trip
Review
Back SMYC Forward
  • Representing the Criteria:
    • A String[] (named checklist) is very flexible
  • Representing the Accomplishments:
    • A conformal boolean[] assumes that everything accomplished is on the checklist
    • A String[] (named completed) is more flexible
Thinking About the Problem
Back SMYC Forward

Determining if One Item has been Completed

javaexamples/programmingpatterns/checklist/Fragments.java (Fragment: 0)
 
Thinking About the Problem (cont.)
Back SMYC Forward
  • What We Have:
    • done will contain true if and only if checklist[index] has been completed
  • What We Need:
    • To nest this loop inside of another loop
  • How?
    • There are many ways to do this incorrectly
Thinking About the Problem (cont.)
Back SMYC Forward

One Incorrect Solution

for all elements in completed { for all elements in checklist { if the completed element does not equal the checklist item return false } }
Thinking About the Problem (cont.)
Back SMYC Forward

Another Incorrect Solution

for all elements in completed { assign false to the accumulator checked for all elements in checklist { if the completed element equals the checklist item { assign true to the accumulator checked break } } if checked is true then return true } return false
The Pattern
Back SMYC Forward

An Inflexible (but Simple) Solution

javaexamples/programmingpatterns/checklist/Inflexible.java (Fragment: 0)
 
The Pattern (cont.)
Back SMYC Forward

A Flexible Solution

javaexamples/programmingpatterns/checklist/Elegant.java (Fragment: outerChecklist)
 
Examples for You to Trace
Back SMYC Forward
  • checklist:
    • "Shirts", "Socks", "Pants", "Skirts"
  • completed for the Inflexible Variant:
    • "Shirts", "Socks", "Pants", "Dresses", "Shoes"
    • "Socks", "Shirts", "Skirts", "Pants"
  • completed for the Flexible Variant when needed is 2):
    • "Shirts", "Socks", "Pants", "Dresses", "Shoes"
    • "Dresses", "Shirts"
There's Always More to Learn
Back -