1

Without giving too much away, I'm making an object which has 9 attributes. Let's use an RPG for example.

float strength, constitution, fortitude, dexterity, intelligence, charisma, wisdom, willpower, perception;

Given that the object is called player, the constructor of

public Player(float strength, float constitution, float fortitude, float dexterity, 
float intelligence, float charisma, float wisdom, float willpower, float perception){}

According to SonarLint: "Constructor has 9 parameters, which is greater than 7 authorized."

While I know that SonarLint saying that won't stop the code actually functioning, if there is a "proper" way of dealing with these situations, I'd love to know!

Thanks

P.s, if this is the wrong forum for such a question, please point me towards the right one!

  • https://stackoverflow.com/questions/58060511/optimization-in-constructor-parameters – Nosrep Mar 29 '20 at 19:13
  • Thank you for the responses so far. I'll look up the Builder Pattern and 'EnumMaps' as suggested. After deciding which suits my situation the most, I'll accept the best answer! – MJDeveloping Mar 29 '20 at 19:56
  • If you don't use an `EnumMap`, you'll regret it when you start to add more functionality to this class. – Dawood ibn Kareem Mar 29 '20 at 20:34

4 Answers4

1

Try to use Builder Pattern, this method is very good for Classes with 4+ parameters.

TomaszC283
  • 17
  • 6
1

Ideally you would split the class into smaller units and compose.

In this case, as they seem to be all of the same kind of thing, a Map (specifically EnumMap) would appear to be appropriate.

As a last resort, there is the Builder Pattern.

 Player player = Player.builder()
     .strength(       )
     .constitution(   )
     // ...
     .perception(     );
Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
0

Try the builder pattern

final class Player {
  private final float strength;

  ...

  static final class Builder {
    private float strength;

    ...

    Builder setStrength(float strength) {
      this.strength = strength;
      return this;
    }

    ...

    Player build() {
      return new Player(this);
    }
  }

  private Player(Builder builder) {
    strength = builder.strength;
    ...
  }

  ...

  public static void main(String[] args) {
    Player player = new Player.Builder()
      .setStrength(18)
      .setConsitution(18)
      .setFortitude(18)
      .build();
   }
}
Juan C Nuno
  • 476
  • 1
  • 3
  • 8
0

It is never advisable and a proper way to define a function which has so many arguments.Ideally it should have 2 to 3 params only.In case where there are more then specified params then you can do any of the below things.

  • Create an class by wrapping up the paramameter(kind of logical grouping of params).
  • Use build patterns as specified above
  • Provide multiple constructor with only required params.
  • Use setter methods for few things, if you can certain parameters.
Krutik Jayswal
  • 3,165
  • 1
  • 15
  • 38