better code - sean parent€¦ · forest hill anerley penge west honor oak park brockley wapping...
TRANSCRIPT
![Page 1: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/1.jpg)
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Better CodeSean Parent | Principal Scientist
![Page 2: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/2.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Better Code
▪ Regular Types ▪ Goal: No Incomplete Types ▪ Algorithms ▪ Goal: No Raw Loops ▪ Data Structures ▪ Goal: No Incidental Data Structures ▪ Runtime Polymorphism ▪ Goal: No Inheritance ▪ Concurrency ▪ Goal: No Raw Synchronization Primitives
http://sean-parent.stlab.cc/papers-and-presentations
2
![Page 3: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/3.jpg)
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
The Knowledge
![Page 4: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/4.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved. 4
![Page 5: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/5.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved. 4
![Page 6: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/6.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved. 5
![Page 7: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/7.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved. 5
![Page 8: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/8.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved. 6
![Page 9: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/9.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved. 6
![Page 10: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/10.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved. 7
![Page 11: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/11.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Key to lines
Metropolitan
Victoria
Circle
Central
Bakerloo
DLR
London Overground
TfL Rail
Piccadilly
Waterloo & City
Jubilee
Hammersmith & City
Northern
District
District open weekends and on some public holidays
Emirates Air Line
Check before you travel§ Services to/from some stations are subject to variation. Please search ‘TfL stations’ for full details
§ Caledonian Road Station closed temporarily from Spring 2016.---------------------------------------------------------------------------§ Holland Park Station closed from Saturday 2 January until early August 2016.---------------------------------------------------------------------------§ Paddington Bakerloo line trains will not stop at this station from Saturday 2 April until early August 2016.---------------------------------------------------------------------------§ Tufnell Park Station closed until mid-March 2016.---------------------------------------------------------------------------
Transport for London January 2016
Key to symbols Explanation of zones
1
3
45
6
2
7
8
9
Station in both zones
Station in both zones
Station in both zones
Station in Zone 9
Station in Zone 6
Station in Zone 5
Station in Zone 3Station in Zone 2
Station in Zone 1
Station in Zone 4
Station in Zone 8
Station in Zone 7
Interchange stations
Step-free access from street to train
Step-free access from street to platform
National Rail
Riverboat services
Airport
Victoria Coach Station
Trams
Emirates Air Line
A
B
C
D
E
F
1 2 3 4 5 6 7 8 9
1 2 3 4 5 76 8 9
A
B
C
D
E
F
2 2/34
3
522
2
5
8 8 6 8
2
4
4
65
9
1
32
4
3
5
3
3
1
1
3
3
5 79 7 7Special fares apply Special faresapply
5
4
4
46
6
River Thames
Regent’s Park
GoodgeStreetBayswater
Warren Street
Aldgate
Farringdon
Barbican
RussellSquare
High Street Kensington
Old Street
Green Park
BakerStreet
NottingHill Gate
Victoria
Mansion House
Temple
OxfordCircus
BondStreet
TowerHill
Westminster
PiccadillyCircus
CharingCross
Holborn
Tower Gateway
Monument
Moorgate
Leicester SquareSt. Paul’s
Hyde Park Corner
Knightsbridge
Angel
Queensway
Marble Arch
SouthKensington
SloaneSquare
Covent Garden
LiverpoolStreet
Great PortlandStreet
Bank
Chancery Lane
LancasterGateHolland
Park
Cannon Street
Fenchurch Street
GloucesterRoad St. James’s
Park
EustonSquareEdgware
Road
Edgware Road
Embankment
Blackfriars
TottenhamCourt Road
King’s CrossSt. Pancras
MarylebonePaddington
Watford High Street
Watford Junction
Bushey
Carpenders Park
Hatch End
North Wembley
South Kenton
Kenton
Wembley Central
Kensal Green
Queen’s Park
Stonebridge Park
Bethnal Green
Cambridge Heath
London Fields
Harlesden
Willesden Junction
Headstone Lane
Harrow &Wealdstone
Kilburn Park
WarwickAvenue
Maida ValeEuston
NewCross Gate
Imperial Wharf
West Croydon
ClaphamJunction
Crystal Palace Norwood Junction
Sydenham
Forest Hill
Anerley
Penge West
Honor Oak Park
Brockley
Wapping
New Cross
Queens RoadPeckham
Peckham Rye
Denmark Hill
Surrey Quays
Whitechapel
WandsworthRoad
Rotherhithe
ShoreditchHigh Street
Haggerston
Hoxton
Shepherd’sBush
Shadwell
CanadaWater
Fulham Broadway
West Brompton
Parsons Green
Putney Bridge
East Putney
Southfields
Wimbledon Park
Wimbledon
Kensington(Olympia)
AldgateEast
BethnalGreen Mile End
Dalston Kingsland
HackneyWick
Homerton
HackneyCentral
RectoryRoad
HackneyDowns
Theydon Bois
Epping
Debden
Loughton
Buckhurst Hill
Leytonstone
Wood StreetBruce Grove
White Hart Lane
Silver Street
Edmonton Green
Southbury
Turkey Street
Theobalds Grove
Cheshunt
Enfield Town
StamfordHill
Bush HillPark
Highams Park
Chingford
Leyton
Woodford
South Woodford
Snaresbrook
Hainault
Fairlop
Barkingside
Newbury Park
Stratford
RodingValley
GrangeHill
Chigwell
Redbridge
GantsHill
Wanstead
Dalston Junction
Canonbury
Stepney Green
SevenSisters
Highbury &Islington
TottenhamHale
WalthamstowCentral
Clapton
St. James Street
StokeNewington
Dagenham East
Dagenham Heathway
Becontree
Upney
Upminster
Upminster Bridge
Hornchurch
Elm Park
Ilford
Goodmayes
Chadwell Heath
Romford
Gidea Park
Harold Wood
Shenfield
Brentwood
Seven Kings
HarringayGreenLanes
WansteadPark
LeytonstoneHigh Road
LeytonMidland Road
Emerson ParkSouth Tottenham
Blackhorse Road
Barking
East Ham
Plaistow
Upton Park
Upper Holloway
CrouchHill
GospelOak
BowChurch
WestHam
BowRoad
Bromley-by-Bow
Island Gardens
Greenwich
Deptford Bridge
South Quay
Crossharbour
Mudchute
Heron Quays
West IndiaQuay
Elverson Road
Devons Road
Langdon Park
All Saints
Canary Wharf
Cutty Sark for Maritime Greenwich
Lewisham
West Silvertown
EmiratesRoyal Docks
EmiratesGreenwichPeninsula
PontoonDock
LondonCity Airport
WoolwichArsenal
King George V
Custom House for ExCeL
Prince Regent
Royal Albert
Beckton Park
Cyprus
Beckton
Gallions Reach
Westferry Blackwall
RoyalVictoria
CanningTown
PoplarLimehouse
EastIndia
StratfordInternational
Star Lane
NorthGreenwich
Maryland
Manor Park
Forest Gate
Oakwood
Cockfosters
Southgate
Arnos Grove
Bounds Green
Turnpike Lane
Wood Green
Manor House
FinsburyPark
Arsenal
KentishTown West
Holloway Road
Caledonian Road
Mill Hill East
Edgware
Burnt Oak
Colindale
Hendon Central
Brent Cross
Golders Green
Hampstead
Belsize Park
Chalk Farm
Camden Town
High Barnet
Totteridge & Whetstone
Woodside Park
West Finchley
Finchley Central
East Finchley
Highgate
Archway
Tufnell Park
Kentish Town
MorningtonCrescent
CamdenRoad
CaledonianRoad &
Barnsbury
Amersham
Chorleywood
Rickmansworth
Chalfont &Latimer
Chesham
Moor Park
Croxley
Watford
Northwood
Northwood Hills
Pinner
North Harrow
Harrow-on-the-Hill
NorthwickPark
PrestonRoad
Wembley Park
Rayners Lane
Stanmore
Canons Park
Queensbury
Kingsbury
Neasden
Dollis Hill
Willesden Green
Swiss Cottage
Kilburn
West Hampstead
Finchley Road
WestHarrow
IckenhamUxbridge
Hillingdon RuislipRuislip Manor
Eastcote
St. John’s Wood
HeathrowTerminal 5
HeathrowTerminal 4
Northfields
Boston Manor
SouthEaling
Osterley
Hounslow Central
Hounslow East
Hounslow West
Hatton CrossHeathrowTerminals 2 & 3
Perivale
Hanger Lane
RuislipGardens
South Ruislip
Greenford
Northolt
South Harrow
Sudbury Hill
Sudbury Town
Alperton
Park Royal
North Ealing
EalingBroadway
West Ruislip
Ealing Common
Gunnersbury
Kew Gardens
Richmond
Acton Town
ChiswickPark
TurnhamGreen
StamfordBrook
RavenscourtPark
WestKensington
BaronsCourt
Earl’sCourt
Shepherd’sBush Market
Goldhawk Road
Hammersmith
Wood Lane
WhiteCity
Finchley Road& Frognal
KensalRise
BrondesburyPark
Brondesbury
KilburnHigh Road
SouthHampstead
WestActon
NorthActon
EastActon
Southwark
Waterloo
London Bridge Bermondsey
Vauxhall
Lambeth NorthPimlico
Stockwell
Brixton
Elephant & Castle
OvalKennington
Borough
Clapham North
Clapham High Street
Clapham Common
Clapham South
Balham
Tooting Bec
Tooting Broadway
Colliers Wood
South Wimbledon
Morden
Latimer Road
Ladbroke Grove
Royal Oak
Westbourne Park
PuddingMill Lane
Acton Central
South Acton
HampsteadHeath
Stratford High Street
Abbey Road
WoodgrangePark
WalthamstowQueen’s Road
7
![Page 12: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/12.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved. 8
![Page 13: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/13.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved. 8
![Page 14: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/14.jpg)
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
“There are rules!” – The Big Lebowski
![Page 15: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/15.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Lower Bound
template <class ForwardIterator, class T, class Compare> ForwardIterator lower_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp) { auto n = distance(first, last);
while (n != 0) { auto h = n / 2; auto m = next(first, h);
if (comp(*m, value)) { first = next(m); n -= h + 1; } else { n = h; } }
return first; }
10
![Page 16: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/16.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved. 11
![Page 17: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/17.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved. 12
ƒ!
Menu Item
![Page 18: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/18.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved. 13
ƒ!
Menu Item
State
![Page 19: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/19.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved. 14
ƒ!
Menu Item
State State’
![Page 20: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/20.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved. 15
![Page 21: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/21.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Good Code
Good code is correct
16
![Page 22: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/22.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Good Code
Good code is correctConsistent; without contradiction
16
![Page 23: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/23.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Simple Bug
void print_string(const char* s) { while (*s != '\0') { cout << *s++; } }
int main() { print_string(nullptr); }
17
![Page 24: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/24.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Simple Bug
void print_string(const char* s) { while (*s != '\0') { Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) cout << *s++; } }
int main() { print_string(nullptr); }
18
![Page 25: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/25.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
void print_string(const char* s) { while (*s != '\0') { cout << *s++; } }
int main() { print_string(nullptr); }
Simple Bug
19
![Page 26: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/26.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
void print_string(const char* s) { while (*s != '\0') { cout << *s++; } }
int main() { print_string(nullptr); }
// FORCE CRASH!
Simple Bug
19
![Page 27: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/27.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Subtle defects
20
![Page 28: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/28.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Subtle defects
Consistency requires context
20
![Page 29: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/29.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Subtle defects
Consistency requires context
template<class T> const T& min(const T& a, const T& b);
Returns: The smaller value.Remarks: Returns the first argument when the arguments are equivalent.
20
![Page 30: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/30.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Subtle defects
Consistency requires context
template<class T> const T& min(const T& a, const T& b);
Returns: The smaller value.Remarks: Returns the first argument when the arguments are equivalent.
template<class T> const T& max(const T& a, const T& b);
Returns: The larger value. Remarks: Returns the first argument when the arguments are equivalent.
20
![Page 31: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/31.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Subtle defects
21
![Page 32: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/32.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Subtle defects
template<typename T>const T& clamp(const T& a, const T& lo, const T& hi){ return min(max(lo, a), hi);}
21
![Page 33: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/33.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Subtle defects
template<typename T>const T& clamp(const T& a, const T& lo, const T& hi){ return min(max(lo, a), hi);}
template<typename T, typename Compare>const T& clamp(const T& a, const T& lo, const T& hi, Compare comp){ return min(max(lo, a, comp), hi, comp);}
21
![Page 34: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/34.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Subtle defects
22
![Page 35: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/35.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Subtle defects
int main() { using pair = pair<int, string>;
pair a = { 1, "OK" };
pair lo = { 1, "FAIL: LO" }; pair hi = { 2, "FAIL: HI" };
a = clamp(a, lo, hi, [](const auto& a, const auto& b) { return a.first < b.first; });
cout << a.second << endl;};
22
![Page 36: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/36.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Subtle defects
int main() { using pair = pair<int, string>;
pair a = { 1, "OK" };
pair lo = { 1, "FAIL: LO" }; pair hi = { 2, "FAIL: HI" };
a = clamp(a, lo, hi, [](const auto& a, const auto& b) { return a.first < b.first; });
cout << a.second << endl;};
FAIL: LO
22
![Page 37: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/37.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Subtle defects
23
![Page 38: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/38.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Subtle defects
template<typename T>const T& clamp(const T& a, const T& lo, const T& hi){ return min(max(a, lo), hi);}
23
![Page 39: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/39.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Subtle defects
template<typename T>const T& clamp(const T& a, const T& lo, const T& hi){ return min(max(a, lo), hi);}
template<typename T, typename Compare>const T& clamp(const T& a, const T& lo, const T& hi, Compare comp){ return min(max(a, lo, comp), hi, comp);}
23
![Page 40: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/40.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Subtle defects
24
![Page 41: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/41.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Subtle defects
template<class T> const T& min(const T& a, const T& b);
Returns: The smaller value.Remarks: Returns the first argument when the arguments are equivalent.
template<class T> const T& max(const T& a, const T& b);
Returns: The larger value. Remarks: Returns the second argument when the arguments are equivalent.
24
![Page 42: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/42.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Subtle defects
template<class T> const T& min(const T& a, const T& b);
Returns: The smaller value.Remarks: Returns the first argument when the arguments are equivalent.
template<class T> const T& max(const T& a, const T& b);
Returns: The larger value. Remarks: Returns the second argument when the arguments are equivalent.
template <class T> const T& max(const T& a, const T& b, const T& c);
Returns: The larger value. Remarks: ???
24
![Page 43: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/43.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Rules are Contentious
25
![Page 44: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/44.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Rules are Contentious
“Names should not be associated with semantics because everybody has their own hidden assumptions about what semantics are, and they clash, causing comprehension problems without knowing why. This is why it's valuable to
write code to reflect what code is actually doing, rather than what code ‘means’: it’s hard to have conceptual clashes about what code actually does.”
– Craig Silverstein, Google
25
![Page 45: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/45.jpg)
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
“There is no spoon.” – The Matrix
![Page 46: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/46.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
27
![Page 47: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/47.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int x;
27
![Page 48: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/48.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int x;// indeterminate value
27
![Page 49: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/49.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int x;// indeterminate value
int x = 1 / 0;
27
![Page 50: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/50.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int x;// indeterminate value
int x = 1 / 0;// undefined behavior
27
![Page 51: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/51.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int x;// indeterminate value
int x = 1 / 0;// undefined behavior
double x = 1.0 / 0.0;
27
![Page 52: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/52.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int x;// indeterminate value
int x = 1 / 0;// undefined behavior
double x = 1.0 / 0.0;// inf
27
![Page 53: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/53.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int x;// indeterminate value
int x = 1 / 0;// undefined behavior
double x = 1.0 / 0.0;// inf
double x = 0.0 / 0.0;
27
![Page 54: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/54.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int x;// indeterminate value
int x = 1 / 0;// undefined behavior
double x = 1.0 / 0.0;// inf
double x = 0.0 / 0.0;// NaN
27
![Page 55: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/55.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int x;// indeterminate value
int x = 1 / 0;// undefined behavior
double x = 1.0 / 0.0;// inf
double x = 0.0 / 0.0;// NaN
struct empty { };
27
![Page 56: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/56.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int x;// indeterminate value
int x = 1 / 0;// undefined behavior
double x = 1.0 / 0.0;// inf
double x = 0.0 / 0.0;// NaN
struct empty { };// sizeof(empty) == 1
27
![Page 57: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/57.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
28
![Page 58: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/58.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int a[0];
28
![Page 59: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/59.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int a[0];// Error
28
![Page 60: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/60.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int a[0];// Error// but common extension
28
![Page 61: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/61.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int a[0];// Error// but common extensionusing empty = int[0];
28
![Page 62: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/62.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int a[0];// Error// but common extensionusing empty = int[0];// sizeof(empty) == 0empty a[2];
28
![Page 63: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/63.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int a[0];// Error// but common extensionusing empty = int[0];// sizeof(empty) == 0empty a[2];// &a[0] == &a[1]
28
![Page 64: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/64.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int a[0];// Error// but common extensionusing empty = int[0];// sizeof(empty) == 0empty a[2];// &a[0] == &a[1]
void f() { return void(); }
28
![Page 65: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/65.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int a[0];// Error// but common extensionusing empty = int[0];// sizeof(empty) == 0empty a[2];// &a[0] == &a[1]
void f() { return void(); }// OK
28
![Page 66: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/66.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int a[0];// Error// but common extensionusing empty = int[0];// sizeof(empty) == 0empty a[2];// &a[0] == &a[1]
void f() { return void(); }// OK
void x = f();
28
![Page 67: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/67.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int a[0];// Error// but common extensionusing empty = int[0];// sizeof(empty) == 0empty a[2];// &a[0] == &a[1]
void f() { return void(); }// OK
void x = f();// Error
28
![Page 68: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/68.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int a[0];// Error// but common extensionusing empty = int[0];// sizeof(empty) == 0empty a[2];// &a[0] == &a[1]
void f() { return void(); }// OK
void x = f();// Error// but void* is a pointer to anything…
28
![Page 69: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/69.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
29
![Page 70: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/70.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
std::vector<int> x = { 1, 2, 3 };try { x.insert(x.begin(), 0);} catch (...) { std::cout << x.size() << std::endl; }
29
![Page 71: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/71.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
std::vector<int> x = { 1, 2, 3 };try { x.insert(x.begin(), 0);} catch (...) { std::cout << x.size() << std::endl; }// Basic Exception Guarantee:// Valid but unspecified
29
![Page 72: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/72.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
std::vector<int> x = { 1, 2, 3 };try { x.insert(x.begin(), 0);} catch (...) { std::cout << x.size() << std::endl; }// Basic Exception Guarantee:// Valid but unspecified
std::vector<int> y = std::move(x);
29
![Page 73: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/73.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
std::vector<int> x = { 1, 2, 3 };try { x.insert(x.begin(), 0);} catch (...) { std::cout << x.size() << std::endl; }// Basic Exception Guarantee:// Valid but unspecified
std::vector<int> y = std::move(x);// Moved from object, x, is valid but unspecified
29
![Page 74: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/74.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Good Code
30
![Page 75: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/75.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Good Code
Good code is correctConsistent; without contradiction
30
![Page 76: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/76.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Good Code
Good code is correctConsistent; without contradiction
Good code has meaning
30
![Page 77: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/77.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Good Code
Good code is correctConsistent; without contradiction
Good code has meaningCorrespondence to an entity; specified, defined
30
![Page 78: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/78.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved. 31
ƒ!
Menu Item
![Page 79: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/79.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Categories of nothing
32
![Page 80: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/80.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Categories of nothing
Absence of something0, Ø, [p, p), void
32
![Page 81: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/81.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Categories of nothing
Absence of something0, Ø, [p, p), void
Absence of meaningNaN, undefined, indeterminate
32
![Page 82: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/82.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
33
![Page 83: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/83.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int x;
33
![Page 84: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/84.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int x;// Partially formed; assign value or destruct
33
![Page 85: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/85.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int x;// Partially formed; assign value or destruct
int x = 1 / 0;
33
![Page 86: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/86.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int x;// Partially formed; assign value or destruct
int x = 1 / 0;// undefined behavior; reading from meaningless value
33
![Page 87: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/87.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int x;// Partially formed; assign value or destruct
int x = 1 / 0;// undefined behavior; reading from meaningless value
double x = 1.0 / 0.0;
33
![Page 88: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/88.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int x;// Partially formed; assign value or destruct
int x = 1 / 0;// undefined behavior; reading from meaningless value
double x = 1.0 / 0.0;// inf; OK, approximation for underflow
33
![Page 89: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/89.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int x;// Partially formed; assign value or destruct
int x = 1 / 0;// undefined behavior; reading from meaningless value
double x = 1.0 / 0.0;// inf; OK, approximation for underflow
double x = 0.0 / 0.0;
33
![Page 90: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/90.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int x;// Partially formed; assign value or destruct
int x = 1 / 0;// undefined behavior; reading from meaningless value
double x = 1.0 / 0.0;// inf; OK, approximation for underflow
double x = 0.0 / 0.0;// NaN; OK, though undefined behavior would also be
33
![Page 91: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/91.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int x;// Partially formed; assign value or destruct
int x = 1 / 0;// undefined behavior; reading from meaningless value
double x = 1.0 / 0.0;// inf; OK, approximation for underflow
double x = 0.0 / 0.0;// NaN; OK, though undefined behavior would also be
33
![Page 92: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/92.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int x;// Partially formed; assign value or destruct
int x = 1 / 0;// undefined behavior; reading from meaningless value
double x = 1.0 / 0.0;// inf; OK, approximation for underflow
double x = 0.0 / 0.0;// NaN; OK, though undefined behavior would also be
struct empty : void { };
33
![Page 93: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/93.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int x;// Partially formed; assign value or destruct
int x = 1 / 0;// undefined behavior; reading from meaningless value
double x = 1.0 / 0.0;// inf; OK, approximation for underflow
double x = 0.0 / 0.0;// NaN; OK, though undefined behavior would also be
struct empty : void { };// sizeof(empty) == 0;
33
![Page 94: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/94.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
34
![Page 95: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/95.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int a[0];
34
![Page 96: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/96.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int a[0];// OK
34
![Page 97: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/97.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int a[0];// OKusing empty = int[0];
34
![Page 98: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/98.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int a[0];// OKusing empty = int[0];// sizeof(empty) == 0empty a[2];
34
![Page 99: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/99.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int a[0];// OKusing empty = int[0];// sizeof(empty) == 0empty a[2];// &a[0] == &a[1]
34
![Page 100: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/100.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int a[0];// OKusing empty = int[0];// sizeof(empty) == 0empty a[2];// &a[0] == &a[1]
void f() { return void(); }
34
![Page 101: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/101.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int a[0];// OKusing empty = int[0];// sizeof(empty) == 0empty a[2];// &a[0] == &a[1]
void f() { return void(); }// OK
34
![Page 102: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/102.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int a[0];// OKusing empty = int[0];// sizeof(empty) == 0empty a[2];// &a[0] == &a[1]
void f() { return void(); }// OK
void x = f();
34
![Page 103: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/103.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
int a[0];// OKusing empty = int[0];// sizeof(empty) == 0empty a[2];// &a[0] == &a[1]
void f() { return void(); }// OK
void x = f();// OK// void* is OK
34
![Page 104: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/104.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
35
![Page 105: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/105.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
std::vector<int> x = { 1, 2, 3 };try { x.insert(x.begin(), 0);} catch (...) { std::cout << x.size() << std::endl; }
35
![Page 106: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/106.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
std::vector<int> x = { 1, 2, 3 };try { x.insert(x.begin(), 0);} catch (...) { std::cout << x.size() << std::endl; }// Basic Exception Guarantee:// Partially formed object. Reading is undefined behavior
35
![Page 107: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/107.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
std::vector<int> x = { 1, 2, 3 };try { x.insert(x.begin(), 0);} catch (...) { std::cout << x.size() << std::endl; }// Basic Exception Guarantee:// Partially formed object. Reading is undefined behavior
std::vector<int> y = std::move(x);
35
![Page 108: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/108.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
How can nothing be something?
std::vector<int> x = { 1, 2, 3 };try { x.insert(x.begin(), 0);} catch (...) { std::cout << x.size() << std::endl; }// Basic Exception Guarantee:// Partially formed object. Reading is undefined behavior
std::vector<int> y = std::move(x);// Moved from object, x, is partially formed
35
![Page 109: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/109.jpg)
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
“That makes you wonder. Take chicken, for example.” – Matrix
![Page 110: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/110.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Specification
37
![Page 111: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/111.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Specification
▪ clone_ptr<T> is like std::unique_ptr<T> but with two additional operations, copy and assignment that copy the object pointed to.
▪ Example implementation of new operations: clone_ptr(const clone_ptr& x) : _ptr(new T(*x)) { } clone_ptr& operator=(const clone_ptr& x) { return *this = clone_ptr(x); }
37
![Page 112: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/112.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Specification
▪ clone_ptr<T> is like std::unique_ptr<T> but with two additional operations, copy and assignment that copy the object pointed to.
▪ Example implementation of new operations: clone_ptr(const clone_ptr& x) : _ptr(new T(*x)) { } clone_ptr& operator=(const clone_ptr& x) { return *this = clone_ptr(x); }
▪ copy-assignment written in terms of copy and move-assignment
37
![Page 113: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/113.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
What is copy?
▪ Copying an object creates a new object which is equal-to and logically disjoint from the original.
T a = b; ⇒ a == b; T a = b; modify(b); ⇒ a != b;
38
![Page 114: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/114.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
“copy” of clone_ptr
clone_ptr<T> a = b; ⇒ a != b;
▪ “Copying” a clone pointer creates an object that is not equal to the original ▪ Contradiction
▪ Defining a copy-constructor that doesn’t copy is dangerous ▪ The compiler may elide copies ▪ Programmers will assume they are substitutable
39
![Page 115: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/115.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Specification: Amendment 1
▪ Two clone_ptrs are considered equal if the value they point to is equal. Because we don’t want to require that the pointed to types are equal operator==() and operator!=() are not implemented. i.e.:
clone_ptr<T> a = b; ⇒ a == b;
However, == is not implemented.
40
![Page 116: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/116.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
What is a pointer?
▪ A pointer is an object that refers to another object via a dereference operation. Two pointers are equal if they refer to the same instance of an object.
a == b; ⇒ &*a == &*b;
41
![Page 117: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/117.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
“equality” of clone_ptr
clone_ptr<T> a = b; ⇒ a == b;
▪ Because clone_ptr is a pointer this would imply:
assert(&*a == &*b);
▪ But that is false - contradiction.
42
![Page 118: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/118.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Specification: Amendment 2
▪ Because clone_ptr<> is not a pointer it is to be renamed indirect<>.
43
![Page 119: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/119.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
What is a const?
▪ const is a type qualifier. An object accessed through a const reference may not be modified.
const T a = b; read(a); ⇒ a == b; modify(a); is not allowed
44
![Page 120: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/120.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
“const” of indirect
const indirect<T> a = b; read(a); ⇏ a == b;
▪ Because const does not propagate (from unique_ptr):
void read(const indirect<T>& x) { modify(*x); }
▪ Contradiction!
45
![Page 121: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/121.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Specification: Amendment 3
▪ Because copy of remote part implies const propagation, get(), operator*() and operator->() must be overloaded:
T* get(); const T* get() const;
T& operator*(); const T& operator*() const;
T* operator->(); const T* operator->() const;
46
![Page 122: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/122.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Alternative Specification:
47
![Page 123: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/123.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Alternative Specification:
▪ clone_ptr<T> is like std::unique_ptr<T> but with one additional operation, clone() that works by copying the object pointed to.
▪ Example implementation of clone operation:
clone_ptr clone() const { return make_clone<T>(**this); }
47
![Page 124: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/124.jpg)
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
“What’s in the box?” – Seven
![Page 125: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/125.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
The Permutation Paradox
49
![Page 126: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/126.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
The Permutation Paradox
50
![Page 127: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/127.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
The Permutation Paradox
50
???
![Page 128: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/128.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
The Permutation Paradox
50
???
nothing ⇒ unsafe
![Page 129: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/129.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
The Permutation Paradox
50
???
nothing ⇒ unsafe
something ⇒ inefficient
![Page 130: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/130.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
The Permutation Paradox
51
![Page 131: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/131.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
“There is a duality between transformations and the corresponding actions: An action is definable in terms of a transformation and vice versa:
The Permutation Paradox
51
![Page 132: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/132.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
“There is a duality between transformations and the corresponding actions: An action is definable in terms of a transformation and vice versa:
void a(T& x) { x = f(x); } // action from transformation
and
T f(T x) { a(x); return x; } // transformation from action
The Permutation Paradox
51
![Page 133: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/133.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
“There is a duality between transformations and the corresponding actions: An action is definable in terms of a transformation and vice versa:
void a(T& x) { x = f(x); } // action from transformation
and
T f(T x) { a(x); return x; } // transformation from action
Despite this duality, independent implementations are sometimes more efficient, in which case both action and transformation need to be provided.”
– Elements of Programming (section 2.5)
The Permutation Paradox
51
![Page 134: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/134.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Purity
52
This section borrowed from Andrei Alexandrescu
![Page 135: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/135.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
▪ Text book purity requires tail-recursion
Purity
52
This section borrowed from Andrei Alexandrescu
![Page 136: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/136.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
▪ Text book purity requires tail-recursion
// If C++ had tail recursion
int helper(int n, int result) { return n <= 1 ? result : helper(n - 1, n * result);}
int factorial(int n) { return helper(n, 1);}
Purity
52
This section borrowed from Andrei Alexandrescu
![Page 137: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/137.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Purity
53
![Page 138: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/138.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
▪ In math, factorial is defined as iteration
Purity
53
![Page 139: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/139.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
▪ In math, factorial is defined as iteration
int factorial(int n) { int result = 1; for (int i = 2; i <= n; ++i) { result *= i; } return result;}
Purity
53
![Page 140: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/140.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Purity
54
![Page 141: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/141.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
▪ Pure functions always return the same result for the same arguments▪ No reading and writing of global variables (global constants are okay)▪ No calling of impure functions▪ Local transient state, inside the function, may be modified▪ Anything reachable from the arguments may be modified
Purity
54
![Page 142: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/142.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
▪ Pure functions always return the same result for the same arguments▪ No reading and writing of global variables (global constants are okay)▪ No calling of impure functions▪ Local transient state, inside the function, may be modified▪ Anything reachable from the arguments may be modified▪ Action to Function Transformation
Purity
54
![Page 143: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/143.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
▪ Pure functions always return the same result for the same arguments▪ No reading and writing of global variables (global constants are okay)▪ No calling of impure functions▪ Local transient state, inside the function, may be modified▪ Anything reachable from the arguments may be modified▪ Action to Function Transformation▪ std::sort is pure
Purity
54
![Page 144: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/144.jpg)
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
“It’s not that I’m lazy, it’s that I just don’t care.” – Office Space
![Page 145: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/145.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Good Code
Good code is correctConsistent; without contradiction
Good code has meaningCorrespondence to an entity; specified, defined
56
![Page 146: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/146.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Good Code
Good code is correctConsistent; without contradiction
Good code has meaningCorrespondence to an entity; specified, defined
Good code is efficient
56
![Page 147: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/147.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Good Code
Good code is correctConsistent; without contradiction
Good code has meaningCorrespondence to an entity; specified, defined
Good code is efficientMaximum effect with minimum resources
56
![Page 148: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/148.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Efficiency
57
![Page 149: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/149.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Choice of data structures and algorithms
Choice of what to optimize for
Efficiency
57
![Page 150: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/150.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Efficiency
58
A B C D E F G
![Page 151: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/151.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Efficiency
58
ABCDEFG
![Page 152: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/152.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
template <class ForwardIterator, class N> auto reverse_n(ForwardIterator f, N n) { if (n < 2) return next(f, n);
auto h = n / 2; auto m1 = reverse_n(f, h); auto m2 = next(m1, n % 2); auto l = reverse_n(m2, h); swap_ranges(f, m1, m2); return l; }
template <class ForwardIterator> void reverse(ForwardIterator f, ForwardIterator l) { reverse_n(f, distance(f, l)); }
O(n log n)
Efficiency
59
Elements of Programming, 10.3
![Page 153: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/153.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Efficiency
60
A B C D E F G
![Page 154: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/154.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Efficiency
60
ABCDEFG
![Page 155: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/155.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Simple Word Model
61
![Page 156: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/156.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Simple Word Model
▪ Current Document ▪ Selection ▪ Provides a range; an empty range denotes a location
62
![Page 157: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/157.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
More Complex Word Model
▪ Need to be able to set the selection in “constant” time ▪ This would imply a vector data structure ▪ Also need constant time insert and erase ▪ This would imply a list data structure
▪ Solution: a more complex data structure such as a rope
63
![Page 158: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/158.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
What is an efficient type?
64
![Page 159: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/159.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
What is an efficient type?
▪ A type is complete if the set of provided basis operations allow us to construct and operate on any valid, representable value
▪ A type is efficient if the set of basis operations allow for any valid operation to be performed in the most efficient way possible for the chosen representation
64
![Page 160: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/160.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
What is an efficient type?
▪ A type is complete if the set of provided basis operations allow us to construct and operate on any valid, representable value
▪ A type is efficient if the set of basis operations allow for any valid operation to be performed in the most efficient way possible for the chosen representation
▪ By simply making all data members public, you provide, by definition, an efficient basis ▪ However, you may fail to protect the invariants of the type, making the approach unsafe
▪ std::move is both unsafe an inefficient.
64
![Page 161: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/161.jpg)
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
“I don’t smoke, I don’t drink... I recycle...” – 50/50
![Page 162: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/162.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Good Code
Good code is correct Consistent; without contradiction
Good code has meaning Correspondence to an entity; specified, defined
Good code is efficient Maximum effect with minimum resources
Good code is reusable Applicable to multiple problems; general in purpose
66
![Page 163: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/163.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Reusable
67
![Page 164: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/164.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Concrete but of general use, i.e. numeric algorithms, utf conversions, …
Generic when algorithm is useful with different modelsSometimes faster to convert one model to another
Runtime dispatched when types not known at compile time
Reusable
67
![Page 165: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/165.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Reusable
68
![Page 166: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/166.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Minimize client dependencies and intrusive requirements
Separate data structures from algorithms
Reusable
68
![Page 167: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/167.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Reusable
69
![Page 168: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/168.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
template <class T, class InputIterator, class OutputIterator>OutputIterator copy_utf(InputIterator first, InputIterator last, OutputIterator result);
const char str[] = u8"Hello World!";vector<uint16_t> out;copy_utf<uint16_t>(begin(str), end(str), back_inserter(out));
Reusable
69
![Page 169: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/169.jpg)
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
“You mean we’re in the future.” – Back to the Future Part II
![Page 170: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/170.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Why Status Quo Will Fail
71
![Page 171: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/171.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
“I’ve assigned this problem [binary search] in courses at Bell Labs and IBM. Professional programmers had a couple of hours to convert the description into a programming language of their choice; a high-level pseudo code was fine…
Ninety percent of the programmers found bugs in their programs (and I wasn’t always convinced of the correctness of the code in which no bugs were found).”
– Jon Bentley, Programming Pearls, 1986
Why Status Quo Will Fail
71
![Page 172: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/172.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
int* lower_bound(int* first, int* last, int value) { while (first != last) { int* middle = first + (last - first) / 2;
if (*middle < value) first = middle + 1; else last = middle; }
return first; }
Why Status Quo Will Fail
72
![Page 173: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/173.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Elements of Programming
Concepts aren’t dead yet in C++ Increased interest in new languages and formalisms Renewed interest in Communication Sequential Processes Renewed interest in Functional Programming ideas Rise of Reactive Programming & Functional Reactive Programming
Signs of Hope
73
![Page 174: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/174.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Work Continues
74
![Page 175: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/175.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved.
Generating Reactive Programs for Graphical User Interfaces from Multi-way Dataflow Constraint Systems, GPCE 2015, Gabriel Foust, Jaakko Järvi, Sean Parent
One Way To Select Many, ECOOP 2016, Jaakko Järvi, Sean Parent
http://sean-parent.stlab.cc/papers-and-presentationshttps://github.com/stlab
Work Continues
74
![Page 176: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/176.jpg)
© 2016 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Write Better Code
75
![Page 177: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/177.jpg)
![Page 178: Better Code - Sean Parent€¦ · Forest Hill Anerley Penge West Honor Oak Park Brockley Wapping New Cross Queens Road Peckham Peckham Rye ... Northwood Hills Pinner North Harrow](https://reader034.vdocuments.site/reader034/viewer/2022052015/602c8b894c629e531538fcd7/html5/thumbnails/178.jpg)