Showing posts with label gotcha. Show all posts
Showing posts with label gotcha. Show all posts

August 10, 2007

Why use parens in Ruby?

Here is another ruby gotcha. Ruby allows you to skip parenthesizing method calls. These two lines are equivelent:

set_table_name "users"
set_table_name("users")

In some cases this will make your code mildly more readable. In practice though this is dangerous. Consider this code:

if arr1.include? "foo" && !arr2.include? "bar" then
  # do something
end

Ruby order of operations says that && will be evaluated prior to calling my_array.include?. So first you evaluate:

"foo" && !arr2.include? "bar"

Which always returns true or false. Then you evaluate;

if arr1.include? true
  # do something
end

Which is clearly not what you intended. This code is correct:

if arr1.include?("foo") && !arr2.include?("bar") then
  # do something
end

As a rule, you should never call a function without parenthesizing the parameters. Even if you are writing code which is not in an IF statement or is in an IF statement, but is not a part of boolean logic, you should still parenthesize it because it is possible someone else will come along and change your code not realizing this gotcha exists. Code defensively, use parens.

May 15, 2007

Rails & MySQL gotcha

Just so you know, don't ever do this in MySQL:

SELECT * FROM foo WHERE id IS NULL;

Why you ask? Seems like a pretty normal thing to do.

MySQL has a known bug that will cause this to return the last row inserted into the DB, ie. something random, instead of what you are looking for. In reality, this was actually a design choice for MySQL. This was how they implemented the ability to figure out the ID for a row inserted with auto-increment. This is fixed in MySQL version 5.0.25 which will probably be in our next upgrade. If you have to use an older version of MySQL, do this:

def find_user_by_id(user_id)
return nil if user_id == nil

@user ||= User.find_by_id(user_id )
@user
end

not this:

def find_user_by_id(user_id)
@user ||= User.find_by_id(user_id )
@user
end

For more information, see...

http://bugs.mysql.com/bug.php?id=14553