Playable ux#7
Conversation
smith11235
commented
May 10, 2015
- 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
There was a problem hiding this comment.
I dont like this rewrite. more code, removed comment, and it does the same thing
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
For the most part I like this refactor, but isn't the ||= and unless redundant?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
can't you do:
@current_user ||= ( session[:id] ||= request.remote_ip.hash + rand(1000) )