The Artima Developer Community
Sponsored Link

Agile Buzz Forum
Premature optimisation in my Rails session code?

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Dave Thomas

Posts: 85
Nickname: pragdave
Registered: Mar, 2003

Dave Thomas is...
Premature optimisation in my Rails session code? Posted: Jul 18, 2012 1:02 PM
Reply to this message Reply

This post originated from an RSS feed registered with Agile Buzz by Dave Thomas.
Original Post: Premature optimisation in my Rails session code?
Feed Title: PragDave
Feed URL: http://pragdave.blogs.pragprog.com/pragdave/atom.xml
Feed Description: The 'Practices' section of Dave Thomas's blog, including stuff on design, architecture, and the Code Kata exercises.
Latest Agile Buzz Posts
Latest Agile Buzz Posts by Dave Thomas
Latest Posts From PragDave

Advertisement

For years and years, I've been writing

def current_user
  @current_user ||= (session[:user_id] && User.find_by_id(session[:user_id]))
end 

The idea was to look up and store the current user object in an instance variable the frst time the method was called, and then use the value in that variable for subsequent calls in the same request. 

But now I'm thinking I'll just write this:

def current_user
  session[:user_id] && User.find_by_id(session[:user_id])
end 

Rails already caches query results, so although the find() method will be called on every call to current_user(), the database will only be accessed once, and the user object will only be constructed once. Why complicate my code with optimizations if I haven't yet identified the performance of this method to be an issue?

Is that reckless of me? Or is the ||= a premature optimization which I should train myself out of?

 

Read: Premature optimisation in my Rails session code?

Topic: Scrum Metrics for Hyperproductive Teams Previous Topic   Next Topic Topic: Bliki: PhoenixServer

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use