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
Missing Period
- Separate instructions with period (
.
)
A Problem
Error: Message includes:ifTrue:
does not exist
Error: Message assert:includes:
does not exist
Keyword-Based Messages
Solution
- keyword messages are built out of fragments
- the message is the longuest sequence of fragments
- use parentheses to delimit multiple keyword messages
A Problem
Error: numbers
is the number 35 and not a collection
Error: Dice new
returns 6 instead of a dice
Forgotten yourself
is equivalent to
Forgotten yourself
Solutions
add:
and setFaces:
return their argument, not the receiver
- send
yourself
after a sequence of messages if you want the receiver
A Problem
Error: nil
does not understand ifFalse:
A Problem
Error: Class True
does not understand ifFalse:
True vs. true
Solution
nil
is the unique instance of the class UndefinedObject
true
is the unique instance of the class True
- Class names start with an uppercase letter
A Problem
Error: aDice roll
returns aDice
instead of a number
A Problem
is equivalent to
A Problem
Error: Dice new
returns the class instead of the new instance
A Problem
is equivalent to
new
is sent to a class
self
is the class Dice
- returns
Dice
and not its newly created instance
Forgetting to Return the Result
Solutions
- in a method,
self
is returned by default
- do not forget the caret
^
to return something else
A Problem
Error: System is frozen
Infinite Loops in Overridden Methods
Solution
- use
super
in overridden methods
What You Should Know
- How to identify common errors faster
- Check periods
.
- Check parentheses
(
and )
- Check carets
^
- Check
yourself
- Use the debugger to understand the problem
/