Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it bad code smell to call public method in private method of same object instance?

share|improve this question
AAMOI do you have a specific example? – Slomojo Jan 12 '11 at 10:58
No, not currently. Just remembered a case which i discussed with my colleagues. Wanted to get some opinions here as well. – Eimantas Jan 12 '11 at 11:15
Usually I can figure out an acronym by the context, but AAMOI I definitely had to look up – Carson Myers Jan 12 '11 at 13:46
@Carson - did you find anything? – Eimantas Jan 12 '11 at 14:38
As A Matter Of Interest – Carson Myers Jan 13 '11 at 14:25
up vote 25 down vote accepted

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.

share|improve this answer
I agree. Some methods provide functionality that is useful both for internals, and for external callers. A red flag might be if the public method was very specific to the internal implementation, but even then, not every class is intended to hide its internals. I often have a data structure "tool" layer underneath a container abstract type layer, for instance - that way I can re-use some of the data structure code for other data structures underlying other containers. – Steve314 Jan 12 '11 at 12:00

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.

share|improve this answer
+1 for 'not a cause for alarm'. Too many times we see a 'smell' and instantly go into refactor mode without thinking. – Michael K Jan 12 '11 at 15:13

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.

share|improve this answer
Why? If an overwritten public method leads to unpleasant surprises, does it really matter who called the method? – user281377 Jan 12 '11 at 11:16
Yes it does. If overriding one public method breaks another public method I can override that other method too. If it breaks a private method, I can ....? – Kim Jan 12 '11 at 11:57
An unpleasant surprise resulting from overriding a public method is a code smell in itself. A code stench, actually. – Larry Coleman Jan 12 '11 at 13:41
Kim: if the method is public, you must assume that other classes call that method just as well... – user281377 Jan 12 '11 at 13:44
@Larry: Exactly! A private method (which usually contains implementation-specific assumptions) calling a public one makes it more likely that overriding the public method would break things. – Kim Jan 14 '11 at 9:51

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.

share|improve this answer

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?)

share|improve this answer

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?

share|improve this answer
Or have the public method delegate to a (new) private method which is invoked by the other private method. But, why bother? – Lawrence Dol Jan 12 '11 at 23:42

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.