Let's say I have a class called ValueSpec
with these immutable instance fields:
min_value: int
max_value: int
description: string
Each instance of ValueSpec
is uniquely named. This unique name is its lookup/primary key in any kind of lookup mechanism we're talking about (database, dictionary, etc.)
When should this unique name be put in ValueSpec
as an instance field, and when should it be decoupled? I am generating a dictionary of these ValueSpec
s either way to achieve optimized lookup, but I'm not sure whether this unique name is considered part of the ValueSpec
conceptually.
Advantages of decoupling this unique name from ValueSpec
:
ValueSpec
's interface is slimmer for any client, especially when its unique name is only used for lookup, and not anything involving the processing ofValueSpec
.- It is possible to instantiate
ValueSpec
before its ID can be determined (unless the field was made mutable).
Advantages of coupling this unique name with ValueSpec
(by adding it as a name: string
field):
- In the event that this unique name is needed later on (let's say logging of the use of this
ValueSpec
), it will need to be passed along withValueSpec
anyway. - It is impossible to instantiate
ValueSpec
without naming it.
In terms of sample Python code, I'm asking when to prefer decoupling ValueSpec
and its unique name like this:
value_specs = {
"unique_name_1": ValueSpec(0, 10, "description_1"),
"unique_name_2": ValueSpec(1, 11, "description_2")
}
and when to prefer coupling it with ValueSpec
like this:
value_specs = {value_spec.name: value_spec for value_spec in (
ValueSpec("unique_name_1", 0, 10, "description_1"),
ValueSpec("unique_name_2", 1, 11, "description_2")
)}