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

I have a set of single purpose functions that I need in two separate controllers. Right now I just have duplicate code and I want to get rid of it. This code is part of the controller and doesn't belong in my service layer. Where would you put it?

share|improve this question
CakePHP calls them Components: maybe that's some inspiration. – Luc Franken Oct 22 '13 at 14:27
up vote 8 down vote accepted

You didn't say what kind of logic it is that you are sharing. In short, is this controller logic or a helper function? The two methods of dealing with this in an object oriented language are inheritance and composition. Inheritance makes sense if there is a shared action between the two controllers. Composition makes sense the rest of the time. The example of using inheritance lies in my original answer below the divider.

It's not uncommon to have a utility class or a helper class depending on your framework. For example, in Java and C# web frameworks you might have a package/namespace for utilities. In Ruby on Rails, you might be taking advantage of the Helper class that shares logic between controllers and views. Basically, it would look like this:

// NOTE: group similar functions
static class LoginUtility
    static bool IsLoggedIn(Request request) { /* ... */ }

Alternatively, you can make it a class you instantiate. The key to the above static class pattern is to make your functions pure functions. In other words, you pass in any state that it needs to do it's job, and the function does not reference any other static state in the system.

In either case, you would access it in each of your controllers like this:

void MyAction()
    if (LoginUtiltiy.IsLoggedIn(Request))
        // Do something ...

Original Answer

You didn't say what your platform was, as that can affect the answer. Assuming that it is an object oriented language, the most common approach is to create a base class that both the controllers extend. For example in Ruby on Rails you might have:

class BaseController < ApplicationController
    def my_special_function
        # ...

class Controller1 < BaseController
    # ...

class Controller2 < BaseController
    # ...

You can translate the idea into other languages as well. The same approach will work for ASP.NET MVC, Apache Wicket, Grails, or just about any other object oriented web framework. If your language is not object oriented, then it really depends on how the framework is designed as to the best approach.

share|improve this answer
The alternative is to encapsulate the logic in a separate class, then instantiate that class and call the logic from both controllers. – Kramii Mar 29 '11 at 13:03
Yup I always favor composition over inheritance – Reno Mar 29 '11 at 13:07
Do Kramii's suggestion rather than add an extra layer of inheritance. But I think his actual question was where in the solution does that class go. – rmx Mar 29 '11 at 13:58
It does depend on what the function and framework is. In rails, it might belong in the Helper class. In ASP.NET or Java, you might have a utility class for that type of function. It really depends on how close to the controller it needs to live. – Berin Loritsch Mar 29 '11 at 14:01
It's controller logic. I just happen to need it in two places. What I have now is a extension class that I use to put the logic in one location. – Erin Mar 30 '11 at 14:09

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.