vjskrw
Last Updated: February 25, 2016
·
361
· KieranP

Structure conditionals properly, fast -> slow

Always put conditionals that don't trigger SQL queries first place in a conditional. Rather than:

if @post.author.posts.count > 1 && @post.published?
  ...
end

Put these quickest conditionals first, so if they return false, the other code won't be executed:

if @post.published? && @post.author.posts.count > 1
  ...
end

The same rule applies for slow methods. Put the quickest ones first, then the slower ones last. e.g.

if @post.published? && @post.calculate_rank > 5.0
  ...
end