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

Elements of Design - Simple Smells

  • Upload
    alaric

  • View
    42

  • Download
    1

Embed Size (px)

DESCRIPTION

Elements of Design - Simple Smells. Basic Design Mistakes. A Class should have. Class Person { String getName(); void setName(String name); int getAge(); void setAge(int age); Car getCar(); void setCar(Car car); } What do we see ? - PowerPoint PPT Presentation

Citation preview

Page 1: Elements of Design - Simple 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: Elements of Design - Simple Smells

S.Ducasse 2

Basic Design Mistakes

Page 3: Elements of Design - Simple 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: Elements of Design - Simple 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: Elements of Design - Simple Smells

S.Ducasse 5

Do not expose implementation

Page 6: Elements of Design - Simple 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: Elements of Design - Simple 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: Elements of Design - Simple 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: Elements of Design - Simple Smells

S.Ducasse 9

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

Page 10: Elements of Design - Simple 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: Elements of Design - Simple 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: Elements of Design - Simple 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: Elements of Design - Simple Smells

S.Ducasse 13

Don’t violate encapsulation

No overuse of accessors

Encapsulation principle: minimize data representation dependencies

Offer complete interface