Skip to content

Playable ux#7

Open
smith11235 wants to merge 12 commits into
masterfrom
playable-ux
Open

Playable ux#7
smith11235 wants to merge 12 commits into
masterfrom
playable-ux

Conversation

@smith11235

Copy link
Copy Markdown
Collaborator
  • reformatted Games#Index, Games#New
  • reformatted game.state serialization logic in model and controller
  • added game based logic for button display (these need to be ajax powered in next build)
  • removed @_current_user in favor of current_user helper method.
  • refactored a lot of logic to be cleaner from deep nesting
  • moved Card to lib/card.rb

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont like this rewrite. more code, removed comment, and it does the same thing

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, using @_current_user all through code is bad.

Where is the @var defined? Why are you referencing it directly? A helper method is more standard which is why I rewrote all the users of @_current_user to just 'current_user'.

Then my weird format, its so less logic is executed on EVERY single call. It adds up.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the most part I like this refactor, but isn't the ||= and unless redundant?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not. I like htat you both call this out though. Because it is something ruby should do better.

But this is the crux. There are multiple lines of logic that could be run here.

Multiple lines of logic that I want executed only 1 time.

In the original code, why are we wasting time re-setting @_current_user = session[:id] every single time the method is called?

With my code, if @current_user is defined, it returns that with 1 if-nil check only.
Then the unless lets me have complex logic that could span multiple lines to determine what the final value will be.

Ideally this syntax would be allowed in ruby (but isnt i believe) by writing a block to be executed:

def some_var
  @some_var_i_define_once ||= {
      session[:id] ||= some-value 
  }
end

But since that formatting is invalid, you need a if/unless to open a block who's result is assigned. But, only if @some_var_i_define_once is nil.

Compare that to:

def some_var
  @some_var_i_define_once = if session[:id]
                                                      session[:id] 
                                                   else
                                                     session[:id] = some-value
                                                   end
end

This is maybe a little clearer without the weird seemingly duplicate check, but more expensive execution wise.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't you do:

@current_user ||= ( session[:id] ||= request.remote_ip.hash + rand(1000) )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants