Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... ·...

71
1 15-214 School of Computer Science Principles of Software Construction: Objects, Design, and Concurrency API Design Christian Kaestner Bogdan Vasilescu Many slides stolen with permission from Josh Bloch (thanks!)

Transcript of Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... ·...

Page 1: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

115-214

SchoolofComputerScience

PrinciplesofSoftwareConstruction:Objects,Design,andConcurrencyAPIDesign

ChristianKaestner BogdanVasilescu

ManyslidesstolenwithpermissionfromJoshBloch(thanks!)

Page 2: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

215-214

Administrivia

• Homework4cduetonight• Homework4bfeedbackavailablesoon• Homework5releasedtomorrow–Workinteams

Page 3: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

315-214

Part1:DesignataClassLevel

DesignforChange:InformationHiding,

Contracts,DesignPatterns,UnitTesting

DesignforReuse:Inheritance,Delegation,

Immutability,LSP,DesignPatterns

Part2:Designing(Sub)systems

UnderstandingtheProblem

ResponsibilityAssignment,DesignPatterns,GUIvsCore,

DesignCaseStudies

TestingSubsystems

DesignforReuseatScale:FrameworksandAPIs

Part3:DesigningConcurrent

Systems

ConcurrencyPrimitives,Synchronization

DesigningAbstractionsforConcurrency

DistributedSystemsinaNutshell

IntrotoJava

Git,CIStaticAnalysis

GUIsUML MoreGit

GUIsPerformance

Design

Page 4: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

415-214

Agenda

• IntroductiontoAPIs:ApplicationProgrammingInterfaces

• AnAPIdesignprocess• Keydesignprinciple:Informationhiding• Concreteadviceforuser-centereddesign

Page 5: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

515-214

Learninggoals• UnderstandandbeabletodiscussthesimilaritiesanddifferencesbetweenAPIdesignandregularsoftwaredesign– Relationshipbetweenlibraries,frameworksandAPIdesign

– Informationhidingasakeydesignprinciple• Acknowledge,andplanforfailuresasafundamentallimitationonadesignprocess

• Givenaproblemdomainwithusecases,beabletoplanacoherentdesignprocessforanAPIforthoseusecases,e.g.,"RuleofThrees"

Page 6: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

615-214

API:ApplicationProgrammingInterface

• AnAPIdefinestheboundarybetweencomponents/modulesinaprogrammaticsystem

Page 7: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

715-214

API:ApplicationProgrammingInterface

• AnAPIdefinestheboundarybetweencomponents/modulesinaprogrammaticsystem

Page 8: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

815-214

API:ApplicationProgrammingInterface

• AnAPIdefinestheboundarybetweencomponents/modulesinaprogrammaticsystem

Page 9: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

915-214

API:ApplicationProgrammingInterface

• AnAPIdefinestheboundarybetweencomponents/modulesinaprogrammaticsystem

Page 10: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

1015-214

LibrariesandframeworksbothdefineAPIs

Library

Framework

public MyWidget extends JContainer {

ublic MyWidget(int param) {/ setup internals, without rendering}

/ render component on first view and resizingprotected void paintComponent(Graphics g) {// draw a red box on his componentDimension d = getSize();g.setColor(Color.red);

g.drawRect(0, 0, d.getWidth(), d.getHeight()); }}

public MyWidget extends JContainer {

ublic MyWidget(int param) {/ setup internals, without rendering}

/ render component on first view and resizingprotected void paintComponent(Graphics g) {// draw a red box on his componentDimension d = getSize();g.setColor(Color.red);

g.drawRect(0, 0, d.getWidth(), d.getHeight()); }}

your code

your code

API

API

Page 11: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

1115-214

WhyisAPIdesignimportant?

• APIscanbeamongyourgreatestassets– Usersinvestheavily:acquiring,writing,learning– Costtostop usinganAPIcanbeprohibitive– SuccessfulpublicAPIscaptureusers

• Canalsobeamongyourgreatestliabilities– BadAPIcancauseunendingstreamofsupportcalls– Caninhibitabilitytomoveforward

Page 12: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

1215-214

PublicAPIsareforever

Yourcode

Yourcolleague

Anothercolleague

SomebodyonthewebSomebodyonthewebSomebodyonthewebSomebodyonthewebSomebodyonthewebSomebodyonthewebSomebodyonthewebSomebodyontheweb

Page 13: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

1315-214

PublicAPIsareforever

Eclipse(IBM)

JDTPlugin(IBM)

CDTPlugin(IBM)

UMLPlugin(thirdparty)

SomebodyonthewebSomebodyonthewebSomebodyonthewebSomebodyonthewebSomebodyonthewebSomebodyonthewebthirdpartyplugin

Page 14: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

1415-214

Evolutionaryproblems:Public(used)APIsareforever

• "Onechancetogetitright"• Canonlyaddfeaturestolibrary• Cannot:– removemethodfromlibrary– changecontractinlibrary– changeplugininterfaceofframework

• DeprecationofAPIsasweakworkaround

awt.Component,deprecated since Java 1.1still included in 7.0

Page 15: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

1515-214

GoodvsBadAPIs

• Lotsofreuse– includingfromyourself

• Lotsofusers/customers• Userbuy-inandlock-in

• Lostproductivity,inefficientreuse

• Maintenanceandcustomersupportliability

Page 16: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

1615-214

CharacteristicsofagoodAPI

• Easytolearn• Easytouse,evenwithoutdocumentation• Hardtomisuse• Easytoreadandmaintaincodethatusesit• Sufficientlypowerfultosatisfyrequirements• Easytoevolve• Appropriatetoaudience

Page 17: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

1715-214

Outlinefortoday

• TheProcessofAPIDesign• Keydesignprinciple:Informationhiding• Concreteadviceforuser-centereddesign

Page 18: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

1815-214

AnAPIdesignprocess

• DefinethescopeoftheAPI– Collectuse-casestories,definerequirements– Beskeptical

• Distinguishtruerequirementsfromso-calledsolutions• "Whenindoubt,leaveitout."

• Draftaspecification,gatherfeedback,revise,andrepeat– Keepitsimple,short

• Codeearly,codeoften– Writeclientcode beforeyouimplementtheAPI

Page 19: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

1915-214

PlanwithUseCases

• ThinkabouthowtheAPImightbeused?– e.g.,getthecurrenttime,computethedifferencebetweentwotimes,getthecurrenttimeinTokyo,getnextweek'sdateusingaMayacalendar,…

• Whattasksshoulditaccomplish?• Shouldallthetasksbesupported?– Ifindoubt,leaveitout!

• HowwouldyousolvethetaskswiththeAPI?

Page 20: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

2015-214

Respecttheruleofthree

• ViaWillTracz (viaJoshBloch),ConfessionsofaUsedProgramSalesman:

Write3implementationsofeachabstractclassorinterfacebeforerelease

– "Ifyouwriteone,itprobablywon'tsupportanother."

– "Ifyouwritetwo,itwillsupportmorewithdifficulty."

– "Ifyouwritethree,itwillworkfine."

Page 21: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

2115-214

Outline

• TheProcessofAPIDesign• Keydesignprinciple:Informationhiding• Concreteadviceforuser-centereddesign

Page 22: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

2215-214

Whichonedoyouprefer?public class Point {

public double x;public double y;

}vs.

public class Point {private double x;private double y;public double getX() { /* … */ }public double getY() { /* … */ }

}

Page 23: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

2315-214

Keydesignprinciple:Informationhiding

• "Whenindoubt,leaveitout.”

• ImplementationdetailsinAPIsareharmful– Confuseusers– Inhibitfreedomtochangeimplementation

Page 24: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

2415-214

Keydesignprinciple:Informationhiding

• Makeclasses,membersasprivateaspossible– Youcanaddfeatures,butneverremoveorchangethebehavioralcontractforanexistingfeature

• Publicclassesshouldhavenopublicfields(withtheexceptionofconstants)

• Minimizecoupling– Allowsmodulestobe,understood,used,built,tested,debugged,andoptimizedindependently

Page 25: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

2515-214

ApplyingInformationHiding:Fieldsvs Getter/SetterFunctions

public class Point {public double x;public double y;

}vs.

public class Point {private double x;private double y;public double getX() { /* … */ }public double getY() { /* … */ }

}

Page 26: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

2615-214

Whichonedoyouprefer?

