Is it a code smell to call public method in private method of the same object instance?
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.
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?)
I know this is an old post, but it's something I've been debating at work. I 'do' consider this a code smell, and can't understand why you would ever want to do this. If a private method must call a public method, then the content of the public method should be taken and placed in a private method, which both methods can then call. Why?
The public method may contain tests that aren't necessary once your executing code internally. It might receive a UserObj and want to test user permissions for example.
After a public call, you may have a requirement to lock the object down, if you're using threading, so internally, you wouldn't want to call back out to a public method.
More likely to introduce circular errors and infinite loops and out of mem exceptions in my opinion.
Plain and simply, bad design and "lazy". Public methods provide access to the outside world. There's no reason to walk back outside when you're already inside.
Imagine the opposite. You are in a private method and you need functionality in a public method What if you couldn't call that public method from the private one. What would you do?
- Create a duplicate private method? No
- Create a special public method for the occassion? No
- Call a special private method that can call public methods? No
- Some sort of super that can do this? No
The answer is clearly that when you want the functionality in a public method you should be able to call that method from methods of this class or from other classes.
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.
The question you need to ask yourself is why does your class have the same need as clients of your class? Usually a class has very different needs from its clients. So yes, this is an indication that you have either
(a) exposed something publicly that should be private; or
(b) the class's behavior isn't narrow enough (think single responsibility principle).