Parentheses Vs. Square Brackets

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

( ) vs. [ ]

Use [ ] for 'Unknown' Execution Occurrence

Use [ expressions ] when:

Example

n timesRepeat: [ self doSomething ]

timesRepeat: executes a number of times its argument, therefore the argument is a block

( ) vs. [ ] Example

x isNil ifTrue: [ self doSomething ]

ifTrue: may execute or not its argument, therefore the argument is a block

( ) vs. [ ] Example

[ self start ] whileTrue: [ self doSomething ]

whileTrue: may execute both its receiver and argument multiple times, therefore they are both a block

Quiz

1 to: n do: ... self doSomething ...
x ifEmpty: ... self doSomething ...

Quiz

1 to: 100 do: [ :i | self doSomething ]
x ifEmpty: [ self doSomething ]

Summary

/