0

Which is better choice out of two different implementation

Hi Refer code below : Both the codes will have global variables as member variable of class say for example management... This is not concern and just wrote them as global to showcase the main question. Which is better ? Implementation 1 or Implementation 2? I feel 2 is better as it does not ask to iterate over all the persons and just maintains the required values in terms of person ids. If another field is there like isSick , we can have another list. This implementation has overhead of storing extra details... Obviously extra memory usage will give us better time complexity and hence #2 is better than #1. But I don't want to simply ignore #1. Is #2 easy to manage ? I mean there can be number of methods like some random events which all will update person unordered map as well as additional data member like lstVeg In terms of code design and maintainability and code readability, is #2 ok to go ahead with ? If not, #1 is the only way with a bad time complexity or is there any other approach ? htt

15th May 2022, 4:32 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
5 Answers
+ 2
Realistically you would use a hybrid. Implementation 1 with something you wont look up too often Implementation 2 with something youd look up constantly
15th May 2022, 5:01 PM
Raul Ramirez
Raul Ramirez - avatar
+ 1
Implementation 2 is better Better speed Easier maintaince Easier readability More flexible For slight cost in memory /storage.
15th May 2022, 6:14 AM
Raul Ramirez
Raul Ramirez - avatar
0
https://code.sololearn.com/ckrG8Db05sQA/?ref=app https://code.sololearn.com/c207DJ1bJyTD/?ref=app Adding both the approach in comment as could not manage to add link into question description
15th May 2022, 4:34 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Thanks for sharing idea
15th May 2022, 6:20 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Just extended with additional member variable and a method. Implementation 1 is extended as 3 and implementation 2 is extended as 4. Still implementation 4 (extended version of 2) is better than 3? I mean is it good to increase space with each data member ? https://code.sololearn.com/cz4CN4tg9Vz3/?ref=app https://code.sololearn.com/c3AgWSNvdRjT/?ref=app
15th May 2022, 3:54 PM
Ketan Lalcheta
Ketan Lalcheta - avatar