rspec/rules/S6244/java/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

54 lines
1.5 KiB
Plaintext

== Why is this an issue?
Some API, like the AWS SDK, heavily rely on the builder pattern to create different data structures. Despite all the benefits, this pattern can become really verbose, especially when dealing with nested structures. In order to reach a more concise code, "Consumer Builders", also called "Consumer Interface" are often introduced.
The idea is to overload the methods taking others structures in a Builder with a Consumer of Builder instead. This enables to use a lambda instead of nesting another Builder, resulting in more concise and readable code.
This rule reports an issue when the Consumer Builder methods could be used instead of the classical ones.
=== Noncompliant code example
[source,java]
----
SendEmailRequest.builder()
.destination(Destination.builder()
.toAddresses("to-email@domain.com")
.bccAddresses("bcc-email@domain.com")
.build())
.build();
----
=== Compliant solution
[source,java]
----
SendEmailRequest.builder()
.destination(d -> d.toAddresses("to-email@domain.com").bccAddresses("bcc-email@domain.com"))
.build();
----
== Resources
* https://aws.amazon.com/fr/blogs/developer/consumer-builders-in-the-aws-sdk-for-java-v2/[Consumer Builders in the AWS SDK for Java v2]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Consider using the Consumer Builder method instead of creating this nested builder
=== Highlighting
* Method call that could be replaced by the consumer builder
endif::env-github,rspecator-view[]