Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Program Style Identifier Names Comments Named Constants Avoiding Code Repetition Giving Least Privilege Efficiency Interfaces."— Presentation transcript:

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 ObjectEditor ABMISpreadsheet Instance weightheight setWeight() new Weight calls writes setHeight() new Height calls writes height getHeight() calls reads getWeight() reads weight calls reads getBMI()

5 AnotherBMISpreadsheet ObjectEditor ABMISpreadsheet Instance weightheight setWeight() new Weight calls writes setHeight() new Height calls writes height getHeight() calls reads getWeight() reads weight calls getBMI() bmi reads

6 AnotherBMISpreadsheet ObjectEditor ABMISpreadsheet Instance weightheight setWeight() new Weight calls writes setHeight() new Height calls writes height getHeight() calls reads getWeight() reads weight calls getBMI() bmi reads

7 Methods that Changes ObjectEditor ABMISpreadsheet Instance weightheight setWeight() new Weight calls writes setHeight() new Height calls writes getBMI() bmi reads

8 setWeight() ObjectEditor ABMISpreadsheet Instance weight setWeight() new Weight calls writes bmi public void setWeight(double newWeight) { weight = newWeight; bmi = weight / (height*height); }

9 setHeight() ObjectEditor ABMISpreadsheet Instance height setHeight() new Height calls writes bmi public void setHeight(double newHeight) { height = newHeight; bmi = weight / (height*height); }

10 getBMI() ObjectEditor ABMISpreadsheet Instance getBMI() bmi reads public double getBMI() { return bmi; }

11 Complete Code 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; }

12 Graphical Algorithm ObjectEditor ABMISpreadsheet Instance weightheight setWeight() new Weight calls writes setHeight() new Height calls writes height getHeight() calls reads getWeight() reads weight calls getBMI() bmi reads

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 today’s topics. Leave Class

16 Stepwise Refinement Declare three instance variables, weight, height and, bmi. The three getter methods return values of the three instance variables. The setter methods assign new weight and height values and compute and assign new BMI value to bmi. Natual Language public void setWeight(double newWeight) { weight = newWeight; bmi = weight/(height*height); } Programming Language Graphical Algorithm weight setWeight() writes getWeight() reads BMI

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/(height*height); } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; bmi = weight/(height*height); } 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/(height*height); }

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/(height*height); } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; bmi = weight/(height*height); } 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; } parameter names never matter to Java contract

23 manufactures Real-World Analogy Accord Specification implements

24 Interface AnotherBMISpreadsheet Instance AnotherBMISpreadsheet instance of AnotherBMISpreadsheet Instance ABMISpreadsheet instance of ABMISpreadsheet Instance ABMISpreadsheet Instance BMISpreadsheet implements

25 Using Interface to Classify BMISpreadsheet Instance AnotherBMISpreadsheet instance of BMISpreadsheet Instance ABMISpreadsheet instance of BMISpreadsheet Instance BMISpreadsheet Instance BMISpreadsheet implements

26 Using Car Specification to Classify manufactures Accord Specification implements Accord

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 public class ABMISpreadsheet implements BMISpreadsheet{ double height, weight; public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return height/(weight*weight); } Interface as Syntactic Specification

29 public class ABMISpreadsheet implements BMISpreadsheet{ double height, weight; public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return 3245.4 } Interface as Syntactic Specification Syntactic Contract Bombay Market Index?

30 Define interface for: –All classes (that are instantiated.) –Some are not. –Include all public instance methods Interface Required

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/(height*height); }

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/(height*height); } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; bmi = weight/(height*height); } public 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 Miser Space Miser Space Time

35 Space Time Time MiserSpace Miser Time-Space Tradeoff

36 Relating Interface and Class Names Class Name: (ABMISpreadsheet, ASpaceEfficientBMISpreadsheet, SpaceEfficientBMISpreadsheet) Impl (BMISpreadsheetImpl, BMISpreadsheetSpaceEfficientImpl) Interface Name: Interface (ABMISpreadsheetInterface)

