Re: asBit

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Re: asBit

Ralph Boland
> I currently writing a code which looks like:
>
> a := some numeric expression + (someBoolean ifTrue: [1] ifFalse: [0]).
>
> And i get somewhat tired repeating the same pattern in multiple places.
> So, i thinking , maybe #asBit convenience method could be useful?
>
> True>>asBit
>  ^ 1
> False>>asBit
>  ^ 0
>
> so, then, in case of need like above, we could write much shorter expressions:
> a := some numeric expression + someBoolean asBit.
>
> And its much less error prone comparing to #ifTrue:ifFalse:, since i
> found its relatively easy to make a mess (swapping 1 with 0),
> especially, when you having negations in expression, because once you see:
>
> (someBoolean not ifTrue: [1] ifFalse: [0])
>
> you always tempted to write it as:
>
> (someBoolean ifTrue: [0] ifFalse: [1]).
>
> or as:
>
> (someBoolean ifFalse: [1] ifTrue: [0]).
>
> but once you start playing with it, its very easy to shoot into own
> feet if you are not careful.
>
> But if we could add #asBit, then we could write:
>
> someBoolean not asBit
>
> which is much more:
> a) concise & cleaner
> b) error proof
>
>
> --
> Best regards,
> Igor Stasenko AKA sig.

I use  "someBoolean card" for this where
card stands for cardinality.
The cardinality of  false is 0
and of  true is 1.

Method "card" can also be used for other classes of object
that have cardinal values associated with them.

Regards,

Ralph Boland


--
Volume * Density = Mass.
Volume / Density = Intelligence.
The latter equation explains the
essentially equal intelligence of men and woman
and the capabilities of some commonly used operating systems.

_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project