program style identifier names comments named constants avoiding code repetition giving least...
Post on 20-Dec-2015
219 views
TRANSCRIPT
Program Style
• Identifier Names
• Comments
• Named Constants
• Avoiding Code Repetition
• Giving Least Privilege
• Efficiency
• Interfaces
Writing Style
• To the point.
• Avoid repetition.
• Good flow.
• Illustrate ideas.
Assumes more than one way to write a document.
ABMISpreadsheet
ObjectEditor
ABMISpreadsheet Instance
weight height
setWeight()
new Weight
calls
writes
setHeight()
new Height
calls
writes
height
getHeight()
calls
reads
getWeight()
reads
weight calls
reads
getBMI()
AnotherBMISpreadsheet
ObjectEditor
ABMISpreadsheet Instance
weight height
setWeight()
new Weight
calls
writes
setHeight()
new Height
calls
writes
height
getHeight()
calls
reads
getWeight()
reads
weight calls
getBMI()
bmi
reads
AnotherBMISpreadsheet
ObjectEditor
ABMISpreadsheet Instance
weight height
setWeight()
new Weight
calls
writes
setHeight()
new Height
calls
writes
height
getHeight()
calls
reads
getWeight()
reads
weight calls
getBMI()
bmi
reads
Methods that Changes
ObjectEditor
ABMISpreadsheet Instance
weight height
setWeight()
new Weight
calls
writes
setHeight()
new Height
calls
writes
getBMI()
bmi
reads
setWeight()
ObjectEditor
ABMISpreadsheet Instance
weight
setWeight()
new Weight
calls
writes
bmi
public void setWeight(double newWeight) {weight = newWeight;bmi = weight / (height*height);
}
setHeight()
ObjectEditor
ABMISpreadsheet Instance
height
setHeight()
new Height
calls
writes
bmi
public void setHeight(double newHeight) {height = newHeight;bmi = weight / (height*height);
}
getBMI()
ObjectEditor
ABMISpreadsheet Instance
getBMI()
bmi
reads
public double getBMI() {return bmi;
}
Complete Codepublic class AnotherBMISpreadsheet {
double height, weight, bmi;public double getHeight() {
return height;}public void setHeight(double newHeight) {
height = newHeight;bmi = weight/(height*height);
}public double getWeight() {
return weight;}public void setWeight(double newWeight) {
weight = newWeight;bmi = weight/(height*height);
}public double getBMI() {
return bmi;}
}
Graphical Algorithm
ObjectEditor
ABMISpreadsheet Instance
weight height
setWeight()
new Weight
calls
writes
setHeight()
new Height
calls
writes
height
getHeight()
calls
reads
getWeight()
reads
weight calls
getBMI()
bmi
reads
English Algorithm
• Declare three instance variables, weight, height and, bmi.• Define three getter methods return values of the three instance variables.• Define two setter methods to change weight and height.•The setter methods assign new weight and height values and compute and assign new BMI value to bmi.
Algorithm
• Description of solution to a problem.• Can be in any “language”
• graphical• natural or programming language• natural + programming language (pseudo code)
• Can describe solution to various levels of detail.
Real-World Algorithm
• Enter Class • Distribute handouts • Set up laptop projection. • Revise topics learnt in the last class. • Teach today’s topics.• Leave Class
Stepwise Refinement
• Declare three instance variables, weight, height and, bmi.• The three getter methods return values of the three instance variables.• The setter methods assign new weight and height values and compute and assign new BMI value to bmi.
Natual Language
public void setWeight(double newWeight) {weight = newWeight;bmi = weight/(height*height);
}
Programming Language
Graphical Algorithmweight
setWeight()
writes
getWeight()
reads
BMI
Object Editor User-Interface?public class AnotherBMISpreadsheet {
double height, weight, bmi;public double getHeight() {
return height;}public void setHeight(double newHeight) {
height = newHeight;bmi = weight/(height*height);
}public double getWeight() {
return weight;}public void setWeight(double newWeight) {
weight = newWeight;bmi = weight/(height*height);
}public double getBMI() {
return bmi;}
}
Similarities in the two Classespublic class ABMISpreadsheet {
double height;public double getHeight() {
return height;}public void setHeight(double newHeight) {
height = newHeight;}double weight;public double getWeight() {
return weight;}public void setWeight(double newWeight) {
weight = newWeight;}public double getBMI() {
return weight/(height*height);}
}
Similarities in the two Classespublic class AnotherBMISpreadsheet {
double height, weight, bmi;public double getHeight() {
return height;}public void setHeight(double newHeight) {
height = newHeight;bmi = weight/(height*height);
}public double getWeight() {
return weight;}public void setWeight(double newWeight) {
weight = newWeight;bmi = weight/(height*height);
}public double getBMI() {
return bmi;}
}
Implementing an Interfacepublic class AnotherBMISpreadsheet implements BMISpreadhsheet {
double height, weight, bmi;public double getHeight() {
return height;}public void setHeight (double newHeight) {
height = newHeight;bmi = calculateBMI();
}public double getWeight() {
return weight;}public void setWeight(double newWeight) {
weight = newWeight;bmi = calculateBMI();
}public double getBMI() {
return bmi;}
}
parameter names never matter to Java
contract
Interface
AnotherBMISpreadsheet
Instance
AnotherBMISpreadsheet instance of
AnotherBMISpreadsheet
Instance
ABMISpreadsheet instance of
ABMISpreadsheet
Instance
ABMISpreadsheet
InstanceBMISpreadsheet
implements
Using Interface to Classify
BMISpreadsheet
Instance
AnotherBMISpreadsheet instance of
BMISpreadsheet
Instance
ABMISpreadsheet instance of
BMISpreadsheet
Instance
BMISpreadsheet
InstanceBMISpreadsheet
implements
Using Car Specification to Classify
manufactures
AccordSpecification
implements
AccordAccord
AccordAccord
Cannot Instantiate Specification
Cannot order car from a specification
•Must order from factory.
•A car defined by Accord specification ordered from factory implementing the specification.
Cannot instantiate interface
•Must instantiate class.
•BMISpreadsheet instance created by instantiating class implementing interface.
public class ABMISpreadsheet implements BMISpreadsheet{double height, weight;public double getHeight() {
return height;}public void setHeight(double newHeight) {
height = newHeight; }public double getWeight() {
return weight;}public void setWeight(double newWeight) {
weight = newWeight; }public double getBMI() {
return height/(weight*weight);}
}
Interface as Syntactic Specification
public class ABMISpreadsheet implements BMISpreadsheet{double height, weight;public double getHeight() {
return height;}public void setHeight(double newHeight) {
height = newHeight; }public double getWeight() {
return weight;}public void setWeight(double newWeight) {
weight = newWeight; }public double getBMI() {
return 3245.4}
}
Interface as Syntactic Specification
Syntactic Contract
Bombay Market Index?
Define interface for:– All classes (that are instantiated.)– Some are not.– Include all public instance methods
Interface Required
Differences in the Two Classespublic class ABMISpreadsheet implements BMISpreadsheet {
double height;public double getHeight() {
return height;}public void setHeight(double newHeight) {
height = newHeight;}double weight;public double getWeight() {
return weight;}public void setWeight(double newWeight) {
weight = newWeight;}public double getBMI() {
return weight/(height*height);}
}
Differences in the Two Classespublic class AnotherBMISpreadsheet implements BMISpreadsheet {
double height, weight, bmi;public double getHeight() {
return height;}public void setHeight(double newHeight) {
height = newHeight;bmi = weight/(height*height);
}public double getWeight() {
return weight;}public void setWeight(double newWeight) {
weight = newWeight;bmi = weight/(height*height);
}public double getBMI() {
return bmi;}
}
ABMISpreadsheet Vs AnotherBMISpreadSheet
• ABMISpreadsheet uses less space (variables)• Getter methods of AnotherBMISpreadhseet are faster.• Setter methods of ABMISpreadsheet are faster.• Usually getter methods are called more often that setter methods - e.g when ObjectEditor refresh command is executed.•Typically AnotherBMISpreadsheet will be faster, overall.
Relating Interface and Class Names
Class Name:•<Qualifier><Interface> (ABMISpreadsheet, ASpaceEfficientBMISpreadsheet, SpaceEfficientBMISpreadsheet)•<Interface><Qualifier>Impl (BMISpreadsheetImpl, BMISpreadsheetSpaceEfficientImpl)
Interface Name:•<ClassName>Interface (ABMISpreadsheetInterface)
Comments
double bmi; //computed by setWeight and setHeight
Single-line comment
/* recompute dependent properties */bmi = weight / (height*height);
Arbitrary comment
/* This version recalculates the bmi when weight or height change, not when getBMI is called*/public class AnotherBMISpreadsheet {…}
Removing Debugging Code
/*System.out.println(newHeight); // debugging
statement*/
System.out.println(newHeight); /*debugging statement */
/*System.out.println(newHeight); /*debugging
statement */*/
Javadoc Conventions
/* This version recalculates the bmi * when weight or height change, not when * getBMI is called */public class AnotherBMISpreadsheet {…}
/* This version recalculates the bmi when weight or height change, not when getBMI is called*/public class AnotherBMISpreadsheet {…}
What to Comment?Any code fragment needing explanation:
•class
•top-level algorithm, author, date modified
•variable declaration
•purpose, where used
•method declaration
•params, return value, algorithm, author, date modified
•statement sequence
•explanation
•Debugging code
What to Comment?
double w; // weight
double weight; // weight
double bmi; // computed by setWeight() and setHeight()
double weight; Self Commenting
Redundant
Bad Variable Name
Useful Comment
/* * @author Prasun Dewan * @param newWeight the new value of the property, weight. * sets new values of the variables, weight and bmi */public void setWeight (double newWeight) {
…}
/* * @author Prasun Dewan * @return the value of the variable, weight */public double getWeight () {
…}
Javadoc TagsJavadoc tags
Improving the Stylepublic class AnotherBMISpreadsheet implements BMISpreadsheet {
double height, weight, bmi;public double getHeight() {
return height;}public void setHeight(double newHeight) {
height = newHeight;bmi = weight/(height*height);
}public double getWeight() {
return weight;}public void setWeight(double newWeight) {
weight = newWeight;bmi = weight/(height*height);
}public double getBMI() {
return bmi;}
}
Code Repetition
Example Changes: LB, Inches
public class AnotherBMISpreadsheet implements BMISpreadsheet {double height, weight, bmi;public double getHeight() {
return height;}public void setHeight(double newHeight) {
height = newHeight;bmi = weight/(height*height);
}public double getWeight() {
return weight;}public void setWeight(double newWeight) {
weight = newWeight;bmi = weight/(height*height);
}}
bmi = (weight/2.2)/(height * 2.54/100*height*2.54/100);
bmi = (weight/2.2)/(height * 2.54/100*height*2.54/100);
How to avoid code duplication?
public class AnotherBMISpreadsheet implements BMISpreadsheet {double height, weight, bmi;public double getHeight() {
return height;}public void setHeight(double newHeight) {
height = newHeight;bmi = (weight/2.2)/(height * 2.54/100*height*2.54/100);
}public double getWeight() {
return weight;}public void setWeight(double newWeight) {
weight = newWeight;bmi = (weight/2.2)/(height * 2.54/100*height*2.54/100);
}}
How to avoid code duplication? (edit)public class AnotherBMISpreadsheet implements BMISpreadsheet {
double height, weight, bmi;public double getHeight() {
return height;}public void setHeight(double newHeight) {
height = newHeight;bmi = computeBMI();
}public double getWeight() {
return weight;}public void setWeight(double newWeight) {
weight = newWeight;bmi = computeBMI();
}public double computeBMI() {
return (weight/2.2)/(height * 2.54/100*height*2.54/100); }
}
How to avoid code duplication?
public class AnotherBMISpreadsheet implements BMISpreadsheet {double height, weight, bmi;public double getHeight() {
return height;}public void setHeight(double newHeight) {
height = newHeight;bmi = calculateBMI();
}public double getWeight() {
return weight;}public void setWeight(double newWeight) {
weight = newWeight;bmi = calculateBMI();
}void calculateBMI() {
return (weight/2.2)/(height * 2.54/100*height*2.54/100); }
Principle of Least Privilege
• Do not give a user of some code more rights than it needs.
•Code is easier to change.
•Need to learn less to use code.
•Less likelihood of accidental or malicious damage to program.
• Like hiding engine details from car driver.
ABMICalculator
ObjectEditor
setWeight()
setHeight()getHeight()
getWeight()
getBMI() calculateBMI()computeBMI()
ABMICalculator User
Only Public Methods in Interfacepublic class AnotherBMISpreadsheet implements BMISpreadhsheet {
double height, weight, bmi;public double getHeight() {
return height;}public void setHeight (double newHeight) {
height = newHeight;bmi = calculateBMI();
}public double getWeight() {
return weight;}public void setWeight(double newWeight) {
weight = newWeight;bmi = calculateBMI();
}public double getBMI() {
return bmi;}double calculateBMI() {
return weight/ (height*height);}
}
not in interface
Method Invocation Syntax
System.out.println(“setWeight called”);
Actual Parameter
Method Name
Target Object
bmi = calculateBMI()
parameterless method
Internal Call
External Call
Function Return Value
public class AnotherBMISpreadsheet implements BMISpreadsheet{double height, weight, bmi;public double getHeight() {
return height;}public void setHeight(double newHeight) {
height = newHeight;bmi = calculateBMI();
}public double getWeight() {
return weight;}public void setWeight(double newWeight) {
weight = newWeight;bmi = calculateBMI();
}public double getBMI() {
return bmi;}double calculateBMI() {
return (weight/2.2)/(height * 2.54/100*height*2.54/100); }
}
Improving the Style
Magic numbers?
public class AnotherBMISpreadsheet implements BMISpreadsheet{double height, weight, bmi;public double getHeight() {
return height;}public void setHeight(double newHeight) {
height = newHeight;bmi = calculateBMI();
}public double getWeight() {
return weight;}public void setWeight(double newWeight) {
weight = newWeight;bmi = calculateBMI();
}public double getBMI() {
return bmi;}double calculateBMI() {
return (weight/LBS_IN_KG) / (height*CMS_IN_INCH/100*height*CMS_IN_INCH/100);
}}
Improving the Style
Named Constants
public class AnotherBMISpreadsheet implements BMISpreadsheet{double height, weight, bmi;...final double LBS_IN_KG = 2.2;final double CMS_IN_INCH = 2.54;double calculateBMI() {
return (weight/LBS_IN_KG) / (height*CMS_IN_INCH/100*height*CMS_IN_INCH/100);
}
}
Declaring Named Constants
Initializing Declaration
Un-initializing Declaration
All Caps by Conventions
Cannot Change Initial Value
Variables Vs Named Constants
public class AnotherBMISpreadsheet implements BMISpreadsheet{double height, weight, bmi;...double lbsInKg = 2.2;double cmsInInch = 2.54;double calculateBMI() { return (weight/lbsInKg) / (height*cmsInInch/100*height*cmsInInch/100);}
}
public class AnotherBMISpreadsheet implements BMISpreadsheet{double height, weight, bmi;...double lbsInKg = 2.2;double cmsInInch = 2.54;double calculateBMI() { lbsInKg = 22; return (weight/lbsInKg) / (height*cmsInInch/100*height*cmsInInch/100);}
}
Accidental or Malicious Modification
Violating Least Privilege
Constant
return (weight/lbsInKg) / (height*cmsInInch/100*height*cmsInInch/100);
return (weight/2.2) / (height*2.54/100*height*2.54/100);
Literals, Named Constants, Constants, Variables
Variable
Literal
return (weight/LBS_IN_KG) / (height*CMS_IN_INCH/100*height*CMS_IN_INCH/100)
Named Constant
Literals Vs Named Constants Vs Variables
Use constants for program values that do not change.– Use named constants for magic numbers
return (weight/2.2) / (height*2.54/100*height*2.54/100);
What is a Magic Number?
return (weight/LBS_IN_KG) / (height*CMS_IN_INCH/100*height*CMS_IN_INCH/100)
Natural Constants
return hoursWorked*hourlyWage + 50;
return hoursWorked*hourlyWage + BONUS;
Human-Created Constant
System.out.println (“Bonus: “ + 50);
• Human-created constant is a magic number.
• Natural constant may be a magic number to some.
What is a magic number?
public class AnotherBMISpreadsheet implements BMISpreadsheet{double height, weight, bmi;...final double LBS_IN_KG = 2.2;final double CMS_IN_INCH = 2.54;double calculateBMI() { return (weight/LBS_IN_KG) /
(height*CMS_IN_INCH/100*height*CMS_IN_INCH/100) ;}
}
More Code Repetition
Within Same Method and Has Same Value
public class AnotherBMISpreadsheet implements BMISpreadsheet{double height, weight, bmi;...final double LBS_IN_KG = 2.2;final double CMS_IN_INCH = 2.54;double calculateBMI() { return (weight/LBS_IN_KG) /
(height*CMS_IN_INCH/100*height*CMS_IN_INCH/100) ;}
}
Removing Code Repetition (edit)
public class AnotherBMISpreadsheet implements BMISpreadsheet{double height, weight, bmi;...final double LBS_IN_KG = 2.2;final double CMS_IN_INCH = 2.54;double calculateBMI() { double heightInMetres = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres) ;}
}
Removing Code Repetition
public class AnotherBMISpreadsheet implements BMISpreadsheet{double height, weight, bmi;double heightInMetres = height*CMS_IN_INCH/100; ...final double LBS_IN_KG = 2.2;final double CMS_IN_INCH = 2.54;double calculateBMI() { return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres) ;}
}
Local Vs Global Variable
public class AnotherBMISpreadsheet implements BMISpreadsheet{double height, weight, bmi;double heightInMetres = height*CMS_IN_INCH/100;public void setHeight(double newHeight) {
height = heightInMetres;bmi = calculateBMI();
}
...final double LBS_IN_KG = 2.2;final double CMS_IN_INCH = 2.54;double calculateBMI() { return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres) ;}
}
Local Vs Global Variable
Violating least privilege
public class AnotherBMISpreadsheet implements BMISpreadsheet{double height, weight, bmi;public double getHeight() {
return height;}public void setHeight(double newHeight) {
height = newHeight;bmi = calculateBMI();
}public double getWeight() {
return weight;}public void setWeight(double newWeight) {
weight = newWeight;bmi = calculateBMI();
}public double getBMI() {
return bmi;}double calculateBMI() {
double heightInMetres = height*CMS_IN_INCH/100;return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres);
}}
Scope
heightInMetres Scope
height Scope
Not a Scope
public class AnotherBMISpreadsheet implements BMISpreadsheet{double height, weight, bmi;public double getHeight() {
return height;}...
}
Scope of Public Items getHeight() Scope
ObjectEditor
ABMISpreadsheet
Identifier Scope
• Region of code where the identifier is visible.
• Arbitrary scopes not possible
• Least Privilege => Make scope as small as possible
public class AnotherBMISpreadsheet implements BMISpreadsheet{double height, weight, bmi;public void setHeight(double newHeight) {
double height = heightInMetres;bmi = calculateBMI();
}
...final double LBS_IN_KG = 2.2;final double CMS_IN_INCH = 2.54;double calculateBMI() { double heightInMetres = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres) ;}
}
Scope
heightInMetres Scope
public class AnotherBMISpreadsheet implements BMISpreadsheet{double height, weight, bmi;...final double LBS_IN_KG = 2.2;final double CMS_IN_INCH = 2.54;double calculateBMI() { double heightInMetres = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres) ;}
}
Initializing Declaration
public class AnotherBMISpreadsheet implements BMISpreadsheet{double height, weight, bmi;...final double LBS_IN_KG = 2.2;final double CMS_IN_INCH = 2.54;double calculateBMI() { double heightInMetres; heightInMetres = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres) ;}
}
Un-initializing Declaration
public class AnotherBMISpreadsheet implements BMISpreadsheet{double height, weight, bmi;...final double LBS_IN_KG = 2.2;final double CMS_IN_INCH = 2.54;double calculateBMI() { double heightInMetres; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres) ;}
}
Un-initialized Variable
public class AnotherBMISpreadsheet implements BMISpreadsheet{double height, weight, bmi;...final double LBS_IN_KG = 2.2;final double CMS_IN_INCH = 2.54;double calculateBMI() { double heightInMetres = height*CMS_IN_INCH/100;; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres) ;}
}
Initializing all Variables
public class AnotherBMISpreadsheet implements BMISpreadsheet{double height, weight, bmi;...final double LBS_IN_KG = 2.2;final double CMS_IN_INCH = 2.54;double calculateBMI() { double heightInMetres = height*CMS_IN_INCH/100;; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres) ;}
}
Initializing all Variables (edit)
public class AnotherBMISpreadsheet implements BMISpreadsheet{double height = 70, weight = 160, bmi = calculateBMI();...final double LBS_IN_KG = 2.2;final double CMS_IN_INCH = 2.54;double calculateBMI() { double heightInMetres = height*CMS_IN_INCH/100;; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres) ;}
}
Initializing all Variables
Printing Propertiespublic class ABMISpreadsheet implements BMISpreadsheet {
double height;public double getHeight() {
return height;}public void setHeight(double newHeight) {
height = newHeight;}double weight;public double getWeight() {
return weight;}public void setWeight(double newWeight) {
weight = newWeight;}public double getBMI() {
return weight/(height*height);}
}
Printing Properties (edit)public class ABMISpreadsheet implements BMISpreadsheet {
double height;public double getHeight() {
return height;}public void setHeight(double newHeight) {
System.out.println(“bmi:” + getBMI());height = newHeight;
}double weight;public double getWeight() {
return weight;}public void setWeight(double newWeight) {
weight = newWeight;}public double getBMI() {
return weight/(height*height);}
}
Printing Propertiespublic class ABMISpreadsheet implements BMISpreadsheet {
double height;public double getHeight() {
return height;}public void setHeight(double newHeight) {
System.out.println(“Weight: “ + weight);System.out.println(“Height: “ + height);double bmi = getBMI();System.out.println(“BMI: “ + bmi);height = newHeight;
}double weight;public double getWeight() {
return weight;}public void setWeight(double newWeight) {
weight = newWeight;}public double getBMI() {
return weight/(height*height);}
}
Less Cluttered Code (edit)public class ABMISpreadsheet implements BMISpreadsheet {
double height;public double getHeight() {
return height;}public void setHeight(double newHeight) {
System.out.println(“Weight: “ + weight);System.out.println(“Height: “ + height);double bmi = getBMI();System.out.println(“BMI: “ + bmi);height = newHeight;
}double weight;public double getWeight() {
return weight;}public void setWeight(double newWeight) {
weight = newWeight;}public double getBMI() {
return weight/(height*height);}
}
Less Cluttered Codepublic class ABMISpreadsheet implements BMISpreadsheet {
double height;public double getHeight() {
return height;}public void setHeight(double newHeight) {
System.out.println(“Weight: “ + weight);System.out.println(“Height: “ + height); System.out.println(“BMI: “ + getBMI());height = newHeight;
}double weight;public double getWeight() {
return weight;}public void setWeight(double newWeight) {
weight = newWeight;}public double getBMI() {
return weight/(height*height);}
}
Removing Duplicationpublic class ABMISpreadsheet implements BMISpreadsheet {
double height;public double getHeight() {
return height;}public void setHeight(double newHeight) {
System.out.println(“Weight: “ + weight);System.out.println(“Height: “ + height); System.out.println(“BMI: “ + getBMI());height = newHeight;
}double weight;public double getWeight() {
return weight;}public void setWeight(double newWeight) {
System.out.println(“Weight: “ + weight);System.out.println(“Height: “ + height); System.out.println(“BMI: “ + getBMI());weight = newWeight;
}public double getBMI() {
return weight/(height*height);}
}
Removing Duplication (edit)public class ABMISpreadsheet implements BMISpreadsheet {
double height;public double getHeight() {
return height;}public void setHeight(double newHeight) {
System.out.println(“Weight: “ + weight);System.out.println(“Height: “ + height); System.out.println(“BMI: “ + getBMI());height = newHeight;
}double weight;public double getWeight() {
return weight;}public void setWeight(double newWeight) {
System.out.println(“Weight: “ + weight);System.out.println(“Height: “ + height); System.out.println(“BMI: “ + getBMI());weight = newWeight;
}public double getBMI() {
return weight/(height*height);}
}
Removing Duplicationpublic class ABMISpreadsheet implements BMISpreadsheet {
double height;public double getHeight() {
return height;}public void setHeight(double newHeight) {
printProperties();height = newHeight;
}double weight;public double getWeight() {
return weight;}public void setWeight(double newWeight) {
printProperties();weight = newWeight;
}public double getBMI() {
return weight/(height*height);}void printProperties() {
System.out.println(“Weight: “ + weight);System.out.println(“Height: “ + height); System.out.println(“BMI: “ + getBMI());
}}
Separation of Concernspublic class ABMISpreadsheet implements BMISpreadsheet {
double height;public double getHeight() {
return height;}public void setHeight(double newHeight) {
printProperties();height = newHeight;
}double weight;public double getWeight() {
return weight;}public void setWeight(double newWeight) {
weight = newWeight;}public double getBMI() {
return weight/(height*height);}void printProperties() {
System.out.println(“Weight: “ + weight);System.out.println(“Height: “ + height); System.out.println(“BMI: “ + getBMI());
}}
Less Cluttered
Independent Code = Method
Separate Method for:
• any independent piece of code.
• even if it is not duplicated.
• if it is more than one line.
public void setWeight(double newWeight) {System.out.println(“Weight: “ + weight);weight = newWeight;
}
public void setWeight(double newWeight) {printWeight();weight = newWeight;
}
Printing BMI in getBMI()public class ABMISpreadsheet implements BMISpreadsheet {
double height;public double getHeight() {
return height;}public void setHeight(double newHeight) {
printProperties();height = newHeight;
}double weight;public double getWeight() {
return weight;}public void setWeight(double newWeight) {
weight = newWeight;}public double getBMI() {
printProperties();return weight/(height*height);
}void printProperties() {
System.out.println(“Weight: “ + weight);System.out.println(“Height: “ + height); System.out.println(“BMI: “ + getBMI());
}}
getBMI() never terminates
Recursive (calls itself, indirectly)
Infinite Recursion
Avoid recursion for now
public class ABMISpreadsheet implements BMISpreadsheet {
public double height, weight, bmi;
...
}
Non Public Instance Variables
public class ABMISpreadsheetWithPublicVariables {
public double height, weight, bmi;
...
}
Making Instance Variables Public
Other Classes
public class ABMISpreadsheetWithPublicVariables {
public double height, weight;
...
}
Hard to Change
Other Classes
public final double CMS_IN_INCH = 2.54;
Public ConstantsInconsistent value cannot be stored
Implementation Independent
public interface BMISpreadsheet {
}
ABMISpreadsheet AnotherBMISpreadsheet