From 9e18a506fb8d9d1d49ee13145ae465eeccca2831 Mon Sep 17 00:00:00 2001 From: hiisuuii Date: Thu, 25 Jan 2024 15:04:12 -0700 Subject: [PATCH 1/3] 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. --- 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..3919db8 --- /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, or if assertions are disabled (default behavior), Minecraft will crash. +Instead of using the assertion pattern `assert someObject != null;`, you should use the null-checking pattern: `if (someObject != null) { /* Code here */ } From 64323f6b0971168013ccff8ffee1cc9a231a42e0 Mon Sep 17 00:00:00 2001 From: hiisuuii Date: Fri, 26 Jan 2024 21:12:51 -0700 Subject: [PATCH 2/3] Clarify description of behavior when assertions are disabled --- tags/faq/assert.ytag | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tags/faq/assert.ytag b/tags/faq/assert.ytag index 3919db8..e94dd4f 100644 --- a/tags/faq/assert.ytag +++ b/tags/faq/assert.ytag @@ -2,5 +2,5 @@ type: text --- -You should avoid using `assert` in your mod code. If the assert passes, it will work as expected. However, if it fails, or if assertions are disabled (default behavior), Minecraft will crash. +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, 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 */ } From 8173d83d1b0f2e7a065febc711d4749a5e98e6ef Mon Sep 17 00:00:00 2001 From: hiisuuii Date: Fri, 26 Jan 2024 21:51:38 -0700 Subject: [PATCH 3/3] clarify again --- tags/faq/assert.ytag | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tags/faq/assert.ytag b/tags/faq/assert.ytag index e94dd4f..addd437 100644 --- a/tags/faq/assert.ytag +++ b/tags/faq/assert.ytag @@ -2,5 +2,5 @@ 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, which may lead to undefined behavior and potentially a NullPointerException. +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 */ }