Re: Call mutators or attributes directly inside class?
by Andrey Tarasevich <andreytarasevich@[EMAIL PROTECTED]
>
May 12, 2008 at 09:00 AM
Travis wrote:
> Is it considered good practice to call a mutator when inside the same
> class or modify the attribute directly?
>
> So if there's a public method SetName() would it be better from
> say ::Init() to call SetName("none") or just set name="none"?
>
It depends.
The purpose of writing accessor methods (mutators etc.) is not just the
blind
formal replacement of direct attribute access with mutators. The purpose
of
writing accessors is to provide interface that decouples the clients of
the
class from the physical implementation details of the corresponding
attribute.
The benefits of this decoupling apply to other cl***** as much as they
apply to
the class itself. For this reason the reasonable strategy to follow is to
use
accessor methods whenever you can, and use direct attribute access only
when you
must.
When you are writing methods that belong to the same class as the
attribute in
question, the choice depends on the nature of the functionality under
consideration. If this is a very low-level functionality that is supposed
to
have knowledge of/rely on the implementation details of the class, then
you
might choose the access the attribute directly (for the sake of
efficiency). If
this is a higher level functionality, you might be better off using the
accessor
methods.
--
Best regards,
Andrey Tarasevich