[squeak-dev] Class with nil environment?!

Frank Shearar frank.shearar at gmail.com
Thu Jul 11 18:25:19 UTC 2013


On 11 July 2013 18:07, tim Rowledge <tim at rowledge.org> wrote:
>>
>> Class >> environment
>>    environment = #deferToSuperclass ifTrue: [ ^ super environment ].
>>    ^ environment
>>
>> would still give the whatever-my-superclass-does while also clearly
>> indicating that noone accidentally forgot to initialise the instvar.
>> Not a nil in sight!
>
> nil is a pretty standard way of indicating nothing….

It's an excellent way of leaving the reader puzzling "is this nothing,
or did the author just not bother to initialise this variable?" It's
the kind of thing that makes far distant code fail (admittedly not in
this case), leaving you wondering "do where did this nil come from,
anyway?"

Or remember the hilarity around wanting a nil in a Set, only nil was
also used as the theres-a-space-here marker. Simply using a unique
sentinel object removes the uninitialised/missing confusion.

> A decent comment would have solved this issue before it became an issue.

I don't think it would have, actually. Not in this instance.

But ANYWAY, there is a proposed solution that needs careful eyes and
brains to look at and ponder over: make Class >> #declare: send the
#environment message instead of accessing the instvar directly.

frank

> Another way of implementing similar functionality without even having a class instance variable (wait a minute, is an instance variable of class Class a class variable or a class' instance variable or an instance of class class instance variable or what?) would just use normal method inheritance and global(ish) variables for the environments -
>
> (root class)> environment
>         ^RootEnvironment
>
> (some other class)>environment
>         ^SpecialEnvironment
>
> Of course, you have to be able to look up SpecialEnvironment in your environment that applies when you compile that, which maybe isn't SpecialEnvironment? And obviously it doesn't allow for resetting the environment on the fly (except with a recompile)
>
> But seriously, having code that says
> do I have Thing set? If so, return my Thing, otherwise return what my parent thinks Thing is
> is pretty common in any hierarchy. I really think that fixing it with a comment is the most useful answer. But then  I wouldn't accept a bit of code that didn't have a clear, meaningful comment in any code review.
>
>
> tim
> --
> tim Rowledge; tim at rowledge.org; http://www.rowledge.org/tim
> Comment my code? Why do you think it's called 'code'?
>
>
>


More information about the Squeak-dev mailing list