Search code examples
functionparametersargumentsencapsulationgetter-setter

Do function arguments violate encapsulation?


This question is related to OOP practice in general. Say we have a class with a public function accepting passed in arguments from outside of the object. Is that not a violation of encapsulation in itself? On the other hand why is this practice used so widely? After all the constructor of the class and member variables are kind of "by-passed" when calling the function. As an relatively new programmer to OOP and my understanding of encapsulation my function parameters are passed into the object through setters, so that I keep all of my functions without any arguments using the passed in member variables only. I know that certain arguments can be passed in through the constructor (BTW, I use dependency injection), but what if those parameters change after the object is being instantiated? There must be a way to change those values after the object is created. So far I found no other option than using setters to accomplish this task, but there is a long lasting discussion among programmers about getters and setters to be "evil" or at least considered no good programming practice. Can anyone tell my where I missed the point and how to solve this dilemma in a clean way? Many thanks in advance for any support.

Here is a concrete very simple example using C#:

we have a form in a windows form project holding 3 textboxes ,named textBox1 and textBox2 and textBox3.

The task is to add values of textBox1 and textBox2 and returning the result to textBox3 using class AddTextboxValues instantiated by event handler any time the value of textBox1 or textBox2 changes:

The way I see it often and ask if is violation of encapsulation:

public class AddTextBoxValues
{
    public double TextBoxValueSum(double textBox1value, double textBox2Value)
    {
       return textBox1value + textBox2Value;
    }
}

This is the way I use at the moment as per my understanding of encapsulation:

public class AddTextBoxValues
{
    private double textBox1Value;
    private double textBoxValue2;
    private double textBoxValue3;

    public double TextBox1Value
    {
        set { textBox1Value = value; }
    }

    public double TextBoxValue2
    {
        set { textBoxValue2 = value; }
    }

    public double TextBoxValue3
    {
        get { return textBoxValue3; }
    }


    public void TextBoxValueSum()
    {
        textBoxValue3= textBox1Value + textBoxValue2;
    }

}

This has also the advantage that it can be injected into the form constructor.

Any comment is highly appreciated.


Solution

  • Thank you very much Jon Skeet, you are a real professional. You diagnosed my problem exactly and opened my eyes for the lack of knowledge to understand and find a solution to my own question. It was indeed a deeper understanding of encapsulation and "object state" which built the missing piece of my puzzle. Everything seems logic and clear now for me and I hope it will help others in the future,too.