Title: Program Style
1Program Style
- Identifier Names
- Comments
- Named Constants
- Avoiding Code Repetition
- Giving Least Privilege
- Efficiency
- Interfaces
2Writing Style
- To the point.
- Avoid repetition.
- Good flow.
- Illustrate ideas.
Assumes more than one way to write a document.
3BMI Spreadsheet
4ABMISpreadsheet
ABMISpreadsheet Instance
weight
height
setWeight()
getWeight()
new Weight
ObjectEditor
5AnotherBMISpreadsheet
ABMISpreadsheet Instance
weight
height
bmi
setWeight()
getWeight()
getBMI()
new Weight
ObjectEditor
6AnotherBMISpreadsheet
ABMISpreadsheet Instance
weight
height
bmi
setWeight()
getWeight()
getBMI()
new Weight
ObjectEditor
7Methods that Changes
ABMISpreadsheet Instance
weight
height
bmi
setWeight()
getBMI()
new Weight
ObjectEditor
8setWeight()
ABMISpreadsheet Instance
weight
bmi
setWeight()
public void setWeight(double newWeight) weight
newWeight bmi weight / (heightheight)
new Weight
ObjectEditor
9setHeight()
ABMISpreadsheet Instance
height
bmi
public void setHeight(double newHeight) height
newHeight bmi weight / (heightheight)
ObjectEditor
10getBMI()
ABMISpreadsheet Instance
bmi
getBMI()
public double getBMI() return bmi
ObjectEditor
11Complete 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
12Graphical Algorithm
ABMISpreadsheet Instance
weight
height
bmi
setWeight()
getWeight()
getBMI()
new Weight
ObjectEditor
13English 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.
14Algorithm
- 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.
15Real-World Algorithm
- Enter Class
- Distribute handouts
- Set up laptop projection.
- Revise topics learnt in the last class.
- Teach todays topics.
- Leave Class
16Stepwise Refinement
17Object 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
18Object Editor User Interface
19Similarities 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)
20Similarities 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
21Interface
22Implementing 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
23Real-World Analogy
24Interface
implements
BMISpreadsheet
instance of
AnotherBMISpreadsheet
AnotherBMISpreadsheet Instance
AnotherBMISpreadsheet Instance
25Using Interface to Classify
implements
BMISpreadsheet
instance of
AnotherBMISpreadsheet
BMISpreadsheet Instance
BMISpreadsheet Instance
26Using Car Specification to Classify
manufactures
27Cannot 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.
28Interface 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)
29Interface 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
30Interface Required
- Define interface for
- All classes (that are instantiated.)
- Some are not.
- Include all public instance methods
31Differences 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)
32Differences 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
33ABMISpreadsheet 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.
34Time-Space Tradeoff
Time
Space
35Time-Space Tradeoff
Space
Time
Time Miser
Space Miser
36Relating Interface and Class Names
- Class Name
- ltQualifiergtltInterfacegt (ABMISpreadsheet,
ASpaceEfficientBMISpreadsheet, SpaceEfficientBMISp
readsheet) - ltInterfacegtltQualifiergtImpl (BMISpreadsheetImpl,
BMISpreadsheetSpaceEfficientImpl) - Interface Name
- ltClassNamegtInterface (ABMISpreadsheetInterface)
37Comments
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)
38Removing Debugging Code
System.out.println(newHeight) /debugging
statement /
/ System.out.println(newHeight) /debugging
statement / /
/ System.out.println(newHeight) // debugging
statement /
39Javadoc 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
40What 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
41What to Comment?
double w // weight
double weight // weight
double weight
double bmi // computed by setWeight() and
setHeight()
42Javadoc 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 ()
43Improving 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
44Why Avoid Code Duplication?
- Less Typing
- Changes (Inches, LB)
- can forget change all repetitions
45Example 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)
46How 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)
47How 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)
48How 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)
49Principle 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
50Only 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)
51Method Invocation Syntax
System.out.println(setWeight called)
External Call
Internal Call
bmi calculateBMI()
52Improving 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)
53Improving 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)
54Declaring 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)
55Variables 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)
56Accidental 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)
57Literals, 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)
58Literals Vs Named Constants Vs Variables
- Use constants for program values that do not
change. - Use named constants for magic numbers
59What 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)
60What is a magic number?
- Human-created constant is a magic number.
- Natural constant may be a magic number to some.
61More 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)
62Removing 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)
63Removing 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)
64Local 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)
65Local 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)
66Scope
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)
67Scope of Public Items
public class AnotherBMISpreadsheet implements
BMISpreadsheet double height, weight,
bmi public double getHeight() return
height ...
68Identifier Scope
- Region of code where the identifier is visible.
- Arbitrary scopes not possible
- Least Privilege gt Make scope as small as possible
69Scope
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
70Initializing 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)
71Un-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)
72Un-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)
73Initializing 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)
74Initializing 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)
75Initializing 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)
76Printing 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)
77Printing 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)
78Printing 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)
79Less 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)
80Less 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)
81Removing 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)
82Removing 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)
83Removing 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())
84Separation 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())
85Independent 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
86Printing 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
87Non Public Instance Variables
- public class ABMISpreadsheet implements
BMISpreadsheet - public double height, weight, bmi
- ...
-
88Making Instance Variables Public
- public class ABMISpreadsheetWithPublicVariables
- public double height, weight, bmi
- ...
-
Other Classes
89Hard to Change
- public class ABMISpreadsheetWithPublicVariables
- public double height, weight
- ...
-
Other Classes
90Consistency Constraints Violated
91Encapsulation Principle
- Do not make instance variables public
- Expose them through public methods
-
92Public Constants
- public final double CMS_IN_INCH 2.54
-
93Principle
- Declare implementation-independent named
constants in interfaces - implementing classes can access them.
-