[英]java ternary hack
So I'm not going for maintainability or elegance here.. looking for a way to cut down on the total tokens in a method just for fun. 所以我不会在这里寻求可维护性或优雅性......寻找一种方法来减少方法中的总令牌只是为了好玩。 The method is comprised of a long nested if-else construct and I've found that (I think) the way to do it with the fewest tokens is the ternary operator.
该方法由一个长嵌套的if-else结构组成,我发现(我认为)使用最少的标记来实现它的方法是三元运算符。 Essentially, I translate this:
基本上,我翻译这个:
String method(param) {
if (param == null)
return error0;
else if (param.equals(foo1))
if (condition)
return bar1;
else
return error1;
else if (param.equals(foo2))
if (condition)
return bar2;
else
return error1;
...
else
return error;
}
to this: 对此:
String method(param) {
return
param == null ?
error0 :
param.equals(foo1) ?
condition ?
bar1 :
error1 :
param.equals(foo2) ?
condition ?
bar2 :
error2 :
...
error
}
However, there are a couple cases where in addition to returning a value I also want to change a field or call a method; 但是,有几种情况除了返回值之外我还想更改字段或调用方法; eg,
例如,
else if (param.equals(foo3))
if (condition) {
field = value;
return bar3;
}
else
return error3;
What would be the cheapest way to do this token-wise? 什么是最便宜的方式来做这个令牌? What I'm doing now is ugly but doesn't waste too many tokens (here the field is a String):
我现在正在做的是丑陋但不浪费太多令牌(这里的字段是一个字符串):
param.equals(foo3) && (field = value) instanceOf String ?
condition ?
bar2 :
error2 :
Again, the point is not good coding, I'm just looking for hacks to decrease the token count. 同样,关键不是好编码,我只是在寻找黑客来减少令牌数量。 If there's a shorter way to write the entire thing I'm open to that as well.
如果有一个较短的方式来写整个事情我也会对此持开放态度。 Thanks for any suggestions.
谢谢你的任何建议。
Edit: Each word and punctuation mark counts as one token. 编辑:每个单词和标点符号都算作一个标记。 So, for example, "instanceOf String" is two tokens, but "!= null" is three.
因此,例如,“instanceOf String”是两个标记,但“!= null”是三个。 The main things I can see for possible improvement are the "&&" and the parentheses.
我可以看到可能改进的主要内容是“&&”和括号。 Is there a way to put "field = value" somewhere besides the conditional, and if not is there a construct that makes "field = value" a boolean without the need for parentheses?
有没有办法在条件之外的某处放置“field = value”,如果没有,是否有一个构造使“field = value”成为一个布尔而不需要括号?
if param is null, return 0 如果param为null,则返回0
Then make a case/switch/select statement on the parameter. 然后在参数上创建一个case / switch / select语句。 That's clean .
那很干净 。
(field = value) instanceof String
Assuming that it already satisfies your needs (and it thus includes returning false when value
is null
), a shorter alternative would then have been 假设它已满足您的需求(因此它包括当
value
null
时返回false),那么就会有更短的替代方案
(field = value) != null
Or if you actually overlooked that and want to make null
return true
as well, then use 或者,如果您实际上忽略了这一点并希望将
null
返回为true
,那么请使用
(field = value) == value
This can be made much shorter if you use 1-letter variable names. 如果使用单字母变量名,则可以缩短此时间。
Further I don't see other ways and I agree with most of us that this all is somewhat nasty ;) 此外,我没有看到其他方式,我同意我们大多数人,这一切都有点讨厌;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.