1 / 16

Common Mistakes and Debugging in VW

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

Download Presentation

Common Mistakes and Debugging in VW

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Common Mistakes and Debugging in VW

  2. Roadmap • Preventing: Most Common Mistakes • Curing: Debugging Fast (from ST Report July 93) • Extras

  3. 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:

  4. 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

  5. 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

  6. 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.

  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. 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

  9. self and super are Read-Only • Do not try to modify self and super

  10. basic* Method Redefinition • Never redefine basic-methods (==, basicNew, basicNew:, basicAt:, basicAt:Put:...) • Never redefine class • Never redefine name on the class side!

  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. 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.

  13. 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!

  14. 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]

  15. 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

More Related