cloudevents / cloudevents/sdk-java
Share rationale behind explicit `throws RuntimeException` clause
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 446
- Forks
- 172
- PR merge metrics
- No merged PRs in 30d
Description
Context
I'm a Java™ programmer seeking guidance on best practice and recommended Java™ programming idioms. Ordinarily, my first inclination is to defer to the most authoritative references for the Java™ programming language.
However, library authors can sometimes offer compelling rationale to do otherwise.
Java™ SE 8 Javadoc for RuntimeException:
public class RuntimeException
extendsException…
RuntimeExceptionand its subclasses are unchecked exceptions.
Unchecked exceptions do NOT need to be declared in a method
or constructor's throws clause if they can be thrown by the
execution of the method or constructor and propagate outside
the method or constructor boundary
…
Java™ Language Specification (JLS)
…
Run-time exception classes are exempted because, in the judgment of
the designers of the Java programming language, having to declare
such exceptions would not aid significantly in establishing the
correctness of programs. Many of the operations and constructs of
the Java programming language can result in exceptions at run time.
The information available to a Java compiler, and the level of analysis
a compiler performs, are usually not sufficient to establish that such
run-time exceptions cannot occur, even though this may be obvious to the
programmer. Requiring such exception classes to be declared would
simply be an irritation to programmers
…
The Issue
TL;DR — It's puzzling why some Java™ SDK for CloudEvents methods are declared with a throws RuntimeException clause.
To elaborate
I have tried to reason about what this library's API designers might have intended to communicate to callers by exposing the atypical public signatures in question. Several hypothetical questions arose. Here are three examples…
- What does this project's authors intend callers to do when calling methods declared with
throws RuntimeException? - What do you foresee would happen if callers ignore (don't handle) your methods'
throws RuntimeExceptions? - What, originally, did you foresee would happen for callers if those
throws RuntimeExceptionwere never declared? - …
Which methods?
All overloaded CloudEventContextWriter.withContextAttribute(...) methods have throws CloudEventRWException declarations…
…
CloudEventContextWriter withContextAttribute(String name, String value) throws CloudEventRWException;
…
Interestingly, the two methods CloudEventContextReaderAdapter.readAttributes() CloudEventContextReaderAdapter.readExtensions() are both declared with throws RuntimeException; the superclass of CloudEventRWException…
…
public void readAttributes(CloudEventContextWriter writer) throws RuntimeException
…
public void readExtensions(CloudEventContextWriter writer) throws RuntimeException
…
// This should never happen because we build that map only through our builders
throw new IllegalStateException("Illegal value inside extensions map: " + key + " " + value);
…
CloudEventContextReaderAdapter.readContext(CloudEventContextWriter) calls both of its sibling methods. However, it IS declared with throws CloudEventRWException…
public void readContext(CloudEventContextWriter writer) throws CloudEventRWException
Or is it just me?
The system I'm currently analyzing and will eventually implement, potentially may consume the public API of the Java™ SDK for CloudEvents.
For not only my project specifically, I imagine it would be helpful for all consuming projects in general to understand the rationale behind this SDK's atypical idiom of explicitly declaring public methods with throws RuntimeException.
Proposed Solution
There's more than one alternative that I presume would have value to the entire community. Here are just two examples…
- Add clear explanations in the Java™ SDK for CloudEvents Javadoc
- Nothing complicated; simply something that clarifies the methods in question's rationale for taking the road less traveled
- Currently, there is nothing documenting why
throws RuntimeExceptionis necessary
- Remove the explictly-declared
throws RuntimeExceptionclauses- But add Javadoc that uses the
@throwsannotation appropriately to document what callers of the method should expect
- But add Javadoc that uses the
- ???
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with CloudEventContextWriter.withContextAttribute(...), CloudEventContextReaderAdapter.readAttributes(), readExtensions(), and readContext() as identified in the issue, then inspect their current declarations and Javadocs. Done means the API's RuntimeException rationale and caller expectations are documented, or the declarations are revised with appropriate @throws documentation, subject to maintainer agreement.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api, documentation
- Issue type
- Documentation
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 32/100