I'm working on a bit of code where we're creating object models but the models have generic keys. For example
class myContact {
var key;
var value;
}
And then in the code instantiating them as follows
myContact = new myContact()
myContact.key = 'address'
myContact.value = '123 my address'
myContact2 = new myContact()
myContact2.key = 'secondAddress'
myContact2.value = '123 my other address'
And then attaching them to a parent object like
myBank = new company();
myBank.addContact(myContact);
myBank.addContact(myContact2);
To me this feels wrong as the model is so loosely defined. However you do know from the class name it's going to be some sort of contact information.
At the same time I can see why this might be useful as if we want to add a different type of contact in future this kind of methodology makes it easy to do so.
Can any one explain to me if this is good practice? Bad practice and the reasons why?
My initial thoughts:
Good practice
- Easy to extend contact types in future.
- Easy to loop through contact information for myBank and get the data.
Bad practice
- If you want to update a specific contact type you have to loop through to find the correct key.
- I've never written code like this which is why it feel like bad practice even though it's perfectly acceptable.
- Anemic model, there's no real need to be a class.
- There's no defined key so we can't delete or add a contact easily without searching through them all again.
- There's no definition of what a contact is or should be.
Any thoughts would be greatly appreciated.
edited: Adding some more thoughts as I go alone