Java operator overloading

Posted by nimcap on Stack Overflow See other posts from Stack Overflow or by nimcap
Published on 2009-10-16T11:56:04Z Indexed on 2010/04/04 16:23 UTC
Read the original article Hit count: 445

Not using operators makes my code obscure.

(aNumber / aNother) * count

is better than

aNumber.divideBy(aNother).times(count)

After 6 months of not writing a single comment I had to write a comment to the simple operation above. Usually I refactor until I don't need comment. And this made me realize that it is easier to read and perceive math symbols and numbers than their written forms.

For example

TWENTY_THOUSAND_THIRTEEN.plus(FORTY_TWO.times(TWO_HUNDERED_SIXTY_ONE))

is more obscure than

20013 + 42*261

So do you know a way to get rid of obscurity while not using operator overloading in Java?

Update: I did not think my exaggeration on comments would cause such trouble to me. I am admitting that I needed to write comment a couple of times in 6 months. But not more than 10 lines in total. Sorry for that.

Update 2: Another example:

budget.plus(bonusCoefficient.times(points))

is more obscure than

budget + bonusCoefficient * points

I have to stop and think on the first one, at first sight it looks like clutter of words, on the other hand, I get the meaning at first look for the second one, it is very clear and neat. I know this cannot be achieved in Java but I wanted to hear some ideas about my alternatives.

© Stack Overflow or respective owner

Related posts about java

Related posts about coding-style