debugging visualworks
DESCRIPTION
TRANSCRIPT
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
S.Ducasse 2
• Preventing: Most Common Mistakes•Curing: Debugging Fast (from ST Report July
93)•Extras
Roadmap
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)
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)
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)
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
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
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
S.Ducasse 9
self and super are Read-Only
•Do not try to modify self and super
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
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
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
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
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
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
S.Ducasse 16