patternjavaMinor
Physics particle collisions
Viewed 0 times
physicsparticlecollisions
Problem
I have a large file that I want to refactor. My idea is to make helper classes with helper methods so that I can modularize methods.
Is that a good idea? For instance before refactoring:
After refactoring:
Is that a good idea or must I use a more elaborate recfactoring? I think it would work but I don't know of specific refactoring patterns.
Is that a good idea? For instance before refactoring:
public void collision(PhysicsCollisionEvent event) {
if (event.getObjectA() instanceof BombControl) {
final Spatial node = event.getNodeA();
effect.killAllParticles();
effect.setLocalTranslation(node.getLocalTranslation());
BombControl bc = (BombControl) event.getObjectA();
effect.emitAllParticles();
} else if (event.getObjectB() instanceof BombControl) {
final Spatial node = event.getNodeB();
effect.killAllParticles();
effect.setLocalTranslation(node.getLocalTranslation());
effect.emitAllParticles();
}
}After refactoring:
public void collision(PhysicsCollisionEvent event) {
CollisionHelper.collisionHelp(event, effect);
}Is that a good idea or must I use a more elaborate recfactoring? I think it would work but I don't know of specific refactoring patterns.
Solution
A couple of simple things I notice right off the bat.
From the comments: you obviously should move the entire huge method body to the helper method, otherwise you have the same problem. Just extract 'til you drop.
- If you have a method which only calls another method, why bother having the first method at all? I'm personally against these one-line wrapper methods and see them as unnecessary noise.
- Your methods should be verb phrases, in general (e.g.,
CollisionHelper.helpCollisionor evenperformCollision, in this case, since it's doing all of your processing).
- I believe there's a standard for replacing
instanceofusage with an actual method from theClassAPI, as follows:BombControl.class.isInstance(event.getObjectA()). You can see more on this Stack Overflow Q&A. (Apparently you're using it correctly here, however.)
From the comments: you obviously should move the entire huge method body to the helper method, otherwise you have the same problem. Just extract 'til you drop.
Context
StackExchange Code Review Q#54825, answer score: 5
Revisions (0)
No revisions yet.