0

Lets say I have class called Connection. One of his members is object that is instance of another class, class VerificationCode.

The class VerificationCode has 2 members: code and expiresAt.

Should I keep the VerificationCode and associate it with the member, or should I associate the 2 members code and expiresAt directly to the Connection class?

What are the advantages or disadvantages in terms of design/modeling?

Example:

Option 1:

Class VerificationCode{
code: string;
expiresAt: number;
}

Class Connection{
a: string;
verificationCode: VerificationCode;
}

Option 2:

Class Connection{
a: string;
code: string;
expiresAt: number;
}
Maor agai
  • 221
  • 1
  • 3
  • 11
  • 1
    Is `VerificationCode` a useful abstraction in your codebase? We cannot answer that. Maybe it is, maybe its properties only ever makes sense as part of `Connection` but not standalone. – VLAZ Aug 30 '22 at 18:59
  • Note that the syntax is `class` and not `Class`. Also note that classes generally require property initialization, which you're not doing here. – jcalz Aug 30 '22 at 19:02

1 Answers1

1

Option 1 is better for me. Because it has:

  • great separation of concerns. Now you have just two fields such as code and expiresAt, but when you will have methods for this variables, then it would be violation of Single Responsibility principle if you will edit code and expiresAt in class Connection
  • has high cohesion in classes. Read more what cohesion is here.
StepUp
  • 36,391
  • 15
  • 88
  • 148