简体   繁体   English

用Java重写条件语句

[英]Rewriting a conditional statement in Java

Say if I have the code below, it's bascially determining some condition is matched and then assign the boolean value, then run some codes. 假设我有下面的代码,它基本上确定一些条件匹配,然后分配布尔值,然后运行一些代码。 Then throw an exception if the booleanValue is false. 如果booleanValue为false,则抛出异常。 What if I want it to throw an exception immediately if the booleanValue is false without running the rest of the codes? 如果booleanValue为false而不运行其余代码,我希望它立即抛出异常怎么办? If I just put the second conditional statement into the first one there will be duplicated codes. 如果我只是将第二个条件语句放入第一个条件语句中,则会有重复的代码。 Please show me a smart way to do this (I've modified the code to be looked like my actual codes). 请告诉我一个聪明的方法(我已修改代码看起来像我的实际代码)。

boolean booleanValue = false;
Permission value;

if (someCondition) {
   value = getPermission_1();
   booleanValue = someMethod(value);
   useValue_1(value);
}
else {
   value = getPermission_2();
   booleanValue = anotherMethod(value);

   useValue_2(value);
}

if (!booleanValue) {
   throw Exception();
}

How about eliminating the boolean variable? 如何消除布尔变量? You could rewrite your code like this: 您可以像这样重写代码:

if (someCondition) {
   if (!someMethod()) {
     throw new Exception();
   }
   some codes...
}
else {
   if (!anotherMethod()) {
     throw new Exception();
   }
   some codes...
}

That looks easier to my eyes, but such things are a matter of taste... 这对我来说看起来更容易,但这样的事情是品味的问题......

Extra advantage: If the exception ends up in a stack-trace you know what the condition was because you have two different throw-statements. 额外优势:如果异常以堆栈跟踪结束,您就知道条件是什么,因为您有两个不同的throw语句。 That may speed up the debugging a bit. 这可能会加快调试速度。

Instead of 代替

 booleanValue = anotherMethod();

you would simply write 你会简单地写

if( !someMethod() )
   throw new SomeException();

On throwing a generic Exception -- don't. 抛出一般的异常 - 不要。 The reason that you're throwing an exception is to tell the caller that something exceptional occurred. 抛出异常的原因是告诉调用者发生了异常。 It is almost always useful to tell them what, otherwise neither the caller nor you can do anything about it. 告诉他们什么是几乎总是有用的,否则呼叫者和你都无法做任何事情。

Assuming the two some codes... are different, you probably want to do: 假设两个some codes...不同,你可能想做:

boolean booleanValue = someCondition ? someMethod() : anotherMethod();
if(!booleanValue) {
    throw new Exception();
}

if(someCondition) {
    // some code
} else {
    // some code
}

If they're the same the if(someCondition) isn't necessary 如果它们是相同的, if(someCondition)不是必需的


If (hypothetically) you have a static analysis tool that doesn't allow ternary expressions, you can replace the first lines with: 如果(假设)你有一个不允许三元表达式的静态分析工具,你可以用以下代码替换第一行:

boolean booleanValue;
if(someCondition) {
    booleanValue = someMethod();
} else {
    booleanValue = anotherMethod();
}

The obvious solution is: 明显的解决方案是:

boolean booleanValue = false;

if (someCondition) {
   booleanValue = someMethod();
   if(booleanValue){
       //some codes...
   }
}
else {
   booleanValue = anotherMethod();
   some codes...
}

if (!booleanValue) {
   throw Exception();
}

... but I don't mind repeating the if(!booleanValue) throw Exception(); ...但我不介意重复if(!booleanValue) throw Exception(); bit, because it's likely a conceptually different reason that you're throwing the exception. 比特,因为这可能是你抛出异常的概念上不同的原因。 (You could provide a better error msg in your exception, for example.) (例如,您可以在异常中提供更好的错误消息。)

Its all very subjective, perhaps? 这一切都很主观,也许吧?

boolean booleanValue = aBoolean;
if (someCondition) {
   if (!someMethod()) {
       throw new SomeException();
   }
   some codes...
} else {
   if (!anotherMethod()) {
     throw new AnotherException();
   }
   some other codes...
}

Your best solution would be... 你最好的解决方案是......

if (someCondition) {
   value = getPermission_1();

   if (!someMethod(value)) {
     throw new SomeException();
   }

   useValue_1(value);
}
else {
   value = getPermission_2();

   if (!anotherMethod(value)) {
     throw new AnotherException();
   }

   useValue_2(value);
}

And you shouldn't look on it as duplicating code because if you want to throw an exception then the expectation would be that the reason for the exception would be different in each case and therefore a different Exception, or different message should be passed in each case. 并且你不应该把它视为重复代码,因为如果你想抛出异常,那么期望的是异常的原因在每种情况下会有所不同,因此应该在每个异常中传递不同的异常或不同的消息。案件。

I'm assuming you want to know which condition was executed and subsequently failed, because all you're getting back is a boolean from your ...Method calls the reason for the failure probably won't be apparent in this scenario. 我假设你想知道哪个条件被执行并且随后失败了,因为你所得到的只是你的一个布尔值...方法调用失败的原因在这种情况下可能不会显而易见。

This kinda smells... as in, "code smell". 这种气味......就像“代码味”一样。 A return value is being translated into an exception. 返回值正在转换为异常。 It seems that if the caller wrote someMethod and anotherMethod then the solution is to rewrite those methods and throw the exception from those methods instead of using a return value. 似乎如果调用者编写someMethod和anotherMethod,那么解决方案是重写这些方法并从这些方法抛出异常而不是使用返回值。 But that's only if the programmer has access to the code. 但这只有程序员才能访问代码。 If it's a 3rd party API call, i suppose the translation might have to happen. 如果是第三方API调用,我认为翻译可能必须发生。

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

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