This post originated from an RSS feed registered with Agile Buzz
by James Robertson.
Original Post: ifNil:
Feed Title: David Buck - Blog
Feed URL: http://www.cincomsmalltalk.com/rssBlog/buck-rss.xml
Feed Description: Smalltalk can do that
One of the new methods added into most Smalltalks is ifNil:. It's a useful method, but many people use it in a strange way. Consider the following code:
self employee manager ifNil: [self report: 'This is a consultant']
This code could have been coded just as easily with an isNil and ifTrue: call and would actually be faster since those two methods are specially optimized in Smalltalk:
self employee manager isNil ifTrue: [self report: 'This is a consultant']
The real power of ifNil: comes when you need to use some object but it might be nil. If it is nil, you want to use some default value. Check the following code: