debugging visualworks

16
S.Ducasse 1 QuickTime™ TIFF (Uncompr are needed t Stéphane Ducasse [email protected] http://www.listic.univ-savoie.f r/~ducasse/ Common Mistakes and Debugging in VW

Upload: the-world-of-smalltalk

Post on 02-Dec-2014

814 views

Category:

Education


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Debugging VisualWorks

S.Ducasse 1

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

Stéphane [email protected]://www.listic.univ-savoie.fr/~ducasse/

Common Mistakes and Debugging in VW

Page 2: Debugging VisualWorks

S.Ducasse 2

• Preventing: Most Common Mistakes•Curing: Debugging Fast (from ST Report July

93)•Extras

Roadmap

Page 3: Debugging VisualWorks

S.Ducasse 3

• true is the boolean value, True its class.

•Book>>initialize• inLibrary := True

•Book>>initialize• inLibrary := true

•nil is not an acceptable receiver for ifTrue:

Common Beginner Bugs (I)

Page 4: Debugging VisualWorks

S.Ducasse 4

•whileTrue: and whileTrue receivers must be a block•

[x<y] whileTrue: [x := x + 3]

• Redefining a class:• Before creating a class, check if it already exists.

This is (sigh) a weakness of the system

Object subclass: #View (Squeak)

• VisualWorks 7.0 has namespaces so less likely to redefine a class

Common Beginner Bugs (II)

Page 5: Debugging VisualWorks

S.Ducasse 5

• In a method self is returned by default. Do not forget ^ for returning something else.

• Packet>>isAddressedTo: aNode• ^ self addressee = aNode name

Common Beginner Bugs (III)

Page 6: Debugging VisualWorks

S.Ducasse 6

•Do not try to access instance variables to initialize them in a class method. It is impossible!

•A class method can only access class instance variables and classVariables.

• -> Define and invoke an initialize method on instances.

Instance Variable Access in Class Method

Page 7: Debugging VisualWorks

S.Ducasse 7

Example

Packet class>>send: aString to: anAddress contents := aString. addressee := anAddress

Instead create an instance and invoke instance methods

Packet class>>send: aString to: anAddress

^ self new contents: aString; addressee: anAddress

Page 8: Debugging VisualWorks

S.Ducasse 8

•Do not try to assign a value to a method argument. Arguments are read only

setName: aStringaString := aString, 'Device'.name := aString

Method Argument Are Read-Only

Page 9: Debugging VisualWorks

S.Ducasse 9

self and super are Read-Only

•Do not try to modify self and super

Page 10: Debugging VisualWorks

S.Ducasse 10

•Never redefine basic-methods (==, basicNew, basicNew:, basicAt:, basicAt:Put:...)

•Never redefine class •Never redefine name on the class side!

basic* Method Redefinition

Page 11: Debugging VisualWorks

S.Ducasse 11

The hash and = Pair

•Redefine hash when you redefine = so that if a = b then a hash = b hash

•Book>>=aBook

^self title = aBook title & (self author = aBook author)

Book>>hash ^self title hash bitXor: self author hash

Page 12: Debugging VisualWorks

S.Ducasse 12

• add: returns the argument and not the receiver, so use yourself to get the collection back.

•Do not subclass Collection classes.

Common Beginner Bugs - Collections

Page 13: Debugging VisualWorks

S.Ducasse 13

•Never iterate over a collection which the iteration somehow modifies.

•timers do: [:aTimer| aTimer isActive ifFalse: [ timers remove: aTimer]]

•First copy the collection• timers copy do: [:aTimer| aTimer isActive • ifFalse: [ timers remove:

aTimer]•Take care, since the iteration can involve

various methods and modifications which may not be obvious!

Don’t iterate over collection and modify it

Page 14: Debugging VisualWorks

S.Ducasse 14

•Basic Printing• Transcript cr; show: ‘The total= ’, self total

printString.

•Use a global or a class to control printing information

Debug ifTrue:[Transcript show: self total printString]

Debug > 4 ifTrue:[Transcript show: self total printString]

Debug print:[Transcript show: self total printString]

Debugging - Hints

Page 15: Debugging VisualWorks

S.Ducasse 15

•Breakpoints• self halt.• self error: ‘ invalid’

•Conditional halti > 10 ifTrue:[self halt]

i haltIfNil

• In Squeak 3.8: haltIf•self haltIf: (i > 10)•i haltIf: [:o | o >10]•self haltIf: #doIt

BreakPoints

Page 16: Debugging VisualWorks

S.Ducasse 16