Open honganhcs opened 2 years ago
Imo, this makes the program (and its classes) a lot simpler to reason about:
To some extent I think this is DRY and reduces code complexity
I agree with @takufunkai. In the future when the program grows and there is a need to share references to the same object, allowing the mutation of objects may result in unwanted side-effects and make debugging more difficult.
I disagree that Person
should be immutable. Certain granular things makes sense to be immutable, such as Phone
, Address
, Name
etc.
However, the entire core of our program revolves around modifying a person. In the real world, if a person changed their phone number, they'd replace my entire phone number. But we wouldn't replace the entire person with someone else entirely.
I find that by needlessly making everything immutable, we end up with silly classes like EditPersonDescriptor
. Combined with the fact every field in Person
is so highly coupled, we end up with a problem where simply adding a new field like Remark
requires changes to an unreasonably large number of files, and is frankly, unmaintainable and much more bug prone than to have a simple, mutable design from the start.
TLDR: Immutability should be applied when it fits the situation. But not when it conflicts with the core purpose of the application.
Thank you for all your inputs! I realised that one reason why editing a person requires replacing the Person object in the UniquePersonList is so that listeners to the ObservableList
May I know if my understanding is correct Prof @damithc?
@honganhcs most likely so. I can't confirm as I wasn't heavily involved in writing the code.
Glad to see this kind of discussions on design decisions. Do keep it up as this is an interesting topic.
In general, we encourage you to question design decisions in the current code and explore alternatives. The work done when exploring alternatives can earn credit (as they require effort) and also can be submitted for CS2103R.
Things to keep in mind:
In this particular case, I remember the Person
class used to be mutable at first and there was a ReadOnlyPerson
interface that was used when Person
objects were passed to a client that shouldn't be updating the objects. At some point, that design was updated to make the Person
class immutable instead. It's pity that the rationale for that was not documented, which also goes to show the value of good developer documentation.
I noticed that the
Person
class and the classes representing its fields all have their instance members declared as final. Thus, forEditCommand
, theexecute
method creates a newPerson
object, which replaces the person-to-edit object in theUniquePersonList
. What are the benefits of doing this, compared to modifying the fields in the person-to-edit object?