Comp 110 Style - PowerPoint PPT Presentation

1 / 97
About This Presentation
Title:

Comp 110 Style

Description:

Corvette Specification. implements. implements. manufactures ... A car defined by Corvette specification ordered from factory implementing the specification ... – PowerPoint PPT presentation

Number of Views:70
Avg rating:3.0/5.0
Slides: 98
Provided by: sasA5
Category:
Tags: comp | corvette | style

less

Transcript and Presenter's Notes

Title: Comp 110 Style


1
Comp 110Style
  • Instructor Sasa Junuzovic

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

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

4
BMI Spreadsheet
5
ABMISpreadsheet
ABMISpreadsheet Instance
weight
height
writes
reads
reads
writes
reads
getWeight()
setWeight()
getHeight()
setHeight()
getBMI()
calls
calls
calls
calls
new weight
new height
weight
height
ObjectEditor
6
AnotherBMISpreadsheet
AnotherBMISpreadsheet Instance
weight
height
bmi
writes
reads
reads
reads
writes
getWeight()
setWeight()
getHeight()
setHeight()
getBMI()
calls
calls
calls
calls
new weight
new height
weight
height
ObjectEditor
7
Methods that Change
ABMISpreadsheet Instance
weight
height
bmi
writes
reads
writes
setWeight()
setHeight()
getBMI()
calls
calls
new weight
new height
ObjectEditor
8
setWeight()
ABMISpreadsheet Instance
weight
bmi
writes
setWeight()
public void setWeight(double newWeight) weight
newWeight bmi weight / (heightheight)
calls
new weight
ObjectEditor
9
setHeight()
ABMISpreadsheet Instance
height
bmi
writes
setHeight()
public void setHeight(double newHeight) height
newHeight bmi weight / (heightheight)
calls
new height
ObjectEditor
10
getBMI()
ABMISpreadsheet Instance
bmi
reads
getBMI()
public double getBMI() return bmi
ObjectEditor
11
Complete Code
public class AnotherBMISpreadsheet double
height, weight, bmi public double
getHeight() return height
public void setHeight(double newHeight)
height newHeight bmi
weight/(heightheight) public double
getWeight() return weight
public void setWeight(double newWeight)
weight newWeight bmi
weight/(heightheight) public double
getBMI() return bmi
12
Graphical Algorithm
ABMISpreadsheet Instance
weight
height
bmi
writes
reads
reads
reads
writes
getWeight()
setWeight()
getHeight()
setHeight()
getBMI()
calls
calls
calls
calls
new weight
new height
weight
height
ObjectEditor
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

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

15
Stepwise Refinement
Natural Language
  • 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

Programming Language
public void setWeight(double newWeight) weight
newWeight bmi weight/(heightheight)
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/(heightheight) public double
getWeight() return weight
public void setWeight(double newWeight)
weight newWeight bmi
weight/(heightheight) public double
getBMI() return bmi
17
ObjectEditor User Interfaces
18
Similarities in the Two Classes
public class AnotherBMISpreadsheet double
height, weight, bmi public double
getHeight() return height
public void setHeight(double
newHeight) height newHeight
bmi weight/(heightheight) public
double getWeight() return weight
public void setWeight(double
newWeight) weight newWeight
bmi weight/(heightheight) 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/(heightheight)

19
Real-World Analogy
implements
manufactures
Corvette Specification
implements
manufactures
20
Interface
21
Implementing an Interface
Contract
public class AnotherBMISpreadsheet implements
BMISpreadsheet double height, weight, bmi
public double getHeight() return
height public void setHeight (double
newHeight) height newHeight
bmi weight/(heightheight)
Parameter names never matter to Java
22
Interface
ABMISpreadsheet
implements
instance of
BMISpreadsheet
ABMISpreadsheet instance
ABMISpreadsheet instance
implements
AnotherBMISpreadsheet
instance of
AnotherBMISpreadsheet instance
AnotherBMISpreadsheet instance
23
Using Interfaces to Classify
ABMISpreadsheet
implements
instance of
BMISpreadsheet
BMISpreadsheet instance
BMISpreadsheet instance
implements
AnotherBMISpreadsheet
instance of
BMISpreadsheet instance
BMISpreadsheet instance
24
Using Car Specifications to Classify
Corvette
implements
manufactures
Corvette
Corvette Specification
implements
Corvette
manufactures
Corvette
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

26
Interface as a Syntactic Specification
public 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/(heightheight)
27
Interface as a Syntactic Specification
public 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
28
Interface Required
  • Define interfaces for
  • All classes (that are instantiated)
  • Some are not
  • Include all public methods

