{"id":51,"date":"2024-03-29T16:02:43","date_gmt":"2024-03-29T16:02:43","guid":{"rendered":"https:\/\/blog.acini.pl\/?p=51"},"modified":"2024-05-20T07:50:00","modified_gmt":"2024-05-20T07:50:00","slug":"guidewire-gems-series-mastering-null-safety-in-gosu","status":"publish","type":"post","link":"https:\/\/blog.acini.pl\/index.php\/2024\/03\/29\/guidewire-gems-series-mastering-null-safety-in-gosu\/","title":{"rendered":"Mastering Null Safety in Gosu"},"content":{"rendered":"\n<p>Guidewire ecosystem holds many gems, worth highlighting. On our blog you will find a series of articles which focus on such interesting features authored by Acini Guidewire experts. We will kickstart this journey with a fundamental concept: Null Safety in Gosu.<br><br>1. One of the core aspects of null safety in Gosu is the use of the null safe operator, which can be applied to various operations to safeguard your code from unexpected null references. Here are some key uses:<br><br>&#8211; Null-safe invocation (?.): You can safely invoke methods on nullable objects without causing null pointer exceptions. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\ud835\ude25\ud835\ude30\ud835\ude28?.\ud835\ude23\ud835\ude22\ud835\ude33\ud835\ude2c\ud835\ude34()<\/code><\/pre>\n\n\n\n<p>&#8211; Null-safe default operator (?:): This operator allows you to provide default values when dealing with potentially null variables<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\ud835\ude37\ud835\ude22\ud835\ude33 \ud835\ude25\ud835\ude2a\ud835\ude34\ud835\ude31\ud835\ude2d\ud835\ude22\ud835\ude3a\ud835\ude15\ud835\ude22\ud835\ude2e\ud835\ude26 = \ud835\ude25\ud835\ude30\ud835\ude28.\ud835\ude15\ud835\ude22\ud835\ude2e\ud835\ude26 ?: \"(\ud835\ude26\ud835\ude2e\ud835\ude31\ud835\ude35\ud835\ude3a)\"<\/code><\/pre>\n\n\n\n<p> &#8211; Null-safe math operators (?*, ?\/, ?+, ?-, ?%): Perform mathematical operations on nullable values while gracefully handling null cases:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\ud835\ude37\ud835\ude22\ud835\ude33 \ud835\ude31\ud835\ude33\ud835\ude2a\ud835\ude24\ud835\ude26 = \ud835\ude31\ud835\ude33\ud835\ude26\ud835\ude2e\ud835\ude2a\ud835\ude36\ud835\ude2e ?* 10<\/code><\/pre>\n\n\n\n<p> &#8211; Null-safe index operator (?[]): Access elements in an array or collection without worrying about null references. However, it does not protect against ArrayIndexOutOfBoundsException: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\ud835\ude37\ud835\ude22\ud835\ude33 \ud835\ude25\ud835\ude30\ud835\ude28 = \ud835\ude25\ud835\ude30\ud835\ude28\ud835\ude34?&#91;\ud835\ude2a\ud835\ude2f\ud835\ude25\ud835\ude26\ud835\ude39]<\/code><\/pre>\n\n\n\n<p>2\ufe0f. Property access Is NullSafe by default. For instance, this code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\ud835\ude29\ud835\ude30\ud835\ude36\ud835\ude34\ud835\ude26?.\ud835\ude12\ud835\ude2a\ud835\ude35\ud835\ude24\ud835\ude29\ud835\ude26\ud835\ude2f?.\ud835\ude1b\ud835\ude22\ud835\ude23\ud835\ude2d\ud835\ude26?.\ud835\ude08\ud835\ude31\ud835\ude31\ud835\ude2d\ud835\ude26<\/code><\/pre>\n\n\n\n<p> is equivalent to: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\ud835\ude29\ud835\ude30\ud835\ude36\ud835\ude34\ud835\ude26.\ud835\ude12\ud835\ude2a\ud835\ude35\ud835\ude24\ud835\ude29\ud835\ude26\ud835\ude2f.\ud835\ude1b\ud835\ude22\ud835\ude23\ud835\ude2d\ud835\ude26.\ud835\ude08\ud835\ude31\ud835\ude31\ud835\ude2d\ud835\ude26<\/code><\/pre>\n\n\n\n<p>It will work if returned types are reference types.<br><br>3. Handling Null Values for Reference and Primitive Types<br><br>For reference types, a null safe operation will simply return null if any part of the chain evaluates to null. If the return type of the entire expression is a primitive type (such as int, long, and boolean) than the expression is not null safe. You can fix it by using @ShortCircuitingProperty annotation on property getter or ?. in your access chain than default primitive type value will be returned (int -&gt; 0, boolean -&gt; false).<br>In specific scenarios, the default values can lead to counterintuitive outcomes. Consider the following examples: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\ud835\ude37\ud835\ude22\ud835\ude33 \ud835\ude35\ud835\ude26\ud835\ude39\ud835\ude35: \ud835\ude1a\ud835\ude35\ud835\ude33\ud835\ude2a\ud835\ude2f\ud835\ude28 = \ud835\ude2f\ud835\ude36\ud835\ude2d\ud835\ude2d\n\n\ud835\ude31\ud835\ude33\ud835\ude2a\ud835\ude2f\ud835\ude35(\ud835\ude35\ud835\ude26\ud835\ude39\ud835\ude35.\ud835\ude0c\ud835\ude2e\ud835\ude31\ud835\ude35\ud835\ude3a)     \/\/ \ud835\ude27\ud835\ude22\ud835\ude2d\ud835\ude34\ud835\ude26 \ud835\ude23\ud835\ude36\ud835\ude35 \ud835\ude26\ud835\ude39\ud835\ude31\ud835\ude26\ud835\ude24\ud835\ude35\ud835\ude26\ud835\ude25 \ud835\ude33\ud835\ude26\ud835\ude34\ud835\ude36\ud835\ude2d\ud835\ude35 \ud835\ude23\ud835\ude3a \ud835\ude25\ud835\ude26\ud835\ude37\ud835\ude26\ud835\ude2d\ud835\ude30\ud835\ude31\ud835\ude26\ud835\ude33 \ud835\ude2a\ud835\ude34 \ud835\ude35\ud835\ude33\ud835\ude36\ud835\ude26\n\ud835\ude31\ud835\ude33\ud835\ude2a\ud835\ude2f\ud835\ude35(\ud835\ude35\ud835\ude26\ud835\ude39\ud835\ude35.\ud835\ude09\ud835\ude2d\ud835\ude22\ud835\ude2f\ud835\ude2c)    \/\/ \ud835\ude27\ud835\ude22\ud835\ude2d\ud835\ude34\ud835\ude26\n\ud835\ude31\ud835\ude33\ud835\ude2a\ud835\ude2f\ud835\ude35(\ud835\ude35\ud835\ude26\ud835\ude39\ud835\ude35.\ud835\ude0f\ud835\ude22\ud835\ude34\ud835\ude0a\ud835\ude30\ud835\ude2f\ud835\ude35\ud835\ude26\ud835\ude2f\ud835\ude35) \/\/ \ud835\ude27\ud835\ude22\ud835\ude2d\ud835\ude34\ud835\ude26\n\n\ud835\ude37\ud835\ude22\ud835\ude33 \ud835\ude22\ud835\ude33\ud835\ude33\ud835\ude22\ud835\ude3a: \ud835\ude1a\ud835\ude35\ud835\ude33\ud835\ude2a\ud835\ude2f\ud835\ude28&#91;] = \ud835\ude2f\ud835\ude36\ud835\ude2d\ud835\ude2d\n\n\ud835\ude31\ud835\ude33\ud835\ude2a\ud835\ude2f\ud835\ude35(\ud835\ude22\ud835\ude33\ud835\ude33\ud835\ude22\ud835\ude3a.\ud835\ude10\ud835\ude34\ud835\ude0c\ud835\ude2e\ud835\ude31\ud835\ude35\ud835\ude3a)     \/\/ \ud835\ude27\ud835\ude22\ud835\ude2d\ud835\ude34\ud835\ude26 \ud835\ude23\ud835\ude36\ud835\ude35 \ud835\ude26\ud835\ude39\ud835\ude31\ud835\ude26\ud835\ude24\ud835\ude35\ud835\ude26\ud835\ude25 \ud835\ude33\ud835\ude26\ud835\ude34\ud835\ude36\ud835\ude2d\ud835\ude35 \ud835\ude23\ud835\ude3a \ud835\ude25\ud835\ude26\ud835\ude37\ud835\ude26\ud835\ude2d\ud835\ude30\ud835\ude31\ud835\ude26\ud835\ude33 \ud835\ude2a\ud835\ude34 \ud835\ude35\ud835\ude33\ud835\ude36\ud835\ude26\n\ud835\ude31\ud835\ude33\ud835\ude2a\ud835\ude2f\ud835\ude35(\ud835\ude22\ud835\ude33\ud835\ude33\ud835\ude22\ud835\ude3a.\ud835\ude0f\ud835\ude22\ud835\ude34\ud835\ude0c\ud835\ude2d\ud835\ude26\ud835\ude2e\ud835\ude26\ud835\ude2f\ud835\ude35\ud835\ude34) \/\/ \ud835\ude2f\ud835\ude36\ud835\ude2d\ud835\ude2d\n<\/code><\/pre>\n\n\n\n<p>Null safety in Gosu is a powerful tool to make your code more robust. By mastering these techniques, you can write more reliable software and avoid the pitfalls of null references.<\/p>\n\n\n<div class=\"taxonomy-post_tag wp-block-post-terms\"><a href=\"https:\/\/blog.acini.pl\/index.php\/tag\/gosu\/\" rel=\"tag\">gosu<\/a><span class=\"wp-block-post-terms__separator\">, <\/span><a href=\"https:\/\/blog.acini.pl\/index.php\/tag\/guidewire\/\" rel=\"tag\">guidewire<\/a><span class=\"wp-block-post-terms__separator\">, <\/span><a href=\"https:\/\/blog.acini.pl\/index.php\/tag\/insurance-ecosystem\/\" rel=\"tag\">insurance ecosystem<\/a><\/div>","protected":false},"excerpt":{"rendered":"<p>Welcome to the inaugural post of the Guidewire Gems series, where we will focus on interesting features of the Guidewire ecosystem. We will kickstart our journey with a fundamental concept: Null Safety in Gosu.<\/p>\n","protected":false},"author":1,"featured_media":53,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_seopress_robots_primary_cat":"","_seopress_titles_title":"","_seopress_titles_desc":"","_seopress_robots_index":"","footnotes":""},"categories":[25],"tags":[7,6,15],"class_list":["post-51","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-our-tips-tricks","tag-gosu","tag-guidewire","tag-insurance-ecosystem"],"_links":{"self":[{"href":"https:\/\/blog.acini.pl\/index.php\/wp-json\/wp\/v2\/posts\/51","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.acini.pl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.acini.pl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.acini.pl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.acini.pl\/index.php\/wp-json\/wp\/v2\/comments?post=51"}],"version-history":[{"count":31,"href":"https:\/\/blog.acini.pl\/index.php\/wp-json\/wp\/v2\/posts\/51\/revisions"}],"predecessor-version":[{"id":146,"href":"https:\/\/blog.acini.pl\/index.php\/wp-json\/wp\/v2\/posts\/51\/revisions\/146"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.acini.pl\/index.php\/wp-json\/wp\/v2\/media\/53"}],"wp:attachment":[{"href":"https:\/\/blog.acini.pl\/index.php\/wp-json\/wp\/v2\/media?parent=51"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.acini.pl\/index.php\/wp-json\/wp\/v2\/categories?post=51"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.acini.pl\/index.php\/wp-json\/wp\/v2\/tags?post=51"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}