Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the practical difference between declaring a function private vs public?

So as I've been reading/learning about classes and the methods within them I've found very little about the practical differences between declaring a method as public versus private.

I know that the difference is a private class can only be accessed within the class, while a public method can be accessed from code outside the class (other classes, functions). But what I really want to know is:

  • Why would you want/not want to declare it one way or another when deploying an application?
  • Are there best practices that can guide whether to declare a method public vs private?

Also, I don't know if it matters, but I am learning primarily VB.Net and C# in a web application environment, so specifics to that would help.

like image 230
Blunderfest Avatar asked Mar 20 '13 01:03

Blunderfest


People also ask

When should a method be public vs private?

Generally you should expose as little as possible and make everything private that is possible. If you make a mistake and hide something you should be exposing, no problem, just make it public.

How are public methods and private methods different?

Public instance methods: - Use if displaying information or interacting with other classes and/or the client. Private instance methods: - Accessible only from within class scope. - Part of the class inner-workings.

What is a private function?

Private function means any gathering of persons for the purpose of deliberation, education, instruction, entertainment, amusement, or dining that is not intended to be open to the public and for which membership or specific invitation is a prerequisite to entry.

What is private and public in JavaScript?

In JavaScript, there are two types of object fields (properties and methods): Public: accessible from anywhere. They comprise the external interface. Until now we were only using public properties and methods. Private: accessible only from inside the class.


1 Answers

Encapsulation means that you should think of each class as a machine that provides a service. For example, a chair allows you to sit on it, or a lawnmower allows you to cut your lawn.

The private methods pertain to the machine's internal workings. In contrast, the public methods relate to how you (other classes) interact with the machine.

Example one: Chair...

When sitting on a chair, you don't need to know volume of stuffing or the number of staples, you basically need to know whether or not it's occupied and if it's stable.

  • Public methods: IsStable, IsOccupied, Sit
  • Private methods: CalculateStuffingVolume, CountNumberOfStaples

Example two: Lawnmower...

For the lawnmower, you need to know if it has enough fuel (or is plugged in), if the blades are sharp, and be able to turn it on.

  • Public methods: GetFuelLevel, IsBladesSharp, TurnOn, TurnOff
  • Private methods: Combust, etc, Too many to imagine.

Conclusion:

So when you're developing all you will see is...

Example one: Chair.Sit, Chair.IsStable and Chair.IsOccupied

or

Example two: Lawnmower.GetFuelLevel, Lawnmower.IsBladesSharp, Lawnmower.TurnOn, LawnMower.TurnOff

As a developer, you will not have to think about number of threads in the uphosltry, the colour of the fuel cap, the number of RPM of the blades or whether the chair is glued or stapled together. This distinction makes it much easier to put your application together without being swamped in detail. Additionally, it allows programmers to expose only necessary information which adds a level of security. As John mentioned, this prevents the Person class from calling Lawnmower.Combust(fuel) when they're not supposed to.

like image 82
Michael Rodrigues Avatar answered Sep 27 '22 01:09

Michael Rodrigues