comp 110 style n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Comp 110 Style PowerPoint Presentation
Download Presentation
Comp 110 Style

play fullscreen
1 / 97

Comp 110 Style

170 Views Download Presentation
Download Presentation

Comp 110 Style

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Comp 110Style Instructor: Jason Carter

  2. 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

  3. 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

  4. BMI Spreadsheet

  5. 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

  6. 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

  7. Methods that Change ABMISpreadsheet Instance weight height bmi writes reads writes setWeight() setHeight() getBMI() calls calls new weight new height ObjectEditor

  8. setWeight() ABMISpreadsheet Instance weight bmi writes setWeight() publicvoidsetWeight(doublenewWeight) { weight = newWeight; bmi = weight / (height*height); } calls new weight ObjectEditor

  9. setHeight() ABMISpreadsheet Instance height bmi writes setHeight() publicvoidsetHeight(doublenewHeight) { height = newHeight; bmi = weight / (height*height); } calls new height ObjectEditor

  10. getBMI() ABMISpreadsheet Instance bmi reads getBMI() publicdoublegetBMI() { return bmi; } ObjectEditor

  11. 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; } }

  12. 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

  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. 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); }

  16. 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; } }

  17. ObjectEditor User Interfaces

  18. 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); } }

  19. Real-World Analogy implements manufactures Corvette Specification implements manufactures

  20. Interface

  21. 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

  22. Interface ABMISpreadsheet implements instance of BMISpreadsheet ABMISpreadsheet instance ABMISpreadsheet instance implements AnotherBMISpreadsheet instance of AnotherBMISpreadsheet instance AnotherBMISpreadsheet instance

  23. Using Interfaces to Classify ABMISpreadsheet implements instance of BMISpreadsheet BMISpreadsheet instance BMISpreadsheet instance implements AnotherBMISpreadsheet instance of BMISpreadsheet instance BMISpreadsheet instance

  24. Using Car Specifications to Classify Corvette implements manufactures Corvette Corvette Specification implements Corvette manufactures Corvette

  25. 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

  26. 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); } }

  27. 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

  28. Interface Required • Define interfaces for • All classes (that are instantiated) • Some are not • Include all public methods

  29. 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); } }

  30. 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

  31. Time Miser Space Miser Time-Space Tradeoff Time Space

  32. Time-Space Tradeoff Space Time Time Miser Space Miser

  33. Relating Interface and Class Names Class Name: • <Qualifier><Interface> • - ABMISpreadsheet • - ASpaceEfficientBMISpreadsheet • - SpaceEfficientBMISpreadsheet • <Interface><Qualifier> Impl • - BMISpreadsheetImpl • - BMISpreadsheetSpaceEfficientImpl Interface Name: • <ClassName>Interface • - ABMISpreadsheetInterface

  34. 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

  35. 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

  36. 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

  37. Programming Style: The Art of Programming  Use Interfaces  Efficiency Comment Program Avoid Code Repetition Giving Least Privilege Use Named Constants

  38. 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);

  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 {…} You should use this convention

  40. Removing Debugging Code • System.out.println(newHeight); /*debugging statement */ • /* • System.out.println(newHeight); /*debugging statement */ • */ • /* • System.out.println(newHeight); // debugging statement • */

  41. 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?

  42. What to Comment? doublew; // weight Bad variable name doubleweight; // weight Redundant doubleweight; Self-commenting doublebmi; // computed by setWeight and setHeight Useful comment

  43. 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 () { • … • }

  44. 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?

  45. Programming Style: The Art of Programming  Use Interfaces  Efficiency  Comment Program Avoid Code Repetition Giving Least Privilege Use Named Constants

  46. 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

  47. 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); } }

  48. 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); } …. }

  49. 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?

  50. Programming Style: The Art of Programming  Use Interfaces  Efficiency  Comment Program  Avoid Code Repetition Giving Least Privilege Use Named Constants