37 Comments double bmi; //computed by setWeight and setHeight Single-line comment /* recompute dependent properties */ bmi = weight / (height*height); Arbitrary comment /* This version recalculates the bmi when weight or height change, not when getBMI is called */ public class AnotherBMISpreadsheet {…}

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 bmi; // computed by setWeight() and setHeight() double weight;Self Commenting Redundant Bad Variable Name Useful Comment

42 /* * @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 Javadoc tags

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/(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

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/(height*height); } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; bmi = weight/(height*height); } bmi = (weight/2.2)/(height * 2.54/100*height*2.54/100);

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/100*height*2.54/100); } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; bmi = (weight/2.2)/(height * 2.54/100*height*2.54/100); }

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/100*height*2.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/100*height*2.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 ObjectEditor setWeight() setHeight()getHeight() getWeight() getBMI()calculateBMI()computeBMI() ABMICalculator User

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/ (height*height); } not in interface

51 Method Invocation Syntax System.out.println(“setWeight called”); Actual Parameter Method Name Target Object bmi = calculateBMI() parameterless method Internal Call External Call Function Return Value

52 public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; bmi = calculateBMI(); } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; bmi = calculateBMI(); } public double getBMI() { return bmi; } double calculateBMI() { return (weight/2.2)/(height * 2.54/100*height*2.54/100); } Improving the Style Magic numbers?

53 public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; bmi = calculateBMI(); } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; bmi = calculateBMI(); } public double getBMI() { return bmi; } double calculateBMI() { return (weight/LBS_IN_KG) / (height*CMS_IN_INCH/100*height*CMS_IN_INCH/100); } Improving the Style Named Constants

54 public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi;... final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; double calculateBMI() { return (weight/LBS_IN_KG) / (height*CMS_IN_INCH/100*height*CMS_IN_INCH/100); } Declaring Named Constants Initializing Declaration Un-initializing Declaration All Caps by Conventions Cannot Change Initial Value

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) / (height*cmsInInch/100*height*cmsInInch/100); }

56 public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi;... double lbsInKg = 2.2; double cmsInInch = 2.54; double calculateBMI() { lbsInKg = 22; return (weight/lbsInKg) / (height*cmsInInch/100*height*cmsInInch/100); } Accidental or Malicious Modification Violating Least Privilege

57 Constant return (weight/lbsInKg) / (height*cmsInInch/100*height*cmsInInch/100); return (weight/2.2) / (height*2.54/100*height*2.54/100); Literals, Named Constants, Constants, Variables Variable Literal return (weight/LBS_IN_KG) / (height*CMS_IN_INCH/100*height*CMS_IN_INCH/100) Named Constant

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

59 return (weight/2.2) / (height*2.54/100*height*2.54/100); What is a Magic Number? return (weight/LBS_IN_KG) / (height*CMS_IN_INCH/100*height*CMS_IN_INCH/100) Natural Constants return hoursWorked*hourlyWage + 50; return hoursWorked*hourlyWage + BONUS; Human-Created Constant System.out.println (“Bonus: “ + 50);

60 Human-created constant is a magic number. Natural constant may be a magic number to some. What is a magic number?

61 public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi;... final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; double calculateBMI() { return (weight/LBS_IN_KG) / (height*CMS_IN_INCH/100*height*CMS_IN_INCH/100); } More Code Repetition Within Same Method and Has Same Value

62 public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi;... final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; double calculateBMI() { return (weight/LBS_IN_KG) / (height*CMS_IN_INCH/100*height*CMS_IN_INCH/100); } Removing Code Repetition (edit)

63 public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi;... final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; double calculateBMI() { double heightInMetres = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } Removing Code Repetition

64 public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; double heightInMetres = height*CMS_IN_INCH/100;... final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; double calculateBMI() { return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } Local Vs Global Variable

65 public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; double heightInMetres = height*CMS_IN_INCH/100; public void setHeight(double newHeight) { height = heightInMetres; bmi = calculateBMI(); }... final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; double calculateBMI() { return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } Local Vs Global Variable Violating least privilege

66 public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; bmi = calculateBMI(); } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; bmi = calculateBMI(); } public double getBMI() { return bmi; } double calculateBMI() { double heightInMetres = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } Scope heightInMetres Scope height Scope Not a Scope

67 public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; public double getHeight() { return height; }... } Scope of Public Items getHeight() Scope ObjectEditor ABMISpreadsheet

68 Identifier Scope Region of code where the identifier is visible. Arbitrary scopes not possible Least Privilege => Make scope as small as possible

69 public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; public void setHeight(double newHeight) { double height = heightInMetres; bmi = calculateBMI(); }... final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; double calculateBMI() { double heightInMetres = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } Scope heightInMetres Scope

70 public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi;... final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; double calculateBMI() { double heightInMetres = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } Initializing Declaration

71 public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi;... final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; double calculateBMI() { double heightInMetres; heightInMetres = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } Un-initializing Declaration

72 public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi;... final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; double calculateBMI() { double heightInMetres; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } Un-initialized Variable

73 public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi;... final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; double calculateBMI() { double heightInMetres = height*CMS_IN_INCH/100;; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } Initializing all Variables

74 public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi;... final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; double calculateBMI() { double heightInMetres = height*CMS_IN_INCH/100;; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } Initializing all Variables (edit)

75 public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height = 70, weight = 160, bmi = calculateBMI();... final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; double calculateBMI() { double heightInMetres = height*CMS_IN_INCH/100;; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } Initializing all Variables

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/(height*height); }

77 Printing Properties (edit) public class ABMISpreadsheet implements BMISpreadsheet { double height; public double getHeight() { return height; } public void setHeight(double newHeight) { System.out.println(“bmi:” + getBMI()); height = newHeight; } double weight; public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return weight/(height*height); }

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(“Height: “ + height); double bmi = getBMI(); System.out.println(“BMI: “ + bmi); height = newHeight; } double weight; public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return weight/(height*height); }

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(“Height: “ + height); double bmi = getBMI(); System.out.println(“BMI: “ + bmi); height = newHeight; } double weight; public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return weight/(height*height); }

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(“Height: “ + height); System.out.println(“BMI: “ + getBMI()); height = newHeight; } double weight; public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return weight/(height*height); }

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(“Height: “ + height); System.out.println(“BMI: “ + getBMI()); height = newHeight; } double weight; public double getWeight() { return weight; } public void setWeight(double newWeight) { System.out.println(“Weight: “ + weight); System.out.println(“Height: “ + height); System.out.println(“BMI: “ + getBMI()); weight = newWeight; } public double getBMI() { return weight/(height*height); }

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(“Height: “ + height); System.out.println(“BMI: “ + getBMI()); height = newHeight; } double weight; public double getWeight() { return weight; } public void setWeight(double newWeight) { System.out.println(“Weight: “ + weight); System.out.println(“Height: “ + height); System.out.println(“BMI: “ + getBMI()); weight = newWeight; } public double getBMI() { return weight/(height*height); }

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/(height*height); } 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/(height*height); } void printProperties() { System.out.println(“Weight: “ + weight); System.out.println(“Height: “ + height); System.out.println(“BMI: “ + getBMI()); } Less Cluttered

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/(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

87 public class ABMISpreadsheet implements BMISpreadsheet { public double height, weight, bmi;... } Non Public Instance Variables

88 public class ABMISpreadsheetWithPublicVariables { public double height, weight, bmi;... } Making Instance Variables Public Other Classes

89 public class ABMISpreadsheetWithPublicVariables { public double height, weight;... } Hard to Change Other Classes

90 Consistency Constraints Violated Inconsistent Values

91 Do not make instance variables public –Expose them through public methods Encapsulation Principle

92 public final double CMS_IN_INCH = 2.54; Public Constants Inconsistent value cannot be stored Implementation Independent public interface BMISpreadsheet { } ABMISpreadsheetAnotherBMISpreadsheet

93 Declare implementation-independent named constants in interfaces –implementing classes can access them. Principle


Download ppt "Program Style Identifier Names Comments Named Constants Avoiding Code Repetition Giving Least Privilege Efficiency Interfaces."

Similar presentations


Ads by Google