This repository has been archived by the owner on May 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Java: add guidelines for using assertions #37 #38
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Additional Guidelines - Java | ||
|
||
## Using assertions | ||
|
||
Refer to the article | ||
_[Programming With Assertions](http://docs.oracle.com/javase/8/docs/technotes/guides/language/assert.html)_ | ||
(from Oracle) for more details on the three general guidelines below. | ||
|
||
1. **Do not use assertions to do any work that your application requires for correct operation.**<br> | ||
If you do, the code will not work as expected when assertions are turned off. | ||
|
||
|
||
1. **Do not use assertions for checking _preconditions_/parameters in public methods.**<br> | ||
Those should be enforced by explicit checks that throw particular, | ||
specified exceptions. e.g. `IllegalArgumentException`, `IndexOutOfBoundsException`, or `NullPointerException`. | ||
|
||
|
||
1. **Assertions may be used to check _postconditions_ and class/method _invariants_ in both public | ||
and nonpublic methods.** | ||
|
||
In addition, | ||
|
||
* **Do not handle 'impossible' exceptions using assertions**.<br> | ||
Instead of handling 'impossible' exceptions using an `assert false` as given below, | ||
throw a runtime error such as an `AssertionError`. | ||
|
||
![](Bad.png) | ||
```java | ||
... | ||
} catch (Exception e) { | ||
assert false : "This exception should not happen"; | ||
} | ||
``` | ||
|
||
![](Good.png) | ||
```java | ||
... | ||
} catch (Exception e) { | ||
throw new AssertionError("This exception should not happen"); | ||
} | ||
``` | ||
|
||
> Rationale: As the program flow has already triggered an exception, switching to assertions is not necessary when | ||
> another exception can handle it just as well. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Section headings should use Title Case like in the other coding standards (linked here for convenience):
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.
Based on this which we are going to adopt soon, headings should be sentence case. Keep this in sentence case and revise coding standards docs later to match the guideline?