69
1 15-214 School of Computer Science Principles of Software Construction: Class invariants, immutability, and testing Josh Bloch Charlie Garrod

Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

115-214

SchoolofComputerScience

PrinciplesofSoftwareConstruction:Classinvariants,immutability,andtesting

JoshBloch CharlieGarrod

Page 2: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

215-214

Administrivia

• Homework4aduetoday,11:59p.m.• Designreviewmeetingismandatory– Butweexpectittobereallyhelpful– Feedbackisawonderfulthing

• PSA– Youhavelessthanoneweeklefttoregistertovote!Dealine isOctober11!

Page 3: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

315-214

KeyconceptsfromTuesday…

• Internalrepresentationsmatter– Thewrongrepresentationcanbetoxic

• Codemustbecleanandconcise– Repetitionistoxic

• Goodcodinghabitsmatter

Page 4: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

415-214

Outline

• Classinvariantsanddefensivecopying• Immutability• Testingandcoverage• Testingforcomplexenvironments• Implementationtestingwithassertions

Page 5: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

515-214

Classinvariants

• Criticalpropertiesofthefieldsofanobject• Establishedbytheconstructor• Maintainedbypublicmethodinvocations–Maybeinvalidatedtemporarilyduringmethodexecution

Page 6: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

615-214

Safelanguagesandrobustprograms

• UnlikeC/C++,Javalanguagesafe– Immunetobufferoverruns,wildpointers,etc.

• Makesitpossibletowriterobust classes– Correctnessdoesn’tdependonothermodules– Eveninsafelanguage,requiresprogrammereffort

Page 7: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

715-214

Defensiveprogramming

• Assumeclientswilltrytodestroyinvariants– Mayactuallybetrue(malicioushackers)– Morelikely:honestmistakes

• Ensureclassinvariantssurviveanyinputs– Defensivecopying–Minimizingmutability

Page 8: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

815-214

public final class Period {private final Date start, end; // Invariant: start <= end

/*** @throws IllegalArgumentException if start > end* @throws NullPointerException if start or end is null*/

public Period(Date start, Date end) {if (start.after(end))

throw new IllegalArgumentException(start + " > " + end);this.start = start;this.end = end;

}

public Date start() { return start; }public Date end() { return end; }... // Remainder omitted

}

Thisclassisnot robust

Page 9: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

915-214

Theproblem:Date ismutable// Attack the internals of a Period instanceDate start = new Date(); // (The current time)Date end = new Date(); // " " "Period p = new Period(start, end);end.setYear(78); // Modifies internals of p!

Page 10: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

1015-214

Thesolution:defensivecopying// Repaired constructor - defensively copies parameterspublic Period(Date start, Date end) {

this.start = new Date(start.getTime());this.end = new Date(end.getTime());if (this.start.after(this.end))throw new IllegalArgumentException(start +“ > "+ end);

}

Page 11: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

1115-214

Afewimportantdetails• Copiesmadebefore checkingparameters• Validitycheckperformedoncopies• Eliminateswindowofvulnerabilitybetweenparametercheckandcopy

• ThwartsmultithreadedTOCTOUattack– Time-Of-Check-To-Time-Of-U

// BROKEN - Permits multithreaded attack!public Period(Date start, Date end) {

if (start.after(end))throw new IllegalArgumentException(start + " > " + end);

// Window of vulnerabilitythis.start = new Date(start.getTime());this.end = new Date(end.getTime());

}

Page 12: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

1215-214

Anotherimportantdetail• Usedconstructor,notclone,tomakecopies– Necessarybecause Date classisnonfinal– Attackercouldimplementmalicioussubclass• Recordsreferencetoeachextantinstance• Providesattackerwithaccesstoinstancelist

• Butwhousesclone,anyway?[EJItem11]

Page 13: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

1315-214

Unfortunately,constructorsareonlyhalfthebattle

// Accessor attack on internals of PeriodPeriod p = new Period(new Date(), new Date());Date d = p.end();p.end.setYear(78); // Modifies internals of p!

Page 14: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

1415-214

Thesolution:moredefensivecopying

// Repaired accessors - defensively copy fieldspublic Date start() {

return new Date(start.getTime());}public Date end() {

return new Date(end.getTime());}

NowPeriodclassisrobust!

Page 15: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

1515-214

Summary• Don’tincorporatemutableparametersintoobject;makedefensivecopies

• Returndefensivecopiesofmutablefields…• Orreturnunmodifiable viewofmutablefields• Reallesson– useimmutablecomponents– Eliminatestheneedfordefensivecopying

Page 16: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

1615-214

Outline

• Classinvariantsanddefensivecopying• Immutability• Testingandcoverage• Testingforcomplexenvironments• Implementationtestingwithassertions

Page 17: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

1715-214

Immutableclasses

