S
S
splunk2016-03-16 01:45:21
.NET
splunk, 2016-03-16 01:45:21

C#. Access statements for hiding, but not for protection?

As I understand it, access modifiers are needed only for hiding class members, but not for protection (reflection). They only help the developer understand which members of the class he has access to.
Roughly speaking, all this is necessary only so that by putting the access operator with a dot ". ", the developer does not see unnecessary "garbage" that he should not touch.
If this is the only function, then why is this topic so bloated in tutorials? Every author shoves these access modifiers into the ugliest code. This is useful only in joint projects. Is it worth bothering with this when you write alone and for yourself?

Answer the question

In order to leave comments, you need to log in

7 answer(s)
A
Alexey Pavlov, 2016-03-16
@splunk

Encapsulation is needed not to protect the code itself (you said correctly - everything can always be seen), but in order to protect the code using the class from changes to this class. There are two concepts - interface and implementation. Any class must have an assignment (and preferably only one assignment), and those elements related to the assignment are made public. And those elements that are not related to the task itself and are intended for auxiliary functions, then these are private elements, the user of this class does not need them.
Example. There is a task - to find the roots of a quadratic equation. The main function is to obtain roots. Usually this is solved using the discriminant, but not necessarily, there are other ways, therefore, the discriminant itself is not needed, because the discriminant is just a way to solve (implementation).
As an example, let there be such a class for solving a quadratic equation:

class KvadrUr
{
    public double A { get; set; }
    public double B { get; set; }
    public double C { get; set; }

    public bool HasRoots { get { return GetDiscr() >= 0; } }

    public KvadrUr()
    {
    }

    public KvadrUr(double a, double b, double c)
    {
        A = a;
        B = b;
        C = c;
    }

    public double[] GetRoots()
    {
        var discr = GetDiscr();
        if (discr < 0) return new[] {double.NaN, double.NaN};
        var sqrt = Math.Sqrt(discr);
        return new[]
        {
            (-B + sqrt) / (2 * A), 
            (-B - sqrt) / (2 * A)
        };
    }

    private double GetDiscr()
    {
        return B*B - 4*A*C;
    }
}

In this class, there is an interface - data (coefficients) and two methods (list of roots and presence of roots). And the method of calculating the discriminant is not necessary for the task.
Now imagine that getting the discriminant would be a public method. Then someone will take and start using this method in their code. And if the author of the class wants to and changes the implementation to another way (according to the Vieta formula, for example), then the discriminant will no longer be needed, and the GetDiscr () method will need to be removed. But in the place where it was used, there will be an error - the GetDiscr () method does not exist. Many programming languages ​​have a special feature associated with interfaces. In C#, this is the concept of the same name - interface. In this example, it would be nice to create an interface like this:
Thus, if the class has an exact indication of what is an interface and what is an implementation, it gives the code more opportunity to change this code.
interface IRoots
{
    bool HasRoots { get; }
    double[] GetRoots();
}
class KvadrUr : IRoots
{
    // ...
}

You can create several different classes - one counts through the discriminant, the other - through the Vieta formula, the third - through the selection of a full square, the fourth - a cubic equation, the fifth - a biquadratic equation. But each of the classes implement the same interface, and each of them can be interchanged.

B
Boris Animal, 2016-03-16
@Casper-SC

In what enterprise? This is like the argument that clothes are needed only for show and to look decent among people, children, etc., but at home you can walk around naked. You can also find fault with any principle of programming. Here you will score on all this, you yourself will not soar that it’s a habit for you to cheat? Therefore, you do not develop a certain skill, because you do not care about hiding. Yes, and in a month you yourself will figure out your big project, what can be called from outside, what not.
Why classes at all? Let's put everything in one class, but each method will have a prefix so that it's clear, otherwise you need to put a point. Here's another!

S
Stanislav Silin, 2016-03-16
@byme

Imagine the following situation:
Person N found out about access modifiers and decided that they were not needed. Those. following this logic, it is necessary to throw away a huge layer of techniques and conventions that have been created over decades for one simple reason - to simplify software development ...
Um .. do you think anyone would want to answer this person seriously?
If you do not understand the essence of the question, you do not need to run to the forum and scribble a question that could have already been asked 100500+ times by the same people as you.
Essentially the question:
It is necessary to worry about this, because it greatly simplifies life for you and the rest. With the help of modifiers, you should describe how your class should be used, protect it from unwanted interference and crooked hands. The fact that all this can be broken with the help of reflection does not mean that you should just forget about it ... Reflection is also a tool designed to help in solving a certain range of problems and how to use it directly depends on the developer ...

R
Roman, 2016-03-16
@yarosroman

It is in C # that there is reflection and metadata for classes, in the same C ++ all this is missing. Plus, reflection does not work in all cases. Plus, in the future, you will have to use the library, and you will simply forget what you can touch (if everything is made public) and what you cannot.

M
Michael, 2016-03-16
@Sing303

Encapsulation improves the reliability of the program and makes the code more self-documenting.
If you are writing a program for yourself, of course you can write as you like, but because of this habit, it will be difficult for you to write a developing project, because. you will get used to the fact that you can change anything in classes, you will design them rigidly, which means that you will not be able to change the interface, which happens very often in a developing project. The same can be true for your project, if your program starts to grow, its complexity grows, dozens of new classes are written, after half a year you will no longer be able to tell how to use this or that class, encapsulation can help here.
It is not clear why you do not want to follow encapsulation in your projects, does it add complexity?

V
VZVZ, 2016-03-16
@VZVZ

And if you write one for yourself, then you will definitely write such shitty code where there is no architecture, everything is piled up right in button_click, and therefore there is nowhere to put a dot in principle, and the classes do not have members that need to be hidden, because in general there are no members , no classes? Seriously?
And if, on the contrary, you write your own high-level and high-quality wrappers for all the libraries you work with, then this is already a harsh enterprise and a lot of people? Thanks for the compliment. I thought I was just a miserable rogue freelancer, but I turn out to be fucking cool, I work in an enterprise and replace a lot of people alone.
About "protection" in general, some kind of crazy reasoning. What are you talking about?

G
Georgy Pelageykin, 2016-03-16
@ArXen42

Access modifiers are more informational than protective. Still, nothing will protect against crooked hands. They help you quickly understand the structure of a class, the roles of its members, and structure your code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question