art_of_readable_code.pdf

(28402 KB) Pobierz
The Art of Readable Code
Dustin Boswell and Trevor Foucher
Beijing
Cambridge
Farnham
Köln
Sebastopol
Tokyo
The Art of Readable Code
by Dustin Boswell and Trevor Foucher
Copyright © 2012 Dustin Boswell and Trevor Foucher. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also
available for most titles (http://my.safaribooksonline.com). For more information, contact our
corporate/institutional sales department: (800) 998-9938 or
corporate@oreilly.com.
Editor:
Mary Treseler
Production Editor:
Teresa Elsey
Copyeditor:
Nancy Wolfe Kotary
Proofreader:
Teresa Elsey
November 2011:
First Edition.
Indexer:
Potomac Indexing, LLC
Cover Designer:
Susan Thompson
Interior Designer:
David Futato
Illustrators:
Dave Allred and Robert Romano
Revision History for the First Edition:
2011-11-01
First release
See
http://oreilly.com/catalog/errata.csp?isbn=9780596802295
for release details.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly
Media, Inc.
The Art of Readable Code,
the image of sheet music, and related trade dress are trademarks of O’Reilly
Media, Inc.
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 O’Reilly Media, Inc., was aware of a trademark
claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and authors assume no
responsibility for errors or omissions, or for damages resulting from the use of the information contained
herein.
ISBN: 978-0-596-80229-5
[LSI]
1320175254
CONTENTS
PREFACE
1
CODE SHOULD BE EASY TO UNDERSTAND
What Makes Code “Better”?
The Fundamental Theorem of Readability
Is Smaller Always Better?
Does Time-Till-Understanding Conflict with Other Goals?
The Hard Part
SURFACE-LEVEL IMPROVEMENTS
vii
1
2
3
3
4
4
Part One
2
PACKING INFORMATION INTO NAMES
Choose Specific Words
Avoid Generic Names Like tmp and retval
Prefer Concrete Names over Abstract Names
Attaching Extra Information to a Name
How Long Should a Name Be?
Use Name Formatting to Convey Meaning
Summary
NAMES THAT CAN’T BE MISCONSTRUED
Example: Filter()
Example: Clip(text, length)
Prefer min and max for (Inclusive) Limits
Prefer first and last for Inclusive Ranges
Prefer begin and end for Inclusive/Exclusive Ranges
Naming Booleans
Matching Expectations of Users
Example: Evaluating Multiple Name Candidates
Summary
AESTHETICS
Why Do Aesthetics Matter?
Rearrange Line Breaks to Be Consistent and Compact
Use Methods to Clean Up Irregularity
Use Column Alignment When Helpful
Pick a Meaningful Order, and Use It Consistently
Organize Declarations into Blocks
Break Code into “Paragraphs”
Personal Style versus Consistency
Summary
7
8
10
13
15
18
20
21
23
24
24
25
26
26
27
27
29
31
33
34
35
37
38
39
40
41
42
43
3
4
iii
Zgłoś jeśli naruszono regulamin