proseidon
proseidon

Reputation: 2305

Is there any point to making public-facing methods for private methods in a class?

Sorry if the question sounds confusing. What I mean is that if I have a class that has a method that does a bunch of calculations and then returns a value, I can either make that method public (which gives my other classes access), or I can make it private and make a public get method.

Something like this:

public publicmethod{
    return privatemethod();
}
private privatemethod{
    //do stuff
    return value;
}

Is this a futile exercise or does it provide additional program security?

Upvotes: 0

Views: 191

Answers (5)

Vlad
Vlad

Reputation: 35584

Well, there is no additional security here. However, such a usage can sometimes make sense.

For example, the private and public method may have different semantics.

// base class
public virtual BuyFood()
{
    BuyPizza();
    BuyCoke();
}
private void BuyPizza()
{
    // ...
}

// derived class
public override void BuyFood()
{
    BuyChopSuey();
}
private void BuyChopSuey()
{
    // ...
}

So your implementation is just calling to a private method -- but what is important, you expose the semantics: your BuyFood operation is just BuyChopSuey(). Your code says: "in this class, buying food is just buying chop suey" in a clear way. You are able to add BuyTsingtaoBeer() into BuyFood() any time without changing the semantics of the both methods.

Upvotes: 4

Guffa
Guffa

Reputation: 700212

If the method only does some calculation and doesn't use or change anything in the object, make it a public static method:

public static CalculationMethod(int input) {
  //do stuff
  return value;
}

That way any code can use the method without having the create an instance of the class:

int result = ClassName.CalculationMethod(42);

Instead of public consider internal, which would give access only to code in the same assembly.

Upvotes: 0

Frédéric Hamidi
Frédéric Hamidi

Reputation: 262919

Well, actually the question is What code do I want to be able to call this method?

  • Any code in general, even from other assemblies? Make the method public.
  • Any code from the same assembly? Make it internal.
  • Only code from this class? Make it private.

Having a private method directly aliased to a public method only makes the private method callable from the outside, which contradicts its private status.

Upvotes: 0

user395760
user395760

Reputation:

It is completely redundant. It does not provide anything except another name for the same thing and another indirection for readers to follow. Simply make a single implementation, and make it public. On the same note, getX() { return x; } setX(T newX) { x = newX; } does not encapsulate anything, at best it's future-proofing.

You may end up implementing a particular function required by an interface in a single line, largely delegating to (possibly private) methods which exist for other good reasons. This is different, and more justified (but again, if it's only return someMethod(); you should probably abolish the private implementation and assume the common name). A particular case if when you need two implement two methods which do the same thing (e.g. from separate interfaces).

Upvotes: 3

TLiebe
TLiebe

Reputation: 7966

I think either way is fine, it's more a matter of style assuming the method doesn't change the state of the class. If you have a class that has a bunch of properties and very few methods, it probably makes more sense to define another property. If you have a lot of methods in the class but few properties, then a method is more consistent with your overall class design.

If the method changes a bunch of other class variables than I'd expose it as a public method instead of a property.

I don't think either way, property or method, is necessarily more secure. It depends on what checks you do - is the caller allowed to perform the calculation? Are all variables used in the calculations within acceptable ranges? Etc. All of these checks can be performed whether you are using a property or a method.

Upvotes: 1

Related Questions