{"id":321,"date":"2010-05-13T10:16:01","date_gmt":"2010-05-13T15:16:01","guid":{"rendered":"http:\/\/www.thaylin.com\/blog\/?p=321"},"modified":"2010-05-13T10:16:53","modified_gmt":"2010-05-13T15:16:53","slug":"nice-tip","status":"publish","type":"post","link":"https:\/\/www.thaylin.com\/blog\/2010\/05\/13\/nice-tip\/","title":{"rendered":"Nice conditional coding tip"},"content":{"rendered":"<p>Tyler from FCNY turned me on to a nice tip on conditional coding.<\/p>\n<p>Let&#8217;s take for example we have a variable that we need to associate with another variable. For instance, we pull in some flashvars and need to check if these exist and if not give them a default value. Let&#8217;s look at a couple of the standard ways we can do this.<!--more--><\/p>\n<pre lang=\"actionscript\" line=\"1\" colla=\"+\">\r\nvar newVar:String;\r\nvar flashVar:Object;\r\nif(flashVar.foo != null)\r\n{\r\n\tnewVar = flashVar.foo;\r\n}else{\r\n\tnewVar = \"defaultFoo\";\r\n}\r\n<\/pre>\n<p>Alternatively:<\/p>\n<pre lang=\"actionscript\" line=\"1\" colla=\"+\">\r\nnewVar = (flashVar.foo != null) ? flashVar.foo : \"defaultFoo\" ;\r\n<\/pre>\n<p>both of these are perfectly valid and legitimate. The first being much more readable than the second. But what about another way?<\/p>\n<p>How about:<\/p>\n<pre lang=\"actionscript\" line=\"1\" colla=\"+\">\r\nnewVar = ( flashVar.foo || \"defaultFoo\" );\r\n<\/pre>\n<p>Nice, short and more readable, this line says if the flashVar.foo has no value then assign it the other value. However, this wouldn&#8217;t be the best option if you&#8217;re looking for numbers and a valid number would be 0 as that would return a false within the OR statement check. So use with caution.<\/p>\n<p>Thanks Tyler! <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tyler from FCNY turned me on to a nice tip on conditional coding. Let&#8217;s take for example we have a variable that we need to associate with another variable. For instance, we pull in some flashvars and need to check if these exist and if not give them a default value. Let&#8217;s look at a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/posts\/321"}],"collection":[{"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/comments?post=321"}],"version-history":[{"count":6,"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/posts\/321\/revisions"}],"predecessor-version":[{"id":327,"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/posts\/321\/revisions\/327"}],"wp:attachment":[{"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/media?parent=321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/categories?post=321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/tags?post=321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}