Re: Using Get() and Set() instead of accessing the variable directly

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Get and Set are felt to be cleaner. They decouple the semantics from the representation.
A common misconception is that efficiency is an issue. It isn't even in the discussion
unless this is done in an inner loop, and then all you have to do is move the body of the
set or get method to the class definition and it compiles as efficiently as an assignment
statement, so at that point the discussion of performance becomes irrlevant.

All you need to do to answer the question is generate a listing (in release mode only) and
see what it does.
joe

On Tue, 20 Mar 2007 09:47:31 -0700, "Nobody" <Nobody@xxxxxxxxx> wrote:

Is it better to access the variable using Set() and Get() methods, rather
than accessing the variable directly?

I suppose I learned early on to access variables directly, instead of using
Set() and Get() methods.
It was easier for simple stuff, but it caught up with me as I progressed
further.
Now, I feel as though accessing variables directly is just bad practice.

An example would be that even within the same class,
I can access private/protected variables, but when I divide up the class
into manageable parts,
I often have to add Get() and Set() methods anyways.

private:
int m_x;
int m_y;
inline void SetX(int NewX){ m_x = newX;} //inlined
inline int GetX(){ return m_x; } //inlined
public:
inline void SetY(int NewY){ m_y = newY;} //inlined
inline int GetY(){ return m_y; } //inlined


void SomeClass::SomeFunc()
{
m_y = 3; //Bad
SetY(3); //Good
m_x = 1; //Bad
SetX(1); //Good
}

I don't know if it really saves any time or not?
I don't know if the compiler treats it as accessing the variable directly or
not?
Maybe you can clear that up for me.

I just think it is just better practice to use Set() and Get() methods for
each variable even if they are only accessed within the same class.

Thoughts, Opinions?

Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Using Get() and Set() instead of accessing the variable directly
    ... I feel as though accessing variables directly is just bad practice. ... I can access private/protected variables, but when I divide up the class ... int m_x; ... I don't know if the compiler treats it as accessing the variable directly or ...
    (microsoft.public.vc.mfc)
  • Re: Using Get() and Set() instead of accessing the variable directly
    ... void SetY(int NewY) { ... I can access private/protected variables, but when I divide up the class into manageable parts, ... int m_x; ... I just think it is just better practice to use Setand Getmethods for each variable even if they are only accessed within the same class. ...
    (microsoft.public.vc.mfc)
  • Re: Delay Routine: Fully-portable C89 if possible
    ... If the number can be negative, I use int. ... If I *really* need to conserve memory, I make an array of raws bytes ... For bigger systems, the tradeoffs for development are different - you don't have to worry so much about the minor details, and can afford to be sloppy about implementation efficiency in the name of developer efficiency. ... why embedded programmers always like to know the exact size of the int ...
    (comp.arch.embedded)
  • Re: Scale Comparison
    ... theory always follows practice, ... Such efficiency is not a goal. ... Don't chase a shadow. ... The best technical exercises for all guitarists. ...
    (rec.music.classical.guitar)
  • Re: SQL Server 2005 Memory edition
    ... completely obsessed with efficiency and reliability (last week I worked ... which means that I can leave database work I ... fnTableWithOneRowValuesBetween (From as Int, ...
    (microsoft.public.sqlserver)