没有合适的资源?快使用搜索试试~ 我知道了~
首页Smalltalk Best Practice Patterns(Kent Beck).pdf
Smalltalk Best Practice Patterns(Kent Beck).pdf

Smalltalk Best Practice Patterns(Kent Beck).pdf
资源详情
资源评论
资源推荐

Coding Patterns page 1 of 147 9/30/2006
Smalltalk Best Practice Patterns
Volume 1: Coding
Kent Beck
First Class Software, Inc.
Draft for comment only.
Copyright (c) 1995, 1996, Kent Beck, All rights reserved.
Please send comments to:
voice: +1.408.338.4649
fax: +1.408.338.1115
email: 70761.1216@compuserve.com

Coding Patterns page 2 of 147 9/30/2006
Table of Contents
TABLE OF CONTENTS .......................................................................................................................................2
PREFACE......................................................................................................................................................5
ACKNOWLEDGMENTS.............................................................................................................................7
INTRODUCTION.........................................................................................................................................8
CODING............................................................................................................................................................9
Talking Programs........................................................................................................................................9
GOOD SOFTWARE...........................................................................................................................................11
S
TYLE.............................................................................................................................................................13
W
HAT’S MISSING? .........................................................................................................................................14
O
RGANIZATION ..............................................................................................................................................15
A
DOPTION ......................................................................................................................................................16
PATTERNS..................................................................................................................................................17
WHY PATTERNS WORK..................................................................................................................................18
R
OLE OF PATTERNS........................................................................................................................................20
Reading......................................................................................................................................................20
Development..............................................................................................................................................20
Review .......................................................................................................................................................20
Documentation ..........................................................................................................................................20
Clean Up ...................................................................................................................................................20
FORMAT .........................................................................................................................................................21
BEHAVIOR .................................................................................................................................................22
METHODS.......................................................................................................................................................23
Composed Method ................................................................................................................................................. 24
Complete Creation Method .................................................................................................................................... 26
Creation Parameter Method ................................................................................................................................... 27
Conversion ............................................................................................................................................................. 28
Converter Method .................................................................................................................................................. 29
Converter Creation Method.................................................................................................................................... 30
Constructor Method ............................................................................................................................................... 31
Query Method ........................................................................................................................................................ 32
Comparing Method ................................................................................................................................................ 33
Execute Around Method ........................................................................................................................................ 34
Debug Printing Method.......................................................................................................................................... 35
MESSAGES......................................................................................................................................................36
Message.................................................................................................................................................................. 37
Choosing Message ................................................................................................................................................. 38
Decomposing Message........................................................................................................................................... 40
Intention Revealing Message ................................................................................................................................. 41
Intention Revealing Selector .................................................................................................................................. 42
Double Dispatch..................................................................................................................................................... 43
Send Back .............................................................................................................................................................. 45
Super ...................................................................................................................................................................... 47
Extending Super..................................................................................................................................................... 48
Modifying Super .................................................................................................................................................... 49
Delegation.............................................................................................................................................................. 50
Simple Delegation.................................................................................................................................................. 51
Self Delegation....................................................................................................................................................... 52
Pluggable Behavior ................................................................................................................................................ 54
Pluggable Selector.................................................................................................................................................. 55
Pluggable Block ..................................................................................................................................................... 56
Collecting Parameter.............................................................................................................................................. 57