29
Differences in the Two Classes
public class AnotherBMISpreadsheet double
height, weight, bmi public double
getHeight() return height
public void setHeight(double
newHeight) height newHeight
bmi weight/(heightheight) public
double getWeight() return weight
public void setWeight(double
newWeight) weight newWeight
bmi weight/(heightheight) 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/(heightheight)

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

31
Time-Space Tradeoff
Time
Space
32
Time-Space Tradeoff
Space
Time
Time Miser
Space Miser
33
Relating Interface and Class Names
Class Name
- ABMISpreadsheet -
ASpaceEfficientBMISpreadsheet -
SpaceEfficientBMISpreadsheet
Impl - BMISpreadsheetImpl
- BMISpreadsheetSpaceEfficientImpl
Interface Name
Interface - ABMISpreadsheetInterface
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

35
Writing Style
  • To the point
  • Avoid repetition
  • Good flow
  • Illustrate ideas

There is more than one way to write a document
that conveys some facts e.g. the influence of
BMI on health
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
37
Programming Style The Art of Programming
?
Use Interfaces
?
Efficiency
Comment Program
Avoid Code Repetition
Giving Least Privilege
Use Named Constants
38
Comments
Single line comments
double bmi //computed by setWeight and setHeight
Arbitrary 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)
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
40
Removing Debugging Code
System.out.println(newHeight) /debugging
statement /
/ System.out.println(newHeight)
/debugging statement / /
/ System.out.println(newHeight) //
debugging statement /
41
What to Comment?
  • Any code fragment needing explanation
  • Class
  • Top-level algorithm, author, date modified
  • Variable declaration
  • Purpose, where used, how its value is computed
  • Method declaration
  • params, return value, algorithm, author, date
    modified
  • Statement sequence
  • Explanation
  • Debugging code

Summarizing vs. Elaborating Comments?
42
What to Comment?
double w // weight
Bad variable name
double weight // weight
Redundant
double weight
Self-commenting
double bmi // computed by setWeight and setHeight
Useful comment
43
JavaDoc Tags
JavaDoc tags
/ _at_author Prasun Dewan _at_param newWeight
the new value of the property, weight. sets
new values of the variables, weight and bmi
/ public void setWeight (double newWeight)

/ _at_author Prasun Dewan _at_return the value
of the variable, weight / public double
getWeight ()
44
Commenting Interfaces
Implementation independent comments
/ _at_param newWeight the new value of the
property, weight / public void setWeight
(double newWeight)
/ _at_return the value of the variable, weight
/ public double getWeight ()
Commenting both interface and implementation?
45
Programming Style The Art of Programming
?
Use Interfaces
?
Efficiency
?
Comment Program
Avoid Code Repetition
Giving Least Privilege
Use Named Constants
46
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 weight/(heightheight) public
double getWeight() return weight
public void setWeight(double newWeight)
weight newWeight bmi
weight/(heightheight) public double
getBMI() return bmi
Code repetition
Assuming ABMICalculator does not exist
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/(heightheight)
48
Re-using Code
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() double calculateBMI()
return weight/(heightheight)
.
49
Changing Re-used Code Once
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
calculateBMI() double calculateBMI()
return (weight/2.2)/(height
2.54/100height2.54/100)
Declaring vs. calling methods?
50
Programming Style The Art of Programming
?
Use Interfaces
?
Efficiency
?
Comment Program
?
Avoid Code Repetition
Giving Least Privilege
Use Named Constants
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/100height2.54/100)

Not in interface
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

ABMICalculator User
ABMICalculator
ObjectEditor
getWeight()
setWeight()
getHeight()
setHeight()
getBMI()
computeBMI()
53
Improving the Style
?
Use Interfaces
?
Efficiency
?
Comment Program
?
Avoid Code Repetition
?
Giving Least Privilege
Use Named Constants
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/100height2.54/100)

Magic numbers
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) /
(heightCMS_IN_INCH/100heightCMS_IN_INCH/100)

Named constants
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) /
(heightCMS_IN_INCH/100heightCMS_IN_INCH/100)

Un-initializing declaration
Initializing declaration
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) /
(heightcmsInInch/100heightcmsInInch/100)

58
Accidental or Malicious Modification
Violating least privilege
public class AnotherBMISpreadsheet implements
BMISpreadsheet double height, weight, bmi
... double lbsInKg 2.2 double
cmsInInch 2.54 double calculateBMI()
lbsInKg 22 return
(weight/lbsInKg) /
(heightcmsInInch/100heightcmsInInch/100)

59
Literals vs. Named Constants vs. Variables
  • Use constants for program values that do not
    change
  • Use named constants for magic numbers

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) /
(heightCMS_IN_INCH/100heightCMS_IN_INCH/100)

