1. S.Ducasse 1
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
Stéphane Ducasse
Stephane.Ducasse@univ-savoie.fr
http://www.listic.univ-savoie.fr/~ducasse/
Common Mistakes and
Debugging in VW
2. S.Ducasse 2
• Preventing: Most Common Mistakes
• Curing: Debugging Fast (from ST Report July 93)
• Extras
Roadmap
3. 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)
4. 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)
5. 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)
6. 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.
InstanceVariable Access in Class Method
7. 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
8. S.Ducasse 8
• Do not try to assign a value to a method argument.
Arguments are read only
setName: aString
aString := aString, 'Device'.
name := aString
Method Argument Are Read-Only
10. 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
11. 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
12. 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
13. 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
14. 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
15. S.Ducasse 15
• Breakpoints
• self halt.
• self error:‘ invalid’
• Conditional halt
i > 10 ifTrue:[self halt]
i haltIfNil
• In Squeak 3.8: haltIf
•self haltIf: (i > 10)
•i haltIf: [:o | o >10]
•self haltIf: #doIt
BreakPoints