Do ALL your variables need to be declared private?

Posted by shovonr on Programmers See other posts from Programmers or by shovonr
Published on 2011-03-13T08:24:00Z Indexed on 2011/03/13 16:17 UTC
Read the original article Hit count: 182

I know that it's best practice to stay safe, and that we should always prevent others from directly accessing a class' properties. I hear this all the time from university professors, and I also see this all the time in a lot of source code released on the App Hub. In fact, professors say that they will actually take marks off for every variable that gets declared public.

Now, this leaves me always declaring variables as private. No matter what. Even if each of these variables were to have both a getter and a setter.

But here's the problem: it's tedious work. I tend to quickly loose interest in a project every time I need to have a variable in a class that could have simply been declared public instead of private with a getter and a setter.

So my question is, do I really need to declare all my variables private? Or could I declare some variables public whenever they require both a getter and a setter?

© Programmers or respective owner

Related posts about best-practices

Related posts about code-quality