Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I avoid making private field variable public for convenience?

I have two objects, A and B. Right now, B stores A as a local class variable.

class B
{
    A myA;
} 

Now, B has a bunch of methods which uses its inner myA to do some stuff, and I can use those methods since they are public.

But sometimes, I need to use the myA itself. So what I did was to make it public, and then I could write myB.myA.someMethodFromA().

Is this okay, or is it bad style? I mean, I know that I could just provide indirect accessors to myA via methods in the B class, but that seems unnecessary when I can just directly access myA.

For example, if myA has a method doStuffviaA, I'd rather say myB.myA.doStuffViaA(), than first having to write a method in B that says

void doStuffViaB() { myA.doStuffViaB() }

But, of course making myA public means that it can be changed without B knowing about it. WHAT TO DO?

like image 237
Dash Avatar asked Dec 06 '25 22:12

Dash


1 Answers

Yes, making it public circumvents encapsulation and code can creep in that puts your object into an ill-defined state.

A half-way house option would be to supply

const A& getMyA() const
{
    return myA;
}

since then, at least, only const functions can be performed on the myA member if accessed using this function. I'm hoping, of course, that doStuffViaA is const, then you'd write

myB.getMyA().someMethodFromA();
like image 160
Bathsheba Avatar answered Dec 08 '25 12:12

Bathsheba