Improving readability of 3d math in Java 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.
@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++)