mardi 30 octobre 2018

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?

Aucun commentaire:

Enregistrer un commentaire