Coding Patterns page 3 of 147 9/30/2006
STATE..........................................................................................................................................................59
INSTANCE VARIABLES....................................................................................................................................60
Common State........................................................................................................................................................ 61
Variable State......................................................................................................................................................... 62
Explicit Initialization.............................................................................................................................................. 63
Lazy Initialization .................................................................................................................................................. 65
Default Value Method............................................................................................................................................ 66
Constant Method.................................................................................................................................................... 67
Direct Variable Access........................................................................................................................................... 68
Indirect Variable Access ........................................................................................................................................ 69
Getting Method ...................................................................................................................................................... 70
Setting Method....................................................................................................................................................... 71
Collection Accessor Method .................................................................................................................................. 72
Enumeration Method.............................................................................................................................................. 74
Boolean Property Setting Method.......................................................................................................................... 75
Role Suggesting Instance Variable Name .............................................................................................................. 76
TEMPORARY VARIABLES................................................................................................................................77
Temporary Variable ............................................................................................................................................... 78
Collecting Temporary Variable.............................................................................................................................. 79
Caching Temporary Variable ................................................................................................................................. 80
Explaining Temporary Variable............................................................................................................................. 81
Reusing Temporary Variable ................................................................................................................................. 82
Role Suggesting Temporary Variable Name.......................................................................................................... 83
COLLECTIONS..........................................................................................................................................84
CLASSES.........................................................................................................................................................85
Collection............................................................................................................................................................... 86
OrderedCollection.................................................................................................................................................. 87
RunArray................................................................................................................................................................ 88
Set .......................................................................................................................................................................... 89
Equality Method..................................................................................................................................................... 91
Hashing Method..................................................................................................................................................... 93
Dictionary............................................................................................................................................................... 94
SortedCollection..................................................................................................................................................... 96
Array ...................................................................................................................................................................... 97
ByteArray............................................................................................................................................................... 98
Interval................................................................................................................................................................... 99
COLLECTION PROTOCOL...............................................................................................................................101
IsEmpty................................................................................................................................................................ 102
Includes:............................................................................................................................................................... 103
Concatentation ..................................................................................................................................................... 104
Enumeration......................................................................................................................................................... 105
Do......................................................................................................................................................................... 106
Collect.................................................................................................................................................................. 107
Select/Reject......................................................................................................................................................... 108
Detect................................................................................................................................................................... 109
Inject:into:............................................................................................................................................................ 110
COLLECTION IDIOMS ....................................................................................................................................111
Duplicate Removing Set....................................................................................................................................... 112
Temporarily Sorted Collection............................................................................................................................. 113
Stack..................................................................................................................................................................... 114
Queue................................................................................................................................................................... 115
Searching Literal.................................................................................................................................................. 117
Lookup Cache ...................................................................................................................................................... 118
Parsing Stream ..................................................................................................................................................... 120
Concatenating Stream .......................................................................................................................................... 121
CLASSES ...................................................................................................................................................122
Simple Superclass Name...................................................................................................................................... 123

Coding Patterns page 4 of 147 9/30/2006
Qualified Subclass Name ..................................................................................................................................... 124
Factored Superclass.............................................................................................................................................. 125
FORMATTING.........................................................................................................................................126
Inline Message Pattern ......................................................................................................................................... 127
Type Suggesting Parameter Name........................................................................................................................ 128
Indented Control Flow ......................................................................................................................................... 129
Rectangular Block................................................................................................................................................ 130
Guard Clause........................................................................................................................................................ 131
Conditional Expression........................................................................................................................................ 132
Simple Enumeration Parameter............................................................................................................................ 134
Cascade ................................................................................................................................................................ 135
Yourself................................................................................................................................................................ 137
Interesting Return Value ...................................................................................................................................... 139
DEVELOPMENT EXAMPLE.................................................................................................................140
PROBLEM .....................................................................................................................................................141
A
RITHMETIC.................................................................................................................................................143
I
NTEGRATION ...............................................................................................................................................145
S
UMMARY....................................................................................................................................................147

Coding Patterns page 5 of 147 9/30/2006
Preface
This preface will explain what this book is about. It will convince you to buy this book, or you will
know why you shouldn’t (more of the former than the latter, I hope).
What’s it all about?
This book is about the simple things experienced, successful Smalltalkers do that beginners don’t.
In a sense it is a style guide. I have tried to penetrate beneath the surface, though, to get at the
human realities that make the rules work instead of focusing solely on the rules themselves.
The topics covered are the daily tactics of programming:
How do you choose names for objects, variables, and methods?
How do you break logic into methods?
How do you communicate most clearly through your code?
These are small issues. There are also many bigger technical reasons why projects fail (and many
more non-technical reasons). The attraction of this set of issues is that they are so tractable. You
don’t have to be a programming wizard to pick good names, you just have to have good advice.
The advice is broken into 89 patterns. Each pattern presents:
a recurring daily programming problem
the tradeoffs that affect solutions to the problem
a concrete recipe to create a solution for the problem
For example, here is a summary of a pattern called “Role Suggesting Temporary Variable Name”:
Problem: What do you name a temporary variable?
Tradeoffs:
You want to include lots of information in the name
You want the name to be short so it is easy to type and doesn’t make formatting
difficult
You don’t want redundant information in the name
You want to communicate why the variable exists
You want to communicate the type of the variable (i.e. what messages it is sent)
Solution: Name the variable after the role it plays. The type can be inferred from context,
and so doesn’t need to be part of the name.
剩余146页未读,继续阅读










安全验证
文档复制为VIP权益,开通VIP直接复制

评论2