3
\$\begingroup\$

I am new to this CompletableFuture function in Java. I have the following code below that works fine but looks quite messy IMHO.

CompletableFuture<String> cf = CompletableFuture.completedFuture(isRQMRunning_3);

if(cf.isDone()) {
    System.out.println("Value- " + cf.get());
    cf = CompletableFuture.completedFuture(startRQM_4);

    if(cf.isDone()) {
        System.out.println("Value- " + cf.get());
        cf = CompletableFuture.completedFuture(pendingChangesStatus);

        if(cf.isDone()) {
            System.out.println("Value- " + cf.get());                   
            cf = CompletableFuture.completedFuture(acceptPendingChanges_8);

            if(cf.isDone()) {
                System.out.println("Value- " + cf.get());                       
                cf = CompletableFuture.completedFuture(loadWorkspace_5);

                if(cf.isDone()) {
                    System.out.println("Value- " + cf.get());
                    cf = CompletableFuture.completedFuture(acceptPendingChanges_8);

                    if(cf.isDone()) {
                        System.out.println("Value- " + cf.get());
                        cf = CompletableFuture.completedFuture(fixConflicts_9);

                        if(cf.isDone()) {
                            System.out.println("Value- " + cf.get());
                        }
                    }
                }
            }
        }
    }           
}

Is it possible to make that look a lot nicer/easier to read?

\$\endgroup\$
4
\$\begingroup\$

You have a lot of repetition in your code. The only thing different with the 2 lines of code repeated over and over is the argument.

You could put all of these arguments into a list and iterate over them, running those 2 lines of code for each:

List<String> states = Arrays.asList(isRQMRunning_3, startRQM_4, 
  pendingChangesStatus, acceptPendingChanges_8, 
 loadWorkspace_5, acceptPendingChanges_8, fixConflicts_9);

for (String state : states)
{
    // Loop until array is complete
    CompletableFuture<String> cf = CompletableFuture.completedFuture(state);

    if (!cf.isDone())
    {
        break;
    }

    System.out.println("Value- " + cf.get());
}
\$\endgroup\$
  • \$\begingroup\$ Great suggestion @dustytrash! However, i am in need of also including the batDir with each. \$\endgroup\$ – StealthRT Sep 10 at 15:10
  • \$\begingroup\$ would it just need to be this CompletableFuture.completedFuture(steps(state, batDir));? \$\endgroup\$ – StealthRT Sep 10 at 15:13
  • \$\begingroup\$ Also, should if (!cf.isDone()) { be if (cf.isDone()) {? If its done then break out, right? \$\endgroup\$ – StealthRT Sep 10 at 15:27
  • \$\begingroup\$ @StealthRT no, if it's done you want to continue. You stop once isDone returns false. What I wrote should do the exact same thing your code does. \$\endgroup\$ – dustytrash Sep 10 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.