3
\$\begingroup\$

Doing graphics in Java, I find myself writing lines like these regularly:

Vector3f b = mid.add( a.mult(yOffset + length / 2) ).add( up.cross(a).mult(xOffset) );

It gets even worse when I'm reusing objects.

Vector3f vTemp = VarPool.getVector3f();
Vector3f b = VarPool.getVector3f();
b.set(mid).addMutable( a.multAndStore(yOffset + length / 2, vTemp) ).addMutable( up.crossAndStore(a, vTemp).multMutable(xOffset) );
//...do some useful computation...
VarPool.recycle(vTemp, b);

I've tried coping using a combination of these two methods:

1. Place 'translations' in comments above dense lines

//i.e. Vector3f b = ( mid + a * (yOffset + length / 2) + (up X a) * xOffset );
//this calculates such and such
b.set(mid).addMutable( a.multAndStore(yOffset + length / 2, vTemp) ).addMutable( up.crossAndStore(a, vTemp).multMutable(xOffset) );

Disadvantages: Extra noise. 'Translation' comments can look like actual code that's been commented out. There's no getting around the real code being hard to read.

2. Break dense chains into more readable lines

b
 .set(mid)
 .addMutable( a.multAndStore(yOffset + length / 2, vTemp) )
 .addMutable( ( vTemp = up.crossAndStore(a, vTemp)
 .multMutable(xOffset) ) );

Disadvantages: Some calculations don't chain up quite as nicely. When different return types are involved (e.g. matrices, quaternions, scalars), chaining is not always logical or possible.

Any other suggestions? Switching language is not really viable.

asked Jul 2, 2012 at 23:43
\$\endgroup\$
2
  • \$\begingroup\$ Use an EL? Still Java, can be extended if you need specialized types, etc. Otherwise... Java is Java, and it's Java. \$\endgroup\$ Commented Jul 2, 2012 at 23:59
  • \$\begingroup\$ @David B: I wouldn't say I'm looking for code review, as I'm really after solutions others may have come up with while staying Java-interoperable -- as a random example, a source processor that can translate between operator-overloaded/method-call notation. \$\endgroup\$ Commented Jul 3, 2012 at 3:26

1 Answer 1

3
\$\begingroup\$

@DaveNewton Not necessarily, sometimes it's Groovy :-)

Advantage

  • code/syntax compatible with java.
  • Operator overrides allow you to write your code so it looks more like your comment i.e. operators rather than methods

Disadvantage

  • Groovy is slower than java (but this can be reduced with Groovy++)
answered Jul 3, 2012 at 2:54
\$\endgroup\$
1
  • \$\begingroup\$ I've looked at some JVM alternatives like Scala/Groovy/Stab. Still considering, although leaning heavily toward Xtend... \$\endgroup\$ Commented Jul 3, 2012 at 3:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.