-1

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?

Hdot
  • 543
  • 1
  • 4
  • 15
  • For better help, please provide a simple realistic representation of the enum, one that is simpler than your actual program, but that compiles, runs and shows us the problem, a [mcve]. – Hovercraft Full Of Eels Jan 27 '19 at 14:22
  • 1
    Make the `next` method take one parameter instead of four. Move the logic dealing with the four fields into the new [parameter object](https://refactoring.com/catalog/introduceParameterObject.html). – jaco0646 Jan 27 '19 at 15:54

1 Answers1

0

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

vavasthi
  • 922
  • 5
  • 14