Why Do We Bother With All These “public” and “private” Keywords?
They are called access modifiers. And to a new programmer all of these “public”, “protected”, and “private” keywords probably seem to add a layer of complexity for something they don’t really care about. But they see it all over programs, even ones written by great developers. So there must be a reason… Let’s talk about that today.
The access modifiers are appropriately labeled that way. They allow you to control access to code. And at this point you’re probably the only person who works in your code base and you’re wondering why you’d need to protect parts of code from yourself. The quick answer is that you’re doing it to make your program simple for you to work with, even when you come back to it a year later. Even if your projects now are largely throw away projects you use as learning tools, I’d still recommend following this best practice.
Minimizing what parts of your code you make public reduces complexity. The public parts of your code are how your objects interact with each other. Remember that the private data and methods of your class can’t be accessed outside of your class. So this comes down to a matter of having less moving parts. The less public code you have, the less levers there are so to speak. I’m sure you’d agree that is simpler in terms of dealing with real life objects too.
Let me give you an analogy to further illustrate my point. Consider yourself as the owner of an auto repair shop. Your “public” data and methods are contained in the front lobby where your customers enter the building to speak with a representative and the ads you may have on the walls there. Think of the auto shops you’ve been to in your life- they are all like this. It’s really simple right. You can look at their pricing, what kinds of tires they have available with data on their performance, or you can get in line to speak to the person working the counter to request a service. That’s it.
Now think of the million other things that pertain to that business that they wisely choose NOT to concern you with. As the business owner, tell me if you’d want to give your customers access to the following actions or information:
How much money each service you offer actually costs you
What temperature they would like the lobby set at
What brand of tools your mechanics use to do their job
What day the garbage company comes to collect your trash
Which of your mechanics called in sick today
Some of these are pretty ridiculous right? The overwhelmingly majority of customers don’t care about these kinds of details- they just want their oil changed. In the case of knowing your cost of performing services, that may inspire customers to argue with you about pricing if they think you are making too much! Beyond burdening your customers with too much information, you may not like sharing your private business.
When you decide to not bother with all these access modifiers and just have everything public so you never run into cases where objects cannot share data and behavior with each other, you are effectively creating an auto repair shop in one giant room that contains everything in the open. The customer counter is set between the car lifts in the garage where loud music is playing. Your private business documents are left out in the open for customers to peruse. Customers could slip on lubricant that spilled on the floor. Customers can access the computer you use to order from your suppliers and change the materials to their own preferences, regardless of impact on your business.
Sure, you could technically run a business that way. I think you would agree it would be a hot mess! But you wouldn’t believe how many newer programmers think that a program simply running with correct output is justification that it is good. Programs like this are said to have a lot of technical debt. They are a nightmare to maintain and build upon. They can cause employees to seek different jobs.
Another way to think of the private access modifier is as a marker for code that pertains to your personal calculations for performing your own work. Have you ever used a piece of paper to make a todo list, a calculation, sketch up a design, or something of the sort? You do this when you have some problem to solve and it’s for your personal reference only. The result of your efforts (getting a landscaping project done, getting a document notarized) is what the public sees but you don’t share your notes publicly. It was your business how you actually went about solving the problem. Others are busy themselves and don’t care- they just sometimes need the result of your efforts.
Now imagine you are on a team of software developers. You get a project and divide the work up fairly. When you need the result of some work they are responsible for providing, you will use one or more of their classes. Now when you go to look at their code, what do you want to see? Do you want to sift through hundreds of public methods trying to figure out which you should call? Or would you be relieved to see you had only one or a few options to choose from?
There’s really all there is to why we bother with access modifiers, at least in a nutshell. You may have noticed some of the other access modifiers, which I mostly won’t touch on in this beginner’s article. I will briefly talk about the next most common one you’ll encounter- “protected”. You can think of protected as somewhere between public and private. It is related to a concept of object oriented programming called inheritance. This allows you to derive classes from other classes. This means you are making a class that contains everything from another class, plus whatever else you would like to add. This is really useful so you don’t have to repeat the same code in a bunch of classes that have a lot of similar data and behavior. I’ll do a separate article on inheritance so I don’t go off on a tangent here.
But “protected” means that derived classes can access the protected code of the base class too. For classes that aren’t inherited, protected acts just like private. In simple language you can think of it like a derived class is a trusted friend of the base class, one you trust sharing certain data with.
You can find more details about access modifiers in the C# programming language here.