35
Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

  • View
    228

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Smalltalk Coding Patterns

mostly fromSmalltalk Best Practice Patterns

Kent BeckPrentice-Hall, 1997

Page 2: Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Stéphane Ducasse «ChapterNr».2

Coding Standards

•Standards–improve communication–let code be the design–make code more habitable–change

Page 3: Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Stéphane Ducasse «ChapterNr».3

Coding Standards for Smalltalk

•Variables have no types•Names can be any length•Operations named with keywords•Pretty printer

Page 4: Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Stéphane Ducasse «ChapterNr».4

Names

•Names should mean something.

•Standard protocols–Object (printOn:, =)–Collection (do:, add:, at:put:, size)

•Standard naming conventions

Page 5: Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Stéphane Ducasse «ChapterNr».5

Intention Revealing Selector

•Readability of message send is more important than readability of method.

•Name should specify what method does, not how.

Page 6: Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Stéphane Ducasse «ChapterNr».6

Method Names

• If there is already a standard name, use it instead of following these rules.

•Three kinds of methods–change state of receiver–change state of argument–return value from receiver

Page 7: Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Stéphane Ducasse «ChapterNr».7

Change state of receiver

•method name is verb phrase–translateBy:–add:

Page 8: Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Stéphane Ducasse «ChapterNr».8

Change state of argument

•Verb phrase ending with preposition like on or to.–displayOn:–addTo:

Page 9: Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Stéphane Ducasse «ChapterNr».9

Return value from receiver

•method name is noun phrase or adjective, a description rather than a command–translatedBy:–size–topLeft

Page 10: Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Stéphane Ducasse «ChapterNr».10

Method Names

•Specialized names for specialized purposes.–Double-dispatching methods–Accessing methods–Query methods–Boolean property setting–Converter methods

Page 11: Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Stéphane Ducasse «ChapterNr».11

Accessing Methods

•Many instance variables have accessing methods, methods for reading and writing them.

•Same name than the instance variables•Accessing methods come in pairs.

•name, name:•width, width:•x, x:

Page 12: Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Stéphane Ducasse «ChapterNr».12

When to use Accessing Methods

•Two opinions:–Always, including an object’s own instance variable

•lazy initialization, subclassing is easier–Only when you need to use it.

•better information hiding•With the refactoring browser it is easy to transform

the class using or not accessing

Page 13: Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Stéphane Ducasse «ChapterNr».13

Query Method• Methods that return a value often describe the

type of the value because they are noun phrases.• Query methods are not noun phrases, but are

predicates. How can we make the return type clear?

• Provide a method that returns a Boolean in the “testing” protocol. Name it by prefacing the property name with a form of “be” or “has”- is, was, will, has

Page 14: Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Stéphane Ducasse «ChapterNr».14

Query Method by Example

• Instead of: Switch>>makeOn status := #onSwitch>>makeOff status := #offSwitch>>status ^status Client>>update self switch status = #on ifTrue: [self light makeOn] self switch status = #off ifTrue: [self light makeOff]

• It is better to defineSwitch>>isOn, Switch>>isOff

• Switch>>on is not a good name... #on: or #isOn ?

Page 15: Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Stéphane Ducasse «ChapterNr».15

Testing Method

•Prefix every testing method with "is".–isNil– isControlWanted–isEmpty–hasBorder

Page 16: Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Stéphane Ducasse «ChapterNr».16

How do you set a boolean property?

Switch>>on: aBoolean isOn := aBoolean• Expose the representation of the status to the

clients• Responsibility of who turn off/on the switch: the

client and not the object itself

• Create two methods beginning with “be”. One has the property name, the other the negation. Add “toggle” if the client doesn’t want to know about the current state

• beVisible/beInvisible/toggleVisible

Page 17: Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Stéphane Ducasse «ChapterNr».17

Boolean Property Setting•Don't make accessing methods whose only argument is a boolean.

•Create two methods beginning with "make". Add "toggle" if necessary.–• makeVisible / makeInvisible / toggleVisible–• makeDirty / makeClean

Page 18: Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Stéphane Ducasse «ChapterNr».18

Converting Method•Often you want to return the receiver in a new format.

