SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard Darren884's Avatar
    Join Date
    Aug 2003
    Location
    Southern California, United States
    Posts
    1,616
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Best way to minimize code in a controller?

    I can only go so far as to optimizing the code in a controller to make it minimal, however what is the best way if I have a lot of actions in a controller to cut that file down to as small as possible? Am I just kind of stuck with having all that code in there? Thanks
    Have a good day.

  2. #2
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Darren884 View Post
    optimizing the code in a controller to make it minimal
    I not sure I'd call minimizing code the same as optimizing it. In my experience, optimal code is clear and concise code. Minimal code goes too far and is usually harder to debug and for others to work on.

    However, the best way I've found to make clear and concise code in controllers is to follow DRY (Don't Repeat Yourself) principles. That is, identify items that are repeated in a number of methods and strip them out into a separate private method. For example:
    This
    Code:
    def show
      @search_data = some_search_information
      @current_date = some_date
      @item = params[:id]
      #.... some other code ....
    end
    
    def edit
      @search_data = some_search_information
      @current_date = some_date
      @item = params[:id]
      #.... some other code ....
    end
    
    def update
        #.... some code to process the update ....
      if update_fails
        @search_data = some_search_information
        @current_date = some_date
        @item = params[:id]
        #.... some more code ....
      end
    end
    Can be replaced with
    Code:
    def show
      get_single_item_info
      #.... some other code ....
    end
    
    def edit
      get_single_item_info
      #.... some other code ....
    end
    
    def update
        #.... some code to process the update ....
      if update_fails
        get_single_item_info
        #.... some more code ....
      end
    private
    def get_single_item_info
        @search_data = some_search_information
        @current_date = some_date
        @item = params[:id]
    end
    Also make sure you use meaningful names for thing. That way you can minimise comments and make debugging easier. For example:
    Code:
    #bad
    a = Math.sqrt(b^2 + c^2)
    
    #good
    longest_side = Math.sqrt(shortest_side^2 + other_side^2)

  3. #3
    SitePoint Enthusiast TomK32's Avatar
    Join Date
    Mar 2008
    Location
    Berchtesgaden, Germany
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Darren884,

    The three things you can move your code to are helpers, models and before/after filters. For the models you might want to look into named scopes which always keep my controller nice and short
    just a geek trying to change the world
    I'm a ruby on rails developer in Vienna, Austria. My github.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •