469,593 Members | 1,741 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,593 developers. It's quick & easy.

Call mutators or attributes directly inside class?

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"?

Jun 27 '08 #1
4 1629
On May 12, 10:26 am, Travis <travis.bow...@gmail.comwrote:
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"?
IMO, when you are writing the implementation of the class, use the
most efficient method, unless it will require a lot of code
duplication.

In the example you provided. I would surely write m_name="none";
Jun 27 '08 #2
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 classes 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
Jun 27 '08 #3
On May 12, 9:00 am, Andrey Tarasevich <andreytarasev...@hotmail.com>
wrote:
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 classes 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
I always had this notion in my head like calling the method would be
cleaner than modifying the attributes directly. Mostly so that later
on you could add things that might be needed to the method without
changing anything else. So many SetText() always calls UpdateScreen()
you know?

The reason why I asked this time though was I am in a situation where
I would be adding multiple lines of checks to in the mutator to
account for the fact that I'm only initializing the attribute in this
case and wouldn't want subsequent functions calls like UpdateText() to
take place versus just setting the attribute directly in my Init().
Jun 27 '08 #4
On 2008-05-12 17:26, 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, in trivial cases such as SetName() I think it is better* to
directly access the data (but see also Andrey Tarasevich's comments
about separating implementation from interface). If you on the other
hand have to make a lot of changes (to several members, or you need to
compute the value somehow) and you already have functions to do so you
should of course use them.

* With better I mean better as in purely subjective better, I can not
come up with any rational arguments against using mutators wherever you can.

--
Erik Wikström
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by John L. Clark | last post: by
2 posts views Thread by james_027 | last post: by
44 posts views Thread by Steven D'Aprano | last post: by
275 posts views Thread by Astley Le Jasper | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.