Presentation is loading. Please wait.

Presentation is loading. Please wait.

C OMP 110 S TYLE Instructor: Jason Carter. 2 I NTERFACES AND M ORE S TYLE Define contracts between our users and implementers Optional – they may not.

Similar presentations


Presentation on theme: "C OMP 110 S TYLE Instructor: Jason Carter. 2 I NTERFACES AND M ORE S TYLE Define contracts between our users and implementers Optional – they may not."— Presentation transcript:

1 C OMP 110 S TYLE Instructor: Jason Carter

2 2 I NTERFACES AND M ORE S TYLE 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 3 T WO W AYS OF D OING THE BMI S PREADSHEET 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 4 BMI S PREADSHEET

5 5 ABMIS PREADSHEET ABMISpreadsheet Instance weight height getWeight()setWeight()getHeight()setHeight()getBMI() ObjectEditor calls writes weight calls reads height calls readswrites calls new weight new height reads

6 6 A NOTHER BMIS PREADSHEET AnotherBMISpreadsheet Instance weight height getWeight()setWeight()getHeight()setHeight()getBMI() ObjectEditor calls writes weight calls reads height calls reads writes calls new weight new height reads bmi

7 7 M ETHODS THAT C HANGE ABMISpreadsheet Instance weight height setWeight()setHeight()getBMI() ObjectEditor writes calls writes calls new weight new height reads bmi

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

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

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

11 11 C OMPLETE C ODE 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 12 G RAPHICAL A LGORITHM ABMISpreadsheet Instance weight height getWeight()setWeight()getHeight()setHeight()getBMI() ObjectEditor calls writes weight calls reads height calls reads writes calls new weight new height reads bmi

13 13 E NGLISH A LGORITHM 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 14 A LGORITHM 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 15 S TEPWISE R EFINEMENT Declare three instance variables weight, height and, bmi Define three getter methods return values of the three instance variables Define two setter methods to change weight and height The setter methods assign new weight and height values and compute and assign new BMI value to bmi public void setWeight( double newWeight) { weight = newWeight; bmi = weight/(height*height); } Natural Language Programming Language

16 16 O BJECT E DITOR U SER I NTERFACE ? 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; }

17 17 O BJECT E DITOR U SER I NTERFACES

18 18 S IMILARITIES IN THE T WO C LASSES 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; } 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); }

19 19 R EAL -W ORLD A NALOGY manufactures Corvette Specification implements manufactures

20 20 I NTERFACE

21 21 public class AnotherBMISpreadsheet implements BMISpreadsheet { double height, weight, bmi; public double getHeight() { return height; } public void setHeight ( double newHeight) { height = newHeight; bmi = weight/(height*height); } … I MPLEMENTING AN I NTERFACE Contract Parameter names never matter to Java

22 22 implements I NTERFACE ABMISpreadsheet BMISpreadsheet ABMISpreadsheet instance ABMISpreadsheet instance instance of AnotherBMISpreadsheet instance AnotherBMISpreadsheet instance instance of

23 23 implements U SING I NTERFACES TO C LASSIFY ABMISpreadsheet BMISpreadsheet instance BMISpreadsheet instance instance of AnotherBMISpreadsheet BMISpreadsheet instance BMISpreadsheet instance instance of

24 24 U SING C AR S PECIFICATIONS TO C LASSIFY manufactures Corvette Specification implements manufactures Corvette

25 25 C ANNOT I NSTANTIATE S PECIFICATION 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 26 I NTERFACE AS A S YNTACTIC S PECIFICATION 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); }

27 27 I NTERFACE AS A S YNTACTIC S PECIFICATION 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 28 I NTERFACE R EQUIRED Define interfaces for All classes (that are instantiated) Some are not Include all public methods

29 29 D IFFERENCES IN THE T WO C LASSES 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; } 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); }

30 30 ABMIS PREADSHEET VS. A NOTHER BMIS PREADSHEET 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 31 T IME -S PACE T RADEOFF Time Miser Space Miser Space Time

32 32 T IME -S PACE T RADEOFF Space Time Time Miser Space Miser

