- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在努力使它尽可能通用。假设在 if 语句中,我正在检查某个 bool 表达式 A 是否为真。假设存在 A 为真的特定情况,A.a 和 A.b,它们是互斥的。另一个 bool 表达式B也是如此。那么,考虑如下代码:
if (A) {
if (A.a) {
somethingSpecificToAa();
foo();
}
} else if (B) {
if (B.a) {
somethingSpecificToBa();
foo();
}
} else {
foo();
}
在我的实际代码中,foo()
不是单个函数,而是多行长代码。重复这么多次它们对我来说似乎很臭,所以我认为需要进行一些重构。
因为 foo()
在以下情况下执行:
我想到了以下几点:
if (A.a) {
somethingSpecificToAa();
} else if (B.a) {
somethingSpecificToBa();
}
if (A.a || B.a || !(A || B)) {
foo();
}
应该有相同的行为。这是最好的方法吗?请注意,第二个示例的第二个 if 语句中的条件实际上非常长,这就是为什么我的代码仍然看起来像第一个示例的原因(我讨厌将单个 if
分成几行.) 我还考虑过制作一个返回 bool
的 lambda,它等同于 A.a || B.a || !(A || B)
并将 lambda 代入第二个 if 语句。或者,我可以保留第一个示例的结构,但将每个 foo()
的许多行替换为一个 (void
) lambda 来做同样的事情,尽管我我不确定这能解决气味。
在这一点上我是否过度设计,考虑 lambda?哪种方法最适合维护干净的代码?
编辑:似乎我让它太通用了。我正在处理 STL 容器,而不是我自己的类,一个更“准确”的例子是:
int shirtACleanliness = calculateCleanliness(shirtA);
if (itemsToWash.contains(shirtA)) { //itemsToWash is a std::set
if (shirtA.cleanliness > shirtACleanliness) {
itemsToWash.erase(shirtA);
shirtA.cleanliness = shirtACleanliness;
itemsToWash.insert(shirtA); //the set is ordered on cleanliness, so this re-inserts in the correct position
doSomeOtherStuff(shirtA);
}
} else if (itemsToDry.contains(shirtA)) { //itemsToDry is a std::vector
if (shirtA.cleanliness > shirtACleanliness) {
itemsToDry.erase(shirtA);
shirtA.cleanliness = shirtACleanliness;
itemsToWash.insert(shirtA);
doSomeOtherStuff(shirtA);
}
} else {
shirtA.cleanliness = shirtACleanliness;
itemsToWash.insert(shirtA);
doSomeOtherStuff(shirtA);
}
//am aware aware contains() is not a method for either type
//and std::vector does not have erase() by value, this is just conceptual
最佳答案
根据您最近的评论,这里有一些可能符合您的用例的伪代码。
Element* element=nullptr;
if(vectorA.contains(X)){
element = vectorA.get(X);
}else if(setB.contains(X)){
element = setB.get(X);
}
if(element != nullptr && element->a){
something(element);
}
if(element == nullptr || element->a){
foo();
}
关于c++ - 建议对以下形式的 if 语句进行重构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55595060/
是否有任何特定于 CoffeeScript 的技巧可以使这看起来更整洁: index = (-> if segment == 'index' return
我正在试验 C# 的不同领域并重构最佳实践/模式。 可以看出,下面的 Validate 方法有 3 个子验证方法。 有没有办法重新设计/重构此方法,以便删除 if 语句? (可能使用委托(delega
我正在制作一个简单的 Rails 站点,它将存储一些日期并执行基本的条件检查。我在下面写了一些方法,并被告知我可以使它们更有效率。我一直挠头,我不知道该怎么做。我应该让 entry.find 全局化吗
有没有更好的方法来编写这个函数?我继承了一些 javascript 代码,如果可能的话,我想让它更简洁。此外,我可能会添加更多“主题”元素,并且不想一遍又一遍地复制和粘贴。 function imag
1. 效果展示 在线查看 2. 开始前说明 效果实现参考源码: Logo 聚集与散开 原效果代码基于 react jsx 类组件实现。依赖旧,代码冗余。
我似乎缺乏足够的咖啡来让我清楚地看到以下问题。 假设我有一个包含两个构造函数和多个字段的类。一个构造函数是无参数构造函数,一个字段依赖于另一个字段。另一个构造函数为其其中一个字段获取注入(inject
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎是题外话,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或include a min
我有一个枚举,里面有一些状态: enum State { A, B, C, D } 以及具有相应状态的对象: class MyObject { State st
我的 build.xml 中有这段代码:
在Delphi XE中,我经常使用重命名变量重构(Ctrl+Shift+E),通过给出更有意义的变量名称来使我的代码更容易理解,例如: 这一切都很好,但是当我使用它时,我在工作空间方面遇到了一个小问题
我实现了一个逻辑来通过data变量计算剩余数量和成本。它循环遍历每个产品,并通过计算已返回数量状态的数量来计算剩余数量,并减去产品数量。 有没有办法重构这段代码,使其看起来更干净、易于理解/可维护?我
我正在学习 Haskell,所以这可能是一些非常微不足道的事情,但我希望得到一些关于如何重写它以及它如何工作的指示。 我有以下工作代码(使用的包: HTF 、 Parsec 和 Flow ): {-#
我有以下代码: switch(equipmentAttachment.AttachmentPosition) { case 'AttachFront': { if(
我正在尝试将代码从 Java Utility Logging 更改为 Log4J2。要更改代码,我想在 Eclipse 中使用代码重构。例如更改:导入 java.util.logging.Logger
我有一个处理 Excel 文件中的行的函数。在这个函数中,我有一个 for 循环。现在,一旦提取一行,我们就会检查各种条件。如果任何条件为假,我们继续下一步row.可以使用模式使这段代码更加结构化吗?
我正在重构一个有很多嵌套调用的程序,例如 ServiceManagement.getGlobalizationService() .createExportCo
我在 JTabbedPane 上重构了许多字段以减少冗余。但是,当我为字段数量设置常量大小时,出现空指针异常。我不太确定为什么会发生这种情况。我做错了什么,更重要的是有人可以解释发生了什么事吗? pu
我试图通过删除 map.setOnPolygonClickListener 和 map.setOnMarkerClickListener 中的重复项来重构以下方法。 两个监听器执行完全相同的操作,我想
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 6 年前。 Improve this ques
当我在这张照片中重构 Storyboard时 link . 我找不到在哪里可以交换标签栏项目的位置。 例如,我想将主菜单更改为索引 0。 这是我的storyboard . 最佳答案 您可以通过拖放标签
我是一名优秀的程序员,十分优秀!