•Prepend "as" to the name of the class of object returned.–asSet (in Collection)–asFloat (in Number)–asComposedText (in Text)

Page 19: Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Stéphane Ducasse «ChapterNr».19

Complete Creation Method•Class methods that create instances are in category "instance creation methods".–Creation followed by initialization is the most flexible.

•Point new x: 0; y: 0–Important to preserve invariants and avoid ill-formed objects.

Page 20: Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Stéphane Ducasse «ChapterNr».20

Complete Creation Method• Instance creation methods should create well-formed instances. Pass all required parameters to them.

•Point x: 0 y: 0•SortedCollection sortBlock: aBlock•Set new

Page 21: Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Stéphane Ducasse «ChapterNr».21

Creation Parameter Method•How should a Complete Creation Method initialize new object?–Separate setters are most flexible

x: aNumber y: anotherNumber^self new

x: aNumber; y: anotherNumber

Page 22: Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Stéphane Ducasse «ChapterNr».22

Creation Parameter Method•Provide a single method that sets all the variables. Preface its name with "set", then the names of the variables.

•Forces the client to specify all arguments•Place to check semantics constraints

x: aNumber y: anotherNumber^self new setX: aNumber y: anotherNumber

Page 23: Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Stéphane Ducasse «ChapterNr».23

Composed Method•How big should a method be?•Write methods that perform one identifiable task.–Few lines per method.–Consistent level of abstraction.–Minimizes number of methods you have to change in subclass.

–Minimizes code copying in subclass.

Page 24: Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Stéphane Ducasse «ChapterNr».24

Composed Method Usage•Top down

self input; process; output•Bottom up

–common expressions–long loop bodies–comments –From client - two or more messages to another object is suspicious

Page 25: Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Stéphane Ducasse «ChapterNr».25

Methods from Comments•Comments indicate "identifiable task"• If you need to comment a block of code, it probably should be a separate method.

•Turn method comment into method name.

Page 26: Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Stéphane Ducasse «ChapterNr».26

Simple Superclass Name•What should we call the root of a hierarchy?–Complex name conveys full meaning.–Simple name is easy to say, type, extend.–But need to show that subclasses are related.

Page 27: Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Stéphane Ducasse «ChapterNr».27

Simple Superclass Name

•Give superclasses simple names: two or (preferably) one word–Number–Collection–VisualComponent

Page 28: Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Stéphane Ducasse «ChapterNr».28

Qualified Subclass Name•What should you call a subclass that plays a role similar to its superclass?–Unique name conveys most information–Derived name communicates relationship to superclass

Page 29: Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Stéphane Ducasse «ChapterNr».29

Qualified Subclass Name•Use names with obvious meaning. Otherwise, prepend an adjective to most important superclass.–OrderedCollection–UndefinedObject–CloneFigureCommand, CompositeCommand, ConnectionCommand

Page 30: Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Stéphane Ducasse «ChapterNr».30

Variables: Roles vs. Types•Types are specified by classes

–aRectangle–aCollection–aView

•Roles - how an object is used –location–employees–topView

Page 31: Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Stéphane Ducasse «ChapterNr».31

Role Suggesting Instance Variable•What should you name an instance variable?–Type is important for understanding implementation. But class comment can describe type.

–Role communicates intent, and this harder to understand than type.

Page 32: Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Stéphane Ducasse «ChapterNr».32

Role Suggesting Instance Variable

•Name instance variables for the role they play. Make the name plural if the variable is a collection.–Point: x, y–Interval: start, stop, step–Polyline: vertices

Page 33: Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Stéphane Ducasse «ChapterNr».33

Type Suggesting Parameter Name•Name of variable can either communicate type or role.

•Keywords communicate their parameter's role, so name of variable should give new information.

Page 34: Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Stéphane Ducasse «ChapterNr».34

Type Suggesting Parameter Name•Name parameters according to their most general expected class, preceded by "a" or "an". If there is more than one parameter with the same expected class, precede the class with a descriptive word.

Page 35: Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997

Stéphane Ducasse «ChapterNr».35

Temporaries•Name temporaries after role they play.•Use temporaries to:

–collect intermediate results–reuse result of an expression–name result of an expression

•Methods are simpler when they don't use temporaries!