java三元黑客
所以我不会在这里追求可维护性或优雅性。寻找一种方法来减少方法中的总令牌只是为了好玩。该方法由一个长嵌套的 if-else 构造组成,我发现(我认为)使用最少标记来完成此操作的方法是三元运算符。本质上,我将以下内容翻译
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;
}
为:
String method(param) {
return
param == null ?
error0 :
param.equals(foo1) ?
condition ?
bar1 :
error1 :
param.equals(foo2) ?
condition ?
bar2 :
error2 :
...
error
}
但是,在某些情况下,除了返回值之外,我还想更改字段或调用方法;例如,
else if (param.equals(foo3))
if (condition) {
field = value;
return bar3;
}
else
return error3;
以令牌方式执行此操作的最便宜的方法是什么?我现在所做的很丑陋,但不会浪费太多令牌(这里的字段是一个字符串):
param.equals(foo3) && (field = value) instanceOf String ?
condition ?
bar2 :
error2 :
同样,重点不是好的编码,我只是在寻找减少令牌数量的技巧。如果有一种更短的方式来写整个事情,我也愿意。感谢您的任何建议。
编辑:每个单词和标点符号都算作一个标记。因此,例如,“instanceOf String”是两个标记,但“!= null”是三个。我认为可能改进的主要内容是“&&”和括号。有没有办法将“field = value”放在条件之外的某个地方,如果没有,是否有一种构造可以使“field = value”成为布尔值而不需要括号?
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. 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; e.g.,
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. 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设它已经满足您的需求(因此包括当
value
为null
时返回 false),则更短的替代方案是Or 如果您实际上忽略了这一点并且想要让
null
也返回true
,然后使用如果您使用 1 个字母的变量名,这可以变得更短。
此外,我没有看到其他方法,我同意我们大多数人的观点,这一切都有点令人讨厌;)
Assuming that it already satisfies your needs (and it thus includes returning false when
value
isnull
), a shorter alternative would then have beenOr if you actually overlooked that and want to make
null
returntrue
as well, then useThis 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 ;)
如果参数为空,则返回 0
然后对参数进行 case/switch/select 语句。那是干净。
if param is null, return 0
Then make a case/switch/select statement on the parameter. That's clean .