- Forward


Missing Values
A Programming Pattern


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Motivation
Back SMYC Forward
  • The Idea:
    • When working with numerical data one often needs to deal with missing values
  • An Example:
    • People sometimes forget to enter weekly expenditures into their household budget program
Review
Back SMYC Forward
  • Given What You Know:
    • You would probably use double values and a sentinel like -1 for missing values
  • Shortcomings:
    • In many situations there is no value that can be used as a sentinel
    • If you forget to check for the sentinel, the calculations will be wrong but the defect will be difficult to localize
Thinking About the Problem
Back SMYC Forward
  • Ideally:
    • Every data type would have a sentinel
  • Unfortunately:
    • The don't
  • Fortunately:
    • null can be used as a sentinel for all reference types
The Pattern
Back SMYC Forward
  • When "Collecting" the Data:
    • Declare a wrapper object (like Double or Integer)
    • If the information isn't missing, use the static valueOf() method to construct the wrapper object, otherwise leave it null
  • When "Processing" the Data:
    • Determine if the wrapper object is null
    • If it is, treat it as missing. If it isn't use the doubleValue() or intValue() method to retrieve the value
Examples
Back SMYC Forward

Use a Default Value

javaexamples/programmingpatterns/MissingValues.java (Fragment: defaultmissing)
 
Examples (cont.)
Back SMYC Forward

Ignore Missing Values

javaexamples/programmingpatterns/MissingValues.java (Fragment: ignoremissing)
 
Examples (cont.)
Back SMYC Forward

Propagate Missing Values

javaexamples/programmingpatterns/MissingValues.java (Fragment: propagatemissing)
 
There's Always More to Learn
Back -