Janko
Janko

Reputation: 9335

Is it ok to pass large objects in method parameters?

I'm doing a Rails application where people can take quizzes. I have a model BrowserGame that's taking care of the controller logic (sessions, redirecting etc.). Currently, this is my #initialize method:

class BrowserGame
  def initialize(controller)
    @controller = controller
  end
end

And in the controller I have a method

class GamesController < ApplicationController

  # actions

  private

  def browser_game
    BrowserGame.new(self)
  end
end

As you can see, I'm passing the whole controller to BrowserGame#initialize (so I can manipulate with sessions and others). Is this a good idea? Are there any side effects, since the controller instance is a large object?

Upvotes: 2

Views: 1123

Answers (2)

ronalchn
ronalchn

Reputation: 12335

There is no problem with passing a large object.

As Darshan says, it is only a pointer.

It would be better to only pass serializable objects if you are forking the process/thread or otherwise trying to create a delayed job to run in the background.

Upvotes: 0

Darshan Rivka Whittle
Darshan Rivka Whittle

Reputation: 34051

Yes, it is fine to pass large objects as method parameters. You're not placing the object on the stack, just a pointer to it. As far as side-effects -- anything you do to @controller from within BrowserGame is seen through any other reference to the controller, but that's probably what you already expect.

Upvotes: 6

Related Questions