Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with...
Transcript of Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with...
1 17-214
PrinciplesofSoftwareConstruction: Objects,Design,andConcurrencySoftwareengineeringinpracticeTeams,branch-baseddevelopment,andworkflowsJoshBloch CharlieGarrod
2 17-214
Administrivia
• Homework5teamsign-updeadlineThursday11:59p.m.– Teamsizes,presentationslots…
• 2ndmidtermexam"inclass"onThursday– Pleasehavemobilephoneorsomeotherwaytoscandocuments– Reviewsessiontoday6-8pmEDT:https://cmu.zoom.us/j/343150293
• RequiredreadingduenextTuesday:– JavaConcurrencyinPractice,Sections11.3and11.4
• Homework5frameworksdiscussion• Onlineformat...
3 17-214
KeyconceptsfromlastThursday
• APIdesignprinciples,part2
4 17-214
Keydesignprinciple:Informationhiding
• "Whenindoubt,leaveitout."
5 17-214
Minimizemutability
• Classesshouldbeimmutableunlessthere'sagoodreasontodootherwise– Advantages:simple,thread-safe,reusable
• Seejava.lang.String– Disadvantage:separateobjectforeachvalue
• Mutableobjectsrequirecarefulmanagementofvisibilityandsideeffects– e.g.Component.getSize()returnsamutableDimension
• Documentmutability– Carefullydescribestatespace
6 17-214
Failfast
• Reporterrorsassoonastheyaredetectable– Checkpreconditionsatthebeginningofeachmethod– Avoiddynamictypecasts,run-timetype-checking
//APropertiesinstancemapsStringstoStringspublicclassPropertiesextendsHashTable{publicObjectput(Objectkey,Objectvalue);//ThrowsClassCastExceptionifthisinstance//containsanykeysorvaluesthatarenotStringspublicvoidsave(OutputStreamout,Stringcomments);}
7 17-214
Subtletiesofinformationhiding
• Preventsubtleleaksofimplementationdetails– Documentation– Lackofdocumentation– Implementation-specificreturntypes– Implementation-specificexceptions– Outputformats– implementsSerializable
8 17-214
Don'tletyouroutputbecomeyourdefactoAPI
• Documentthefactthatoutputformatsmayevolveinthefuture• Provideprogrammaticaccesstoalldataavailableinstringform
9 17-214
Don'tletyouroutputbecomeyourdefactoAPI
• Documentthefactthatoutputformatsmayevolveinthefuture• Provideprogrammaticaccesstoalldataavailableinstringform
publicclassThrowable{publicvoidprintStackTrace(PrintStreams);publicStackTraceElement[]getStackTrace();//since1.4}publicfinalclassStackTraceElement{publicStringgetFileName();publicintgetLineNumber();publicStringgetClassName();publicStringgetMethodName();publicbooleanisNativeMethod();}
10 17-214
Today:Towardsoftwareengineeringinpractice
• Twopuzzlers• Softwareengineeringforteams
– Challengesofworkingasateam– Toolsandprocessesforteams
• Branch-baseddevelopment,etal.
11 17-214
1.“TimeforaChange” (2002)
Ifyoupay$2.00foragasketthatcosts$1.10,howmuchchangedoyouget? publicclassChange{publicstaticvoidmain(Stringargs[]){System.out.println(2.00-1.10);}}
From An Evening Of Puzzlers by Josh Bloch
12 17-214
Whatdoesitprint?
(a) 0.9 (b) 0.90 (c) It varies (d) None of the above
publicclassChange{publicstaticvoidmain(Stringargs[]){System.out.println(2.00-1.10);}}
13 17-214
(a)0.9(b)0.90(c)Itvaries(d)Noneoftheabove:0.8999999999999999 Decimalvaluescan'tberepresentedexactlyby float or double
Whatdoesitprint?
14 17-214
Anotherlook
publicclassChange{publicstaticvoidmain(Stringargs[]){System.out.println(2.00-1.10);}}
15 17-214
Howdoyoufixit?
//Youcouldfixitthisway...importjava.math.BigDecimal;publicclassChange{publicstaticvoidmain(Stringargs[]){System.out.println(newBigDecimal("2.00").subtract(newBigDecimal("1.10")));}}
//...oryoucouldfixitthiswaypublicclassChange{publicstaticvoidmain(Stringargs[]){System.out.println(200-110);}}
Prints0.90
Prints90
16 17-214
Themoral
• Avoid float and doublewhereexactanswersarerequired– Forexample,whendealingwithmoney
• UseBigDecimal,int,orlonginstead
17 17-214
2.“AChangeisGonnaCome”
Ifyoupay$2.00foragasketthatcosts$1.10,howmuchchangedoyouget? importjava.math.BigDecimal;publicclassChange{publicstaticvoidmain(Stringargs[]){BigDecimalpayment=newBigDecimal(2.00);BigDecimalcost=newBigDecimal(1.10);System.out.println(payment.subtract(cost));}}
18 17-214
Whatdoesitprint?
importjava.math.BigDecimal;publicclassChange{publicstaticvoidmain(Stringargs[]){BigDecimalpayment=newBigDecimal(2.00);BigDecimalcost=newBigDecimal(1.10);System.out.println(payment.subtract(cost));}}
(a)0.9(b)0.90(c)0.8999999999999999(d)None of the above
19 17-214
(a)0.9(b)0.90(c)0.8999999999999999(d)Noneoftheabove:0.899999999999999911182158029987476766109466552734375
WeusedthewrongBigDecimalconstructor
Whatdoesitprint?
20 17-214
Anotherlook
importjava.math.BigDecimal;publicclassChange{publicstaticvoidmain(Stringargs[]){BigDecimalpayment=newBigDecimal(2.00);BigDecimalcost=newBigDecimal(1.10);System.out.println(payment.subtract(cost));}}
Thespecsays: publicBigDecimal(doubleval)TranslatesadoubleintoaBigDecimalwhichistheexactdecimalrepresentationofthedouble'sbinaryfloating-pointvalue.
21 17-214
Howdoyoufixit?
importjava.math.BigDecimal;publicclassChange{publicstaticvoidmain(Stringargs[]){BigDecimalpayment=newBigDecimal("2.00");BigDecimalcost=newBigDecimal("1.10");System.out.println(payment.subtract(cost));}}
Prints0.90
22 17-214
The moral
• Use newBigDecimal(String), not newBigDecimal(double)
• BigDecimal.valueOf(double) is better, but not perfect – Use it for non-constant values.
• For API designers – Make it easy to do the commonly correct thing – Make it hard to misuse – Make it possible to do exotic things
23 17-214
Today:Towardsoftwareengineeringinpractice
• Twopuzzlers• Softwareengineeringforteams
– Challengesofworkingasateam– Toolsandprocessesforteams
• Branch-baseddevelopment,etal.
24 17-214
Softwareengineeringisinherentlycollaborative
25 17-214
Challengesofworkingasateam:
26 17-214
Challengesofworkingasateam:Aligningexpectations
• Howdoestheteammakedecisions?• Howdoyoudividethework?• Doestheteamsharethesamegoalsandincentives?• Whathappenswhenworkisn’tcompletedasexpected?• Whendoteammembersliketowork?• Whatothercommitmentsdoyourteammembershave?• Wherewillyougettheworkdone?• ...
27 17-214
Decidewhattobuild,thendesigntheAPI
//Acollectionofelements(rootofthecollectionhierarchy)publicinterfaceCollection<E>{//Ensuresthatcollectioncontainsobooleanadd(Eo);//Removesaninstanceofofromcollection,ifpresentbooleanremove(Objecto);//Returnstrueiffcollectioncontainsobooleancontains(Objecto);//Returnsnumberofelementsincollectionintsize();//ReturnstrueifcollectionisemptybooleanisEmpty();...//Remainderomitted}
BasicProcess:(1) Determineminimal
featureset(2) DrawUMLonthe
whiteboard.(3) SketchoutyourAPIon
paper(4) Writeexamplecode(5) Review(6) Repeat
28 17-214
BreakuptasksintoGitHubIssues
Issuescanrepresentbothtasksandbugsthatneedtobefixed.Issuesshouldbe:● areasonablechunkofwork● focusedandcohesive
29 17-214
BreakuptasksintoGitHubIssues
30 17-214
Uselabelstoindicatepriorityanddifferentiatebugsfromfeatures
31 17-214
Considerusingmilestones(e.g.,HW5a,HW5b)
32 17-214
Howdoesalargesoftwareprojectgettobeoneyearlate?
33 17-214
Howdoesalargesoftwareprojectgettobeoneyearlate?Onedayatatime.— FredBrooks,TheMythicalMan-Month
https://en.wikipedia.org/wiki/The_Mythical_Man-Month
34 17-214
UseasimpleKanbanboardtomeasureprogress
35 17-214
UseasimpleKanbanboardtomeasureprogress
36 17-214
Single-branchdevelopmentdoesn’tscaletoteams
Master
37 17-214
Usesimplebranch-baseddevelopment
Createanewbranchforeachfeature.● allowsparalleldevelopment● nodealingwithhalf-finishedcode● nomergeconflicts!
Everycommitto“master”shouldpassyourCIchecks.
38 17-214
Git,practically
• Gitstoreseachversionasasnapshot• Iffileshavenotchanged,onlyalinktothepreviousfileisstored• EachversionisreferredbytheSHA-1hashofthecontents
39 17-214
gitcommit
Graphicsbyhttps://learngitbranching.js.org
40 17-214
gitbranchnewImage
41 17-214
gitcommit
42 17-214
gitcheckoutnewImage;gitcommit
43 17-214
Summary
• Identifyanddiscussriskswithinyourteam– Gettoknowyourteammates,andagreeonyourprocess
• Usestandardtoolstoimproveyourprocess