Within same method and has the same value
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
heightCMS_IN_INCH/100 return
(weight/LBS_IN_KG) /
(heightInMetersheightInMeters)
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
heightCMS_IN_INCH/100 return
(weight/LBS_IN_KG) /
(heightInMetersheightInMeters)
63
Local vs. Global Variable
public class AnotherBMISpreadsheet implements
BMISpreadsheet double height, weight, bmi
double heightInMeters heightCMS_IN_INCH/100
... final double LBS_IN_KG 2.2
final double CMS_IN_INCH 2.54 double
calculateBMI() return
(weight/LBS_IN_KG) /
(heightInMetersheightInMeters)
64
Local vs. Global Variable
public class AnotherBMISpreadsheet implements
BMISpreadsheet double height, weight, bmi
double heightInMeters heightCMS_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) /
(heightInMetersheightInMeters)
Violating least privilege
65
scope
height scope
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/(heightheight) double
calculateBMI () double heightInMetres
heightCMS_IN_INCH/100 return
(weight/LBS_IN_KG) / (heightInMetresheightInMetre
s)
Not a scope
heightInMeters scope
66
Scope of Public Items
getWeight() scope
public class AnotherBMISpreadsheet implements
BMISpreadsheet double height, weight, bmi
public double getWeight()
return weight
ObjectEditor
ABMISpreadsheet
67
Identifier Scope
  • Region of code where the identifier is visible
  • Arbitrary scopes not possible
  • Least Privilege Make scope as small as possible

68
Scope
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/(heightheight) double
calculateBMI() () double heightInMetres
heightCMS_IN_INCH/100 return
(weight/LBS_IN_KG) / (heightInMetresheightInMetre
s)
heightInMeters scope
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
heightCMS_IN_INCH/100 return
(weight/LBS_IN_KG) / (heightInMetresheightInMetre
s)
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
heightCMS_IN_INCH/100 return
(weight/LBS_IN_KG) / (heightInMetresheightInMetre
s)
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)
/ (heightInMetresheightInMetres)
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)
/ (heightInMetresheightInMetres)
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 heightCMS_IN_INCH/100
return (weight/LBS_IN_KG) /
(heightInMetresheightInMetres)
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 heightCMS_IN_INCH/100
return (weight/LBS_IN_KG) /
(heightInMetresheightInMetres)
75
Printing Properties
public 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/(heightheight)
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/(heightheight)
77
Printing Properties
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/(heightheight)
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/(heightheight)
79
Less Cluttered Code
public 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/(heightheight)

80
Removing Duplication
public 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/(heightheight)

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/(heightheight) void
printProperties() System.out.println(W
eight weight) System.out.println(He
ight height) System.out.println(BMI
getBMI())
82
Removing Duplication
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
void printProperties()
System.out.println(Weight weight)
System.out.println(Height height)
System.out.println(BMI getBMI())
83
Separation of Concerns
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
void printProperties()
System.out.println(Weight weight)
System.out.println(Height height)
System.out.println(BMI getBMI())
Less cluttered
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
85
Printing BMI in getBMI()
public class ABMISpreadsheet implements
BMISpreadsheet double height
public double getBMI()
printProperties() return
weight/(heightheight) void
printProperties() System.out.println(We
ight weight) System.out.println(Hei
ght height) System.out.println(BMI
getBMI())
getBMI() never terminates
Recursive, calls itself indirectly
Infinite recursion
Avoid recursion for now
86
Non-public Instance Variables
public class ABMISpreadsheet implements
BMISpreadsheet double height, weight, bmi

87
Making Instance Variables Public
public class ABMISpreadsheet implements
BMISpreadsheet public double height,
weight, bmi
Other classes
88
Hard to Change
public class ABMISpreadsheet implements
BMISpreadsheet public double height,
weight
Other classes
89
Consistency Constraints Violated
Inconsistent values
90
Preconditions Cannot be Enforced
More on this later
91
Encapsulation Principle
  • Do not make instance variables public
  • Expose them through public methods

92
Public Constants
Inconsistent value cannot be stored
public final double CMS_IN_INCH 2.54
public interface BMISpreadsheet public
final double CMS_IN_INCH 2.54
Implementation independent
ABMISpreadsheet
AnotherBMISpreadsheet
93
Principle
  • Declare implementation-independent named
    constants in interfaces
  • implementing classes can access them

94
(No Transcript)
95
Extra Slides
96
Stepwise Refinement
Natural Language
  • Declare two instance variables
  • weight and height
  • Define a getter method that computes the value of
    BMI and returns it

Programming Language
public double getBMI() return
weight/(heightheight)
97
Real-World Analogy
implements
manufactures
Corvette Specification
implements
manufactures
Write a Comment
User Comments (0)
About PowerShow.com