The "||" idiom is popular in many languages, but keep in mind that it skips not just NULL (nil) values! It also skips otherwise perfectly desirable values like FALSE, 0 (zero), or "" (empty string).
In SQL there is the handy COALESCE function [1] which does exactly what I want in this case: It simply returns the first non-NULL value of its arguments, even if it is FALSE, 0 or "".
def get_cached_user(user_id = nil, username = nil)
user = coalesce(
cache.get_user_by_id(user_id),
cache.get_user_by_username(username),
db.get_user_by_id(user_id),
db.get_user_by_username(username)
)
if user.nil? raise ValueError('User not found')
cache.set_user(user, user.id, user.username)
return user
end
I always wonder why none of the other programming languages provide a handy COALESCE function/operator out of the box. That way, they wouldn't encourage bug-provoking hacks such as abusing the "||" operator.
Of course you can always implement your own COALESCE function, but that won't provide the nice short-circuit ability. So this is only feasible in languages with macros (e.g. Lisp), or languages which are lazy-evaluated (e.g. Haskell).
All other languages should either provide a COALESCE function/operator, or a sane alternative to "||" which skips only NULL values and nothing else.
> All other languages should either provide a COALESCE function/operator, or a sane alternative to "||" which skips only NULL values and nothing else.
In Ruby, only nil and false are false, so you can use || to catch 0s and empty strings. In Perl, 0s and empty strings are false, but there's the // operator (as well as the || or or operators) that catches the first defined value.
I'm not sure if this is something specific to the database framework that you're using - sorry if you know all that already.
In SQL there is the handy COALESCE function [1] which does exactly what I want in this case: It simply returns the first non-NULL value of its arguments, even if it is FALSE, 0 or "".
I always wonder why none of the other programming languages provide a handy COALESCE function/operator out of the box. That way, they wouldn't encourage bug-provoking hacks such as abusing the "||" operator.Of course you can always implement your own COALESCE function, but that won't provide the nice short-circuit ability. So this is only feasible in languages with macros (e.g. Lisp), or languages which are lazy-evaluated (e.g. Haskell).
All other languages should either provide a COALESCE function/operator, or a sane alternative to "||" which skips only NULL values and nothing else.
[1] http://www.postgresql.org/docs/9.1/static/functions-conditio...