Common Mistakes and Debugging in VW Stphane Ducasse
Common Mistakes and Debugging in VW Stéphane Ducasse Stephane. Ducasse@univ-savoie. fr http: //www. listic. univ-savoie. fr/~ducasse/ S. Ducasse 1
Roadmap • • • S. Ducasse Preventing: Most Common Mistakes Curing: Debugging Fast (from ST Report July 93) Extras 2
Common Beginner Bugs (I) • true is the boolean value, True its class. • • Book>>initialize in. Library : = True • • Book>>initialize in. Library : = true • nil is not an acceptable receiver for if. True: S. Ducasse 3
Common Beginner Bugs (II) • • while. True: and while. True receivers must be a block [x<y] while. True: [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) • S. Ducasse Visual. Works 7. 0 has namespaces so less likely to redefine a class 4
Common Beginner Bugs (III) • • • S. Ducasse In a method self is returned by default. Do not forget ^ for returning something else. Packet>>is. Addressed. To: a. Node ^ self addressee = a. Node name 5
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 class. Variables. -> Define and invoke an initialize method on instances. • S. Ducasse 6
Example Packet class>>send: a. String to: an. Address contents : = a. String. addressee : = an. Address Instead create an instance and invoke instance methods Packet class>>send: a. String to: an. Address ^ self new contents: a. String; addressee: an. Address S. Ducasse 7
Method Argument Are Read-Only • Do not try to assign a value to a method argument. Arguments are read only set. Name: a. String : = a. String, 'Device'. name : = a. String S. Ducasse 8
self and super are Read-Only • S. Ducasse Do not try to modify self and super 9
basic* Method Redefinition • Never redefine basic-methods (==, basic. New: , basic. At: Put: . . . ) • • Never redefine class Never redefine name on the class side! S. Ducasse 10
The hash and = Pair • • Redefine hash when you redefine = so that if a = b then a hash = b hash Book>>=a. Book ^self title = a. Book title & (self author = a. Book author) Book>>hash ^self title hash bit. Xor: self author hash S. Ducasse 11
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. S. Ducasse 12
Don’t iterate over collection and modify it • • Never iterate over a collection which the iteration somehow modifies. timers do: [: a. Timer| a. Timer is. Active if. False: [ timers remove: a. Timer]] • • S. Ducasse First copy the collection timers copy do: [: a. Timer| a. Timer is. Active if. False: [ timers remove: a. Timer] Take care, since the iteration can involve various methods and modifications which may not be obvious! 13
Debugging - Hints • • Basic Printing • Use a global or a class to control printing information Transcript cr; show: ‘The total= ’, self total print. String. Debug if. True: [Transcript show: self total print. String] Debug > 4 if. True: [Transcript show: self total print. String] Debug print: [Transcript show: self total print. String] S. Ducasse 14
Break. Points • • Breakpoints • Conditional halt • self halt. self error: ‘ invalid’ i > 10 if. True: [self halt] i halt. If. Nil • S. Ducasse In Squeak 3. 8: halt. If • self halt. If: (i > 10) • i halt. If: [: o | o >10] • self halt. If: #do. It 15
S. Ducasse 16
- Slides: 16