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.
-
\$\begingroup\$ Use an EL? Still Java, can be extended if you need specialized types, etc. Otherwise... Java is Java, and it's Java. \$\endgroup\$Dave Newton– Dave Newton2012年07月02日 23:59:37 +00:00Commented 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\$ROBOKiTTY– ROBOKiTTY2012年07月03日 03:26:19 +00:00Commented Jul 3, 2012 at 3:26
1 Answer 1
@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++)
-
\$\begingroup\$ I've looked at some JVM alternatives like Scala/Groovy/Stab. Still considering, although leaning heavily toward Xtend... \$\endgroup\$ROBOKiTTY– ROBOKiTTY2012年07月03日 03:34:43 +00:00Commented Jul 3, 2012 at 3:34