Program Style - PowerPoint PPT Presentation

1 / 93
About This Presentation
Title:

Program Style

Description:

Program Style. Identifier Names. Comments. Named Constants. Avoiding Code Repetition ... Writing Style. To the point. Avoid repetition. Good flow. Illustrate ideas. ... – PowerPoint PPT presentation

Number of Views:58
Avg rating:3.0/5.0
Slides: 94
Provided by: Prasun2
Learn more at: http://www.cs.unc.edu
Category:
Tags: program | style

less

Transcript and Presenter's Notes

Title: Program Style


1
Program Style
  • Identifier Names
  • Comments
  • Named Constants
  • Avoiding Code Repetition
  • Giving Least Privilege
  • Efficiency
  • Interfaces

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

Assumes more than one way to write a document.
3
BMI Spreadsheet
4
ABMISpreadsheet
ABMISpreadsheet Instance
weight
height
setWeight()
getWeight()
new Weight
ObjectEditor
5
AnotherBMISpreadsheet
ABMISpreadsheet Instance
weight
height
bmi
setWeight()
getWeight()
getBMI()
new Weight
ObjectEditor
6
AnotherBMISpreadsheet
ABMISpreadsheet Instance
weight
height
bmi
setWeight()
getWeight()
getBMI()
new Weight
ObjectEditor
7
Methods that Changes
ABMISpreadsheet Instance
weight
height
bmi
setWeight()
getBMI()
new Weight
ObjectEditor
8
setWeight()
ABMISpreadsheet Instance
weight
bmi
setWeight()
public void setWeight(double newWeight) weight
newWeight bmi weight / (heightheight)
new Weight
ObjectEditor
9
setHeight()
ABMISpreadsheet Instance
height
bmi
public void setHeight(double newHeight) height
newHeight bmi weight / (heightheight)
ObjectEditor
10
getBMI()
ABMISpreadsheet Instance
bmi
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) pub
lic 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
setWeight()
getWeight()
getBMI()
new Weight
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
Real-World Algorithm
  • Enter Class
  • Distribute handouts
  • Set up laptop projection.
  • Revise topics learnt in the last class.
  • Teach todays topics.
  • Leave Class

16
Stepwise Refinement
17
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/(heightheight) pub
lic double getWeight() return
weight public void setWeight(double
newWeight) weight newWeight bmi
weight/(heightheight) public double
getBMI() return bmi
18
Object Editor User Interface
19
Similarities in the two Classes
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)

20
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) pub
lic double getWeight() return
weight public void setWeight(double
newWeight) weight newWeight bmi
weight/(heightheight) public double
getBMI() return bmi
21
Interface
22
Implementing an Interface
public 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
23
Real-World Analogy
24
Interface
implements
BMISpreadsheet
instance of
AnotherBMISpreadsheet
AnotherBMISpreadsheet Instance
AnotherBMISpreadsheet Instance
25
Using Interface to Classify
implements
BMISpreadsheet
instance of
AnotherBMISpreadsheet
BMISpreadsheet Instance
BMISpreadsheet Instance
26
Using Car Specification to Classify
manufactures
27
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.

28
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
height/(weightweight)
29
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
30
Interface Required
  • Define interface for
  • All classes (that are instantiated.)
  • Some are not.
  • Include all public instance methods

31
Differences in the Two Classes
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)
32
Differences in the Two Classes
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) pub
lic double getBMI() return bmi
33
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.

34
Time-Space Tradeoff
Time
Space
35
Time-Space Tradeoff
Space
Time
Time Miser
Space Miser
36
Relating Interface and Class Names
  • Class Name
  • ltQualifiergtltInterfacegt (ABMISpreadsheet,
    ASpaceEfficientBMISpreadsheet, SpaceEfficientBMISp
    readsheet)
  • ltInterfacegtltQualifiergtImpl (BMISpreadsheetImpl,
    BMISpreadsheetSpaceEfficientImpl)
  • Interface Name
  • ltClassNamegtInterface (ABMISpreadsheetInterface)

37
Comments
Single-line comment
double bmi //computed by setWeight and setHeight
Arbitrary comment
/ This version recalculates the bmi when weight
or height change, not when getBMI is
called / public class AnotherBMISpreadsheet
/ recompute dependent properties / bmi weight
/ (heightheight)
38
Removing Debugging Code
System.out.println(newHeight) /debugging
statement /
/ System.out.println(newHeight) /debugging
statement / /
/ System.out.println(newHeight) // debugging
statement /
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
40
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

41
What to Comment?
double w // weight
double weight // weight
double weight
double bmi // computed by setWeight() and
setHeight()
42
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 ()
43
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) pub
lic double getBMI() return bmi
44
Why Avoid Code Duplication?
  • Less Typing
  • Changes (Inches, LB)
  • can forget change all repetitions

45
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/(heightheight) public double
getWeight() return weight public void
setWeight(double newWeight) weight
newWeight bmi weight/(heightheight)
bmi (weight/2.2)/(height 2.54/100height2.54/
100)
bmi (weight/2.2)/(height 2.54/100height2.54/
100)
46
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/100height2.54/100)
public double getWeight()
return weight public void
setWeight(double newWeight) weight
newWeight bmi (weight/2.2)/(height
2.54/100height2.54/100)
47
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/100height2.54/100)

48
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/100height2.54/100)

