0

I took over someone's project and I came across with a function where it takes a condition and checks if that condition has children. If yes it will check if the children are objects or other conditions. If it is an object it will go ahead and do the logic other wise if it is condition it will continue checking what is under it and so on.... I know this is too complicated and I can't explain it more than this but I wrote a similar logic for scenario.

In my example CondtionList extends the Condition class and the tree structure looks like this

                        condition
                          /   \  
                     object    condition
                              /     |    \ 
                        object  condition object
                                 /     \ 
                               object  condition

What I am trying to do here is to make a recursive call for multilayered tree like structure Condition. It works fine until it gets to the return value which calls back my recursive repeatedly before it finishes executing this function.

Here is my code:

private boolean[] isNumberEvenOrOdd(Condition condition){
    boolean isEven = false;
    boolean isOdd = false;
    boolean[] isNumberEvenOrOdd = new boolean[2];

    if(condition instanceOf ConditionList) {
        for(Condition child : ConditionList.getConditions()){
            if (child instanceOf ConditionList){
                isNumberEvenOrOdd(child);
            }
            else{
                if((Integer)child % 2 == 0) {
                    isEven = true;
                }
                else {
                    isOdd = true;
                }
            }
        }
    }

    isNumberEvenOrOdd[0] = isEven;
    isNumberEvenOrOdd[1] = isOdd;

    return isNumberEvenOrOdd;
}

Thanks in advance for your help.

Zelalem
  • 392
  • 5
  • 7
  • 1
    Two things are really irritating in this sample: 1) you use the methods name as a variable name. 2) Also, why do you return isEvenOrOdd as an array? A number MUST always be either even or odd. So you could call the method "isEven" and simply return true if the number is even, otherwise return false. This code cleanup might help you to get to the solution. – dinosaur Oct 30 '18 at 15:31
  • For which child object do you want to know if it's odd or even? For all of them? Or for a specific one? If you want to do this for all of them, you will have to create a list or use a boolean in the objects. – dinosaur Oct 30 '18 at 15:45
  • 1
    lot of mess here as pointed by dinosaur.Why unnecessary flags like `isEven` & `isOdd` ,one's reverse is other. also there should be return keyword before the `isNumberEvenOrOdd` method inside to exit the recursion. – Navoneel Talukdar Oct 30 '18 at 17:30
  • I know the sample is a mess however, as I mentioned earlier, I got this logic from someone else and that person wasn't considering multiple layers when writing this code. My task is to make this method functional for multi-layered without making big changes. – Zelalem Oct 30 '18 at 17:37
  • @dinosaur to answer your question, I need to know for all of them. – Zelalem Oct 30 '18 at 18:05

0 Answers0