Question about class length

It’s a pretty simple question, but how long should classes be? I have created a class and wonder if it’s become too long, and maybe I should approach this another way?

Here is the class and a sample object made from it. It will be used to manipulate numbers in a practice project I am working on:

class classList {
    constructor(className, hitDice, statChoice, strength, dexterity, constitution, intelligence, wisdom, charisma, skillChoice, acrobatics, animalHandling, arcana, athletics, deception, history, insight, intimidation, investigation, medicine, nature, perception, performance, persuasion, religion, sleightOfHand, stealth, survival) {
        this.className = className;
        this.hitDice = hitDice;
        this.statChoice = statChoice;
        this.strength = strength;
        this.dexterity = dexterity;
        this.constitution = constitution;
        this.intelligence = intelligence;
        this.wisdom = wisdom;
        this.skillChoice = skillChoice;
        this.charisma = charisma;
        this.acrobatics = acrobatics;
        this.animalHandling = animalHandling;
        this.arcana = arcana;
        this.athletics = athletics;
        this.deception = deception;
        this.history = history;
        this.insight = insight;
        this.intimidation = intimidation;
        this.investigation = investigation;
        this.medicine = medicine;
        this.nature = nature;
        this.perception = perception;
        this.performance = performance;
        this.persuasion = persuasion;
        this.religion = religion;
        this.sleightOfHand = sleightOfHand;
        this.stealth = stealth;
        this.survival = survival;
    }
}

const artificer = new classList(`Artificer`, 8, false, false, false, true, true, false, false, 2, false, false, `choose`, false, false, `choose`, false, false, `choose`, `choose`, `choose`, `choose`, false, false, false, `choose`, false, false);

Once class require more than a few arguments, it might be good idea to try to simplify it.

For example creating new object from the above class:

new classList(`Artificer`, 8, false, false, false, true, true, false, false, 2, false, false, `choose`, false, false, `choose`, false, false, `choose`, `choose`, `choose`, `choose`, false, false, false, `choose`, false, false)

That’s not too convenient. Keeping track of what is what will require going back and forth to the class definition. Making one mistake in the order of the arguments is going to be very hard to notice.

As a rule of thumb, if you find you have more than 3 parameters for a function, then there are too many parameters.

You have 28, this is essentially impossible to use: to stress what @sanity said:

You will make errors, and they will be extraordinary hard to debug. You need to input arguments in the exact order required by the constructor, and you need to keep that order in your head.

You then have 28 mutable variables that I assume you will want to operate on, and it’s all a recipe for having a very bad time. To stress again: you need to keep track of how all those values interact in your head all at once, and that just isn’t feasible.

You need to think carefully about whether you need a class here: it implies you are going to add methods to operate on the values. And there are too many values.

You need to figure out how you can break this down. Are all of these values just a number on a scale? If not, can you separate the ones that aren’t? Can you categorise the values? What are the types of those values? How do they interact? Given the context, are there a max number of points that are shared between some of these values (if so those values can be extracted and grouped together)? &c.

From looking at what this is, I assume it’s only going to get [a lot] more complicated, and you need to figure out ways to make it as uncomplicated as possible (sidenote: a type-checked language/tool will help a lot here).


Putting that aside. For starters, pass an object to the constructor, so you have a single paramer rather than 28, and you construct it like:

new MyClass({
  all: "of",
  the: "properties",
  are: "now",
  immediately: "visible",
  and: "named",
  so: "any",
  errors: "are",
  really: "obvious",
}):

(again, sidenote: this is also why a typechecker, ie Typescript, is useful)

And minor:

  • By convention, classes are written in PascalCase, not camelCase.
  • Your class is called class, this is a little confusing.
  • Your class has a property called class name, also confusing.
1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.