stoop sed-smells

Post on 27-Nov-2014

510 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

S.Ducasse 1

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

Stéphane DucasseStephane.Ducasse@univ-savoie.frhttp://www.iam.unibe.ch/~ducasse/

Elements of Design -Simple Smells

S.Ducasse 2

Basic Design Mistakes

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!

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?

S.Ducasse 5

Do not expose implementation

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!

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

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!!

S.Ducasse 9

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

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)’)

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

S.Ducasse 12

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

aGrObject displayOn: self

•And not:

MyWindow>>displayObject: aGrObject

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

S.Ducasse 13

Don’t violate encapsulation

No overuse of accessors

Encapsulation principle: minimize data representation dependencies

Offer complete interface

top related