java三元黑客

发布于 2024-08-20 20:53:22 字数 1568 浏览 9 评论 0原文

所以我不会在这里追求可维护性或优雅性。寻找一种方法来减少方法中的总令牌只是为了好玩。该方法由一个长嵌套的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沙与沫 2024-08-27 20:53:22
(field = value) instanceof String

假设它已经满足您的需求(因此包括当 valuenull 时返回 false),则更短的替代方案是

(field = value) != null

Or 如果您实际上忽略了这一点并且想要让 null 也返回 true,然后使用

(field = value) == value

如果您使用 1 个字母的变量名,这可以变得更短。

此外,我没有看到其他方法,我同意我们大多数人的观点,这一切都有点令人讨厌;)

(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

(field = value) != null

Or if you actually overlooked that and want to make null return true as well, then use

(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 ;)

捂风挽笑 2024-08-27 20:53:22

如果参数为空,则返回 0
然后对参数进行 case/switch/select 语句。那是干净

if param is null, return 0
Then make a case/switch/select statement on the parameter. That's clean .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文