I have some methods in my code that essentially hide/show some Views, like showTitleHideBody(), showBodyHideTitle(), etc. They just change their Views' (tvTitle, tvBody) visibility.

Initially those Views were globals, but as it is not good practice, I moved the Views to OnCreateView and added parameters to those methods that need them. Now, I call them like showTitleHideBody(tvTitle, tvBody).

But whenever I need to call some method it'll always be doThisDoThatDoThose(tvThis, btnThat, smthThose). Methods like these are called multiple times in my code and I'm always passing the same parameters.

So, is there any better way to do this than repeatedly passing the same ~2-3 parameters everytime I call some method? Or is that ok and the recommended way, and trying to "fix" this would just further complicate things?

2 Answers 2


Two to three parameters would not give me much heartburn. If the number grows too large or even the smaller number is wearisome, then the simplest answer is to create an object or data structure containing both and pass it around. This is the approach recommended in §13.1 of Code Complete where Steve McConnell writes:

Use structures to simplify parameter lists. You can simplify routine parameter lists by using structured variables. [...] Rather than passing each of the elements individually, you can group related elements inot a structure and pass the whole enchilada as a group structure.


Michael's answer is right on target but I might go a little further based on this:

So, is there any better way to do this than repeatedly passing the same ~2-3 parameters everytime I call some method?

If you find that the same parameters are used in a large swath of your functions, you probably have identified a context for your methods. It Depends a bit on your style, the language, and the scope of the problem, but you might want to create a class that is constructed with these parameters and the methods are member methods of that class.

Additionally, if this context object is constant through an execution path, you might want to structure your program so that it is always available (with proper scoping, of course.) There are various approaches to this depending on things like what framework(s) might be involved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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