我想要执行的操作:我正在尝试减少条件运算符,因为Sonar正在为它提供错误
if (!parseBooleanFromString(response.getBuy().getHasEligibleAccounts()) &&
(!parseBooleanFromString(response.getSell().getHasEligibleAccounts()) &&
(!parseBooleanFromString(response.getExchange().getHasEligibleAccounts()) &&
(!parseBooleanFromString(response.getWorkplaceRetirement().getHasPlansEligibleForChangeContributions()) &&
(!parseBooleanFromString(response.getWorkplaceRetirement().getHasPlansEligibleForChangeInvestments())))))) {
//Success
} else {
//Failure
}
private boolean parseBooleanFromString(String mStr) {
return Boolean.parseBoolean(mStr);
}
我尝试了什么: 我试图将所有布尔值放在一个列表中并检查
答案 0 :(得分:0)
您还可以将这些条件移动到不同的函数中,这些函数在内部调用其他函数并返回单个布尔结果。这样,如果条件将在内部评估并返回结果,则上面只有一个函数。
答案 1 :(得分:0)
由于您正在检查每个语句是否为false,因此如何在内存中保留全局整数:private int product = 1;
。创建一个单独的方法来计算产品(将字符串替换为布尔解析器):
private void updateProduct(String mStr){
if (Boolean.parseBoolean(mStr)) //If true, condition should fail
product *= 0;
else
product *= 1;
}
本质上,你没有运行'if语句'而是乘以布尔值:
product = 1;
updateProduct(response.getBuy().getHasEligibleAccounts());
updateProduct(response.getSell().getHasEligibleAccounts());
//etc
if (product > 0){
//success
} else {
//failure
}
说明:如果条件为真,则产品将始终为0.产品的唯一实例是> 0表示所有陈述均为假
答案 2 :(得分:0)
不确定声纳会抱怨什么,但你有很多冗余的括号和令人困惑的否定。使用DeMorgans法则,您至少可以简化为:
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_VERBOSE_MAKEFILE on)
set(can_use_assembler TRUE)
enable_language(ASM)
add_definitions(-v -DANDROID -DNDEBUG -DOC_ARM_ASM)
if(CMAKE_SYSTEM_PROCESSOR STREQUAL armv7-a)
set(TEST_SOURCES ../../../../libs/lib/arm_android/armbits.s
../../../../libs/arm_android/armfrag.s
../../../../libs/arm_android/armidct.s
../../../../libs/arm_android/armloop.s
../../../../libs/lib/arm/armopts-gnu.s
)
endif()
add_library(omnitus STATIC ${TEST_SOURCES}
)
target_include_directories(omnitus PUBLIC
../../../../libs/include
)
target_include_directories(omnitus PUBLIC
../../../../libs/arm
)
target_include_directories(omnitus PUBLIC
../../../../libs/include
)
或者如果您使用更多java 8语法
boolean b = parseBooleanFromString(response.getBuy().getHasEligibleAccounts())
|| parseBooleanFromString(response.getSell().getHasEligibleAccounts())
|| parseBooleanFromString(response.getExchange().getHasEligibleAccounts())
|| parseBooleanFromString(response.getWorkplaceRetirement().getHasPlansEligibleForChangeContributions())
|| parseBooleanFromString(
response.getWorkplaceRetirement().getHasPlansEligibleForChangeContributions());
if (!b) {
答案 3 :(得分:0)
我会做这样的事情:
private boolean checkEligibility(LaunchPoints response) {
final String trueStr = "true";
if (trueStr.equals(response.getBuy().getHasEligibleAccounts())) return true;
if (trueStr.equals(response.getSell().getHasEligibleAccounts())) return true;
[...]
return false;
}
这个想法是,跳过解析布尔值,只检查“true”并使你的条件更具可读性。