public class Rectangle {public Rectangle(Point e, Point f) …

}

vs.public class Rectangle {public Rectangle(PolarPoint e, PolarPoint f) …

}

Page 27: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

2715-214

ApplyingInformationhiding:Interfacevs.ClassTypes

public class Rectangle {public Rectangle(Point e, Point f) …

}

vs.public class Rectangle {public Rectangle(PolarPoint e, PolarPoint f) …

}

Page 28: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

2815-214

Still…

public class Rectangle {public Rectangle(Point e, Point f) …

}…Point p1 = new PolarPoint(…);Point p2 = new PolarPoint(…);Rectangle r = new Rectangle(p1, p2);

Page 29: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

2915-214

Still…

public class Rectangle {public Rectangle(Point e, Point f) …

}…Point p1 = new PolarPoint(…);Point p2 = new PolarPoint(…);Rectangle r = new Rectangle(p1, p2);…Point p3 = new PolarPoint(…);Point p4 = new PolarPoint(…);Rectangle r2 = new Rectangle(p3, p4);…

Page 30: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

3015-214

public class Rectangle {public Rectangle(Point e, Point f) …

}…Point p1 = PointFactory.Construct(…); // new PolarPoint(…); insidePoint p2 = PointFactory.Construct(…); // new PolarPoint(…); insideRectangle r = new Rectangle(p1, p2);

ApplyingInformationhiding:Factories

Page 31: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

3115-214

ApplyingInformationhiding:Factories

• Considerimplementingafactorymethodinsteadofaconstructor

• Factorymethodsprovideadditionalflexibility– Canbeoverridden– Canreturninstanceofanysubtype;hidesdynamictypeofobject

– Canhaveadescriptivemethodname

Page 32: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

3215-214

ApplyingInformationHiding:HideClientBoilerplateCode• Generallydoneviacut-and-paste• Ugly,annoying,anderror-prone

import org.w3c.dom.*;import java.io.*;import javax.xml.transform.*;import javax.xml.transform.dom.*;import javax.xml.transform.stream.*;

/** DOM code to write an XML document to a specified output stream. */static final void writeDoc(Document doc, OutputStream out)throws IOException{

try {Transformer t = TransformerFactory.newInstance().newTransformer();t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, doc.getDoctype().getSystemId());t.transform(new DOMSource(doc), new StreamResult(out)); // Does actual writing

} catch(TransformerException e) {throw new AssertionError(e); // Can’t happen!

}}

Page 33: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

3315-214

TheexceptionhierarchyinJava

Throwable

Exception

RuntimeException IOException

EOFException

FileNotFoundException

NullPointerException

IndexOutOfBoundsException

ClassNotFoundException… …

. . .

Object

Recall unchecked

checked

Page 34: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

3415-214

ApplyingInformationHiding:HideClientBoilerplateCode• Generallydoneviacut-and-paste• Ugly,annoying,anderror-prone

import org.w3c.dom.*;import java.io.*;import javax.xml.transform.*;import javax.xml.transform.dom.*;import javax.xml.transform.stream.*;

/** DOM code to write an XML document to a specified output stream. */static final void writeDoc(Document doc, OutputStream out)throws IOException{

try {Transformer t = TransformerFactory.newInstance().newTransformer();t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, doc.getDoctype().getSystemId());t.transform(new DOMSource(doc), new StreamResult(out)); // Does actual writing

} catch(TransformerException e) {throw new AssertionError(e); // Can’t happen!

}}

Won’t compile

Page 35: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

3515-214

ApplyingInformationHiding:HideInformationDetails• Subtleleaksofimplementationdetailsthrough– Documentation• E.g.,donotspecifyhashfunctions

– Implementation-specificreturntypes/exceptions• E.g.,PhonenumberAPIthatthrowsSQLexceptions

– Outputformats• E.g.,implements Serializable

• Lackofdocumentationà Implementationbecomesspecificationà nohiding

Page 36: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

3615-214

Outline

• TheProcessofAPIDesign• Keydesignprinciple:Informationhiding• Concreteadviceforuser-centereddesign

Page 37: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

3715-214

Applyprinciplesofuser-centereddesign

• e.g.,"PrinciplesofUniversalDesign"– Equitableuse