33 33 R ELATING I NTERFACE AND C LASS N AMES Class Name: - ABMISpreadsheet - ASpaceEfficientBMISpreadsheet - SpaceEfficientBMISpreadsheet - ABMISpreadsheet - ASpaceEfficientBMISpreadsheet - SpaceEfficientBMISpreadsheet Impl - BMISpreadsheetImpl - BMISpreadsheetSpaceEfficientImpl Impl - BMISpreadsheetImpl - BMISpreadsheetSpaceEfficientImpl Interface Name: Interface - ABMISpreadsheetInterface Interface - ABMISpreadsheetInterface

34 34 P ROGRAMMING S TYLE : T HE A RT OF P ROGRAMMING 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 35 W RITING S TYLE 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 36 P ROGRAMMING S TYLE : T HE A RT OF P ROGRAMMING 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 37 P ROGRAMMING S TYLE : T HE A RT OF P ROGRAMMING Use Interfaces Efficiency Comment Program Avoid Code Repetition Giving Least Privilege Use Named Constants

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

39 39 J AVA D OC C ONVENTIONS /* 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 40 System.out.println(newHeight); /*debugging statement */ R EMOVING D EBUGGING C ODE /* System.out.println(newHeight); /*debugging statement */ */ /* System.out.println(newHeight); // debugging statement */

41 41 W HAT TO C OMMENT ? 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 42 W HAT TO C OMMENT ? double w; // weight double weight; // weight double weight; double bmi; // computed by setWeight and setHeight Bad variable name Redundant Self-commenting Useful comment

43 43 J AVA D OC T AGS /* * @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

44 44 C OMMENTING I NTERFACES /* * @param newWeight the new value of the property, weight */ public void setWeight ( double newWeight) { } /* * @return the value of the variable, weight */ public double getWeight () { } Implementation independent comments Commenting both interface and implementation?

45 45 P ROGRAMMING S TYLE : T HE A RT OF P ROGRAMMING Use Interfaces Efficiency Comment Program Avoid Code Repetition Giving Least Privilege Use Named Constants

46 46 I MPROVING THE S TYLE 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 Assuming ABMICalculator does not exist

47 47 I MPROVING THE S TYLE (E DIT ) public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; … public void setHeight( double newHeight) { height = newHeight; bmi = calculateBMI(); } … public void setWeight( double newWeight) { weight = newWeight; bmi = calculateBMI(); } public double getBMI() { return bmi; } double calculateBMI() { return weight/(height*height); }

48 48 R E - USING C ODE 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/(height*height); } …. }

49 49 C HANGING R E - USED C ODE O NCE 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/100*height*2.54/100); } … } Declaring vs. calling methods?

50 50 P ROGRAMMING S TYLE : T HE A RT OF P ROGRAMMING Use Interfaces Efficiency Comment Program Avoid Code Repetition Giving Least Privilege Use Named Constants

51 51 O NLY P UBLIC M ETHODS IN I NTERFACE public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; public double getHeight() { return height; } public void setHeight( double newHeight) { height = newHeight; bmi = calculateBMI(); } … double calculateBMI() () { return (weight/2.2)/(height * 2.54/100*height*2.54/100); } … } Not in interface

52 52 P RINCIPLE OF L EAST P RIVILEGE Do not give a user of some code more rights than it needs Code is easier to change Need to learn less to use code Less likelihood of accidental or malicious damage to program Like hiding engine details from car driver ObjectEditor ABMICalculator User ABMICalculator getWeight() setWeight() getHeight() setHeight() getBMI() computeBMI()

53 53 I MPROVING THE S TYLE Use Interfaces Efficiency Comment Program Use Named Constants Avoid Code Repetition Giving Least Privilege

54 54 I MPROVING THE S TYLE public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; public double getHeight() { return height; } public void setHeight( double newHeight) { height = newHeight; bmi = calculateBMI(); } … double calculateBMI() () { return (weight/2.2)/(height * 2.54/100*height*2.54/100); } … } Magic numbers

55 55 I MPROVING THE S TYLE public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; public double getHeight() { return height; } public void setHeight( double newHeight) { height = newHeight; bmi = calculateBMI(); } … double calculateBMI() () { return (weight/LBS_IN_KG) / (height*CMS_IN_INCH/100*height*CMS_IN_INCH/100); } … } Named constants

