Forums Register Login

Difference between public method and public/private method

+Pie Number of slices to send: Send
Greetings, fellow ranchers. While perusing the internet for help for a solution to a programming exercise, I came across two almost identical solutions. The first example is similar to the solution provided in my textbook. The second example is very different from anything I have seen so far. Why, in the second example, do they go to the extra trouble to have a public AND a private method?

The author did talk about having private instance methods and public accessor/mutator methods. Is this a similar issue?

Example One (similar to what I've been learning so far, in that it only has a public method)


Example Two (very different from the text I'm using SO FAR because it has a public and private method with the same name)
1
+Pie Number of slices to send: Send
It's broken up into two methods for logical/design reasons. One method just gets the max depth of the whole tree. The other gets the max depth from a given node. They apparently decided that outside users of that class would only need to ever find the max depth of the tree, so there was no reason to expose the other method to them.

1
+Pie Number of slices to send: Send
These are recursive methods which traverse a tree. Recursive means that in order to find the solution, the method calls itself. In the first example, the method takes Node as a parameter. So each time it encounters a new Node, it calls itself passing the Node as the argument. In the second example there is no parameter, we just want to find out how deep the tree goes. But for recursive walking of a tree to work, we have to pass a node in. So we create an auxiliary private method that takes the Node parameter.
+Pie Number of slices to send: Send
Thanks, guys.

Dennis, the recursion part I get. It's the methodology of having two methods, a public and a private that I was asking about.

Jeff, is that that a "best practice?" Should I incorporate that in my code?
1
+Pie Number of slices to send: Send
 

Ken Austin wrote:Dennis, the recursion part I get. It's the methodology of having two methods, a public and a private that I was asking about.


It does not sound like you are getting it. The recursion is the reason, in this case, for the construct.

Jeff, is that that a "best practice?" Should I incorporate that in my code?


Do you need it? Do you have a use case that requires it? Then, no. If you don't have a reason for it, you don't do it.

1
+Pie Number of slices to send: Send
 

Ken Austin wrote:
Jeff, is that that a "best practice?" Should I incorporate that in my code?



Breaking a method down into logical, simpler steps is a best practice, up to a point. In this case there is a sensible division between the jobs of the two methods, so I would say it was a good choice here. You should incorporate this practice into your code in cases where it would foster code reuse or where a potentially "too long" method would benefit from being broken down into logical, simpler steps.

Making a method that is a "sub-step" (not to be confused with this) of another method private when there is no reason in your design for external users of the class to call the directly is a best practice. I don't know enough about the overall requirements and design to comment on whether it was a good choice here, but if this is just an isolated code snippet provided for the purposes of demonstration, there is no right/wrong to it here. You should incorporate this practice into your code in cases where your design suggests no reason for a "sup-step" method to be called by external users of the clas.
+Pie Number of slices to send: Send
Okay, now that I have read Dennis' response again, I understand it a little better. Thanks, Bear, for pointing that out.

Thanks, Jeff. I appreciate the clarification.
+Pie Number of slices to send: Send
You're welcome.
Did you miss me? Did you miss this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1257 times.
Similar Threads
Binary Search Tree
Perform a swap of all children of nodes at a given depth
Finding height of tree and depth of nodes in huffman tree
Binary search tree remove node
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 08:39:21.