The Public Force and Private Army

 Ah, the everlasting war between public and private. Unlike in real life, where private army wouldn't really be beneficial to everyone involved, privates are very useful in the realm of coding.

 Well actually, privates don't really exist in the realm of coding too. Yes, the complier won't allow it, but if you somehow force the computer to, say, call a private function(such as using reflection), it will happily do that because computer doesn't actually care about that. Objects are just sequence of bits that computer just cuts up to show you. Your Vector2 with x and y variable is just 8-byte(if you are using 4-byte floats) object that has some sequence of bits that could be cut in half and be interpreted as 2 4-byte floats. You could just cut it to 8 1-byte numbers but that would just be garbage data with no meaning so you just say "give me x" and the computer would carefully read the first 4 bytes and interprete it as a 4-byte float. Even if the x is private, you could still just say "give me the first 4 bytes and interprete them as a 4-byte float" and it's basically the same.

 So why do privates exist? It's about sending a message. No, really. By saying this component is private, you are saying that you really wouldn't like other objects modify of call that. You can't actually prevent it entirely, but you are discouraging it. And if someone goes through the hoops of using the private component and messes up, it's just their fault.
And second usage is signifying that this is an internal implementation detail that might change in the future and others should rely on its behaviour or even, its existence.

 You could make everything public(or the language might not even have such options) and comment some of them like

// DO NOT MODIFY THIS OR THE ENTIRE UNIVERSE EXPLODES

and it's basically the same. Well, kind of. You can't still prevent sleepy coworker(or even yourself) from accidently using it. You could say it just says "I don't trust anyone(even myself) to not mess this up so I will just prevent anyone from using it at all"

Comments

Popular posts from this blog

Abstraction and Encapsulation

Uncomfortable Truth about Coding Tutorial Videos

The Humble Beginning