What are the benefits of a proper review?

So yes, the question basically says it all. What do you get when you make sure that private members / methods / regardless of whether they are marked as private (or protected, or public, or internal, etc.) Accordingly?

I mean, of course, I could just mark all my methods as public, and everything should work fine. Of course, if we talked about good programming practice (by the way, I am a solid supporter), I would call the method closed if it should be marked as such, without question.

But put aside good programming practice and just look at it in terms of actual quantitative gain. What do I get to correctly define my methods, members, classes, etc.?

I think that this will mainly lead to increased productivity, but I would appreciate it if someone could provide more detailed information about this.

(For the purposes of this question, I think more about C # .NET, but hey, feel free to give answers to any language / frame that you find appropriate.)

EDIT . Most of them noted that this does not lead to an increase in productivity, and, looking back, I do not even know why I thought so. Probably lack of coffee.

In any case, any good programmer should know how the right areas (1) help maintain your code / (2) control the proper use of your library / application / package; I was curious to find out if there was any other benefit from this, which is clearly not obvious. Based on the answers below, it looks like it basically boils down to just these two things.

+4
source share
5 answers

Performance has nothing to do with the visibility of methods. Virtual methods have some overhead, but that’s not why we are dealing. This is due to code maintenance. Your public methods are the APIs for your class or library. As a class developer, you want to provide some assurance to the outside world that future changes will not violate other people's code. By noting some private methods, you will remove the ability of users to depend on certain implementations, which allows you to freely modify this implementation as you wish.

Even languages ​​that do not have visibility modifiers, such as python, have conventions for marking methods as internal and subject to change. Prefixing the method with _underscore (), you signal to the outside world that if you use this method, you do it at your own peril and risk, as it can change at any time.

Public methods, on the other hand, are an explicit entry into your code. Every effort should be made to make publicly available methods backward compatible in order to avoid the errors described above.

+8
source

Thanks to the best encapsulation, you provide the best API. Only methods / properties that are of interest to the user of your class are available: visible. In addition, you warrant that certain variables that should not be called / changed cannot be called / changed.

It is most important. Why do you think this will increase productivity?

+5
source

As I can see, you get two important functions from the right area. You have reduced your API and are clearly focused on the task.

Secondly, you get a less fragile implementation, as you can freely change implementation details without changing the open API.

I don't see how accessibility modifiers can affect performance in any way.

+5
source

Basically, there are two types of methods / properties.

  • This is useful for completing a task for those who use it. (Recommended Scope: Public )
  • It is useful for the above methods to complete your task. (Recommended Scope: Private or Protected )

Type 1 methods are the only methods that require any client code and do not need any other method. This avoids confusion, makes things simple, and prevents the client code from doing something wrong.

Type 2 methods are methods into which type 1 methods are divided. They help 1st class methods to accomplish their task and still allow them to be simple, concise, less complex and readable. They are not needed for client code, but only for the class / module itself.

A vivid example is a car. You have a gas pedal, brakes, gearbox, etc. You do not have an interface with little details for what is under the hood. This is for the mechanic.

+2
source

In C # programming, this helps ensure that your API / classes / methods / members are "easy to use correctly and difficult to use incorrectly . "

+2
source

All Articles