Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Methods using private members or public accessors

I realize this probably cannot be answered, but I'm looking for whether there is some sort of guidance about whether to use private members directly or public accessors inside class methods.

For example, consider the following code (in Java, but it would look very similar in C++):

public class Matrix {

    // Private Members
    private int[][] e;
    private int numRows;
    private int numCols;

    // Accessors
    public int rows(){ return this.numRows; }
    public int cols(){ return this.numCols; }

    // Class Methods
    // ...
    public void printDimensions()
    {
        // [A] Using private members
        System.out.format("Matrix[%d*%d]\n", this.numRows, this.numCols);


        // [B] Using accessors
        System.out.format("Matrix[%d*%d]\n", this.rows(), this.cols());
    }

The printDimensions() function illustrates two ways to get the same information, [A] using private members (this.numRows, this.numCols) or [B] via accessors (this.rows(), this.cols()).

On one hand, you may prefer using the accessors since there is no way you could inadvertently change the value of the private member variables. On the other, you may prefer accessing the private members directly in hopes that it would remove the unnecessary function call.

I guess my question is, is either the de-facto standard or preferred?

like image 262
jedwards Avatar asked Jun 05 '12 17:06

jedwards


People also ask

Should methods be public or private?

Generally you should expose as little as possible and make everything private that is possible. If you make a mistake and hide something you should be exposing, no problem, just make it public.

What is the difference between public and private methods?

Public instance methods: - Use if displaying information or interacting with other classes and/or the client. Private instance methods: - Accessible only from within class scope. - Part of the class inner-workings.

Can public methods access private variables?

Answer 51c025e6282ae349350092d1 But after the object is created you can only access the private variable through the public methods of the constructor and no longer change it directly in any way.

What is public/private and protected methods?

Broadly speaking, public means everyone is allowed to access, private means that only members of the same class are allowed to access, and protected means that members of subclasses are also allowed. However, each language adds its own things to this. For example, C++ allows you to inherit non-publicly.


1 Answers

It's a style call. I prefer to use accessors, because IMHO the function call overhead is small enough that in most cases it doesn't matter, and this usage preserves the data abstraction. If i later want to change the way the data is stored, i only need to change the accessors, instead of hunting for all the places where i touched the variables.

I don't feel strongly about it, though, and i would break this "rule" if i thought i had a good reason to.

like image 182
theglauber Avatar answered Sep 25 '22 13:09

theglauber