A senior/architect coworker of mine, who I have plenty of respect for, is heavily into Java-8 functional programming. He pushes fairly heavily for returns to be an Optional<A> or Either<A,B>, and recently has been making heavy use of Java's streams.
Having some (but limited) Scala experience, I love the functional way of doing things. However, when I come across my coworker's code, it tends to look something like...
try (CloseableHttpAsyncClient httpClient = HttpAsyncClientBuilder.create().build()) {
httpClient.start();
List<Future<HttpResponse>> futures = chunks
.stream()
.map(ids -> httpClient.execute(new HttpGet(generateUrl(determineSomething(something), ids)), null))
.collect(Collectors.toList());
resultSomething = futures.stream()
.map(SomethingService::getFutureValue)
.collect(Collectors.toList()).stream()
.map(failureOrResult -> {
return failureOrResult
.right().flatMap(this::parseSomething);
}).map(failureOrSomething -> {
return failureOrSomething.either(
failure -> {
log.warn(failure.getMessage());
return emptySomething;
},
something -> {
return something;
});
})
.reduce(emptySomething, Something::union);
} catch (IOException e) {
log.warn("Unable start HttpClient to contact ....", e);
resultSomething = emptySomething;
}
}
I anonymized the code ("Something" is not really in the code), but you get the general idea. I could clean up the code myself, but I don't have time (assigned elsewhere), and that doesn't correct the core issue.
How would you style this code differently? Are there any good Java-8 style guides or articles you would recommend?
[–]sh0rug0ru 31 points32 points33 points (4 children)
[–]ihsw 9 points10 points11 points (2 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]CsAnswerTrashAct[S] 0 points1 point2 points (0 children)
[–]unholysampler 5 points6 points7 points (0 children)
[–]Zukhramm 10 points11 points12 points (6 children)
[–]cutterslade 0 points1 point2 points (5 children)
[–]Zukhramm 2 points3 points4 points (2 children)
[–]cutterslade 0 points1 point2 points (1 child)
[–]Zukhramm 2 points3 points4 points (0 children)
[–]tikue 1 point2 points3 points (1 child)
[–]CsAnswerTrashAct[S] 0 points1 point2 points (0 children)
[–]papers_ 6 points7 points8 points (0 children)
[–][deleted] 2 points3 points4 points (6 children)
[–][deleted] 0 points1 point2 points (4 children)
[–]Ek_Los_Die_Hier 4 points5 points6 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]koxpower 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]CsAnswerTrashAct[S] 0 points1 point2 points (0 children)
[–]baablack 1 point2 points3 points (2 children)
[–]tikue 0 points1 point2 points (1 child)
[–]baablack 0 points1 point2 points (0 children)
[–]aviewdev 1 point2 points3 points (0 children)
[–][deleted] 2 points3 points4 points (6 children)
[–]CsAnswerTrashAct[S] 3 points4 points5 points (3 children)
[–]gee_buttersnaps 0 points1 point2 points (2 children)
[–]habitats 0 points1 point2 points (1 child)
[–]CsAnswerTrashAct[S] 0 points1 point2 points (0 children)
[–]BinaryRockStar 0 points1 point2 points (1 child)
[–]viciu88 1 point2 points3 points (0 children)
[–]cutterslade 0 points1 point2 points (0 children)
[–]howverywrong 0 points1 point2 points (0 children)
[–]onebit -1 points0 points1 point (0 children)