Reducing redundancy in methods

176 Views Asked by At

I have an Enum, with an abstract method, and every case in the enum implements this method. However, in each implementation the variable declarations are always the same, and so I end up with lots of redundancy.

public FSA next(Player targetPlayer, Player bot, double prevDist, double newDist) {
      Melee tmp;
      double weaponRange = (bot.getHolding().isGun()) ? Double.POSITIVE_INFINITY :
          (tmp = (Melee) bot.getHolding()).getRange();
      int ammoLeft = (bot.getHolding().isGun()) ? bot.getHolding().getAmmo() : 0;

      int botHealth = bot.getHealth();
      int enemyHealth = targetPlayer.getHealth();

      ...
    }

What is the best way to reduce this redundancy? My initial thoughts are to create a helper method private Object[] getInfo(Player target, Player bot) that returns an array with the variables needed. But to me this doesn't seem too clean, what would be the best way to do this?

1

There are 1 best solutions below

0
vavasthi On

Make all the variables part of a new utility class. Provide a protected method on the base class with the initialization function.