160 likes | 300 Views
Common Mistakes and Debugging in VW. Roadmap. Preventing: Most Common Mistakes Curing: Debugging Fast (from ST Report July 93) Extras. Common Beginner Bugs (I). true is the boolean value, True its class. Book>>initialize inLibrary := True Book>>initialize inLibrary := true
E N D
Roadmap • Preventing: Most Common Mistakes • Curing: Debugging Fast (from ST Report July 93) • Extras
Common Beginner Bugs (I) • 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 (II) • 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 (III) • In a method self is returned by default. Do not forget ^ for returning something else. • Packet>>isAddressedTo: aNode • ^ self addressee = aNode name
Instance Variable Access in Class Method • 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.
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
Method Argument Are Read-Only • Do not try to assign a value to a method argument. Arguments are read only • setName: aString • aString := aString, 'Device'. • name := aString
self and super are Read-Only • Do not try to modify self and super
basic* Method Redefinition • Never redefine basic-methods (==, basicNew, basicNew:, basicAt:, basicAt:Put:...) • Never redefine class • Never redefine name on the class side!
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
Common Beginner Bugs - Collections • add: returns the argument and not the receiver, so use yourself to get the collection back. • Do not subclass Collection classes.
Don’t iterate over collection and modify it • 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!
Debugging - Hints • 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]
BreakPoints • 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