D vs. C#

Yigal Chripun yigal100 at gmail.com
Sun Oct 21 18:06:03 PDT 2007


Janice Caron wrote:
> On 10/21/07, Janice Caron <caron800 at googlemail.com> wrote:
>> On 10/21/07, Yigal Chripun <yigal100 at gmail.com> wrote:
>>> object.doSomething(params);
>>> ---
>>> is more OOP correct than:
>>> ---
>>> a = object.getField();
>>> b = doSomthing(a, params);
>>> object.setField(b);
>> I think that's wrong.
> 
> Also, the fact that one can write a getter function without a setter
> function allows one to define properties which are read-only to the
> outside world, but read-write to the containing object, which again is
> something you can't do with a plain member variable.

you're correct with you comment but that just shows that you're a true 
c++ programmer and completely missed my point.

I haven't said that fields are better than properties. what i meant was 
the use of encapsulation from the OOP perspective. In pure OOP, method 
calls are considered messages to objects and the objects have attached 
behavior that handles its inner state. if you want to manipulate its 
inner state, the proper OOP way is to have the object contain such 
behavior itself and all you need to do is to send the object a message, 
rather than get its inner state with a getter and perform the action 
yourself. the latter breaks encapsulation from an OOP perspective.
properties are easy to misuse by a programmer to break said 
encapsulation and implement the latter instead of the former design.
that's not to say that getters should be avoided, on the contrary, 
please do use them when _appropriate_.



More information about the Digitalmars-d mailing list