Call mutators or attributes directly inside class?

T

Travis

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

Christopher

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";
 
T

Travis

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.

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().
 
E

Erik Wikström

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top