970 likes | 1.17k Views
Comp 110 Style. Instructor: Jason Carter. Interfaces and More Style. Define contracts between our users and implementers Optional – they may not be used Good style to use them Will study additional elements of style in this chapter. Two Ways of Doing the BMI Spreadsheet.
E N D
Comp 110Style Instructor: Jason Carter
Interfaces and More Style • Define contracts between our users and implementers • Optional – they may not be used • Good style to use them • Will study additional elements of style in this chapter
Two Ways of Doing the BMI Spreadsheet • ABMISpreadsheet is one way to implement the spreadsheet user-interface • Let us create AnotherBMISpreadsheet to illustrate another way • Difference is in number of variables used
ABMISpreadsheet ABMISpreadsheet Instance weight height reads writes reads writes reads getWeight() setWeight() getHeight() setHeight() getBMI() calls calls calls calls new weight new height weight height ObjectEditor
AnotherBMISpreadsheet AnotherBMISpreadsheet Instance weight height bmi reads writes reads reads writes getWeight() setWeight() getHeight() setHeight() getBMI() calls calls calls calls new weight new height weight height ObjectEditor
Methods that Change ABMISpreadsheet Instance weight height bmi writes reads writes setWeight() setHeight() getBMI() calls calls new weight new height ObjectEditor
setWeight() ABMISpreadsheet Instance weight bmi writes setWeight() publicvoidsetWeight(doublenewWeight) { weight = newWeight; bmi = weight / (height*height); } calls new weight ObjectEditor
setHeight() ABMISpreadsheet Instance height bmi writes setHeight() publicvoidsetHeight(doublenewHeight) { height = newHeight; bmi = weight / (height*height); } calls new height ObjectEditor
getBMI() ABMISpreadsheet Instance bmi reads getBMI() publicdoublegetBMI() { return bmi; } ObjectEditor
Complete Code publicclassAnotherBMISpreadsheet { double height, weight, bmi; publicdoublegetHeight() { return height; } publicvoidsetHeight(doublenewHeight) { height = newHeight; bmi = weight/(height*height); } publicdoublegetWeight() { return weight; } publicvoidsetWeight(doublenewWeight) { weight = newWeight; bmi = weight/(height*height); } publicdoublegetBMI() { returnbmi; } }
Graphical Algorithm ABMISpreadsheet Instance weight height bmi reads writes reads reads writes getWeight() setWeight() getHeight() setHeight() getBMI() calls calls calls calls new weight new height weight height ObjectEditor
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
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
Stepwise Refinement • 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 Natural Language Programming Language publicvoidsetWeight(doublenewWeight) { weight = newWeight; bmi = weight/(height*height); }
ObjectEditor User Interface? publicclassAnotherBMISpreadsheet { double height, weight, bmi; publicdoublegetHeight() { return height; } publicvoidsetHeight(doublenewHeight) { height = newHeight; bmi = weight/(height*height); } publicdoublegetWeight() { return weight; } publicvoidsetWeight(doublenewWeight) { weight = newWeight; bmi = weight/(height*height); } publicdoublegetBMI() { returnbmi; } }
Similarities in the Two Classes publicclassAnotherBMISpreadsheet { double height, weight, bmi; publicdoublegetHeight() { return height; } publicvoidsetHeight(double newHeight) { height = newHeight; bmi = weight/(height*height); } publicdoublegetWeight() { return weight; } publicvoidsetWeight(double newWeight) { weight = newWeight; bmi = weight/(height*height); } publicdoublegetBMI() { returnbmi; } } publicclassABMISpreadsheet { double height; publicdoublegetHeight() { return height; } publicvoidsetHeight(double newHeight) { height = newHeight; } double weight; publicdoublegetWeight() { return weight; } publicvoidsetWeight(double newWeight) { weight = newWeight; } publicdoublegetBMI() { return weight/(height*height); } }
Real-World Analogy implements manufactures Corvette Specification implements manufactures
Implementing an Interface Contract publicclassAnotherBMISpreadsheetimplementsBMISpreadsheet { double height, weight, bmi; publicdoublegetHeight() { return height; } publicvoidsetHeight (doublenewHeight) { height = newHeight; bmi = weight/(height*height); } … Parameter names never matter to Java
Interface ABMISpreadsheet implements instance of BMISpreadsheet ABMISpreadsheet instance ABMISpreadsheet instance implements AnotherBMISpreadsheet instance of AnotherBMISpreadsheet instance AnotherBMISpreadsheet instance
Using Interfaces to Classify ABMISpreadsheet implements instance of BMISpreadsheet BMISpreadsheet instance BMISpreadsheet instance implements AnotherBMISpreadsheet instance of BMISpreadsheet instance BMISpreadsheet instance
Using Car Specifications to Classify Corvette implements manufactures Corvette Corvette Specification implements Corvette manufactures Corvette
Cannot Instantiate Specification • Cannot order a car from a specification • Must order from factory • A car defined by Corvette specification ordered from factory implementing the specification • Cannot instantiate interface • Must instantiate class • BMISpreadsheet instance created by instantiating class implementing interface
Interface as a Syntactic Specification publicclassABMISpreadsheetimplementsBMISpreadsheet{ double height; publicdoublegetHeight() { return height; } publicvoidsetHeight(double newHeight) { height = newHeight; } double weight; publicdoublegetWeight() { return weight; } publicvoidsetWeight(doublenewWeight) { weight = newWeight; } publicdoublegetBMI() { return weight/(height*height); } }
Interface as a Syntactic Specification publicclassABMISpreadsheetimplementsBMISpreadsheet{ double height; publicdoublegetHeight() { return height; } publicvoidsetHeight(double newHeight) { height = newHeight; } double weight; publicdoublegetWeight() { return weight; } publicvoidsetWeight(doublenewWeight) { weight = newWeight; } publicdoublegetBMI() { return 13450; } } Syntactic Contract Bombay Market Index
Interface Required • Define interfaces for • All classes (that are instantiated) • Some are not • Include all public methods
Differences in the Two Classes publicclassAnotherBMISpreadsheet { double height, weight, bmi; publicdoublegetHeight() { return height; } publicvoidsetHeight(double newHeight) { height = newHeight; bmi = weight/(height*height); } publicdoublegetWeight() { return weight; } publicvoidsetWeight(double newWeight) { weight = newWeight; bmi = weight/(height*height); } publicdoublegetBMI() { returnbmi; } } publicclassABMISpreadsheet { double height; publicdoublegetHeight() { return height; } publicvoidsetHeight(double newHeight) { height = newHeight; } double weight; publicdoublegetWeight() { return weight; } publicvoidsetWeight(double newWeight) { weight = newWeight; } publicdoublegetBMI() { return weight/(height*height); } }
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
Time Miser Space Miser Time-Space Tradeoff Time Space
Time-Space Tradeoff Space Time Time Miser Space Miser
Relating Interface and Class Names Class Name: • <Qualifier><Interface> • - ABMISpreadsheet • - ASpaceEfficientBMISpreadsheet • - SpaceEfficientBMISpreadsheet • <Interface><Qualifier> Impl • - BMISpreadsheetImpl • - BMISpreadsheetSpaceEfficientImpl Interface Name: • <ClassName>Interface • - ABMISpreadsheetInterface
Programming Style: The Art of Programming • Science of programming • Does the program work correctly? • Art programming • Does the program follow “good” style rules? • Good style rules make it easier to • Get the program working correctly • Change the program
Writing Style • To the point • Avoid repetition • Good flow • Illustrate ideas There is more than one way to write a document that conveys some facts: e.g. the influence of BMI on health
Programming Style: The Art of Programming • Define interfaces • Make programs efficient • Space vs. time • Comment program • Use appropriate identifier names • Use named constants • Variables vs. names • Constants vs. magic numbers • Avoid code repetition • Give least privilege • No public instance variables • Implementation independent constants in interfaces • Initialize variables • Independent code in separate method There is more than one way to write a program that correctly meets its specification: e.g. implement the BMI user-interface
Programming Style: The Art of Programming Use Interfaces Efficiency Comment Program Avoid Code Repetition Giving Least Privilege Use Named Constants
Comments Single line comments doublebmi; //computed by setWeight and setHeight Arbitrary comments • /* This version recalculates the bmi • when weight or height change, not when • getBMI is called • */ • public class AnotherBMISpreadsheet {…} • /* recompute dependent properties */ • bmi = weight / (height * height);
JavaDoc Conventions • /* This version recalculates the bmi • when weight or height change, not when • getBMI is called • */ • public class AnotherBMISpreadsheet {…} • /* This version recalculates the bmi • * when weight or height change, not when • * getBMI is called • */ • public class AnotherBMISpreadsheet {…} You should use this convention
Removing Debugging Code • System.out.println(newHeight); /*debugging statement */ • /* • System.out.println(newHeight); /*debugging statement */ • */ • /* • System.out.println(newHeight); // debugging statement • */
What to Comment? • Any code fragment needing explanation • Class • Top-level algorithm, author, date modified • Variable declaration • Purpose, where used, how its value is computed • Method declaration • params, return value, algorithm, author, date modified • Statement sequence • Explanation • Debugging code Summarizing vs. Elaborating Comments?
What to Comment? doublew; // weight Bad variable name doubleweight; // weight Redundant doubleweight; Self-commenting doublebmi; // computed by setWeight and setHeight Useful comment
JavaDoc Tags JavaDoc tags • /* • * @author Prasun Dewan • * @paramnewWeight 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 () { • … • }
Commenting Interfaces Implementation independent comments • /* • * @paramnewWeight the new value of the property, weight • */ • public void setWeight (double newWeight) { } • /* • * @return the value of the variable, weight • */ • public double getWeight () { } Commenting both interface and implementation?
Programming Style: The Art of Programming Use Interfaces Efficiency Comment Program Avoid Code Repetition Giving Least Privilege Use Named Constants
Improving the Style publicclassAnotherBMISpreadsheetimplementsBMISpreadsheet{ double height, weight, bmi; publicdoublegetHeight() { return height; } publicvoidsetHeight(doublenewHeight) { height = newHeight; bmi = weight/(height*height); } publicdoublegetWeight() { return weight; } publicvoidsetWeight(doublenewWeight) { weight = newWeight; bmi = weight/(height*height); } publicdoublegetBMI() { returnbmi; } } Code repetition Assuming ABMICalculator does not exist
Improving the Style (Edit) publicclassAnotherBMISpreadsheetimplementsBMISpreadsheet{ double height, weight, bmi; … publicvoidsetHeight(doublenewHeight) { height = newHeight; bmi = calculateBMI(); } … publicvoidsetWeight(doublenewWeight) { weight = newWeight; bmi = calculateBMI(); } publicdoublegetBMI() { returnbmi; } double calculateBMI() { return weight/(height*height); } }
Re-using Code publicclassAnotherBMISpreadsheetimplementsBMISpreadsheet{ double height, weight, bmi; publicdoublegetHeight() { return height; } publicvoidsetHeight(doublenewHeight) { height = newHeight; bmi = calculateBMI(); } publicdoublegetWeight() { return weight; } publicvoidsetWeight(doublenewWeight) { weight = newWeight; bmi = calculateBMI(); } doublecalculateBMI() { return weight/(height*height); } …. }
Changing Re-used Code Once publicclassAnotherBMISpreadsheetimplementsBMISpreadsheet{ double height, weight, bmi; … publicvoidsetHeight(doublenewHeight) { height = newHeight; bmi = calculateBMI(); } publicdoublegetWeight() { return weight; } publicvoidsetWeight(doublenewWeight) { weight = newWeight; bmi = calculateBMI(); } doublecalculateBMI() { return (weight/2.2)/(height * 2.54/100*height*2.54/100); } … } Declaring vs. calling methods?
Programming Style: The Art of Programming Use Interfaces Efficiency Comment Program Avoid Code Repetition Giving Least Privilege Use Named Constants