I went little fancy and wrote Selenium page-object with Java 8 streaming as mentioned in below code and got a review comment that my code is breaking Law of Demeter, since I am doing lot of operations in a single line. I was suggested to break the code to first stream to collect to list and run another stream operation to do match( in short break it down into multiple streams as needed). I was not convinced as Stream is introduced for handling data processing and if we break it down into multiple streams, there is no point using stream. Previously I have worked for a cyber security project, where millions of records were processed with streaming with multiple logic operations to sort the data.
Please share your thoughts, I have changed it as the reviewer suggested but he couldn't explain why and I want to learn more about streams and right way of utilizing this powerful addition to java 8.
Here is the sample code:
listOfWebElements.stream().filter(element -> element.getText().contains(name)).findFirst().ifPresent(match -> match.click());
is line I am referring to in this question, providing the method so that it makes more sense.
@FindBy(css = "#someTable td.some-name li a") List<WebElement> listOfWebElements;
public SomeClass doSomething(String name) {
wait.until(visibilityOfAllElements(listOfWebElements));
listOfWebElements.stream().filter(element -> element.getText().contains(name)).findFirst()
.ifPresent(match -> match.click());
return new SomeClass(driver);
}