PHP 1, Java 0: The method assertEquals(Object, Object) is ambiguous for the type

Th old Ashley would never say this, maybe I’ve been working with PHP too long, but seriously, I’m going to have to side with the dynamic language crowd on this one – I think I have been spending too much time developing with Magento!

After an Eclipse upgrade a whole raft of my unit tests started failing to compile with the error: The method assertEquals(Object, Object) is ambiguous for the type. What this means is I’m passing an int and and Integer into a method that has two different signatures: assertEquals(Object, Object) and assertEquals(int, int) both of which could be called, thanks to autoboxing.

I’m hardly doing a lot of Java coding these days, and relish the chance to get stuck in to a little (truthfully, I still feel like PHP coding is ‘hacking’ compared to a good session on Java). But here I am confessing that I honestly cannot be f’ed fixing these errors – I mean seriously, int, and Integer, autoboxing it’s all more work for no gain in this case.

And before anyone responds to tell me that there is a perfectly good reason we have primitives and Objects in Java, and that autoboxing is a great solution to deal with converting between the two. Or that allowing polymorphism through method overloading is a powerful OO tool in Java, I know all of that, I understand it, I just do not want to have to change this:

assertEquals(-11.70, orderItem.getCommission());

into this, 88 times:

assertEquals(new Double(-11.70), orderItem.getCommission());

I hate to moan about a language feature and then not suggest something constructive. How about a version of Java called ‘slow Java‘, where there are no primitives and everything is really an object (some smart ass will tell me it’s called Eiffel right?). If someone clever writes a static analysis tool that will then optimize away the unnecessary new() calls (like where I have an object int that doesn’t need to be on the heap), then It’ll be slower sure, but maybe not impossibly slow? When you can rent cores for 10c an hour, does it really matter anymore? Would slow Java really get spanked in real-world applications? (like my unit tests)

</rant>

You might also be interested in:

  1. shuffle() or: How I Learned to Stop Worrying and Love PHP
  2. Magento 1.1.7 Google Checkout and Free Shipping
  3. My list of PHP language features with hard-to-imagine-use-cases: #1 – The @ symbol that supresses errors
  4. Taking GWT 1.5 RC1 for a spin – a review of the new GWT release candidate.
  5. Magento Events Explained and a few Gotchas avoided!


Tagged as , , , , , + Categorized as Language Design, PHP, Web Development

3 Comments

  1. using assertEquals for doubles is deprecated. You should use a delta/tolerance

    If you use assertEquals(double, double, 0.0), the autoboxing ambiguity will take go away as a side benefit.

    int/Integer might still be an issue of course…

  2. Thanks Greg, good point re: doubles.

  3. The all-object Java with invisible autoboxing already exists: It’s called Scala. It fixes a number of Java’s annoyances concerning types.

Leave a Comment

Name:

Email:

Website:

Sporadic Tweeting...

  • New Post: Why I don’t use Flash, ever (and neither should you NZ Herald): Seriously, just don’t do it – I think I’... http://bit.ly/c9p3p9 1 week ago
  • New Post: A new ASchroder.com for 2010 or, why I haven’t written anything for a couple of weeks…: Tonight I releas... http://bit.ly/91NeAk 1 week ago
  • Don't skip the last session. One of the best is last at #irwd Stephan Spencer was insightful last year. Highly recommended. 3 weeks ago
  • More updates...

What I'm listening to

  • The xx - xx
  • Mos Def - The Ecstatic
  • Phoenix - Wolfgang Amadeus Phoenix
  • Jane's Addiction - Nothing's Shocking
  • Florence + The Machine - Lungs
  • Vampire Weekend - Contra
  • The Temper Trap - Conditions
  • The White Stripes - De Stijl
  • Grizzly Bear - Veckatimest
  • Passion Pit - Manners
  • Everclear - Sparkle And Fade
  • Lisa Crawley - Hello, Goodbye and Everything Inbetween