From aedd73c0a3de1027401493c529fc83430359af13 Mon Sep 17 00:00:00 2001 From: hiisuuii Date: Tue, 4 Jun 2024 08:31:26 -0600 Subject: [PATCH] Add `assert` tag (#155) * 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 --- tags/faq/assert.ytag | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 tags/faq/assert.ytag diff --git a/tags/faq/assert.ytag b/tags/faq/assert.ytag new file mode 100644 index 0000000..addd437 --- /dev/null +++ b/tags/faq/assert.ytag @@ -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 */ }