Use your words
Most programming languages today, barring EsoLangs and the APL family of languages, use English for their APIs.
Some languages' designer, like for Python and Ruby, went to great lengths to make those languages' APIs read like a valid English sentence (more or less.)
The thing with using a spoken language for APIs to be consumed by a computer, is that sometimes the meaning of words in the natural language get hijacked into meaning something not quite the same.
A great example for that is the word 'private'.
In English, the word 'private' means one very particular thing: something that is personal, not to be interacted with by anyone other than its owner. Not quite, but not too far removed from a secret.
What's the point?
Lately, as part of my Elixir-to-F# reimplementation journey, I defined several private types.
In F# when a type is private it requires a public constructor to create instances of, but it also requires a public getter to access its members, so they can be viewed or updated.
All that added boilerplate got me thinking, "is it worth it?!"
When I created the private types, and their public constructors, I had a notion of the word 'private' like that in the English language: the type is a secret, any attempt to create an instance of it must be allowed by the type owner - me.
No team member, and for that matter me, two months in the future, having forgotten the context and implementation details, is such a "team member", can create an instance of the private type without my say so - no instances showing up in places I don't expect them.
But then it struck me: the constructor is public!
Any member can easily just code privateType.create args
and have an instance of that type, no permission needed.
Rogue instances can crop up everywhere in my otherwise pristine code!
What's the point?! What did I gain by making the type private?
And more importantly, why is my favorite language lying to me?
"Private" doesn't mean secret, only accessible by whom I trust and allow.
It means something else!
A trusted gatekeeper
So I asked ChatGPT, obviously.
And the answer I got really made sense to me, so I'm sharing it here in my own words and style.
In programming, and that's true for all languages that implement the "private" accessibility control, no matter what it is actually called, that words means something not quite the same as the English meaning.
Close, that's why it was chosen, but not entirely the same.
In programming when you define something as private, and assuming you do want it to be accessible to the other code, you must also provide APIs (e.g., methods, functions, etc.) to create and access that something. Those implementations must be public.
But, how those public APIs look like and behave... that's entirely up to the implementor!
Are you already seeing where I'm going with this? What's the gain?
By controlling the only ways to create and get the private "something", we make sure that whoever is creating and getting is only able to create and get what we want them to!
For example, in one of my private types the age must be a positive integer, greater than 0.
By making the type private and only exposing a public constructor of my doing, I made sure that no team member can just do privateType (age=-10)
and create an invalid instance of that type.
Oh, no. The only way to get an instance is doing privateType.create (age=XYZ)
, and if that XYZ
is invalid, e.g., negative integer or zero, the instance will simply not be created.
(What happens then is also the responsibility of the private type - no concern of whomever is trying to instantiate the type!)
Same for getting data from the instance.
No longer can anyone do privateTypeInstance.internalDataNooneShouldHaveAccessTo
!
The getter is public, sure, but I, the implementor of it, decide what data is accessible through it.
"Private" in programming means something on the lines of a trusted gatekeeper: it only allows interactions with the data it guards in certain, permissible, ways.
So, the same as the English definition?
Coming to think about it, looking at my own definition of the word "private" in English, I can see the resemblance.
As in English so in programming, "private" denotes something to be kept away from the public, not interacted with by those not permitted.
But unlike English, it doesn't mean a secret, simply something that is fenced off, and only allowed access via the gate, that is being guarded by a trusted guard.
The issue of what's to gain has given me a lot of headaches in the last few days, trying to understand what's to gain by it.
If you had that same question, hope I just cleared it away for you (otherwise, I'm sure ChatGPT can. 🤪)
Top comments (0)