{"id":12,"date":"2019-05-28T01:00:58","date_gmt":"2019-05-28T01:00:58","guid":{"rendered":"https:\/\/www.great-white-software.com\/blog\/?p=12"},"modified":"2019-06-21T01:54:22","modified_gmt":"2019-06-21T01:54:22","slug":"code-for-safety","status":"publish","type":"post","link":"https:\/\/www.great-white-software.com\/blog\/2019\/05\/28\/code-for-safety\/","title":{"rendered":"Code for safety"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Lots of time you see code like<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>static flag as boolean\u00a0\nif flag then return\n\nflag = true\n\n\/\/ rest of method\n\nflag = false\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In a simple method\/event with only a handful of lines there\u2019s probably nothing wrong with this<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But what do you do when the method spans into hundreds or thousands of lines ?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And if you use the philosophy of \u201cbail early\u201d ?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Then your methods\/events start to look like<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>static flag as boolean \nif flag then return\nflag = true\n\nif condition then\n  flag = false\n  return\nend if\n\nif condition2 then\n  if condition3 then\n    flag = false\n    return\n  end if\nelse\n    flag = false\n    return\nend if\n\nflag = false\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And following all those returns &amp; making sure you reset the flag correctly in every one is tedious.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So what if you could make it so you NEVER had to worry about the flag remaining set when you returned ?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Remember that objects get destroyed when the last reference to them is removed &#8211; they automatically get cleaned up.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">At first glance it seems that you should be able to do<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>static flag as date\nif flag &lt;> nil then return\nflag = new Date()\n\n\/\/ rest of method\nreturn<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">and then when the method ends the date object would go out of scope, get destroyed and the world would be happy &amp; safe for not forgetting to flip that flag.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Sadly because the variable is a static this wont happen &#8211; the static persists across calls and so it wont get destroyed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So the trick is how to make the variable persist across method calls AND also get destroyed when things go out of scope.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But that doesn\u2019t mean we\u2019re stuck.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One issue we do have is that in the way we\u2019ve set up our flag property its a value type which means other code can\u2019t hold on to a reference to it to do anything later.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And because its a static declared in a method other code outside this method cannot touch it to flip its value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So we need to deal with both these issues.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The second one is easiest to deal with &#8211; we can simply move the static from being declared in the method to a property on the class that is only used by this method (although its much more usual to see such a blocking action like this happen in several methods or events rather than just a single one)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So we\u2019ll change<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>static flag as date<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">to a similarly named property on this class \/ window&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By simply doing this we make it possible to use an object that, when created and then goes out of scope makes it so we can use the constructor &amp; destructor to change out flag so things get blocked &amp; allowed as needed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><br>\nSo how do we have something flip the flag when we need ?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In theory we want our code to look like<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if flag then return \/\/ remember flag is a private \n                    \/\/ or protected property on the class\n\ndim flipper as new FlagFlipper( ???????? )\n\n\/\/ rest of method\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">So what does this \u201cFlipper\u201d thing looks like &amp; how does it work ?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We\u2019ll create a class &#8211; FlagFlipper.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And this class needs to then be able to know how to set and clear the flag we set up. But we really don\u2019t want this new class to have to reach inside other classes to do this.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And this is where delegates come into play.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A delegate is a \u201ctype safe function pointer\u201d which means that<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">it\u2019s a function you can pass around like any other parameter<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;it\u2019s type safe &#8211; you can\u2019t just pass any old function&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So our new class is going to look something like<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Class FlagFlipper\n  Constructor( ?????????? )\n    \/\/ flip the flag in the target class somehow\n\n  End Constructor\n\n  Destructor()\n    \/\/ flip the flag in the target class somehow\n  End Destructor\nEnd Class\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To this we\u2019ll add 2 delegates<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Delegate SetFlag()\nDelegate ClearFlag()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Note that we do not put code in these.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Recall we do not want the flag flipper to know the innards of a class to know how to do its work.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So we need to \u201cpass\u201d the right \u201cfunction\u201d to the flipper so it can call them without having to know anything more than the two delegates.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So how do we do that ?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Well &#8230; addressof CREATES an instance of a delegate<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So that seems like we need to use that somehow<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We want the actual management of the flag to be in our original class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So the methods to set and clear the flag should be part of that class (and it really doesn\u2019t matter if this is a class\/layout\/ etc) Lets suppose we were originally using a Window &#8211; so we\u2019ll add code to the Window<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In my example so far things look like<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Window Window1\n\n  Event Open\n    foo\n  End Event\n\n  Method foo\n    if flag then return\n\n    dim flipper as new FlagFlipper( ??????????? )\n\n    foo \/\/ to make it so we can see the \n        \/\/ second call to foo is blocked\n\n  End Method\n\n  Private Property flag as boolan\n\nEnd Window<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And as stated we\u2019re going to add two methods &#8211; setflag and clear flag<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now things look like<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Window Window1\n\n  Event Open\n    foo\n  End Event\n\n  Sub foo\n    if flag then return\n\n    dim flipper as new FlagFlipper( ??????????? )\n\n    foo \/\/ to make it so we can see the \n        \/\/ second call to foo is blocked\n\n  End Sub\n  \n  Private Sub SetFlag()\n    flag = true\n  End Sub\n  Private Sub ClearFlag()\n    flag = false\n  End Sub\n\n  Private Property flag as boolan\n\nEnd Window<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now how do we have to make the Constructor &amp; set up of the instance look so the right setflag \/ clearflag methods are called ?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Well &#8230; delegates &amp; address of !<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If we make the constructor look like the following&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Class FlagFlipper\n  Constructor( setFlagMethod as SetFlag, \n                 clearFlagMethod as ClearFlag )\n    \/\/ flip the flag in the target class somehow\n    setFlagMethod.Invoke\n    mSetFlag = setFlagMethod\n    mClearFlag = clearFlagMethod \n\n  End Constructor\n\n  Destructor()\n    \/\/ flip the flag in the target class somehow\n\n  End Destructor\n\n  Delegate SetFlag()\n  Delegate ClearFlag()\n\n  Private mSetFlag as SetFlag\n  Private mClearFlag as ClearFlag \n\nEnd Class<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Note that it holds on to a reference to the method to call to set or clear the flag<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And the set up in the method that originally needed the flag looks like<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Window Window1\n\n  Event Open\n    foo\n  End Event\n\n  Sub foo\n    if flag then return\n\n    dim flipper as new FlagFlipper( AddressOf SetFlag,\n                      AddressOf ClearFlag )\n\n    foo \/\/ to make it so we can see the \n        \/\/ second call to foo is blocked\n\n  End Sub\n  \n  Private Sub SetFlag()\n    flag = true\n  End Sub\n\n  Private Sub ClearFlag()\n    flag = false\n  End Sub\n\n  Private Property flag as boolan\n\nEnd Window<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And now, by using an object that when it goes out of scope automatically cleans up after itself we\u2019ll no longer have to worry about \u201cdid I clear that flag ?\u201d<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here\u2019s the <a href=\"http:\/\/great-white-software.com\/blog_code\/Flipper.xojo_binary_project.zip\">completed project<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lots of time you see code like In a simple method\/event with only a handful of lines there\u2019s probably nothing wrong with this But what do you do when the method spans into hundreds or thousands of lines ? And if you use the philosophy of \u201cbail early\u201d ? Then your methods\/events start to look &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.great-white-software.com\/blog\/2019\/05\/28\/code-for-safety\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Code for safety&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[1],"tags":[2],"class_list":["post-12","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-safe-code"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/12","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/comments?post=12"}],"version-history":[{"count":4,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/12\/revisions"}],"predecessor-version":[{"id":157,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/12\/revisions\/157"}],"wp:attachment":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/media?parent=12"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/categories?post=12"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/tags?post=12"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}