• Designisusefulandmarketabletopeoplewithdiverseabilities– Flexibilityinuse

• Designaccommodatesawiderangeofindividualpreferences– Simpleandintuitiveuse

• Useofthedesigniseasytounderstand– Perceptibleinformation

• Designcommunicatesnecessaryinformationeffectivelytouser– Toleranceforerror– Lowphysicaleffort– Sizeandspaceforapproachanduse

Page 38: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

3815-214

Achievingflexibilityinusewhileremainingsimpleandintuitive:Minimizeconceptualweight

• APIshouldbeassmallaspossiblebutnosmaller–Whenindoubt,leaveitout

• Conceptualweight:HowmanyconceptsmustaprogrammerlearntouseyourAPI?– APIsshouldhavea"highpower-to-weightratio"

• Goodexamples:– java.util.*– java.util.Collections

Page 39: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

3915-214

What’swronghere?

public class Thread implements Runnable {// Tests whether current thread has been interrupted.// Clears the interrupted status of current thread.public static boolean interrupted();

}

Page 40: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

4015-214

Unintuitivebehavior:sideeffects

• UserofAPIshouldnotbesurprisedbybehavior,aka“theprincipleofleastastonishment”– It'sworthextraimplementationeffort– It'sevenworthreducedperformance

public class Thread implements Runnable {

// Tests whether current thread has been interrupted.// Clears the interrupted status of current thread.public static boolean interrupted();

}

Page 41: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

4115-214

• Dowhatyousayyoudo:– "Don'tviolatethePrincipleofLeastAstonishment"

Goodnamesdrivegooddesign

Page 42: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

4215-214

Discussthesenames

– get_x()vs getX()– Timervs timer– isEnabled()vs.enabled()– computeX()vs.generateX()?– deleteX()vs.removeX()?

Page 43: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

4315-214

Goodnamesdrivegooddesign(2)

• Followlanguage- andplatform-dependentconventions,e.g.,– Typographical:

• get_x() vs. getX()• timer vs. Timer, HTTPServlet vs HttpServlet• edu.cmu.cs.cs214

– Gramatical (seenext)

Page 44: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

4415-214

Goodnamesdrivegooddesign(3)

• Nounsforclasses– BigInteger,PriorityQueue

• Nounsoradjectivesforinterfaces– Collection,Comparable

• Nouns,linkingverbsorprepositionsfornon-mutativemethods– size,isEmpty,plus

• Actionverbsformutativemethods– put,add,clear

Page 45: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

4515-214

Goodnamesdrivegooddesign(4)• Useclear,specificnamingconventions– getX() andsetX() forsimpleaccessors andmutators

– isX() forsimpleboolean accessors– computeX() formethodsthatperformcomputation– createX() ornewInstance() forfactorymethods– toX() formethodsthatconvertthetypeofanobject– asX() forwrapperoftheunderlyingobject

Page 46: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

4615-214

Goodnamesdrivegooddesign(5)

• Beconsistent– computeX()vs.generateX()?– deleteX()vs.removeX()?

• Avoidcrypticabbreviations– Good:Font,Set,PrivateKey,Lock,ThreadFactory,TimeUnit,Future<T>

– Bad:DynAnyFactoryOperations,_BindingIteratorImplBase,ENCODING_CDR_ENCAPS,OMGVMCID

Page 47: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

4715-214

DonotviolateLiskov'sbehavioralsubtypingrules

• Useinheritanceonlyfortruesubtypes• Examples:

1) class Stack extends Vector …

2) // A Properties instance maps Strings to Stringspublic class Properties extends HashTable {

public Object put(Object key, Object value);…

}

Page 48: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

4815-214

Favorcompositionoverinheritance

// A Properties instance maps Strings to Stringspublic class Properties extends HashTable {

public Object put(Object key, Object value);…

}

public class Properties {private final HashTable data = new HashTable();public String put(String key, String value) {

data.put(key, value);}…

}

Page 49: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

4915-214

Minimizemutability

• Classesshouldbeimmutableunlessthere’sagoodreasontodootherwise– Advantages:simple,thread-safe,reusable– Disadvantage:separateobjectforeachvalue

Bad:Date,CalendarGood:TimerTask

Page 50: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

5015-214