56 56 D ECLARING N AMED C ONSTANTS public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi;... final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; … double calculateBMI() { return (weight/LBS_IN_KG) / (height*CMS_IN_INCH/100*height*CMS_IN_INCH/100); } … } Un-initializing declaration Initializing declaration

57 57 M ORE ON V ARIABLES VS. N AMED C ONSTANTS 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); } … }

58 58 A CCIDENTAL OR M ALICIOUS M ODIFICATION public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi;... double lbsInKg = 2.2; double cmsInInch = 2.54; … double calculateBMI() { lbsInKg = 22; return (weight/lbsInKg) / (height*cmsInInch/100*height*cmsInInch/100); } … } Violating least privilege

59 59 L ITERALS VS. N AMED C ONSTANTS VS. V ARIABLES Use constants for program values that do not change Use named constants for magic numbers

60 60 M ORE C ODE R EPETITION public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi;... final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; … double calculateBMI() { return (weight/LBS_IN_KG) / (height*CMS_IN_INCH/100*height*CMS_IN_INCH/100); } … } Within same method and has the same value

61 61 R EMOVING C ODE R EPETITION (E DIT ) public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi;... final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; … double calculateBMI() { double heightInMeters = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMeters*heightInMeters); } … }

62 62 R EMOVING C ODE R EPETITION public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi;... final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; … double calculateBMI() { double heightInMeters = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMeters*heightInMeters); } … }

63 63 L OCAL VS. G LOBAL V ARIABLE public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; double heightInMeters = height*CMS_IN_INCH/100;... final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; … double calculateBMI() { return (weight/LBS_IN_KG) / (heightInMeters*heightInMeters); } … }

64 64 L OCAL VS. G LOBAL V ARIABLE public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; double heightInMeters = height*CMS_IN_INCH/100;... final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; … public void setHeight( double newHeight) { heightInMeters = newHeight; bmi = calculateBMI(); } … double calculateBMI() { return (weight/LBS_IN_KG) / (heightInMeters*heightInMeters); } … } Violating least privilege

65 65 public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; … public void setHeight( double newHeight) { height = newHeight; bmi = calculateBMI(); } public double getWeight() { return weight; } public void setWeight( double newWeight) { weight = newWeight; bmi = weight/(height*height); } … double calculateBMI () { double heightInMetres = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } … } SCOPE heightInMeters scope Not a scope height scope

66 66 S COPE OF P UBLIC I TEMS public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; … public double getWeight() { return weight; } … } public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; … public double getWeight() { return weight; } … } ObjectEditor ABMISpreadsheet getWeight() scope

67 67 I DENTIFIER S COPE Region of code where the identifier is visible Arbitrary scopes not possible Least Privilege => Make scope as small as possible

68 68 S COPE public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; … public void setHeight( double newHeight) { height = newHeight; bmi = calculateBMI(); } public double getWeight() { return weight; } public void setWeight( double newWeight) { weight = newWeight; bmi = weight/(height*height); } … double calculateBMI() () { double heightInMetres = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } … } heightInMeters scope

69 69 I NITIALIZING D ECLARATION 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); } … }

70 70 U N - INITIALIZING D ECLARATION public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi; … final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; … double calculateBMI() () { double heightInMetres; heightInMeters = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } … }

71 71 U N - INITIALIZED V ARIABLE 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); } … }

72 72 I NITIALIZING A LL V ARIABLES 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); } … }

73 73 I NITIALIZING A LL V ARIABLES (E DIT ) public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height = 60, weight = 70, bmi = getBMI(); … final double LBS_IN_KG = 2.2; final double CMS_IN_INCH = 2.54; … double calculateBMI() () { double heightInMetres = height*CMS_IN_INCH/100; return (weight/LBS_IN_KG) / (heightInMetres*heightInMetres); } … }

74 74 I NITIALIZING A LL V ARIABLES 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); } … }

75 75 P RINTING P ROPERTIES 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); }

