3

I have always provided getters and setters for most class properties.

Although i have read that this is bad - http://www.codeweavers.net/getters-and-setters-are-evil/

Doesnt dependency injection and unit testing require that most properties have a setter?

1
  • 1
    its not getter and setter issue. its public and private issue. Even in private properties you need getter and setter so you have control over reading and writing the properties. Aug 23, 2012 at 13:27

4 Answers 4

5

Getter and setter should be always used. The reason of getter or setter is not to provide a public interface to internal properties, rather to provide a control over read/write of a property. They provide abstraction over the class properties.

Even your class properties is private you need getter and setter. This allows to control the value just before assigning or reading.

Think about a class you designed long ago where you do some common calculation for each read.

class A{
    private decimal x;
    public void do_stuff(){
        decimal a = this.x/70;
        // process with a
    }
    public void do_anoter_stuff(){
        decimal a = this.x/70;
        // process again a
    }
}

Now you want to change the factor (70). how do you do it? change it in every place? Better design it this way.

class A{
    private decimal x;
    private get_x(){ return this.x/70; }
    public void do_stuff(){
        // process with get_x()
    }
    public void do_anoter_stuff(){
        // process again get_x()
    }
}

The fact is blindly using getter and setters for every property is evil. The rule of thumb is. Declare all properties as private with private getter and setter. Later change the visibility of the getters and setters only to allow access from outer world when needed

2
  • 1
    I think this basically misses the point of the referenced article. Aug 24, 2012 at 0:03
  • see the answer that I posted. Aug 24, 2012 at 1:30
2

What that article is saying is that OO means encapsulation of data and providing behaviour. By providing properties you're not encapsulating data. For example, you could implement a Account class like this:

public class Account
{
  public decimal Balance { get;set; }
}

But, that's no different, conceptually, than:

public class Account 
{
  public decimal Balance;
}

In either case the class has no behaviour. All the behaviour that operates on Account needs to be external to Account. What the article is saying is that behaviour and state should live together. So, you might have something like this instead:

public class Account 
{
  private decimal balance;
  //...
  public void DepositFunds(Money money)
  {
    balance += ValidateAndConvert(money);
  }
  public void WithdrawFunds(Money money)
  {
    balance -= ValidateAndConvert(money);
  }
  public void AdjustBalance(Money money)
  {
    balance -= ValidateAndConvert(money);
  }
  private decimal ValidateAndConvert(Money money)
  {
    // TODO: validate, convert
  }
}

With an account that has a balance as a property or a field, external logic can modify it as it sees fit. this usually scatters business logic across the code base. If logic to withdraw funds from an account was required to check the balance and verify that the account cannot be overdrawn, or only overdrawn by a certain amount, many places in the code would have to provide that logic. If that logic needed to change many places would have to be found and changed (the risk being one gets missed and inconsistent withdrawl occurs). When the data is encapsulated within the object and only behaviour is provided, there's no way for that logic to be scattered about the code base. This also allows more explicit code. What may have been account.Balance -= someValue; could have been a withdrawl, an adjustment, etc. Now it can be explicit: account.Widthdraw(someValue); or account.AdjustBalance(someOtherValue);`--it's explicit what is happening to the account.

2
  • Even if you do not provide properties to public interface you'll need to use getter and setter for your private data. It provides Information Hiding. Aug 24, 2012 at 1:43
  • The point of the article is that properties don't really provide hiding. You're opening your private data through a property interface. Yes, you can change the implementation details behind the scenes (like calculating instead of storing the same type of data) but a property basically says that something accepts the entire range of values for that time. e.g. Decimal. If I had a Balance property, it should support all ranges of Decimal. A balance of Decimal.MaxValue doesn't make any sense. Aug 24, 2012 at 2:08
1

What that article is saying is that you should not expose your private data though public getters and setters. It is not implicitly wrong to have getters and setters for all members (and some people will argue that it is a good idea although in practical terms few people actually bother for every private variable ... i've not yet met anyone who actually does anyway)

What is a bad idea is to have public getters and setters for every variable. This almost guarantees a poorly encapsulated class.

In c++ it is possible to get around the private access with friendship to provide white box tests, and i think you can do the same using other mechanisms in other languages, but i am not sure (I've only every developed commercially in c++, and i don't test apps i write for fun in Java and C#). Possibly Reflection will allow the behaviour you are looking for.

-1

I wouldn't add getters/setters unless you need them, otherwise it's hard to know what data/methods other classes are depending on when you need to make a change. Plus, overuse of getters/setters can be a symptom of poor object-oriented design which is all about data encapsulation.

Getters can certainly help state-based unit testing, although you may consider limiting the scope so that only the test class has access to them. Or even better, do the equality checking in the class itself by creating an equals method.

Dependency injection helps you avoid getters because you can instead do interaction-based testing by injection mock objects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.