I have two functions, the first function adds elements to a database, the second deletes the same elements.
To reduce duplicate code, I can implement a generic function in two ways:
// First solution void UpdateDatabase(list, enum operation_type) { for element in list: for properties in element.all_properties(): if properties.valid(): switch (operation_type): case ADD: AddToDatabase(properties); case DELETE: DeleteFromDatabase(properties); // Second solution void UpdateDatabase(list, lambda L) { for element in list: for properties in element.all_properties(): if properties.valid(): L(properties); }
To use the first solution, I would write:
UpdateDatabase(list, OperationType::ADD); UpdateDatabase(list, OperationType::DELETE);
To use the second solution, I would write
UpdateDatabase(list, p -> AddToDatabase(p)); UpdateDatabase(list, p -> DeleteFromDatabase(p));
which solution is better from the code cleanliness/maintainability point of view? Is there an alternative third solution?
submitted by /u/mercury0114
[link] [comments]
from Software Development – methodologies, techniques, and tools. Covering Agile, RUP, Waterfall + more! https://ift.tt/ZMeXkw0