Common Errors

Damien Cassou, St├ęphane Ducasse and Luc Fabresse http://stephane.ducasse.free.fr

What You Will Learn

Find and fix common mistakes faster

A Problem

DiceHandle >> + aDiceHandle
   | res |
   res := DiceHandle new
   self dices do: [ :each | ... ].

Missing Period

DiceHandle >> + aDiceHandle
   | res |
   res := DiceHandle new.
   self dices do: [ :each | ... ].

A Problem

x includes: 33
   ifTrue: [ self do something ]

Error: Message includes:ifTrue: does not exist

self assert: players includes: aPlayer

Error: Message assert:includes: does not exist

Keyword-Based Messages

Solution

self assert: (players includes: aPlayer)

A Problem

numbers := OrderedCollection new
             add: 35

Error: numbers is the number 35 and not a collection

Dice >> setFaces: aNumber
   ^ faces := aNumber

Dice class >> new
   ^ super new setFaces: 6

Error: Dice new returns 6 instead of a dice

Forgotten yourself

numbers := OrderedCollection new
             add: 35;
             yourself

is equivalent to

| numbers |
numbers := OrderedCollection new.
numbers add: 35.
numbers

Forgotten yourself

Solutions

numbers := OrderedCollection new
             add: 35;
             yourself
Dice class >> new
   ^ super new setFaces: 6; yourself

A Problem

Book>>borrow
   inLibrary ifFalse: [ ... ].
   ...

Error: nil does not understand ifFalse:

A Problem

Book>>initialize
   inLibrary := True

Book>>borrow
   inLibrary ifFalse: [ ... ].
  ...

Error: Class True does not understand ifFalse:

True vs. true

Solution

Book>>initialize
   inLibrary := true

A Problem

Dice >> roll
   faces atRandom

Error: aDice roll returns aDice instead of a number

A Problem

Dice >> roll
   faces atRandom

is equivalent to

Dice >> roll
   faces atRandom.
   ^ self

A Problem

Dice class >> new
   super new
      setFaces: 0;
      yourself

Error: Dice new returns the class instead of the new instance

A Problem

Dice class >> new
   super new
      setFaces: 0;
      yourself

is equivalent to

Dice class >> new
   super new
      setFaces: 0;
      yourself.
   ^ self

Forgetting to Return the Result

Solutions

Dice >> roll
   ^ faces atRandom

Dice class >> new
   ^ super new
      setFaces: 0;
      yourself

A Problem

Dice class >> new
   ^ self new
      setFaces: 0;
      yourself

Error: System is frozen

Infinite Loops in Overridden Methods

Solution

Dice class >> new
   ^ super new
      setFaces: 0;
      yourself

What You Should Know

/