-
Recent Posts
Popular Posts
- Rent or Own: Amazon EC2 vs. Colocation Comparison for Hadoop Clusters 27 comment(s) | 10890 view(s)
- Mysql Replication Adapter 26 comment(s) | 6700 view(s)
- Making sure Ruby Daemons die 20 comment(s) | 7401 view(s)
- Matching Impedance: When to use HBase 19 comment(s) | 22421 view(s)
- Goodbye MapReduce, Hello Cascading 17 comment(s) | 9748 view(s)
- Rapleaf Challenge Problem 12 comment(s) | 3824 view(s)
- BloomFilter 11 comment(s) | 5483 view(s)
- Using random numbers in Hadoop MapReduce is dangerous 11 comment(s) | 4057 view(s)
- Ruby and HBase 10 comment(s) | 5294 view(s)
- Cycles of Doom in Batch Processing Workflows 10 comment(s) | 2678 view(s)
Categories
- Anonymouse (2)
- Apache (1)
- bash (1)
- Cascading (6)
- Daemons (1)
- encryption (1)
- Extensions (2)
- Google (1)
- Grub (1)
- Hadoop (22)
- HBase (6)
- HDFS (4)
- Kickstart (1)
- MapReduce (9)
- mcrypt (1)
- Miscellaneous (26)
- Mongrel (2)
- Mysql (2)
- OpenSocial (1)
- Operations (1)
- Ruby (7)
- Security (2)
- Thrift (6)
- Xen (1)
Archives
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- March 2009
- February 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007

Avoiding Java varargs snafus
Since Java 1.5, Java has allowed you to take advantage of “varargs“, a usability feature that many other languages support. It lets you write really clean code and support some pretty cool use cases.
However, there is at least one possible pitfall of using varargs. Consider the method below:
(do some filtering)
}
filter();
Let’s say you factor the existing filtering logic out into a FilterStrategy class, and change the method to take an array of FilterStrategy objects using varargs syntax so that it looks like this instead:
for (FilterStrategy: strategies) {
(use the strategies)
}
}
filter();
The interesting thing to take note of is that your refactoring actually didn’t break any of the callers – it just silently turned them into no-ops. This is because, as the docs say, varargs “automates and hides the process” of passing an array. Putting no arguments in a call to filter is the same as
In some cases, this may be what you want. However, in the case we described above, it’s certainly not – it’s hard to imagine a filter method where not filtering anything is the common case.
The problem here arises from the misuse of varargs. Using varargs basically means “zero or more”, when in fact this method is looking for “one or more”. However, nothing in the method signature is actually enforcing this. Luckily, in cases like this, there is an easy solution:
// use first
(...)
// use rest
for (FilterStrategy: rest) {
(...)
}
}
// this will be a compile error
filter();
Now, after the refactoring, the compiler will tell you that you’ve made an error in calling the filter method.
Finally, it’s worth noting that another way to avoid problems like this is to just not use varargs at all when they don’t make sense. I know that I personally have used varargs in the past when I knew the production code would be using an array, but the testing code would like to just pass one or two of something. This is probably a significant misuse of the construct for very little usability gain, so I would discourage others from going down the same road. (You can usually approximate the same effect with Arrays.asList anyways.)