473,503 Members | 9,057 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1890
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2380
by: Carlos Ribeiro | last post by:
Hello all, I'm posting this to the list with the intention to form a group of people interested in this type of solution. I'm not going to spam the list with it, unless for occasional and...
8
2406
by: Matthew Bell | last post by:
Hi, I've got a question about whether there are any issues with directly calling attributes and/or methods of a threaded class instance. I wonder if someone could give me some advice on this. ...
1
2700
by: John L. Clark | last post by:
I am curious as to the rationale, and effect, of having default namespaces not applying (directly) to attributes (see http://www.w3.org/TR/REC-xml-names/#defaulting). Given an attribute without a...
16
2897
by: digitalorganics | last post by:
What's the difference between initializing class variables within the class definition directly versus initializing them within the class's __init__ method? Is there a reason, perhaps in certain...
4
3285
by: Pedro Werneck | last post by:
Hi all I noticed something strange here while explaining decorators to someone. Not any real use code, but I think it's worth mentioning. When I access a class attribute, on a class with a...
2
2128
by: james_027 | last post by:
hi everyone, I am now in chapter 5 of Dive Into Python and I have some question about it. From what I understand in the book is you define class attributes & data attributes like this in python...
0
1214
by: dph5010 | last post by:
I'm trying to make an object that will reflect the response from isbndb.com's API so I can serialize the results into my code. Right now I have code that works, however, the structure isn't quite the...
44
2884
by: Steven D'Aprano | last post by:
I have a class which is not intended to be instantiated. Instead of using the class to creating an instance and then operate on it, I use the class directly, with classmethods. Essentially, the...
275
12033
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
7193
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6975
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5562
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4992
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
371
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.