Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are some better ways to avoid the do-while(false); hack in Java?

Tags:

java

do-while

When the code flow is like this:

if(check())
{
  ...
  ...
  if(check())
  {
    ...
    ...
    if(check())
    {
      ...
      ...
    }
  }
}

I have generally seen this work around to avoid this messy code flow:

do {
    if(!check()) break;
    ...
    ...
    if(!check()) break;
    ...
    ...
    if(!check()) break;
    ...
    ...
} while(false);

What are some better ways that avoid this workaround/hack so that it becomes a higher-level (industry level) code?

Are there maybe constructs that come from Apache commons or Google Guava?

Note: this is a copy of the same question for C++. The best answers there are truly functions pointers and the GOTO command. Both doesn't exist in Java. I am eagerly interested in the same thing for Java.

Putting it into a new function and using return is in my opinion not a good solution, because return quits the method. So if my class has 20 methods with these constructs, I would have to add 20 additional methods to get this done. That's why GOTO was the best answer for C++.

like image 652
Kenyakorn Ketsombut Avatar asked Aug 30 '13 09:08

Kenyakorn Ketsombut


3 Answers

What is this "while(false)" nonsence? Use a label!

myLabel: {
    if(!check()) break myLabel;
    ...
    ...
    if(!check()) break myLabel;
    ...
    ...
    if(!check()) break myLabel;
    ...
}

It's core Java: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html

like image 131
mvmn Avatar answered Nov 10 '22 05:11

mvmn


To reduce the cyclomatic complexity, you can separate the logics into submethods:

public void go() {
    first();
}

public void first() {
    if(check()) {
        // #1
        second();
    }
}

public void second() {
    if(check()) {
        // #2
        third();
    }
}

public void third() {
    if(check()) {
        // #3
    }
}
like image 44
sp00m Avatar answered Nov 10 '22 06:11

sp00m


Because you can have this 20 times in a function to pass 20 serial steps. If I do it like this, the final if slides already 20 tabs to the right, which makes the code unreadable.

This comment of yours shows the issue: you are imagine the wrong code structure.

For example your code is like this

if (bool) {
     // do something
     if (anotherBoolean) {
         //do even more
         if (thirdBoolean) {
             // here we go! I spare deeper structure...
         }
     }
}

This can be refactored easily to:

public void method() {
    if (bool) {
        doSomeStuff();
    }
}

public void doSomeStuff() {
    if (anotherBoolean) {
        doThirdStuff();
    }
}

public void doThirdStuff() {
    if (thirdBoolean) {
        doEvenMore();
    }
}

public void doEvenMore() {
    // deeper structure
}

That kind of code structure would be a good sample of Clean Code as the methods are doing their "single purpose" stuff, you don't do a while-loop hack plus you even might reuse the methods somewhere.

like image 29
WarrenFaith Avatar answered Nov 10 '22 05:11

WarrenFaith