• Classwhoseinstancescannotbemodified• Examples:String,Integer,BigInteger• How,why,andwhentousethem

Page 18: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

1815-214

Howtowriteanimmutableclass

• Don’tprovideanymutators• Ensurethatnomethodsmaybeoverridden• Makeallfieldsfinal• Makeallfieldsprivate• Ensuresecurityofanymutablecomponents

Page 19: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

1915-214

public final class Complex {private final double re, im;

public Complex(double re, double im) {this.re = re;this.im = im;

}

// Getters without corresponding setterspublic double realPart() { return re; }public double imaginaryPart() { return im; }

// subtract, multiply, divide similar to addpublic Complex add(Complex c) {

return new Complex(re + c.re, im + c.im);}

Immutableclassexample

Page 20: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

2015-214

@Override public boolean equals(Object o) {if (!(o instanceof Complex)) return false;Complex c = (Complex)o;return Double.compare(re, c.re) == 0 &&

Double.compare(im, c.im) == 0;}

@Override public int hashCode() {return 31*Double.hashCode(re) + Double.hashCode(im);

}

@Override public String toString() {return String.format("%d + %di”, re, im)";

}}

Immutableclassexample(cont.)Nothinginterestinghere

Page 21: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

2115-214

Distinguishingcharacteristic

• Returnnewinstanceinsteadofmodifying• Functionalprogramming• Mayseemunnaturalatfirst• Manyadvantages

Page 22: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

2215-214

Advantages

• Simplicity• InherentlyThread-Safe• Canbesharedfreely• Noneedfordefensivecopies• Excellentbuildingblocks

Page 23: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

2315-214

Majordisadvantage

• Separateinstanceforeachdistinctvalue• Creatingtheseinstancescanbecostly

BigInteger moby = ...; // A million bits longmoby = moby.flipBit(0); // Ouch!

• Problemmagnifiedformultistepoperations–Well-designedimmutableclassesprovidecommonmultistepoperationsasprimitives

– Alternative:mutablecompanionclass• e.g.,StringBuilder forString

Page 24: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

2415-214

Whentomakeclassesimmutable

• Always,unlessthere'sagoodreasonnotto• Alwaysmakesmall“valueclasses”immutable!– Examples:Color,PhoneNumber,Unit– Date andPoint weremistakes!– Expertsoftenuselong insteadofDate

Page 25: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

2515-214

Whentomakeclassesmutable

• Classrepresentsentitywhosestatechanges– Real-world- BankAccount,TrafficLight– Abstract- Iterator, Matcher, Collection– Processclasses- Thread,Timer

• Ifclassmustbemutable,minimizemutability– Constructorsshouldfullyinitializeinstance– Avoidreinitializemethods

Page 26: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

2615-214

Outline

• ClassInvariants• Immutability• Testingandcoverage• Testingforcomplexenvironments• Implementationtestingwithassertions

Page 27: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

2715-214

Whydowetest?

Page 28: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

2815-214

Testingdecisions

• Whotests?– Developerswhowrotethecode– QualityAssuranceTeamandTechnicalWriters– Customers

• Whentotest?– Beforeandduringdevelopment– Aftermilestones– Beforeshipping– Aftershipping

Page 29: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

2915-214

Testdrivendevelopment

• Writetestsbeforecode• Neverwritecodewithoutafailingtest• Codeuntilthefailingtestpasses

Page 30: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

3015-214

Whyusetestdrivendevelopment?

• Forcesyoutothinkaboutinterfacesearly• Higherproductquality– Bettercodewithfewerdefects

• Highertestsuitequality• Higherproductivity• It’sfuntowatchtestspass

Page 31: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

3115-214

TDDinpractice

• EmpiricalstudiesonTDDshow:–Mayrequiremoreeffort–Mayimprovequalityandsavetime

• SelectiveuseofTDDisbest• AlwaysuseTDDforbugreports– Regressiontests

Page 32: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

3215-214

Howmuchtesting?

• Yougenerallycannottestallinputs– Toomany– usuallyinfinite

• Butwhenitworks,exhaustivetestingisbest!

Page 33: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

3315-214

Whatmakesagoodtestsuite?

• Provideshighconfidencethatcodeiscorrect• Short,clear,andnon-repetitious–Moredifficultfortestsuitesthanregularcode– Realistically,testsuiteswilllookworse

• Canbefuntowriteifapproachedinthisspirit

Page 34: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

3415-214

Nextbestthingtoexhaustivetesting:randominputs

• Alsoknowasfuzztesting,torturetesting• Try“random”inputs,asmanyasyoucan– Chooseinputstotickleinterestingcases– Knowledgeofimplementationhelpshere

• Seedrandomnumbergeneratorsotestsrepeatable

Page 35: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

3515-214

Black-boxtesting