• Component.getSize() returnsDimension• Dimension ismutable• EachgetSize callmustallocateDimension • Causesmillionsofneedlessobjectallocations• AlternativeaddedinJava1.2butoldclientcodestillslow:getX(),getY()

• Documentmutability– Carefullydescribestatespace–Makeclearwhenit'slegaltocallwhichmethod

Mutabilityandperformance

Page 51: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

5115-214

Overloadmethodnamesjudiciously• Avoidambiguousoverloadsforsubtypes

– Recallthesubtletiesofmethoddispatch:public class Point() {

private int x;private int y;public boolean equals(Point p) {

return this.x == p.x && this.y == p.y;}

}• Ifyoumustbeambiguous,implementconsistentbehavior

public class TreeSet implements SortedSet {public TreeSet(Collection c); // Ignores order.public TreeSet(SortedSet s); // Respects order.

}

Page 52: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

5215-214

Useappropriateparameter&returntypes

• Favorinterfacetypesoverclassesforinput– Providesflexibility,performance

• Usemostspecificpossibleinputparametertype– Moveserrorfromruntimetocompiletime

• Don'tuseString ifabettertypeexists– Stringsarecumbersome,error-prone,andslow

• Don'tusefloatingpointformonetaryvalues– Binaryfloatingpointcausesinexactresults!

• Usedouble (64bits)ratherthanfloat (32bits)– Precisionlossisreal,performancelossnegligible

Page 53: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

5315-214

Useconsistentparameterordering

• AnegregiousexamplefromC:– char* strncpy(char* dest, char* src, size_t n);– void bcopy(void* src, void* dest, size_t n);

Page 54: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

5415-214

Useconsistentparameterordering

• AnegregiousexamplefromC:– char* strncpy(char* dest, char* src, size_t n);– void bcopy(void* src, void* dest, size_t n);

• Somegoodexamples:java.util.Collections – firstparameteralwayscollectiontobemodifiedorqueried

java.util.concurrent – timealwaysspecifiedaslongdelay,TimeUnit unit

Page 55: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

5515-214

Avoidlonglistsofparameters• Especiallywithrepeatedparametersofthesametype

HWND CreateWindow(LPCTSTR lpClassName, LPCTSTR lpWindowName,DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam);

• Longlistsofidenticallytypedparams harmful– Programmerstransposeparametersbymistake– Programsstillcompileandrun,butmisbehave!

• Threeorfewerparametersisideal• Techniquesforshorteningparameterlists– Breakupmethod– Createhelperclasstoholdparameters– BuilderPattern

Page 56: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

5615-214

What’swronghere?

// A Properties instance maps Strings to Stringspublic class Properties extends HashTable {

public Object put(Object key, Object value);

// Throws ClassCastException if this instance// contains any keys or values that are not Stringspublic void save(OutputStream out, String comments);

}

Page 57: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

5715-214

Failfast• Reporterrorsassoonastheyaredetectable– Checkpreconditionsatthebeginningofeachmethod– Avoiddynamictypecasts,run-timetype-checking

// A Properties instance maps Strings to Stringspublic class Properties extends HashTable {

public Object put(Object key, Object value);

// Throws ClassCastException if this instance// contains any keys or values that are not Stringspublic void save(OutputStream out, String comments);

}

Page 58: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

5815-214

Throwexceptionstoindicateexceptionalconditions• Don’tforceclienttouseexceptionsforcontrolflow

private byte[] a = new byte[CHUNK_SIZE];

void processBuffer (ByteBuffer buf) {try {

while (true) {buf.get(a);processBytes(a, CHUNK_SIZE);

}} catch (BufferUnderflowException e) {

int remaining = buf.remaining();buf.get(a, 0, remaining);processBytes(a, remaining);

}}

• Conversely,don’tfailsilentlyThreadGroup.enumerate(Thread[] list)

Page 59: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

5915-214

Avoidcheckedexceptionsifpossible

• Overuseofcheckedexceptionscausesboilerplatetry {

Foo f = (Foo) g.clone();} catch (CloneNotSupportedException e) {

// Do nothing. This exception can't happen.

}

Page 60: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

6015-214

Avoidreturnvaluesthatdemandexceptionalprocessing• Returnzero-lengtharrayoremptycollection,notnull

