Home > Software design >  Naming a subclass to store requirements of the parent class
Naming a subclass to store requirements of the parent class

Time:05-20

I have the class Party and inside i have some restrictions to join that party.

Looking like this:

class Party {
    String id;
    String name;
    Instant date;
    Requirement requirement;
}

class Requirement {
    int age;
    String dressCode;
    boolean drinkIncluded;
    ....
}

My question is: How should i name the subclass Requirement? I came up with three options but i cannot tell what is the best naming convention.

  1. Requirements (in plural)

  2. Requirement (in singular)

  3. Requirements_configuration

Is there some design pattern that i am missing to solve this or i just can name it in plural in this scenario?

CodePudding user response:

Perfectly acceptable to have plurals.

Requirements is not a subclass but here an embedded type. Requirements "is not" a party(inheritance). A party "has" requirements (composition).

CodePudding user response:

Requirement is not a subclass, since you are not inheriting it from the superclass Party. I would name it Requirements, as there is not one requirement but many more because you have four variables.

kind regards

CodePudding user response:

In my view, you should use singular name as there is no collection of them. What I mean is to pluralize when you have collection of requirements:

class Party {
    String id;
    String name;
    Instant date;
    Array<Requirement> requirements; // 's' is to indicate that it 
        is a collection of requirements
}

However, class should have singular name:

class Requirement {
    // ... the code is omitted for the brevity
    String dressCode; // this is just property which describes requirement
}

We can look at simple example with Person:

class Person {
    String id;
    String name;    
}

and it is not really cool when we pluralize name:

class Persons {
    String id;
    String name;    
}

Read more about сlasses naming: singular or plural

  • Related