简体   繁体   English

我可以在JAVA的非分配情况下使用条件运算符吗?

[英]Can I use the Conditional Operators in a non-assignment situation in JAVA?

In case of assignment the situation is simple, 如果分配情况很简单,

result = testCondition ? value1 : value2;

But what if I want to use it instead of an if statement? 但是,如果我想使用它而不是if语句呢? for instance in a logging situation: 例如,在记录情况下:

logger.shouldDebbug ? logger.log("logging") : (what to do if not?);

In the case I don't what to do anything in the case of false, can I still use this Operator? 如果在假的情况下我什么都不做,我还能使用这个操作员吗?

Yes you can if you wrap them in a returning function, but no you shouldn't. 是的,如果你把它们包裹在一个返回的函数中,你可以,但不,你不应该。

In your example of the logger, let your logger output to void, discard the input when debugging isn't enabled. 在您的记录器示例中,让记录器输出为void,在未启用调试时丢弃输入。

You do not want to riddle your code with all these logging checks. 您不希望使用所有这些日志记录检查来谜题。 Perform a check as least and as central as possible. 尽可能少地进行检查。

Either have a check in the logger.log function if debugging is enabled, or replace the logger with a dummy mock that does nothing except accept input and immediately discard it. 如果启用了调试,则可以检查logger.log函数,或者使用除接受输入之外什么都不做的虚拟模拟替换记录器并立即丢弃它。

If you use standard logging frameworks like log4j you can set debugging levels, where you show only info or more serious, only warnings or more serious, only errors or more serious. 如果您使用log4j之类的标准日志记录框架,则可以设置调试级别,其中您只显示信息或更严重,仅警告或更严重,仅错误或更严重。

The same goes for other "quick" checks. 其他“快速”检查也是如此。 If you find yourself using a certain pattern a lot, write a utility class for it with a static method if need be, so you have one place, where you have to change stuff, instead of 200 code points that you have to update when going to production. 如果您发现自己经常使用某种模式,请在需要时使用静态方法为其编写实用程序类,这样您就有了一个地方,您必须更改内容,而不是在进行时必须更新的200个代码点生产。

You could use it if you insist, by defining a meaningless variable and take advantage of the functions' side-effects , but that's not a very good coding habit. 如果你坚持,你可以使用它,通过定义一个无意义的变量并利用函数的副作用 ,但这不是一个非常好的编码习惯。 It's purely a work-around . 这纯粹是一种解决方法

For example: 例如:

public static boolean test() {
    return 1>0;
}

public static int success() {
    System.out.println("true");
    return 0; // has no meaning whatsoever
}

public static int fail() {
    System.out.println("false");
    return 0; // has no meaning whatsoever
}

public static void main(String[] args) {
    int meaningless = test() ? success() : fail();
}

Everything has been explained in comments, so I will put here only some idea: 一切都在评论中解释,所以我在这里只提出一些想法:

public class Ternary {
    private final boolean condition;
    private Ternary(boolean condition) { this.condition = condition; }
    public static Ternary of(boolean condition) { return new Ternary(condition); } 
    public Ternary onTrue(Runnable r) { if (condition) { r.run(); } return this; }
    public Ternary onFalse(Runnable r) { if (!condition) { r.run(); } return this; }
}

Example of usage: 用法示例:

Ternary.of(o != null).onTrue(() -> doSomething()).onFalse(() -> doSomethingElse());

But simplier would be to write: 但更简单的是写:

if (o != null) { doSomething(); } else { doSomethingElse(); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM