Is it bad code smell to call public method in private method of same object instance?
|
|
No not bad smell. This might be needed, why do you suspect it to be wrong ? A method at an atomic level is an independent entity that does a task. As long as it does a task anyone who has access to it can call it to get the task done. |
|||
|
|
It might lead to unpleasant surprises if someone who hasn't read the source code of this class tries to subclass it and overrides the public method. Whether that is a real concern obviously depends on your situation. Maybe you should consider making the public method or even the class final. |
|||||||||||||||
|
|
Code smell? Yes, not a really bad one, but a good indicator that the class may have too many responsibilities. Take it as a sign that the class may need to be broken up into different objects, private methods shouldn't really need to call public methods of the same object, certainly in a clean OO design. Of course, once you've inspected the class and the reasons for the method call are clear, it may be a perfectly reasonable use, in general you'd expect utility methods for the class to be private, but if one is useful enough to be public and utilised by other methods, I would, generally, expect those methods to be public also. As with all code smells, this is a motivation for further code inspection, rationalise and maybe refactor, but not a cause for alarm. |
|||||
|
|
I dont think we can genaralize. It all depends very heavily on context. I might have, say, a public utility method in a class which is used by other classes, and also some private method in the same class. |
|||
|
|
|
NO, no bad smell here. if we implement interface of a queue with List, is it a bad smell to just call the proper List functions in order to achieve the implementation of queue easily? if you have something and you want to convert it to something else(like wrapper) then its not a bad smell, its code re-usability with design pattern acted in function level(is a function an object?) |
|||
|
|
|
No. What else should be done in that case? Make the private method public or the public method private? Copy-Paste the code from the public method to the private one? |
|||
|
|
In my code I often create lazy load getters, which is to say the object gets initialized the first time it's requested and thereafter reuses the same instantiated object. However, an object instantiated using a lazy load implies that it may not necessarily be instantiated at any given point. Rather than wrap my head around the sequence of calls such that I know that that object is already instantiated or repeating the same code of a lazy load inside another method, I simply call the lazy loader whenever I need that object. Just as you can use public methods in a smart way, you can also use them incorrectly. An example of this might be a public method which processes its parameters before calling another private method. It would be a mistake to call that public method casually simply because you have the same parameters. The mistake is subtle but it's a design error more than anything else and requires that you learn to manage with the parameters of the internal method rather than the parameters of the public method. So to answer your question, it's certainly not bad code if you use it correctly. |
|||
|
|