-
Notifications
You must be signed in to change notification settings - Fork 873
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
Refactor io.opentelemetry.instrumentation.resources.ContainerResource
to avoid using null
#6889
Refactor io.opentelemetry.instrumentation.resources.ContainerResource
to avoid using null
#6889
Conversation
Refactor class `ContainerResource` so that its methods return `Optional`s instead of `null`. This makes for safer code that doesn't rely on `null` to signal the absence of result. Moreover, `Optional.map()` and `Optional.orElseGet()` use the same functional style as `Stream`, which is well readable. Issue-Id: open-telemetry#6694 Issue-Id: open-telemetry/opentelemetry-java#2337
2eb89f9
to
29cbbfa
Compare
.map(ContainerResource::getIdFromLine) | ||
.filter(Optional::isPresent) | ||
.findFirst() | ||
.orElse(Optional.empty()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't
.map(ContainerResource::getIdFromLine) | |
.filter(Optional::isPresent) | |
.findFirst() | |
.orElse(Optional.empty()); | |
.flatMap(ContainerResource::getIdFromLine); |
work here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, unfortunately flatMap()
won't work here because ContainerResource::getIdFromLine
returns Optional<String>
and we end up with Stream<Optional<String>>
. It would work as the last step though, to "pop" the Optional<Optional<String>>
:
.map(ContainerResource::getIdFromLine) | |
.filter(Optional::isPresent) | |
.findFirst() | |
.orElse(Optional.empty()); | |
.map(ContainerResource::getIdFromLine) | |
.filter(Optional::isPresent) | |
.findFirst() | |
.flatMap(identity()); |
Is the last line clearer this way?
.../library/src/test/java/io/opentelemetry/instrumentation/resources/ContainerResourceTest.java
Outdated
Show resolved
Hide resolved
In general I agree with this statement; but keep in mind that this project prefers to use
Looks pretty good! 🚀 Thanks! |
Several test cases in `ContainerResourceTest` have the same outcome, so instead of duplicating the assertions inside each test method, take advantage of `@ParameterizedTest` to inject inputs and expected outputs multiple times into the same test. This improves test code readability by reducing test method length and making identical cases more obvious. Also rename test methods to better express the tested code's expected behaviour. Issue-Id: open-telemetry#6694 Issue-Id: open-telemetry/opentelemetry-java#2337
29cbbfa
to
034a2c1
Compare
Thank you very much for the review @mateuszrzeszutek ! Would you mind labelling this PR with "hacktoberfest-accepted" so it counts toward my Hacktoberfest 2022 efforts? 😁 |
…e` to avoid using null (open-telemetry#6889) While I was looking at issues open-telemetry#6694 and open-telemetry/opentelemetry-java#2337, I saw that the code in `io.opentelemetry.instrumentation.resources.ContainerResource` used `null` several times as return value which isn't safe. Nowadays, `Optional` is better suited to signal the absence of a result, so I refactored `ContainerResource` to use `Optional`s instead of null. On the way, I also refactored this class's unit tests into parameterised tests to reduce test code duplication. These improvements should help implementing a solution to open-telemetry#6694. Co-authored-by: Trask Stalnaker <[email protected]>
This was discussed a long while back, but never documented (and recently came up in #6889) Co-authored-by: Fabrizio Ferri-Benedetti <[email protected]>
…e` to avoid using null (open-telemetry#6889) While I was looking at issues open-telemetry#6694 and open-telemetry/opentelemetry-java#2337, I saw that the code in `io.opentelemetry.instrumentation.resources.ContainerResource` used `null` several times as return value which isn't safe. Nowadays, `Optional` is better suited to signal the absence of a result, so I refactored `ContainerResource` to use `Optional`s instead of null. On the way, I also refactored this class's unit tests into parameterised tests to reduce test code duplication. These improvements should help implementing a solution to open-telemetry#6694. Co-authored-by: Trask Stalnaker <[email protected]>
…e` to avoid using null (open-telemetry#6889) While I was looking at issues open-telemetry#6694 and open-telemetry/opentelemetry-java#2337, I saw that the code in `io.opentelemetry.instrumentation.resources.ContainerResource` used `null` several times as return value which isn't safe. Nowadays, `Optional` is better suited to signal the absence of a result, so I refactored `ContainerResource` to use `Optional`s instead of null. On the way, I also refactored this class's unit tests into parameterised tests to reduce test code duplication. These improvements should help implementing a solution to open-telemetry#6694. Co-authored-by: Trask Stalnaker <[email protected]>
This was discussed a long while back, but never documented (and recently came up in open-telemetry#6889) Co-authored-by: Fabrizio Ferri-Benedetti <[email protected]>
…e` to avoid using null (open-telemetry#6889) While I was looking at issues open-telemetry#6694 and open-telemetry/opentelemetry-java#2337, I saw that the code in `io.opentelemetry.instrumentation.resources.ContainerResource` used `null` several times as return value which isn't safe. Nowadays, `Optional` is better suited to signal the absence of a result, so I refactored `ContainerResource` to use `Optional`s instead of null. On the way, I also refactored this class's unit tests into parameterised tests to reduce test code duplication. These improvements should help implementing a solution to open-telemetry#6694. Co-authored-by: Trask Stalnaker <[email protected]>
This was discussed a long while back, but never documented (and recently came up in open-telemetry#6889) Co-authored-by: Fabrizio Ferri-Benedetti <[email protected]>
While I was looking at issues #6694 and open-telemetry/opentelemetry-java#2337, I saw that the code in
io.opentelemetry.instrumentation.resources.ContainerResource
usednull
several times as return value which isn't safe. Nowadays,Optional
is better suited to signal the absence of a result, so I refactoredContainerResource
to useOptional
s instead of null.On the way, I also refactored this class's unit tests into parameterised tests to reduce test code duplication. These improvements should help implementing a solution to #6694.