• Lookatspecifications,notcode• Testrepresentativecases• Testboundaryconditions• Testinvalid(exception)cases• Don’ttestunspecifiedcases

Page 36: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

3615-214

White-boxtesting

• Lookatspecificationsand code• Writeteststo:– Checkinterestingimplementationcases–Maximizebranchcoverage

Page 37: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

3715-214

Codecoveragemetrics

• Methodcoverage– coarse• Branchcoverage– fine• Pathcoverage– toofine– Costishigh,valueislow– (Relatedtocyclomatic complexity)

Page 38: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

3815-214

Coveragemetrics:usefulbutdangerous

• Cangivefalsesenseofsecurity• Examplesofwhatcoverageanalysiscouldmiss– Datavalues– Concurrencyissues– raceconditionsetc.– Usabilityproblems– Customerrequirementsissues

• Highbranchcoverageisnot sufficient

Page 39: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

3915-214

Testsuites– idealandreal

• Idealtestsuites– Uncoverallerrorsincode– Test“non-functional”attributessuchasperformanceandsecurity

–Minimumsizeandcomplexity• RealtestSuites– Uncoversomeportionoferrorsincode– Haveerrorsoftheirown– Arenonethelesspriceless

Page 40: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

4015-214

Outline

• Classinvariants• Immutability• Testingandcoverage• Testingforcomplexenvironments• Implementationtestingwithassertions

Page 41: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

4115-214

Problemswhentestingsomeapps

• User-facingapplications– Usersclick,drag,etc.,andinterpretoutput– Timingissues

• Testingagainstbiginfrastructure– Databases,webservices,etc.

• Realworldeffects– Printing,mailingdocuments,etc.

• Collectivelycomprisethetestenvironment

Page 42: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

4215-214

Example– Tiramisuapp

• Mobilerouteplanningapp• AndroidUI• BackenduseslivePATdata

Page 43: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

4315-214

Anotherexample

• 3rdpartyFacebook apps• Androiduserinterface• BackendusesFacebookdata

Page 44: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

4415-214

TestinginrealenvironmentsCode FacebookAndroidclient

void buttonClicked() {render(getFriends());

}List<Friend> getFriends() {

Connection c = http.getConnection();FacebookApi api = new Facebook(c);List<Node> persons = api.getFriends("john");for (Node person1 : persons) {

for (Node person2 : persons) {…}

}return result;

}

Page 45: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

4515-214

EliminatingAndroiddependencyCode FacebookTestdriver

@Test void testGetFriends() {assert getFriends() == ...;

} List<Friend> getFriends() {

Connection c = http.getConnection();FacebookAPI api = new FacebookAPI(c);List<Node> persons = api.getFriends("john");for (Node person1 : persons) {

for (Node person2 : persons) {…}

}return result;

}

Page 46: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

4615-214

Thatwon’tquitework

• GUIapplicationsprocessthousands ofevents• Solution:automatedGUItestingframeworks– AllowstreamsofGUIeventstobecaptured,replayed

• Thesetoolsaresometimescalledrobots

Page 47: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

4715-214

EliminatingFacebook dependencyCode Mock

Facebook

@Test void testGetFriends() {assert getFriends() == …;

} List<Friend> getFriends() {

FacebookApi api = new MockFacebook(c);List<Node> persons = api.getFriends("john");for (Node person1 : persons) {

for (Node person2 : persons) {…}

}return result;

}

Testdriver

Page 48: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

4815-214

Thatwon’tquitework!

• Changingproductioncodefortestingunacceptable• Problemcausedbyconstructor incode• Usefactoryinsteadofconstructor• Usetoolstofacilitatethissortoftesting– Dependencyinjectiontools,e.g.,Dagger,Guice–MockobjectframeworkssuchasMockito

Page 49: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

4915-214

Faultinjection

• Mockscanemulatefailuressuchastimeouts• Allowsyoutoverifytherobustnessofsystem

Code MockFacebookTestdriver

Page 50: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

5015-214

Advantagesofusingmocks

• Testcodelocallywithoutlargeenvironment• Enabledeterministictests• Enablefaultinjection• Canspeeduptestexecution– e.g.,avoidslowdatabaseaccess

• Cansimulatefunctionalitynotyetimplemented• Enabletestautomation

Page 51: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

5115-214

DesignImplications

• Thinkabouttestabilitywhenwritingcode• Whenamockmaybeappropriate,designforit• Hidesubsystemsbehindaninterfaces• Usefactories,notconstructorstoinstantiate• Useappropriatetools– Dependencyinjectionormockingframeworks

Page 52: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

5215-214

MoreTestingin15-313FoundationsofSoftwareEngineering• Manualtesting• Securitytesting,penetrationtesting• Fuzztestingforreliability• Usabilitytesting• GUI/Webtesting• Regressiontesting• Differentialtesting• Stress/soaktesting

Page 53: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

5315-214

