Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the benefits of proper scoping?

Tags:

scope

c#

So yeah, the question basically says it all. What do you gain when you ensure that private members / methods / whatever are marked private (or protected, or public, or internal, etc) appropriately?

I mean, of course I could just go and mark all my methods as public and everything should still work fine. Of course, if we'd talk about good programming practice (which I am a solid advocate of, by the way ), I'd mark a method as private if it should be marked as such, no questions asked.

But let's set aside good programming practice, and just look at this in terms of actual quantitative gain. What do I get for proper scoping of my methods, members, classes, etc.?

I'm thinking that this would most generally translate to performance gains, but I'd appreciate it if someone could provide more detail about it.

(For purposes of this question, I'm thinking more along C#.NET, but hey, feel free to provide answers on whatever language / framework you deem fit.)

EDIT: Most pointed out that this doesn't lead to performance gain, and yeah, thinking back, I don't even know why I thought that. Lack of coffee probably.

In any case, any good programmer should know about how proper scopes (1) help your code maintenance / (2) control the proper use of your library / app / package; I was kinda curious as to whether or not there was any other benefit you get from it that's not apparently obvious outright. Based on the answers below, it looks like it basically sums up to just those two things most importantly.

like image 833
Richard Neil Ilagan Avatar asked Nov 30 '22 05:11

Richard Neil Ilagan


2 Answers

Performance has absolutely nothing to do with the visibility of methods. Virtual methods have some overhead, but that's not why we scope. It has to do with maintenance of code. Your public methods are the API to your class or library. You as a class designer want to provide some guarantee to the outside world that future changes aren't going to break other peoples code. By marking some methods private, you take away the ability for users to depend on certain implementations which allows you freedom to change that implementation at will.

Even languages that don't have visibility modifiers, like python, have conventions for marking methods as internal and subject to change. By prefixing the method with an _underscore(), you're signalling to the outside world that if you use that method, you do so at your own risk, as it can change at any time.

On the other hand, public methods are an explicit entry way into your code. Every effort should go towards making public methods backward compatible to avoid the pitfalls I described above.

like image 73
Josh Smeaton Avatar answered Dec 05 '22 04:12

Josh Smeaton


By better encapsulation, you provide a better API. Only methods / properties that are of interest of the user of your class are available : visible. Next to that, you ensure that certain variables that should not be called / modified, cannot be called/modified.

That's the most important thing. Why do you think this would lead to performance gains ?

like image 40
Frederik Gheysels Avatar answered Dec 05 '22 03:12

Frederik Gheysels