Simpler Conditional Testing

With all the recent talk and buzz surrounding the unsurprising Ruby programming language, I couldn’t help but think about other ways to simplify programming.  Consider the following:

if (foo == bar, baz, "faz")

Why not use syntax like that featured above to mean “If foo equals bar, or foo equals baz, or foo equals the string ‘faz’”?

This would be equivalent to this common way of testing one variable against multiple values:

if (foo == bar || foo == baz || foo == "faz")

I think such a syntax would jive perfectly with Ruby’s DRY (Don’t Repeat Yourself) principle, which essentially prefers as little code repetition as possible, and with its parallel assignment.  And of course, I would love to see this implemented in other languages, too.

What do you think?

Update: Thanks to Michael Fleet for clarifying that this is indeed possible in Ruby with this syntax:

if [ bar, baz, faz ].include?(foo) do
   #...
end

For more information, see the ruby array.include? documentation.


July 20th, 2006. (Updated: June 18, 2008 at 5:30pm.)
Alan Hogan (@alanhogan).  Contact · About