当KISS和DRY碰撞时

发布于 2024-12-03 03:30:33 字数 1459 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

萧瑟寒风 2024-12-10 03:30:33

两者都可以。

对于循环,您并没有真正重复自己,因为唯一重复的部分是“语法混乱”(在您的情况下并没有太多)。您没有重复/复制“应用程序逻辑”代码。

如果您喜欢“函数”风格,也许可以使用 Guava 库(它具有 Function 接口和许多在集合上使用它们的辅助方法)。这就是 DRY(因为你不重复自己,而是重复使用已经存在的代码),并且仍然是 KISS(因为这些是很好理解的模式)。

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).

记忆之渊 2024-12-10 03:30:33

如果您只需在整个应用程序中执行此操作 4 次,并且转换确实像您的示例一样微不足道,那么我会选择在通用解决方案上随时编写 4 个 for 循环。

使用通用解决方案会严重影响可读性,并且您实际上不会从中获得任何好处。

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.

成熟的代价 2024-12-10 03:30:33

像 DRY 和 KISS 这样的一般原则永远不会一直有效

IMO,答案是忘记教条(至少对于这个问题),并思考什么可以为您提供最佳/最可读的解决方案。

如果重复的 x 4 代码更容易理解并且它不是维护负担(即您不需要对其进行太多更改) ),那么这是正确的解决方案。

(蒂洛的答案也是正确的......IMO)

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)

失去的东西太少 2024-12-10 03:30:33

我认为KISS和DRY并不矛盾。我宁愿说Java不允许你在不重复的情况下表达简单。

首先,如果您引入正确命名的方法来从 List 转换为 List 等等,而不是一直重复循环,那么它将是 DRY同时还剩下KISS。

但我建议是看看替代语言,它们允许您充分利用 DRY,同时仍然推广 KISS,例如在 Scala 中:

val listOfB = listOfA map convertAtoB
val listOfC = listOfB map convertBtoC
val listOfD = listOfC map convertCtoD

其中 convertAtoB 是一个接受类型 A 的项并返回 B 的函数:

def convertAtoB(a: A): B = //...

或者您甚至可以链接这些 map 调用。

I think it is not that KISS and DRY contradict each other. I would rather say that Java does not allow you to express simplicity while not repeating yourself.

First of all if you introduce properly named methods to convert from List<A> to List<B> and so on instead of repeating the loop all the time it would be DRY while still remaining KISS.

But what I would advice is to look at alternate languages that allow you to take full odvantage of DRY while still promote KISS, e.g. in Scala:

val listOfB = listOfA map convertAtoB
val listOfC = listOfB map convertBtoC
val listOfD = listOfC map convertCtoD

Where convertAtoB is a function taking an item of type A and returning B:

def convertAtoB(a: A): B = //...

Or you can even chain these map calls.

策马西风 2024-12-10 03:30:33

您可以将转换函数移至 CFactory 中:

convertAndCopy(listOfA, listOfB, CFactory.getConverterFromAToB());

这种方式代码非常可读/简单,并且可以促进代码重用(也许您稍后需要在另一个上下文中使用转换器对象)。

实现:(

public <S, T> void convertAndCopy(List<A> listofA, List<B> listOfB, Function<A, B> f) {
  listOfB.addAll(Collections2.transform(listOfA,f));
}

使用番石榴迭代器)。

我什至不确定你应该在这里 DRY,你可以直接使用:

listOfB.addAll(Collections2.transform(listOfA,CFactory.getConverterFromAToB()));

You could move the conversion function into CFactory:

convertAndCopy(listOfA, listOfB, CFactory.getConverterFromAToB());

The code is quite readable/simple this way and you promote code reuse (maybe you will need to use the converter object later in another context).

Implementation :

public <S, T> void convertAndCopy(List<A> listofA, List<B> listOfB, Function<A, B> f) {
  listOfB.addAll(Collections2.transform(listOfA,f));
}

(using guava Iterators).

I'm not even sure that you should DRY here, you could use directly:

listOfB.addAll(Collections2.transform(listOfA,CFactory.getConverterFromAToB()));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文