In business apps, the need for default values happen all the time, so the idiom has value (not sure for the message name though). We could use x := [ self thing ] ifError: [ someDefault ] for these purposes. Triggering errors is not too nice still. Now, what if self itself is nil or empty? BTW, isEmptyOrNil exists in the image for Collections and UndefinedObject. Empty has no meaning for Object, so why test against empty in the name? In the image, I see that we do have #default: anObject in several places. It seems to serve the same intent. What is the idiom for such things in Pharo? Importing idioms from other languages works but if we do have one already, we will introduce confusion. Phil On Mon, Jan 5, 2015 at 1:19 PM, Tudor Girba <tudor@tudorgirba.com> wrote:
This is not about taste. This is about not promoting the use of nil or dependency or the meaning of empty collection.
A better way is to look at the upstream logic and modify that one so that it does not need to know about nil or empty.
Cheers, Doru
On Mon, Jan 5, 2015 at 1:17 PM, Sebastian Sastre < sebastian@flowingconcept.com> wrote:
taste is taste but would you care to illustrate your point with examples? Iâm curious about it
On Jan 5, 2015, at 6:12 AM, stepharo <stepharo@free.fr> wrote:
You summarise well the kind of code I do not like. isNil everywhere and horrible tests.
Stef
Le 4/1/15 23:27, Sebastian Sastre a écrit :
Hi guys,
Iâve started to use this little one:
Object>>ifNilOrEmpty: aBlock
self ifNil: [ ^ aBlock value ].
(self isCollection and: [ self isEmpty ]) ifTrue: [ ^ aBlock value ].
^ self.
It allows you to do the widely known JavaScript one-liner:
var stuff = this.thing || âsome default value for when this.thing is undefined, null or an empty stringâ.
but in smalltalk in this way:
stuff := self thing ifNilOrEmpty: [ âsome default value for when self thing is nil or an empty stringâ ]
simple thing feels practical and nice :)
-- www.tudorgirba.com
"Every thing has its own flow"
-- --- Philippe Back Visible Performance Improvements Mob: +32(0) 478 650 140 | Fax: +32 (0) 70 408 027 Mail:phil@highoctane.be | Web: http://philippeback.eu Blog: http://philippeback.be | Twitter: @philippeback Youtube: http://www.youtube.com/user/philippeback/videos High Octane SPRL rue cour Boisacq 101 | 1301 Bierges | Belgium Pharo Consortium Member - http://consortium.pharo.org/ Featured on the Software Process and Measurement Cast - http://spamcast.libsyn.com Sparx Systems Enterprise Architect and Ability Engineering EADocX Value Added Reseller