package java.awt.image;public interface BufferedImageOp {

// Returns the rendering hints for this operation,// or null if no hints have been set.public RenderingHints getRenderingHints();

}

• DonotreturnaString ifabettertypeexists

Page 61: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

6115-214

Don'tletyouroutputbecomeyourdefactoAPI• Documentthefactthatoutputformatsmayevolveinthefuture

• Provideprogrammaticaccesstoalldataavailableinstringformpublic class Throwable {public void

printStackTrace(PrintStream s);}

Page 62: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

6215-214

Don'tletyouroutputbecomeyourdefactoAPI• Documentthefactthatoutputformatsmayevolveinthefuture• Provideprogrammaticaccesstoalldataavailableinstringform

public class Throwable {public void printStackTrace(PrintStream s);public StackTraceElement[] getStackTrace();

}

public final class StackTraceElement {public String getFileName();public int getLineNumber();public String getClassName();public String getMethodName();public boolean isNativeMethod();

}

Page 63: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

6315-214

Documentationmatters

Reuseissomethingthatisfareasiertosaythantodo.Doingitrequiresbothgooddesignandverygooddocumentation.Evenwhenweseegooddesign,whichisstillinfrequently,wewon'tseethecomponentsreusedwithoutgooddocumentation.

– D.L.Parnas,SoftwareAging.Proceedingsofthe16thInternationalConferenceonSoftwareEngineering,1994

Page 64: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

6415-214

ContractsandDocumentation

• APIsshouldbeself-documenting– Goodnamesdrivegooddesign

• Documentreligiouslyanyway– Allpublicclasses– Allpublicmethods– Allpublicfields– Allmethodparameters– Explicitlywritebehavioralspecifications

• Documentationisintegraltothedesignanddevelopmentprocess

Page 65: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

6515-214

Summary

• Acceptthefactthatyou,andothers,willmakemistakes– UseyourAPIasyoudesignit– Getfeedbackfromothers– Thinkintermsofusecases(domainengineering)– Hideinformationtogiveyourselfmaximumflexibilitylater

– Designforinattentive,hurriedusers– Documentreligiously

Page 66: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

6615-214

BONUS:APIREFACTORING

Page 67: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

6715-214

1.Sublist operationsinVectorpublic class Vector {

public int indexOf(Object elem, int index);public int lastIndexOf(Object elem, int index);...

}

• Notverypowerful- supportsonlysearch• Hardtousewithoutdocumentation

Page 68: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

6815-214

Sublistoperationsrefactoredpublic interface List {

List subList(int fromIndex, int toIndex);...

}

• Extremelypowerful- supportsall operations• Useofinterfacereducesconceptualweight– Highpower-to-weightratio

• Easytousewithoutdocumentation

Page 69: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

6915-214

2.Thread-localvariables

// Broken - inappropriate use of String as capability.// Keys constitute a shared global namespace.public class ThreadLocal {

private ThreadLocal() { } // Non-instantiable

// Sets current thread’s value for named variable.public static void set(String key, Object value);

// Returns current thread’s value for named variable.public static Object get(String key);

}

Page 70: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

7015-214

Thread-localvariablesrefactored(1)public class ThreadLocal {

private ThreadLocal() { } // Noninstantiable

public static class Key { Key() { } }

// Generates a unique, unforgeable keypublic static Key getKey() { return new Key(); }

public static void set(Key key, Object value);public static Object get(Key key);

}

• Works,butrequiresboilerplatecodetousestatic ThreadLocal.Key serialNumberKey = ThreadLocal.getKey();ThreadLocal.set(serialNumberKey, nextSerialNumber());System.out.println(ThreadLocal.get(serialNumberKey));

Page 71: Principles of Software Construction: Objects, Design, and …ckaestne/15214/s2017/slides/... · 2017-05-04 · Good names drive good design (4) • Use clear, specific naming conventions

7115-214

Thread-localvariablesrefactored(2)public class ThreadLocal<T> {

public ThreadLocal() { }public void set(T value);public T get();

}

• RemovesclutterfromAPIandclientcodestatic ThreadLocal<Integer> serialNumber =

new ThreadLocal<Integer>();serialNumber.set(nextSerialNumber());System.out.println(serialNumber.get());