Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use collections vs multiple properties

I'm building a relatively simple class (ex: Person) for an ASP.NET application that has several related boolean properties (ex: Certifications). I have the option of storing them as multiple properties:

Class Person
    Property HasCertL1
    Property HasCertL2
    Property HasCertL3
End Class

Or using a collection and enum:

Enum Cert
    L1
    L2
    L3
End Enum

Class Person
    Property Certs as List(Of Cert)
End Class

The class will not be used elsewhere, and the items (Certs) will be not be updated without recompiling the class anyway. I'm looking for reasons to choose one over the other. Can anyone offer any best practices or point me to some resources that cover this that I may have missed? Thanks in advance!

Cheers, JE

like image 444
Jens Ehrich Avatar asked Jun 07 '11 17:06

Jens Ehrich


2 Answers

My vote is to use a collection. Here's why: when you set the class up with a property for each level of certification, if you were to add a level of certification, you'd have to add extra code to check that people had that extra level. That requires, possibly, a change to every other class that uses Person.

If you use a list, the Person class doesn't change; there's just a change in the available values for the list. That requires far fewer code changes to check for a particular level of certification.

like image 139
KeithS Avatar answered Oct 06 '22 10:10

KeithS


I think it's always a good approach to keep an eye on extendability of your applications. With that in mind I would go for the collection approach. However it's not clear from your question if there are just three certifications or if there are more.

You're talking about only updating certifications with recompiling in that case it sounds that there are options for extension.

like image 43
ChristiaanV Avatar answered Oct 06 '22 08:10

ChristiaanV