When KISS and DRY collide

Either is fine.

With the loops, you are not really repeating yourself, because the only parts that are repetitive is "syntactic clutter" (and not too much of that in your case). You are not repeating/duplicating "application logic" code.

If you like the "Function" style, maybe make use of the Guava library (which has the Function interface and many helper methods that work with them on collections). That is DRY (because you don't repeat yourself, and re-use code that already exists), and still KISS (because those are well understood patterns).


General principles like DRY and KISS never work all of the time.

IMO, the answer is to forget the dogma (at least for this problem), and think about what gives you the best / most readable solution.

If the duplicated x 4 code is easier to understand and it is not a maintenance burden (i.e. you don't need to change it a lot), then it is the right solution.

(And Thilo's answer is right too ... IMO)


If you only have to do this 4 times in your whole application, and the conversion is really as trivial as your examples, I would choose writing 4 for loops any time over the generic solution.

Readability suffers a lot from using that generic solution and you don't actually gain anything from it.

Tags:

Java

Dry