{"id":17,"date":"2009-01-20T15:44:27","date_gmt":"2009-01-20T20:44:27","guid":{"rendered":"http:\/\/blog.bitsofgenius.com\/?p=17"},"modified":"2011-01-26T12:39:28","modified_gmt":"2011-01-26T16:39:28","slug":"when-an-empty-string-is-really-an-empty-string","status":"publish","type":"post","link":"https:\/\/blog.bitsofgenius.com\/?p=17","title":{"rendered":"When an empty string is really an empty string&#8230;"},"content":{"rendered":"<p>Sometimes clarity of thinking during late night programming is elusive&#8230;<\/p>\n<pre lang=\"charp\" line=\"1\">\r\n(footerMessage.Trim().Length == 0 ? string.Empty : footerMessage + \"\\r\\n\\r\\n\")\r\n<\/pre>\n<p>There are two bloopers in this one line of code:<\/p>\n<ul>\n<li>a string with nothing but spaces really is an empty string (for practical reasons).<\/li>\n<li>it would be nice to put two line breaks on the empty string as well as the non-empty string.<\/li>\n<\/ul>\n<p>So file this one under redundant, i.e. A practical dictionary entry for Redundant would say &#8220;See Redundant&#8221;<\/p>\n<p>Note: if your not familiar with this construct, it is a carry over from the C-language.\u00a0 It simplifies an if statement, when the goal is to return one of two values to a variable, depending on the result of a boolean test.\u00a0 These quick tests can be quite elaborate, but are most effective for generating max, min, floor (etc, etc) functions where the evaluation is short.<\/p>\n<pre lang=\"csharp\" line=\"1\" escaped=\"true\">\r\n\r\n\/* usual evaluation *\/\r\n\r\nint Productivity_Percent = 90;\r\n\r\nfloat AnnualPay = 35000;\r\n\r\nif (Productivity_Percent &gt; 85) Annual_Pay = 50000;\r\n\r\n\/* ... or ... *\/\r\n\r\nint Productivity_Percent = 90;\r\n\r\nfloat AnnualPay = Productivity_Percent &gt; 85 ? 50000 : 35000;\r\n\r\n<\/pre>\n<p>The immediate eval is not always efficient.\u00a0 Usually the code must evaluate both possible answers, before evaluating which one to return&#8211;which can waste time on unneeded work.  And in the case above, the if statement is actually slightly more efficient than the immediate eval.<\/p>\n<p>It can be very useful for an inline lookup when initializing a value, instead of using a case statement.\u00a0 E.g.<\/p>\n<pre lang=\"csharp\" line=\"1\">\r\npublic void Initialize (string Method)\r\n{\r\n   int PortValue = (Method == \"Full Power\", 37, (Method == \"Half Power\", 45, 7));\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes clarity of thinking during late night programming is elusive&#8230; (footerMessage.Trim().Length == 0 ? string.Empty : footerMessage + &#8220;\\r\\n\\r\\n&#8221;) There are two bloopers in this one line of code: a string with nothing but spaces really is an empty string (for practical reasons). it would be nice to put two line breaks on the empty [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"class_list":["post-17","post","type-post","status-publish","format-standard","hentry","category-code-bloopers"],"_links":{"self":[{"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=\/wp\/v2\/posts\/17","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=17"}],"version-history":[{"count":1,"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=\/wp\/v2\/posts\/17\/revisions"}],"predecessor-version":[{"id":414,"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=\/wp\/v2\/posts\/17\/revisions\/414"}],"wp:attachment":[{"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=17"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=17"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=17"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}