76 76 P RINTING P ROPERTIES (E DIT ) public class ABMISpreadsheet implements BMISpreadsheet { double height; … public void setHeight( double newHeight) { System.out.println(“Height: “ + newHeight); height = newHeight; } double weight; … public void setWeight( double newWeight) { weight = newWeight; } public double getBMI() { return weight/(height*height); }

77 77 P RINTING P ROPERTIES public class ABMISpreadsheet implements BMISpreadsheet { double height; … public void setHeight( double newHeight) { System.out.println(“Weight: “ + weight); System.out.println(“Height: “ + height); double bmi = getBMI(); System.out.println(“BMI: “ + bmi); height = newHeight; } double weight; … public void setWeight( double newWeight) { weight = newWeight; } public double getBMI() { return weight/(height*height); }

78 78 L ESS C LUTTERED C ODE (E DIT ) public class ABMISpreadsheet implements BMISpreadsheet { double height; … public void setHeight( double newHeight) { System.out.println(“Weight: “ + weight); System.out.println(“Height: “ + height); double bmi = getBMI(); System.out.println(“BMI: “ + bmi); height = newHeight; } double weight; … public void setWeight( double newWeight) { weight = newWeight; } public double getBMI() { return weight/(height*height); }

79 79 L ESS C LUTTERED C ODE 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/(height*height); }

80 80 R EMOVING D UPLICATION 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/(height*height); }

81 81 R EMOVING D UPLICATION (E DIT ) public class ABMISpreadsheet implements BMISpreadsheet { double height; … public void setHeight( double newHeight) { printProperties(); height = newHeight; } double weight; … public void setWeight( double newWeight) { printProperties(); weight = newWeight; } public double getBMI() { return weight/(height*height); } void printProperties() { System.out.println(“Weight: “ + weight); System.out.println(“Height: “ + height); System.out.println(“BMI: “ + getBMI()); }

82 82 R EMOVING D UPLICATION 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 83 S EPARATION OF C ONCERNS 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 84 I NDEPENDENT C ODE = M ETHOD 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 85 P RINTING BMI IN GET BMI() public class ABMISpreadsheet implements BMISpreadsheet { double height; … public double getBMI() { printProperties(); return weight/(height*height); } … void printProperties() { System.out.println(“Weight: “ + weight); System.out.println(“Height: “ + height); System.out.println(“BMI: “ + getBMI()); } getBMI() never terminates Recursive, calls itself indirectly Infinite recursion Avoid recursion for now

86 86 N ON - PUBLIC I NSTANCE V ARIABLES public class ABMISpreadsheet implements BMISpreadsheet { double height, weight, bmi; … }

87 87 M AKING I NSTANCE V ARIABLES P UBLIC public class ABMISpreadsheet implements BMISpreadsheet { public double height, weight, bmi; … } Other classes

88 88 H ARD TO C HANGE public class ABMISpreadsheet implements BMISpreadsheet { public double height, weight; … } Other classes

89 89 C ONSISTENCY C ONSTRAINTS V IOLATED Inconsistent values

90 90 P RECONDITIONS C ANNOT BE E NFORCED More on this later

91 91 E NCAPSULATION P RINCIPLE Do not make instance variables public Expose them through public methods

92 92 public final double CMS_IN_INCH = 2.54; P UBLIC C ONSTANTS public interface BMISpreadsheet { public final double CMS_IN_INCH = 2.54; } Inconsistent value cannot be stored Implementation independent ABMISpreadsheet AnotherBMISpreadsheet

93 93 P RINCIPLE Declare implementation-independent named constants in interfaces implementing classes can access them

94 94

95 95 E XTRA S LIDES

96 96 S TEPWISE R EFINEMENT Declare two instance variables weight and height Define a getter method that computes the value of BMI and returns it public double getBMI() { return weight/(height*height); } Natural Language Programming Language

97 97 R EAL -W ORLD A NALOGY manufactures Corvette Specification implements manufactures


Download ppt "C OMP 110 S TYLE Instructor: Jason Carter. 2 I NTERFACES AND M ORE S TYLE Define contracts between our users and implementers Optional – they may not."

Similar presentations


Ads by Google