Outline

• ClassInvariants• Immutability• Testsuitesandcoverage• Testingforcomplexenvironments• Implementation-testingwithassertions

Page 54: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

5415-21454

Whatisanassertion?

• Statementcontainingboolean expressionthatprogrammerbelievestobetrue:

assert speed <= SPEED_OF_LIGHT;

• Evaluatedatruntime– throwsError iffalse• Disabledbydefault- noperformanceeffect• Typicallyenabledduringdevelopment• Canenableinthefieldwhenproblemsoccur!

Page 55: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

5515-214

Syntax

AssertStatement:assert Expression1 ;assert(Expression1, Expression2) ;

• Expression1 - assertedcondition(boolean)• Expression2 - detailmessageof AssertionError

55

Page 56: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

5615-214

Whyuseassertions?

• Document&testprogrammer'sassumptions– e.g.,classinvariants

• Verifyprogrammer’sunderstanding• Quicklyuncoverbugs• Increaseconfidencethatprogramisbug-free• Assertsturnblackboxtestsintowhiteboxtests

56

Page 57: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

5715-21457

Lookfor“assertivecomments”int remainder = i % 3;if (remainder == 0) {

...} else if (remainder == 1) {

...} else { // (remainder == 2)

...}

Page 58: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

5815-21458

Replacewithrealassertions!int remainder = i % 3;if (remainder == 0) {

...} else if (remainder == 1) {

...} else {

assert remainder == 2;...

}

Page 59: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

5915-21459

Usesecondargumentforfailurecapture

if (i % 3 == 0) {...

} else if (i % 3 == 1) {...

} else {assert (i % 3 == 2, i);...

}

Page 60: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

6015-21460

Lookforswitchwithnodefaultswitch(flavor) {

case VANILLA:...break;

case CHOCOLATE:...break;

case STRAWBERRY:...

}

Page 61: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

6115-21461

Addan“assertivedefault”switch(flavor) {case VANILLA:...break;

case CHOCOLATE:...break;

case STRAWBERRY:...break;

default:assert (false, flavor);

}

Page 62: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

6215-21462

Donotuseassertionsforpublic preconditions

/*** Sets the refresh rate.** @param rate refresh rate, in frames per second.* @throws IllegalArgumentException if rate <= 0* or rate > MAX_REFRESH_RATE.*/public void setRefreshRate(int rate) {

if (rate <= 0 || rate > MAX_REFRESH_RATE)throw new IllegalArgumentException(...);

setRefreshInterval(1000 / rate);}

Page 63: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

6315-21463

Do useassertionsfornon-public preconditions

/*** Sets the refresh interval (which must correspond* to a legal frame rate).** @param interval refresh interval in ms*/private void setRefreshInterval(int interval) {

assert interval > 0 && interval <= 1000, interval;... // Set the refresh interval

}

Page 64: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

6415-21464

Do useassertionsforpostconditions/*** Returns BigInteger whose value is (this-1 mod m).* @throws ArithmeticException if m <= 0, or this* BigInteger is not relatively prime to m.*/public BigInteger modInverse(BigInteger m) {

if (m.signum() <= 0)throw new ArithmeticException(m + "<= 0");

... // Do the computationassert this.multiply(result).mod(m).equals(ONE);return result;

}

Page 65: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

6515-21465

Complexpostconditions

void foo(int[] a) {// Manipulate contents of array...

// Array will appear unchanged}

Page 66: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

6615-21466

Assertionsforcomplexpostconditions

void foo(final int[] a) {class DataCopy {

private int[] aCopy;DataCopy() { aCopy = (int[]) a.clone(); }boolean isConsistent() {

return Arrays.equals(a, aCopy);}

}DataCopy copy = null;assert (copy = new DataCopy()) != null;... // Manipulate contents of arrayassert copy.isConsistent();

}

Page 67: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

6715-21467

Caveat– assertsmustnothavesideeffectsvisibleoutsideotherasserts

Dothis:boolean modified = set.remove(elt);assert modified;

Notthis:assert set.remove(elt); //Bug!

Page 68: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

6815-21468

Sermon:acceptassertionsintoyourlife

• Programmer’sinteriormonologue:– “Nowatthispoint,weknow...”

• During,notafter,development• Quicklybecomessecondnature• Paysbigcode-qualitydividends

Page 69: Principles of Software Constructioncharlie/courses/15-214/2016-fall... · – Feedback is a wonderful thing • PSA – You have less than one week left to ... – Process classes

6915-214

Conclusion

• Tomaintainclassinvariants–Minimizemutability–Makedefensivecopieswhererequired

• Interfacetestingiscritical– Designinterfacestofacilitatetesting– Coveragetoolscanhelpgaugetestsuitequality

• Useassertionstotestimplementationdetails– Assertsamplifythevalueofyourinterfacetests