开发者

Rewriting a conditional statement in Java

Say if I have the code below, it's bascially determining some condition is mat开发者_如何学编程ched and then assign the boolean value, then run some codes. Then throw an exception if the booleanValue is false. What if I want it to throw an exception immediately if the booleanValue is false without running the rest of the codes? 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. 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:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