views:

196

answers:

7

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. 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();
}
+4  A: 

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.

Joel
You are right, but what I asked is a different question that's why I didn't put a particular excpetion name.
newguy
Just to point out it'd be if(!someMethod()) to keep with the original code
MadMurf
In fact the problem is more complicated than that please have a look at my modified codes.
newguy
Don't see how that complicates it more... my solution or @Nils or this one still apply to your updated scenario.
MadMurf
Same solution as Nils
Joel
+2  A: 

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();
}
Michael Mrozek
That's bad code style for me. Would fail at the checkStyle stage.
newguy
@newguy Well, you're going to have to define what your good code style is then; I have no problem with that code
Michael Mrozek
My project uses a check style plugin that doesn't allow any code like the first line so I have to obey that rule.
newguy
@newguy Your project doesn't allow ternary expressions? That seems...bad
Michael Mrozek
+7  A: 

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.

Nils Pipenbrinck
I like this because it looks simple and clear.
newguy
+1  A: 

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

Matt McHenry
That's not the solution. What I want is to throw the exception immediately after the booleanValue is assigned because I don't want the rest of the codes to be run.
newguy
@newguy: look again, please. That's what it does. A little unclearly for my taste though. I prefer the answer given by Joel and Nils.
Skip Head
@newguy: This solution does give you what you asked for. Personally I dont like the style, but its probably the solution with the least edited difference.
Akusete
if anotherMethod() gives a false boolean it will still run the rest of the codes, that's not what I want.
newguy
@newguy: you are right, it can be assumed you would see the pattern in the first block statement, and infer the solution.
Akusete
+1  A: 

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...
}
Akusete
+1  A: 

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.

MadMurf
A: 

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.

Paul Sasik