r/functionalprogramming • • Feb 14 '23

Question Can this method be more functional?

This is some Java code for a GUI component that changes text and color based on the state of an inner linked list. Can it be more functional style?

   private void render() {
        if (listIsEmpty())
            configureLayout(State.EMPTY);

        else if (allEqual())
            configureLayout(State.ALL_EQUAL);

        else if (isSortedAscending())
            configureLayout(State.ASCENDING);

        else if (isSortedDescending())
            configureLayout(State.DESCENDING);

        else
            configureLayout(State.UNSORTED);
    }

EDIT: I refactored the previous code to the following. Is it better already?

   private void render() {
        configureLayout(determineState());
    }

    private State determineState() {
        if (listIsEmpty()) return State.EMPTY;
        if (allEqual()) return State.ALL_EQUAL;
        if (isSortedAscending()) return State.ASCENDING;
        if (isSortedDescending()) return State.DESCENDING;
        return State.UNSORTED;
    }
4 Upvotes

13 comments sorted by

View all comments

Show parent comment

2

u/raulalexo99 Feb 14 '23

Okay this is interesting. Needs a lot of code in Java though. I will try it.

3

u/raulalexo99 Feb 14 '23 edited Feb 14 '23

Okay thanks to your answer I came up with a somewhat FP equivalent of this in Java. It requires a lot of code but it is kinda what I was looking for. This may not be worth it because of the amount of code and I think I might just use the easy multi - if method. Anyways for those interested, here is the solution:

// Helper class to hold a tester function and a state
private record StatePredicate(Predicate<ObservableListState> test, State state) {
    public boolean apply(ListState state) {
        return test.test(state);
    }
}

// Create a list to map each test function to a State
  private List<StatePredicate> stateTests() {
    return List.of(
            new StatePredicate(this::isListEmpty, State.EMPTY),
            new StatePredicate(this::allEqual, State.ALL_EQUAL),
            new StatePredicate(this::isSortedAscending, State.ASCENDING),
            new StatePredicate(this::isSortedDescending, State.DESCENDING),
            new StatePredicate(state -> true, State.UNSORTED)
    );
}

// Use the testers to render the component:
private void render(ListState state) {
    configureLayoutForState(evaluateState(state));
}

private State evaluateState(ObservableListState state) {
    return stateTests().stream()
            .filter(test -> test.apply(state))
            .map(StatePredicate::state)
            .findFirst()
            .orElseThrow(() -> new IllegalStateException("Cannot determine state for this component"));
}

This is the full code of the class inside my project for anyone interested: https://github.com/AlexAnguloMtz/Linked-Lists-Desktop-GUI/blob/master/src/main/java/com/demo/list/view/components/SortingState.java

It works but it looks kinda cluttered... but hey, it's Java