Skip to content

Commit

Permalink
Add assert tag (#155)
Browse files Browse the repository at this point in the history
* Create assert.ytag

It's fairly common in the mod dev channels to see people using asserts when they shouldn't. A quick tag to explain why they shouldn't be used and what to use instead would be beneficial.

* Clarify description of behavior when assertions are disabled

* clarify again

---------

Co-authored-by: modmuss <[email protected]>
  • Loading branch information
hiisuuii and modmuss50 authored Jun 4, 2024
1 parent cbd63cf commit aedd73c
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions tags/faq/assert.ytag
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: text

---

You should avoid using `assert` in your mod code. If the assert passes, it will work as expected. However, if it fails, Minecraft will crash. Assertions are also disabled by default in the JVM, and will be ignored when your mod is run outside an IDE, which may lead to undefined behavior and potentially a NullPointerException.
Instead of using the assertion pattern `assert someObject != null;`, you should use the null-checking pattern: `if (someObject != null) { /* Code here */ }

0 comments on commit aedd73c

Please sign in to comment.