tags:

views:

29

answers:

1

I am trying to apply a save method in a backing Java bean which will take the table rows that are selected and save them in the database. However, let's say the user changes his choices a little (changes 1 out of his 5 choices). I am wondering about the algorithm I am going to apply if it matters in efficiency in the long term or not....

here it goes :

  1. every time the user clicks the button (save) I will delete all his previous choices and insert all the current choices to the database
  2. once the button is clicked --- see which rows the user de-selected and delete their rows from the database and add the new ones???

is choice number 2 better or not than choice 1 .......or it doesn't really matter for number of choices that will not exceed 15 ??

Thanks

+1  A: 

I would definitely go for option 2, try to figure out the minimum number of operations you need to perform.

It is, however, fairly normal to fall back to option 1 in times of deadlines etc. since it is a bit easier to implement.

There shouldn't, however, be that much harder to figure out what the changes are, since it doesn't seem to me that you're changing the rows themselves. Either you're deleting ones that had their checkmark cleared, or you insert ones that had their checkmark set.

Simply store a list of primary key values of whatever is in the database, then compare to that list when you iterate through the new list when the user wants to persist the changes.

A minimal work solution here would also mean you would be a bit more future-proof in terms of refactoring, changes, or additions. For instance, what if there in the future is data attached to any of those rows. You would need to keep that as well. Generally I'm a bit opposed to writing code just for the sake of "what if", but here I feel it's more like "why wouldn't you ..." than that.

So my advice is go for option 2. Not much more work.

Lasse V. Karlsen