[Newbies] Efficient date parsing

Bert Freudenberg bert at freudenbergs.de
Thu Apr 3 06:38:45 UTC 2008


On 03.04.2008, at 07:49, Ramon Leon wrote:
>>
>> Yeah, shortly after I posted that, I remembered that. :)
>>
>> Also, for the beginners. you need to initialize dateCache to
>> a Dictionary.
>> the normal way to do that is to have an instance side method called
>> #initialize:
>>
>>        initialize
>>                super initialize. "NEVER NEVER leave this out"
>>                dateCache := Dictionary new.
>>
>> When you save this, squeak will ask "I dunno dateCache", and
>> you should make it an instance variable.
>>
>> --
>> Randal L. Schwartz - Stonehenge Consulting Services, Inc. -
>
> Unless you're doing a class side #initialize, in which case you  
> don't want
> to call super initialize.

Indeed, that's one reason why I prefer lazy initialization over  
#initialize:

dateFrom: aString
	dateCache ifNil: [dateCache := Dictionary new].
	^dateCache at: aString ifAbsentPut: [Date from: aString].

(although the canonical way is to have the dateCache init code in a  
#dateCache accessor, then always use "self dateCache").

One major plus of lazy initialization is that this supports code  
upgrades of a running system, where #initialize is usually not run  
again.

Also, if this indeed is for log data with many identical dates in a  
row you might flush the cache from time to time, or indeed even only  
check if the next date is the same as the previous ... knowledge of  
your domain beats any general optimization ;)

- Bert -




More information about the Beginners mailing list