Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with...

43
1 17-214 Principles of Software Construction: Objects, Design, and Concurrency Software engineering in practice Teams, branch-based development, and workflows Josh Bloch Charlie Garrod

Transcript of Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with...

Page 1: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

1 17-214

PrinciplesofSoftwareConstruction: Objects,Design,andConcurrencySoftwareengineeringinpracticeTeams,branch-baseddevelopment,andworkflowsJoshBloch CharlieGarrod

Page 2: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

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

Page 3: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

3 17-214

KeyconceptsfromlastThursday

•  APIdesignprinciples,part2

Page 4: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

4 17-214

Keydesignprinciple:Informationhiding

•  "Whenindoubt,leaveitout."

Page 5: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

5 17-214

Minimizemutability

•  Classesshouldbeimmutableunlessthere'sagoodreasontodootherwise–  Advantages:simple,thread-safe,reusable

•  Seejava.lang.String–  Disadvantage:separateobjectforeachvalue

•  Mutableobjectsrequirecarefulmanagementofvisibilityandsideeffects–  e.g.Component.getSize()returnsamutableDimension

•  Documentmutability–  Carefullydescribestatespace

Page 6: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

6 17-214

Failfast

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

//APropertiesinstancemapsStringstoStringspublicclassPropertiesextendsHashTable{publicObjectput(Objectkey,Objectvalue);//ThrowsClassCastExceptionifthisinstance//containsanykeysorvaluesthatarenotStringspublicvoidsave(OutputStreamout,Stringcomments);}

Page 7: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

7 17-214

Subtletiesofinformationhiding

•  Preventsubtleleaksofimplementationdetails–  Documentation–  Lackofdocumentation–  Implementation-specificreturntypes–  Implementation-specificexceptions–  Outputformats–  implementsSerializable

Page 8: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

8 17-214

Don'tletyouroutputbecomeyourdefactoAPI

•  Documentthefactthatoutputformatsmayevolveinthefuture•  Provideprogrammaticaccesstoalldataavailableinstringform

Page 9: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

9 17-214

Don'tletyouroutputbecomeyourdefactoAPI

•  Documentthefactthatoutputformatsmayevolveinthefuture•  Provideprogrammaticaccesstoalldataavailableinstringform

publicclassThrowable{publicvoidprintStackTrace(PrintStreams);publicStackTraceElement[]getStackTrace();//since1.4}publicfinalclassStackTraceElement{publicStringgetFileName();publicintgetLineNumber();publicStringgetClassName();publicStringgetMethodName();publicbooleanisNativeMethod();}

Page 10: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

10 17-214

Today:Towardsoftwareengineeringinpractice

•  Twopuzzlers•  Softwareengineeringforteams

–  Challengesofworkingasateam–  Toolsandprocessesforteams

•  Branch-baseddevelopment,etal.

Page 11: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

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

Page 12: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

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

Page 13: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

13 17-214

(a)0.9(b)0.90(c)Itvaries(d)Noneoftheabove:0.8999999999999999 Decimalvaluescan'tberepresentedexactlyby float or double

Whatdoesitprint?

Page 14: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

14 17-214

Anotherlook

publicclassChange{publicstaticvoidmain(Stringargs[]){System.out.println(2.00-1.10);}}

Page 15: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

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

Page 16: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

16 17-214

Themoral

•  Avoid float and doublewhereexactanswersarerequired–  Forexample,whendealingwithmoney

•  UseBigDecimal,int,orlonginstead

Page 17: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

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

Page 18: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

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

Page 19: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

19 17-214

(a)0.9(b)0.90(c)0.8999999999999999(d)Noneoftheabove:0.899999999999999911182158029987476766109466552734375

WeusedthewrongBigDecimalconstructor

Whatdoesitprint?

Page 20: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

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.

Page 21: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

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

Page 22: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

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

Page 23: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

23 17-214

Today:Towardsoftwareengineeringinpractice

•  Twopuzzlers•  Softwareengineeringforteams

–  Challengesofworkingasateam–  Toolsandprocessesforteams

•  Branch-baseddevelopment,etal.

Page 24: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

24 17-214

Softwareengineeringisinherentlycollaborative

Page 25: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

25 17-214

Challengesofworkingasateam:

Page 26: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

26 17-214

Challengesofworkingasateam:Aligningexpectations

•  Howdoestheteammakedecisions?•  Howdoyoudividethework?•  Doestheteamsharethesamegoalsandincentives?•  Whathappenswhenworkisn’tcompletedasexpected?•  Whendoteammembersliketowork?•  Whatothercommitmentsdoyourteammembershave?•  Wherewillyougettheworkdone?•  ...

Page 27: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

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

Page 28: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

28 17-214

BreakuptasksintoGitHubIssues

Issuescanrepresentbothtasksandbugsthatneedtobefixed.Issuesshouldbe:●  areasonablechunkofwork●  focusedandcohesive

Page 29: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

29 17-214

BreakuptasksintoGitHubIssues

Page 30: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

30 17-214

Uselabelstoindicatepriorityanddifferentiatebugsfromfeatures

Page 31: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

31 17-214

Considerusingmilestones(e.g.,HW5a,HW5b)

Page 32: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

32 17-214

Howdoesalargesoftwareprojectgettobeoneyearlate?

Page 33: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

33 17-214

Howdoesalargesoftwareprojectgettobeoneyearlate?Onedayatatime.— FredBrooks,TheMythicalMan-Month

https://en.wikipedia.org/wiki/The_Mythical_Man-Month

Page 34: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

34 17-214

UseasimpleKanbanboardtomeasureprogress

Page 35: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

35 17-214

UseasimpleKanbanboardtomeasureprogress

Page 36: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

36 17-214

Single-branchdevelopmentdoesn’tscaletoteams

Master

Page 37: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

37 17-214

Usesimplebranch-baseddevelopment

Createanewbranchforeachfeature.●  allowsparalleldevelopment●  nodealingwithhalf-finishedcode●  nomergeconflicts!

Everycommitto“master”shouldpassyourCIchecks.

Page 38: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

38 17-214

Git,practically

•  Gitstoreseachversionasasnapshot•  Iffileshavenotchanged,onlyalinktothepreviousfileisstored•  EachversionisreferredbytheSHA-1hashofthecontents

Page 39: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

39 17-214

gitcommit

Graphicsbyhttps://learngitbranching.js.org

Page 40: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

40 17-214

gitbranchnewImage

Page 41: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

41 17-214

gitcommit

Page 42: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

42 17-214

gitcheckoutnewImage;gitcommit

Page 43: Josh Bloch Charlie Garrodcharlie/courses/17-214/2020-spring/slides/... · no dealing with half-finished code no merge conflicts! Every ... Git, practically • Git stores each version

43 17-214

Summary

•  Identifyanddiscussriskswithinyourteam–  Gettoknowyourteammates,andagreeonyourprocess

•  Usestandardtoolstoimproveyourprocess