the_art_of_readable_code.pdf

(25472 KB) Pobierz
779269987.007.png
779269987.008.png
The Art of Readable Code
Dustin Boswell and Trevor Foucher
Beijing
Cambridge
Farnham
Köln
Sebastopol
Tokyo
779269987.009.png 779269987.010.png 779269987.001.png 779269987.002.png 779269987.003.png 779269987.004.png 779269987.005.png 779269987.006.png
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
Indexer: Potomac Indexing, LLC
Cover Designer: Susan Thompson
Interior Designer: David Futato
Illustrators: Dave Allred and Robert Romano
November 2011:
First Edition.
Revision History for the First Edition:
2011-11-01 First release
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
C O N T E N T S
PREFACE
vii
CODE SHOULD BE EASY TO UNDERSTAND
1
1
What Makes Code “Better”?
2
The Fundamental Theorem of Readability
3
Is Smaller Always Better?
3
Does Time-Till-Understanding Conflict with Other Goals?
4
The Hard Part
4
Part One SURFACE-LEVEL IMPROVEMENTS
PACKING INFORMATION INTO NAMES
7
2
Choose Specific Words
8
Avoid Generic Names Like tmp and retval
10
Prefer Concrete Names over Abstract Names
13
Attaching Extra Information to a Name
15
How Long Should a Name Be?
18
Use Name Formatting to Convey Meaning
20
Summary
21
NAMES THAT CAN’T BE MISCONSTRUED
23
3
Example: Filter()
24
Example: Clip(text, length)
24
Prefer min and max for (Inclusive) Limits
25
Prefer first and last for Inclusive Ranges
26
Prefer begin and end for Inclusive/Exclusive Ranges
26
Naming Booleans
27
Matching Expectations of Users
27
Example: Evaluating Multiple Name Candidates
29
Summary
31
AESTHETICS
33
4
Why Do Aesthetics Matter?
34
Rearrange Line Breaks to Be Consistent and Compact
35
Use Methods to Clean Up Irregularity
37
Use Column Alignment When Helpful
38
Pick a Meaningful Order, and Use It Consistently
39
Organize Declarations into Blocks
40
Break Code into “Paragraphs”
41
Personal Style versus Consistency
42
Summary
43
iii
Zgłoś jeśli naruszono regulamin