The Artima Developer Community
Sponsored Link

Agile Buzz Forum
ifNil:

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
James Robertson

Posts: 29924
Nickname: jarober61
Registered: Jun, 2003

David Buck, Smalltalker at large
ifNil: Posted: Jun 4, 2004 3:13 PM
Reply to this message Reply

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
Latest Agile Buzz Posts
Latest Agile Buzz Posts by James Robertson
Latest Posts From David Buck - Blog

Advertisement
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:

   manager := self employee manager ifNil: [defaultManager]

If you didn't have ifNil:, you'd have to code it like this:

   manager := self employee manager isNil ifTrue: [self employee manager] ifFalse: [defaultManager]

There's where the real usefulness of ifNil: comes in.

Read: ifNil:

Topic: A much needed vacation Previous Topic   Next Topic Topic: Overheard in a Java group

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use