{"id":1021,"date":"2021-01-05T16:40:00","date_gmt":"2021-01-05T23:40:00","guid":{"rendered":"https:\/\/www.great-white-software.com\/blog\/?p=1021"},"modified":"2021-01-05T18:45:58","modified_gmt":"2021-01-06T01:45:58","slug":"practices-you-may-see-in-use-but-youll-regret-using","status":"publish","type":"post","link":"https:\/\/www.great-white-software.com\/blog\/2021\/01\/05\/practices-you-may-see-in-use-but-youll-regret-using\/","title":{"rendered":"Practices you may see in use but you&#8217;ll regret using"},"content":{"rendered":"\n<p>Some code looks enticing &#8211; but in the long run may cause you as many headaches as it solves.<\/p>\n\n\n\n<p>Some is really innocuous looking like :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var position as integer = otherString.IndexOf(EndOfLine)\nvar leftChars as string = otherString.left(position)\nvar rest as string = otherString.Middle(position + 1)<\/code><\/pre>\n\n\n\n<p>There&#8217;s an assumption in this little bit of code and it may not be entirely obvious what it is.<\/p>\n\n\n\n<p>Try it on a mac and its probably fine. And on Windows it makes a mess. The reason is the EndOfLine marker on Windows is of course two characters not 1 and so the assignment of rest is wrong and off by 1.<\/p>\n\n\n\n<p>The fix is to simply use the Length of EndOfLine instead of 1. Note that autocomplete dislikes EndOfLine.Length and it wont compile forcing you to use a temporary variable for this. <a href=\"http:\/\/feedback.xojo.com\/case\/63211\">see this bug report <\/a> and you appear to not be able to extend it since there&#8217;s a global method and class with the same name in the framework. <\/p>\n\n\n\n<p>The following works but you end up declaring a variable to hold the EOL char(s).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var position as integer = otherString.IndexOf(EndOfLine)\nvar leftChars as string = otherString.left(position)\nvar eol as String = EndOfLine\nvar rest as string = otherString.Middle(position + eol.length)<\/code><\/pre>\n\n\n\n<p>Another that I&#8217;ve run into is TextInputStream.ReadLine. The issue here is that if you assume ReadLine reads &#8220;one line&#8221; then you can be in for a surprise since &#8220;one line&#8221; depends on what line endings are in use in the file.<\/p>\n\n\n\n<p>If you&#8217;re on macOS reading lines from a file from Windows then the lines will include half of the Windows end of line marker (which is 2 bytes &#8211; &amp;h0D + &amp;h0A)<\/p>\n\n\n\n<p>And TextinputStream has no mechanism built in to say &#8220;please translate line endings as you read&#8221; which would be really handy. So you either read all the text in (hoping its not GB of data) and use ReplaceLineEndings, or read &#8220;line by line&#8221; and clean them up. Or write a new Class that wraps a binary stream that does allow you to specify both an encoding and whether line endings should be converted to the platform specific ones. I chose the latter route. I&#8217;ll see about publishing my class on my Github<\/p>\n\n\n\n<p>Any others you&#8217;ve run into that look simple and clear but are actually problematic ?<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some code looks enticing &#8211; but in the long run may cause you as many headaches as it solves. Some is really innocuous looking like : There&#8217;s an assumption in this little bit of code and it may not be entirely obvious what it is. Try it on a mac and its probably fine. And &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.great-white-software.com\/blog\/2021\/01\/05\/practices-you-may-see-in-use-but-youll-regret-using\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Practices you may see in use but you&#8217;ll regret using&#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_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1021","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/1021","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=1021"}],"version-history":[{"count":4,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/1021\/revisions"}],"predecessor-version":[{"id":1031,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/1021\/revisions\/1031"}],"wp:attachment":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/media?parent=1021"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/categories?post=1021"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/tags?post=1021"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}