49
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
ABMICalculator User
setWeight()
getWeight()
setHeight()
getHeight()
getBMI()
calculateBMI()
computeBMI()
ObjectEditor
50
Only Public Methods in Interface
public 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/
(heightheight)
51
Method Invocation Syntax
System.out.println(setWeight called)
External Call
Internal Call
bmi calculateBMI()
52
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() 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/100height2.54/100)
53
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() 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) /
(heightCMS_IN_INCH/100heightCMS_IN_INCH/100)

54
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)

55
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/100he
ightcmsInInch/100)
56
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) /
(heightcmsInInch/100heightcmsInInch/100)
57
Literals, Named Constants, Constants, Variables
return (weight/2.2) / (height2.54/100height2.54
/100)

return (weight/LBS_IN_KG) / (heightCMS_IN_INCH/10
0heightCMS_IN_INCH/100)
return (weight/lbsInKg) / (heightcmsInInch/100he
ightcmsInInch/100)

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

59
What is a Magic Number?
return (weight/2.2) / (height2.54/100height2.54
/100)

return (weight/LBS_IN_KG) / (heightCMS_IN_INCH/10
0heightCMS_IN_INCH/100)
return hoursWorkedhourlyWage 50
return hoursWorkedhourlyWage BONUS
System.out.println (Bonus 50)
60
What is a magic number?
  • Human-created constant is a magic number.
  • Natural constant may be a magic number to some.

61
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)

62
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()
return (weight/LBS_IN_KG) /
(heightCMS_IN_INCH/100heightCMS_IN_INCH/100)

63
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 heightInMetres
heightCMS_IN_INCH/100 return
(weight/LBS_IN_KG) / (heightInMetresheightInMetre
s)
64
Local Vs Global Variable
public class AnotherBMISpreadsheet implements
BMISpreadsheet double height, weight,
bmi double heightInMetres heightCMS_IN_INCH/1
00 ... final double LBS_IN_KG 2.2 final
double CMS_IN_INCH 2.54 double calculateBMI()
return (weight/LBS_IN_KG) /
(heightInMetresheightInMetres)
65
Local Vs Global Variable
public class AnotherBMISpreadsheet implements
BMISpreadsheet double height, weight,
bmi double heightInMetres heightCMS_IN_INCH/1
00 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) / (heightInMetresheightInMetre
s)
66
Scope
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
heightCMS_IN_INCH/100 return
(weight/LBS_IN_KG) / (heightInMetresheightInMetre
s)
67
Scope of Public Items
public class AnotherBMISpreadsheet implements
BMISpreadsheet double height, weight,
bmi public double getHeight() return
height ...
68
Identifier Scope
  • Region of code where the identifier is visible.
  • Arbitrary scopes not possible
  • Least Privilege gt Make scope as small as possible

69
Scope
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
heightCMS_IN_INCH/100 return
(weight/LBS_IN_KG) / (heightInMetresheightInMetre
s)
heightInMetres Scope
70
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)
71
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
heightInMetres heightCMS_IN_INCH/100
return (weight/LBS_IN_KG) / (heightInMetresheigh
tInMetres)
72
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) / (heightInMetresheight
InMetres)
73
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
heightCMS_IN_INCH/100 return
(weight/LBS_IN_KG) / (heightInMetresheightInMetre
s)
74
Initializing all Variables (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
heightCMS_IN_INCH/100 return
(weight/LBS_IN_KG) / (heightInMetresheightInMetre
s)
75
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) / (heightInMetresheig
htInMetres)
76
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)
77
Printing Properties (edit)
public class ABMISpreadsheet implements
BMISpreadsheet double height public double
getHeight() return height public void
setHeight(double newHeight) System.out.pri
ntln(bmi getBMI()) height
newHeight double weight public double
getWeight() return weight public void
setWeight(double newWeight) weight
newWeight public double getBMI() return
weight/(heightheight)
78
Printing Properties
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(Heigh
t 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/(heightheight)
79
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(Heigh
t 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/(heightheight)
80
Less Cluttered Code
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(Heigh
t 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/(heightheight)

81
Removing Duplication
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(Heigh
t 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/(heightheight)

82
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(Heigh
t 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/(heightheight)

83
Removing Duplication
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)
printProperties() weight
newWeight public double getBMI() return
weight/(heightheight) void
printProperties() System.out.println(Weight
weight) System.out.println(Height
height) System.out.println(BMI
getBMI())
84
Separation of Concerns
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() return
weight/(heightheight) void
printProperties() System.out.println(Weight
weight) System.out.println(Height
height) System.out.println(BMI
getBMI())
85
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
86
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/(heightheight) 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
87
Non Public Instance Variables
  • public class ABMISpreadsheet implements
    BMISpreadsheet
  • public double height, weight, bmi
  • ...

88
Making Instance Variables Public
  • public class ABMISpreadsheetWithPublicVariables
  • public double height, weight, bmi
  • ...

Other Classes
89
Hard to Change
  • public class ABMISpreadsheetWithPublicVariables
  • public double height, weight
  • ...

Other Classes
90
Consistency Constraints Violated
91
Encapsulation Principle
  • Do not make instance variables public
  • Expose them through public methods

92
Public Constants
  • public final double CMS_IN_INCH 2.54

93
Principle
  • Declare implementation-independent named
    constants in interfaces
  • implementing classes can access them.
Write a Comment
User Comments (0)
About PowerShow.com