stoop sed-smells

13
S.Ducasse 1 QuickTime™ TIFF (Uncompr are needed t Stéphane Ducasse Stephane.Ducasse@univ-sav oie.fr http://www.iam.unibe.ch/ ~ducass e/ Elements of Design - Simple Smells

Upload: the-world-of-smalltalk

Post on 27-Nov-2014

510 views

Category:

Technology


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Stoop sed-smells

S.Ducasse 1

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

Stéphane [email protected]://www.iam.unibe.ch/~ducasse/

Elements of Design -Simple Smells

Page 2: Stoop sed-smells

S.Ducasse 2

Basic Design Mistakes

Page 3: Stoop sed-smells

S.Ducasse 3

A Class should haveClass Person {

String getName(); void setName(String name);int getAge(); void setAge(int age);Car getCar(); void setCar(Car car);

}What do we see ?

A class should have one main responsibility and some behavior not just holding stateMinimal access to its data!

Page 4: Stoop sed-smells

S.Ducasse 4

Confusing Class City extends Place { … }Class Jerusalem extends City implements Capital { … }Class TelAviv extends City { … }

What is wrong here?

Confusing inheritance and instantiationToo much inheritance?

Page 5: Stoop sed-smells

S.Ducasse 5

Do not expose implementation

Page 6: Stoop sed-smells

S.Ducasse 6

Do not overuse conversionsnodes asSet removes all the duplicated nodes (if node knows how to compare). But a systematic use of asSet to protect yourself from duplicate is not good

nodes asSet asOrderedCollectionreturns an ordered collection after removing duplicates

Look for the real source of duplication if you do not want it!

Page 7: Stoop sed-smells

S.Ducasse 7

Hiding missing informationDictionary>>at: aKey This raises an error if the key is not found

Dictionary>>at: aKey ifAbsent: aBlockAllows one to specify action aBlock to be done when the key does not exist.

Do not overuse it:nodes at: nodeId ifAbsent:[ ]

This is bad because at least we should know that the nodeId was missing

Page 8: Stoop sed-smells

S.Ducasse 8

Avoid returning nilAvoid to return special results as nil

messages := self fetchMessages.messages isNil  ifFalse: [ messages dispatchFrom: self ]

What if we would simply return an empty collection infetchMessages instead of nil?

Less conditional and ugly tests!!

Page 9: Stoop sed-smells

S.Ducasse 9

Objects not strings!• Strings are dead objects• You can only concatenate strings• Use objects not their textual representation

Page 10: Stoop sed-smells

S.Ducasse 10

Objects not tuples!• spec first• spec second• spec third

• spec action• spec selector• spec menuItem

• And add a printing• aSpec(‘open’, #openBrowser, ‘open (O)’)

Page 11: Stoop sed-smells

S.Ducasse 11

Tell, Don’t Ask• no condition and case based on the receiver

type

• Use polymorphism as much as possible to avoid type checking

Page 12: Stoop sed-smells

S.Ducasse 12

Tell, Don’t Ask!MyWindow>>displayObject: aGrObject

aGrObject displayOn: self

•And not:

MyWindow>>displayObject: aGrObject

aGrObject isSquare ifTrue: […]aGrObject isCircle ifTrue: […]…

Page 13: Stoop sed-smells

S.Ducasse 13

Don’t violate encapsulation

No overuse of accessors

Encapsulation principle: minimize data representation dependencies

Offer complete interface