Table Of ContentTHE KOLLECTED
KODE VICIOUS
This page intentionally left blank
THE KOLLECTED
KODE VICIOUS
OPINIONATED ADVICE FOR
PROGRAMMERS
George V. Neville-Neil
Boston • Columbus • New York • San Francisco • Amsterdam • Cape Town
Dubai • London • Madrid • Milan • Munich • Paris • Montreal • Toronto • Delhi • Mexico City
São Paulo • Sydney • Hong Kong • Seoul • Singapore • Taipei • Tokyo
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks.
Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have
been printed with initial capital letters or in all capitals.
The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of
any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential dam-
ages in connection with or arising out of the use of the information or programs contained herein.
For information about buying this title in bulk quantities, or for special sales opportunities (which may include electronic
versions; custom cover designs; and content particular to your business, training goals, marketing focus, or branding
interests), please contact our corporate sales department at [email protected] or (800) 382-3419.
For government sales inquiries, please contact [email protected].
For questions about sales outside the U.S., please contact [email protected].
Visit us on the Web: informit.com/aw
Library of Congress Control Number: 2020942789
Copyright © 2021 Pearson Education, Inc.
Cover images: Little Princess/Shutterstock, alexslb/Shutterstock, silky/Shutterstock
All rights reserved. This publication is protected by copyright, and permission must be obtained from the publisher prior
to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic,
mechanical, photocopying, recording, or likewise. For information regarding permissions, request forms and the appro-
priate contacts within the Pearson Education Global Rights & Permissions Department, please visit www.pearson.com/
permissions.
ISBN-13: 978-0-13-678824-9
ISBN-10: 0-13-678824-6
ScoutAutomatedPrintCode
To Kaz, who had to hear these often angry outbursts before they ever
reached the page.
v
This page intentionally left blank
Contents
Foreword by Donald E. Knuth (DK) ........................... xi
Preface ....................................................xiii
Acknowledgments .........................................xvi
About the Author ......................................... xvii
1 The Kode at Hand .......................................................1
1.1 Resource Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2 Big Big Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.3 Coded Arrangements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.4 Code Abuse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.5 Nesting Tendencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.6 Choking on Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.7 Cursed Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
1.8 Forced Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
1.9 A Nice Piece...of Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
1.10 Something Rotten in... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
1.11 Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
1.12 Lost . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
1.13 Copying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
1.14 Top Five Koding Peeves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
1.15 Linguistically Lost . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
1.16 Check in Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
2 Koding Konundrums ................................................. 51
2.1 Ode to the Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
2.2 How Much + in C++? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
2.3 Something Sleek and Modern . . . . . . . . . . . . . . . . . . . . . . . . 58
2.4 What’s in a Cache Miss? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
2.5 Code Spelunking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
vii
viii Contents
2.6 Input Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
2.7 Dickering with Docs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
2.8 What’s in the Foo Field? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
2.9 Testy Tester . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
2.10 How to Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
2.11 Leave the Test Modes In! . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
2.12 Maintenance Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
2.13 Merge Early . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
2.14 Multicore Manticore . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
2.15 This Is Not a Product . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
2.16 Heisenbugs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
2.17 I Don’t Want Your Dirty PDFs . . . . . . . . . . . . . . . . . . . . . . . . 109
2.18 Pining for a PIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
2.19 Reboot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
2.20 Code Scanners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
2.21 Debugging Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
2.22 Sanity vs. Visibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
3 Systems Design ...................................................... 127
3.1 Abstractions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
3.2 Driven . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
3.3 Driven Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
3.4 Changative Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
3.5 Threading the Needle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
3.6 Threads Still Unsafe? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
3.7 Authentication vs. Encryption . . . . . . . . . . . . . . . . . . . . . . . 149
3.8 Authentication Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
3.9 Authentication by Example . . . . . . . . . . . . . . . . . . . . . . . . . 155
3.10 Cross-Site Scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
3.11 Phishing and Infections . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
3.12 UI Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Contents ix
3.13 Secure Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
3.14 Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
3.15 Secure P2P . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
4 Machine to Machine ................................................ 189
4.1 Stepping on Toes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
4.2 Paucity of Ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
4.3 Protocol Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
4.4 Which Came First? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
4.5 Debugging the Network . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
4.6 Latency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
4.7 Long Distance Runaround . . . . . . . . . . . . . . . . . . . . . . . . . . 211
4.8 The Network Is the Computer . . . . . . . . . . . . . . . . . . . . . . . . 215
4.9 Failure to Scale. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
4.10 Port Squatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
4.11 Networking in the Raw . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
4.12 Pointless PKI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
4.13 Standard on Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
5 Human to Human ................................................... 235
5.1 Of Pride and... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
5.2 What Color Is Your...? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
5.3 Broken Builds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
5.4 What Is Intelligence? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
5.5 Review the Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
5.6 The Naming of Hosts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
5.7 Hosting an Interview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
5.8 Mythical . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
5.9 The Obsolete Koder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
5.10 With Great Power... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
5.11 The Letter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270