Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested Blocks return 0 for enhanced forEach populated with lambda function #42

Open
aaghamohammadi opened this issue Feb 16, 2020 · 4 comments

Comments

@aaghamohammadi
Copy link
Contributor

Hi @mauricioaniche
I am writing tests for NumberOfMaxNestedBlock.java . I have come accross the following issue:

class NestedBlocks8 {
        public void m1() {
        List<Integer> integers = Arrays.asList(3, 9, 7, 6, 10, 20);
        integers.forEach(i -> System.out.println(50 / i));
    }
}

I expect that the max depth of m1() equals one. However, the getMaxNestedBlocks() returns zero. Indeed, It happens when the enhanced version of For along with a lambda function is employed.

@mauricioaniche
Copy link
Owner

mauricioaniche commented Feb 16, 2020

Hmm, this one is tricky. Maybe whenever there's a lambda expression, we do a +1 in the current nested block? Would that make sense to all lambdas in a method?

@aaghamohammadi
Copy link
Contributor Author

I don't think so. How about this code?

shapes.stream() 
      .filter(s -> s.getColor() == BLUE)
      .forEach(s -> s.setColor(RED));

I think the nested level of this code is one (not 2).

@mauricioaniche
Copy link
Owner

Yeah, that would be one! With the implementation I have in mind, whenever there's a lambda declaration, we do a +1, whenever it finishes it do a -1 (that's what happens with the other blocks, e.g., for, while, ...). So, in this case:

shapes.stream() <-- level 0
      .filter(s -> s.getColor() == BLUE <inside the lambda level 1>) <-- level 0 again
      .forEach(s -> s.setColor(RED)  <inside the lambda level 1>); <-- level 0 again

So, max = 1

@aaghamohammadi
Copy link
Contributor Author

Thank you for your response.
Oh, I see. I think your approach is correct. By the way, I suppose CBO has a problem yet. Consider Box<A> (the CBO should be 2 not 1).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants