[英]Simplify complicated ternary condition
I want to assign value to result as "read_only", "write_only" or "read_write". 我想将值分配给结果为“read_only”,“write_only”或“read_write”。 For that I want to check resourceKey.
为此,我想检查resourceKey。
result = resourceKey.get("read") == null ?
Constants.WRITE_ONLY :
resourceKey.get("write") == null ?
Constants.READ_ONLY :
Constants.READ_WRITE;
I want to simplify this ternary condition. 我想简化这种三元条件。
Consider breaking this functionality out into a utility function. 考虑将此功能分解为实用功能。
void Thing() {
// stuff
var result = GetPermissions(resourceKey);
// other stuff
}
static int GetPermissions(resourceKey) {
if( resourceKey.get("read") == null )
return Constants.WRITE_ONLY;
if( resourceKey.get("write") == null )
return Constants.READ_ONLY;
return Constants.READ_WRITE;
}
Check out the code review stack exchange . 查看代码审查堆栈交换 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.