comp 110 style
DESCRIPTION
Comp 110 Style. Instructor: Jason Carter. Interfaces and More Style. Define contracts between our users and implementers Optional – they may not be used Good style to use them Will study additional elements of style in this chapter. Two Ways of Doing the BMI Spreadsheet. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/1.jpg)
COMP 110STYLE
Instructor: Jason Carter
![Page 2: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/2.jpg)
2
INTERFACES AND MORE STYLE Define contracts between our users and
implementers Optional – they may not be used Good style to use them Will study additional elements of style in this
chapter
![Page 3: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/3.jpg)
3
TWO WAYS OF DOING THE BMI SPREADSHEET
ABMISpreadsheet is one way to implement the spreadsheet user-interface
Let us create AnotherBMISpreadsheet to illustrate another way
Difference is in number of variables used
![Page 4: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/4.jpg)
4
BMI SPREADSHEET
![Page 5: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/5.jpg)
5
ABMISPREADSHEETABMISpreadsheet Instance
weight height
getWeight()
setWeight()
getHeight()
setHeight() getBMI()
ObjectEditor
calls
writes
weight
calls
reads
height
calls
reads writes
callsnew
weight
new height
reads
![Page 6: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/6.jpg)
6
ANOTHERBMISPREADSHEETAnotherBMISpreadsheet Instance
weight height
getWeight()
setWeight()
getHeight()
setHeight() getBMI()
ObjectEditor
calls
writes
weight
calls
reads
height
calls
readswrites
callsnew
weight
new height
reads
bmi
![Page 7: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/7.jpg)
7
METHODS THAT CHANGEABMISpreadsheet Instance
weight height
setWeight()
setHeight() getBMI()
ObjectEditor
writes
calls
writes
callsnew
weight
new height
reads
bmi
![Page 8: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/8.jpg)
8
SETWEIGHT()ABMISpreadsheet Instance
weight
setWeight()
ObjectEditor
writes
calls new
weight
bmi
public void setWeight(double newWeight) {
weight = newWeight;bmi = weight / (height*height);
}
![Page 9: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/9.jpg)
9
SETHEIGHT()ABMISpreadsheet Instance
height
setHeight()
ObjectEditor
writes
calls new
height
bmi
public void setHeight(double newHeight) {
height = newHeight;bmi = weight / (height*height);
}
![Page 10: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/10.jpg)
10
GETBMI()ABMISpreadsheet Instance
getBMI()
ObjectEditor
reads
bmi
public double getBMI() {return bmi;
}
![Page 11: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/11.jpg)
11
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; }}
![Page 12: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/12.jpg)
12
GRAPHICAL ALGORITHMABMISpreadsheet Instance
weight height
getWeight()
setWeight()
getHeight()
setHeight() getBMI()
ObjectEditor
calls
writes
weight
calls
reads
height
calls
readswrites
callsnew
weight
new height
reads
bmi
![Page 13: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/13.jpg)
13
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
![Page 14: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/14.jpg)
14
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
![Page 15: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/15.jpg)
15
STEPWISE REFINEMENT
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
public void setWeight(double newWeight) {
weight = newWeight;bmi = weight/(height*height);
}
Natural Language
Programming Language
![Page 16: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/16.jpg)
16
OBJECTEDITOR 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; }}
![Page 17: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/17.jpg)
17
OBJECTEDITOR USER INTERFACES
![Page 18: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/18.jpg)
18
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; }}
public 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); }}
![Page 19: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/19.jpg)
19
REAL-WORLD ANALOGY
manufactures
Corvette Specification
implements
implementsmanufactures
![Page 20: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/20.jpg)
20
INTERFACE
![Page 21: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/21.jpg)
21
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); } …
IMPLEMENTING AN INTERFACE Contract
Parameter names never
matter to Java
![Page 22: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/22.jpg)
22
implements
implements
INTERFACE
ABMISpreadsheet
BMISpreadsheetABMISpreadsheet
instance
ABMISpreadsheetinstance
instance of
AnotherBMISpreadsheet
AnotherBMISpreadsheetinstance
AnotherBMISpreadsheetinstance
instance of
![Page 23: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/23.jpg)
23
implements
implements
USING INTERFACES TO CLASSIFY
ABMISpreadsheet
BMISpreadsheetBMISpreadsheet
instance
BMISpreadsheetinstance
instance of
AnotherBMISpreadsheet
BMISpreadsheetinstance
BMISpreadsheetinstance
instance of
![Page 24: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/24.jpg)
24
USING CAR SPECIFICATIONS TO CLASSIFY
manufactures
Corvette Specification
implements
implementsmanufactures
Corvette
Corvette
Corvette
Corvette
![Page 25: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/25.jpg)
25
CANNOT INSTANTIATE SPECIFICATION Cannot order a car from a specification
Must order from factory A car defined by Corvette specification ordered from
factory implementing the specification
Cannot instantiate interface Must instantiate class BMISpreadsheet instance created by instantiating
class implementing interface
![Page 26: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/26.jpg)
26
INTERFACE AS A SYNTACTIC SPECIFICATIONpublic 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); }}
![Page 27: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/27.jpg)
27
INTERFACE AS A SYNTACTIC SPECIFICATIONpublic 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 13450; }}
Syntactic Contract
Bombay Market Index
![Page 28: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/28.jpg)
28
INTERFACE REQUIRED Define interfaces for
All classes (that are instantiated) Some are not Include all public methods
![Page 29: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/29.jpg)
29
DIFFERENCES 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; }}
public 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); }}
![Page 30: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/30.jpg)
30
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
![Page 31: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/31.jpg)
31
TIME-SPACE TRADEOFF
Time MiserSpace Miser
Space
Time
![Page 32: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/32.jpg)
32
TIME-SPACE TRADEOFF
Space
Time
Time MiserSpace Miser
![Page 33: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/33.jpg)
33
RELATING INTERFACE AND CLASS NAMESClass Name:
<Qualifier><Interface> - ABMISpreadsheet- ASpaceEfficientBMISpreadsheet- SpaceEfficientBMISpreadsheet
<Interface><Qualifier> Impl- BMISpreadsheetImpl- BMISpreadsheetSpaceEfficientImpl
Interface Name:
<ClassName>Interface- ABMISpreadsheetInterface
![Page 34: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/34.jpg)
34
PROGRAMMING STYLE: THE ART OF PROGRAMMING
Science of programming Does the program work correctly?
Art programming Does the program follow “good” style rules?
Good style rules make it easier to Get the program working correctly Change the program
![Page 35: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/35.jpg)
35
WRITING STYLE To the point Avoid repetition Good flow Illustrate ideasThere is more than one way to write a document
that conveys some facts: e.g. the influence of BMI on health
![Page 36: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/36.jpg)
36
PROGRAMMING STYLE: THE ART OF PROGRAMMING
Define interfaces Make programs
efficient Space vs. time
Comment program Use appropriate
identifier names Use named constants
Variables vs. names Constants vs. magic
numbers
Avoid code repetition Give least privilege
No public instance variables
Implementation independent constants in interfaces
Initialize variables Independent code in
separate method
There is more than one way to write a program that correctly meets its specification: e.g.
implement the BMI user-interface
![Page 37: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/37.jpg)
37
PROGRAMMING STYLE: THE ART OF PROGRAMMING
Use Interfaces
Efficiency
Comment Program
Avoid Code Repetition
Giving Least Privilege
Use Named Constants
![Page 38: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/38.jpg)
38
COMMENTS
double bmi; //computed by setWeight and setHeight
Single line comments
/* This version recalculates the bmi when weight or height change, not when getBMI is called*/public class AnotherBMISpreadsheet {…}
/* recompute dependent properties */bmi = weight / (height * height);
Arbitrary comments
![Page 39: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/39.jpg)
39
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 {…}
You should use this convention
![Page 40: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/40.jpg)
40
System.out.println(newHeight); /*debugging statement */
REMOVING DEBUGGING CODE
/* System.out.println(newHeight); /*debugging
statement */ */
/* System.out.println(newHeight); // debugging
statement */
![Page 41: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/41.jpg)
41
WHAT TO COMMENT? Any code fragment needing explanation
ClassTop-level algorithm, author, date modified
Variable declarationPurpose, where used, how its value is computed
Method declarationparams, return value, algorithm, author, date modified
Statement sequenceExplanation
Debugging codeSummarizing vs. Elaborating Comments?
![Page 42: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/42.jpg)
42
WHAT TO COMMENT?
double w; // weight
double weight; // weight
double weight;
double bmi; // computed by setWeight and setHeight
Bad variable name
Redundant
Self-commenting
Useful comment
![Page 43: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/43.jpg)
43
JAVADOC TAGS
/* * @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 tags
![Page 44: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/44.jpg)
44
COMMENTING INTERFACES
/* * @param newWeight the new value of the property, weight */public void setWeight (double newWeight) { }
/* * @return the value of the variable, weight */public double getWeight () { }
Implementation independent comments
Commenting both interface and implementation?
![Page 45: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/45.jpg)
45
PROGRAMMING STYLE: THE ART OF PROGRAMMING
Use Interfaces
Efficiency
Comment Program
Avoid Code Repetition
Giving Least Privilege
Use Named Constants
![Page 46: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/46.jpg)
46
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
Assuming ABMICalculator does not exist
![Page 47: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/47.jpg)
47
IMPROVING THE STYLE (EDIT)public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; … public void setHeight(double newHeight) { height = newHeight; bmi = calculateBMI(); } … public void setWeight(double newWeight) { weight = newWeight; bmi = calculateBMI(); } public double getBMI() { return bmi; }
double calculateBMI() { return weight/(height*height); }}
![Page 48: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/48.jpg)
48
RE-USING CODEpublic 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(); } double calculateBMI() { return weight/(height*height); } ….}
![Page 49: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/49.jpg)
49
CHANGING RE-USED CODE ONCEpublic class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; … public void setHeight(double newHeight) { height = newHeight; bmi = calculateBMI(); } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; bmi = calculateBMI(); }
double calculateBMI() { return (weight/2.2)/(height * 2.54/100*height*2.54/100); } …}
Declaring vs. calling methods?
![Page 50: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/50.jpg)
50
PROGRAMMING STYLE: THE ART OF PROGRAMMING
Use Interfaces
Efficiency
Comment Program
Avoid Code Repetition
Giving Least Privilege
Use Named Constants
![Page 51: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/51.jpg)
51
ONLY PUBLIC METHODS IN INTERFACE
public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; bmi = calculateBMI(); } … double calculateBMI() () { return (weight/2.2)/(height * 2.54/100*height*2.54/100); } …}
Not in interface
![Page 52: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/52.jpg)
52
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
ObjectEditor
ABMICalculator User ABMICalculator
getWeight() setWeight()
getHeight() setHeight()
getBMI() computeBMI()
![Page 53: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/53.jpg)
53
IMPROVING THE STYLE
Use Interfaces
Efficiency
Comment Program
Use Named Constants
Avoid Code Repetition
Giving Least Privilege
![Page 54: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/54.jpg)
54
IMPROVING THE STYLE
public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; bmi = calculateBMI(); } … double calculateBMI() () { return (weight/2.2)/(height * 2.54/100*height*2.54/100); } …}
Magic numbers
![Page 55: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/55.jpg)
55
IMPROVING THE STYLE
public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; bmi = calculateBMI(); } … double calculateBMI() () { return (weight/LBS_IN_KG) / (height*CMS_IN_INCH/100*height*CMS_IN_INCH/100); } …}
Named constants
![Page 56: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/56.jpg)
56
DECLARING 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); } …}
Un-initializing declarationInitializing declaration
![Page 57: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/57.jpg)
57
MORE ON 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); } …}
![Page 58: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/58.jpg)
58
ACCIDENTAL OR MALICIOUS MODIFICATION
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); } …}
Violating least privilege
![Page 59: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/59.jpg)
59
LITERALS VS. NAMED CONSTANTS VS. VARIABLES
Use constants for program values that do not change Use named constants for magic numbers
![Page 60: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/60.jpg)
60
MORE CODE REPETITION
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); } …}
Within same method and has the same value
![Page 61: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/61.jpg)
61
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 heightInMeters = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMeters*heightInMeters); } …}
![Page 62: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/62.jpg)
62
REMOVING CODE REPETITION
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 heightInMeters = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMeters*heightInMeters); } …}
![Page 63: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/63.jpg)
63
LOCAL VS. GLOBAL VARIABLE
public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; double heightInMeters = 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) / (heightInMeters*heightInMeters); } …}
![Page 64: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/64.jpg)
64
LOCAL VS. GLOBAL VARIABLEpublic class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; double heightInMeters = height*CMS_IN_INCH/100; ... final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; … public void setHeight(double newHeight) { heightInMeters = newHeight; bmi = calculateBMI(); } … double calculateBMI() { return (weight/LBS_IN_KG) / (heightInMeters*heightInMeters); } …}
Violating least privilege
![Page 65: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/65.jpg)
65
public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; … public void setHeight(double newHeight) { height = newHeight; bmi = calculateBMI(); } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; bmi = weight/(height*height); } … double calculateBMI () { double heightInMetres = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } …}
SCOPE
heightInMeters scope
Not a scope
height scope
![Page 66: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/66.jpg)
66
SCOPE OF PUBLIC ITEMS
public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; … public double getWeight() { return weight; } …}
ObjectEditor
ABMISpreadsheet
getWeight() scope
![Page 67: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/67.jpg)
67
IDENTIFIER SCOPE Region of code where the identifier is visible Arbitrary scopes not possible Least Privilege => Make scope as small as
possible
![Page 68: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/68.jpg)
68
SCOPEpublic class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; … public void setHeight(double newHeight) { height = newHeight; bmi = calculateBMI(); } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; bmi = weight/(height*height); } … double calculateBMI() () { double heightInMetres = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } …}
heightInMeters scope
![Page 69: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/69.jpg)
69
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 = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } …}
![Page 70: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/70.jpg)
70
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; heightInMeters = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } …}
![Page 71: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/71.jpg)
71
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; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } …}
![Page 72: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/72.jpg)
72
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; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } …}
![Page 73: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/73.jpg)
73
INITIALIZING ALL VARIABLES (EDIT)
public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height = 60, weight = 70, bmi = getBMI(); … 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); } …}
![Page 74: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/74.jpg)
74
INITIALIZING ALL VARIABLES
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); } …}
![Page 75: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/75.jpg)
75
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); }}
![Page 76: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/76.jpg)
76
PRINTING PROPERTIES (EDIT)
public class ABMISpreadsheet implements BMISpreadsheet { double height; … public void setHeight(double newHeight) { System.out.println(“Height: “ + newHeight); height = newHeight; } double weight; … public void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return weight/(height*height); }}
![Page 77: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/77.jpg)
77
PRINTING PROPERTIESpublic class ABMISpreadsheet implements BMISpreadsheet { double 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 void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return weight/(height*height); }}
![Page 78: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/78.jpg)
78
LESS CLUTTERED CODE (EDIT)public class ABMISpreadsheet implements BMISpreadsheet { double 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 void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return weight/(height*height); }}
![Page 79: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/79.jpg)
79
LESS CLUTTERED CODEpublic class ABMISpreadsheet implements BMISpreadsheet { double 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 void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return weight/(height*height); }}
![Page 80: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/80.jpg)
80
REMOVING DUPLICATIONpublic class ABMISpreadsheet implements BMISpreadsheet { double 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 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); }}
![Page 81: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/81.jpg)
81
REMOVING DUPLICATION (EDIT)public class ABMISpreadsheet implements BMISpreadsheet { double height; … public void setHeight(double newHeight) { printProperties();height = newHeight; } double 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()); }}
![Page 82: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/82.jpg)
82
REMOVING DUPLICATIONpublic class ABMISpreadsheet implements BMISpreadsheet { double height; … public void setHeight(double newHeight) { printProperties(); height = newHeight; } double weight; … public void setWeight(double newWeight) { printProperties(); weight = newWeight; } … void printProperties() { System.out.println(“Weight: “ + weight); System.out.println(“Height: “ + height); System.out.println(“BMI: “ + getBMI()); }}
![Page 83: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/83.jpg)
83
SEPARATION OF CONCERNSpublic class ABMISpreadsheet implements BMISpreadsheet { double height; … public void setHeight(double newHeight) { printProperties(); height = newHeight; } double weight; … public void setWeight(double newWeight) { printProperties(); weight = newWeight; } … void printProperties() { System.out.println(“Weight: “ + weight); System.out.println(“Height: “ + height); System.out.println(“BMI: “ + getBMI()); }}
Less cluttered
![Page 84: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/84.jpg)
84
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;}
![Page 85: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/85.jpg)
85
PRINTING BMI IN GETBMI()
public class ABMISpreadsheet implements BMISpreadsheet { double height; … 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
![Page 86: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/86.jpg)
86
NON-PUBLIC INSTANCE VARIABLES
public class ABMISpreadsheet implements BMISpreadsheet { double height, weight, bmi; …}
![Page 87: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/87.jpg)
87
MAKING INSTANCE VARIABLES PUBLIC
public class ABMISpreadsheet implements BMISpreadsheet { public double height, weight, bmi; …}
Other classes
![Page 88: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/88.jpg)
88
HARD TO CHANGE
public class ABMISpreadsheet implements BMISpreadsheet { public double height, weight; …}
Other classes
![Page 89: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/89.jpg)
89
CONSISTENCY CONSTRAINTS VIOLATED
Inconsistent values
![Page 90: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/90.jpg)
90
PRECONDITIONS CANNOT BE ENFORCED
![Page 91: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/91.jpg)
91
ENCAPSULATION PRINCIPLE Do not make instance variables public
Expose them through public methods
![Page 92: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/92.jpg)
92
public final double CMS_IN_INCH = 2.54;
PUBLIC CONSTANTS
public interface BMISpreadsheet {
public final double CMS_IN_INCH = 2.54;
}
Inconsistent value cannot be stored
Implementation independent ABMISpreadsheet
AnotherBMISpreadsheet
![Page 93: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/93.jpg)
93
PRINCIPLE Declare implementation-independent named
constants in interfaces implementing classes can access them
![Page 94: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/94.jpg)
94
![Page 95: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/95.jpg)
95
EXTRA SLIDES
![Page 96: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/96.jpg)
96
STEPWISE REFINEMENT
Declare two instance variables weight and height
Define a getter method that computes the value of BMI and returns it
public double getBMI() {return weight/(height*height);
}
Natural Language
Programming Language
![Page 97: Comp 110 Style](https://reader035.vdocuments.us/reader035/viewer/2022070421/568160c3550346895dcfecd6/html5/thumbnails/97.jpg)
97
REAL-WORLD ANALOGY
manufactures
Corvette Specification
implements
implementsmanufactures