Why Strings as Classes?

Nick Sabalausky a at a.a
Wed Aug 27 14:08:47 PDT 2008


"Denis Koroskin" <2korden at gmail.com> wrote in message 
news:op.ugj2yeu0o7cclz at proton.creatstudio.intranet...
> On Wed, 27 Aug 2008 21:23:53 +0400, bearophile <bearophileHUGS at lycos.com> 
> wrote:
>
>> Robert Fraser:
>>> It's a good feature to have (I wouldn't consider a list class complete
>>> without it), it just shouldn't be abused.
>>
>> Its code:
>>
>> final Ref nth (int n) {
>>     auto p = this;
>>     for (int i; i < n; ++i)
>>         p = p.next;
>>     return p;
>> }
>>
>> If the usage patterns show this is positive, then a static pointer/index 
>> pair may be added, that keeps the last accessed pointer/index, this may 
>> speed up things if the items are accessed sequentially, or even if they 
>> are accessed with some several forward skips, avoiding starting from the 
>> beginning each time :-)
>>
>> Bye,
>> bearophile
>
> I believe this breaks encapsulation, unless an iterator is returned and 
> passed as a start. Find is a better name for such operation.

The way I see it, encapsulation is all about the black box idea. And the 
only things you can see from outside the black box are the inputs and 
outputs. So, if encapsulation is desired, things like "Indexing" and "Find" 
should be considered black boxes (ie, encapsulation). And if "Indexing" and 
"Find" are black boxes, that means as defining them purely in terms of the 
relationship between their input and output. So this is how I consider them 
to be defined:

Indexing: Return the element at position N.
Find: Return the position of the element that is equal (value-equal or 
identity-equal, depending on the find) to X .

So if there's a function that returns the element at position N and you call 
it "find" just because of the *internal implementation* of the function is 
an incrementing loop, I consider that to be both a violation of 
encapsulation (because the name has been chosen based on the inner workings 
of the function, not it's input-output relationship) and a violation of the 
definitions. 





More information about the Digitalmars-d mailing list