<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="https://www.w3.org/2005/Atom" xmlns:sy="https://purl.org/rss/1.0/modules/syndication/" xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:wfw="https://wellformedweb.org/CommentAPI/" xmlns:slash="https://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>Tyler Fang's Blog</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/tfang/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/tfang/feed.xml</link>
    <description>Everything on JavaScript (except a couple things)</description>
    <lastBuildDate>Wed, 17 Apr 2019 04:05:37 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>[JavaScript] Is Function.prototype.bind() a bad practice?</title>
      <link>https://docs.microsoft.com/archive/blogs/tfang/javascript-is-function-prototype-bind-a-bad-practice</link>
      <pubDate>Fri, 06 Jun 2014 12:04:00 GMT</pubDate>
      <dc:creator><![CDATA[Tyler Fang]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/tfang/2014/06/06/javascript-is-function-prototype-bind-a-bad-practice/</guid>
      <description><![CDATA[There are many articles out there explaining what Function.prototype.bind() does for us, but it...]]></description>
      <content:encoded><![CDATA[<p>There are many articles out there explaining what Function.prototype.bind() does for us, but it seems few of them bothered explaining what happened behind the scene for the bind() function. Chances are - if you work on JavaScript - you probably have been using bind() all around in your code. When bind() is called on a function, it returns a new function with same implementation and "this" points to the object of your choice, but is that it?</p>
<p>bind() is massively used in many JavaScript projects, which is not surprising, given the magic it offers. A recent scan through the source code I work on indicated more than 2300 of them. The large scale of usage raise my concerns about what impact this massive usage might bring - especially on performance and memory footprint. Clearly, I was not alone: Patrick Mueller wrote this great article specifically about the matter:&nbsp;<a title="https://pmuellr.blogspot.com/2010/06/bind-considered-harmful.html" href="https://blogs.msdn.com/controlpanel/blogs/posteditor.aspx/bind() considered harmful">bind() considered harmful</a>.</p>
<p>Something we should always keep in mind is that bind() returns a new function, as Patrick mentioned, which is basically separated from the original one (except that they share the implementation). Instead of the garbage it might create (frankly I think it's unlikely that someone would write code to bind() functions in a loop), I'd mention that you should be careful when distinguishing both copies and which one you should use, especially when it comes to add/remove event listeners. Given the easy-going nature of JavaScript, you will only realize you used the wrong one at run time.</p>
<p>An interesting fact about bind() is that it could do a bit more than just binding an object to "this", it also accepts a list of arguments which you could pass to the function you call bind() on. The interesting part is how few this feature has been used - out of 2300+ uses of bind() in our code, less than 50 of them were accompanied with optional arguments. And you could easily see why - just by looking at the <a title="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind?redirectlocale=en-US&amp;redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FFunction%2Fbind" href="https://blogs.msdn.com/controlpanel/blogs/posteditor.aspx/example">example </a>on MDN:</p>
<pre class="scroll"><code class="js">function list() {<br /> return Array.prototype.slice.call(arguments);<br /> }<br /> <br /> var list1 = list(1, 2, 3); // [1, 2, 3]<br /> <br /> // Create a function with a preset leading argument<br /> var leadingThirtysevenList = list.bind(undefined, 37); // this could easily creates problem if bound function is not properly named<br /> <br /> var list2 = leadingThirtysevenList(); // [37]<br /> var list3 = leadingThirtysevenList(1, 2, 3); // [37, 1, 2, 3]</code></pre>
<p>If you failed to maintain a good naming convention on the bound functions, it could soon become error-prone. The problem is that the clients of this bound function might not have knowledge on what arguments it already travels with. In fact, many developers just assign the bound function back to the original function itself, since they really don't needs an object that only holds the implementation. Just as Patrick mentioned in his article - this could easily gets in the way of debugging.&nbsp;</p>
<p><span style="font-size: medium;"><strong>Is var that = this a better solution?</strong></span></p>
<p>Obviously, we can't get rid of bind(), regardless it's actual harmful or not. JavaScript utilize callback (or eventhandler, whichever way you call it) far more than a lot of other languages. And due to the dynamic binding of JavaScript, you probably can't live without it. But there is a simple (and elegant, in my opinion) alternative though - you could use the pattern of defining a local variable that saves off the mighty "this" object in the scope. And naturally it will be available to any callbacks you defined via closure. "that" seems to be a popular choice of name for this matter, I've seen other names such as "context" or "scope" used in this matter as well.</p>
<p>In theory this should be better than using bind(), assuming your sole purpose is to provide access to this in the callback (as mentioned above, this takes up most part of bind()'s usage). Since it performs a simple variable assignment, and potentially bypassed some logic that bind() might have (eg. analyzing arguments). I should mention this is just an educated guess from me, but doing a simple test on jsPerf seems to support it: <a title="https://jsperf.com/bind-vs-that/3" href="https://blogs.msdn.com/controlpanel/blogs/posteditor.aspx/bind-vs-that">bind-vs-that</a>.</p>
<p>Your thoughts? Feel free to let me know by leaving a comment!</p>]]></content:encoded>
    </item>
    <item>
      <title>[C#] Properly Re-throwing Exceptions in C# (or .NET to be General)</title>
      <link>https://docs.microsoft.com/archive/blogs/tfang/c-properly-re-throwing-exceptions-in-c-or-net-to-be-general</link>
      <pubDate>Fri, 14 Sep 2012 15:59:16 GMT</pubDate>
      <dc:creator><![CDATA[Tyler Fang]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/tfang/2012/09/14/c-properly-re-throwing-exceptions-in-c-or-net-to-be-general/</guid>
      <description><![CDATA[If you've spent almost your entire college with Java and recently become a newbie to .NET and C#...]]></description>
      <content:encoded><![CDATA[<p>If you've spent almost your entire college with Java and recently become a newbie to .NET and C# like me, you will probably the same question I asked myself today sooner or later (better sooner...): How to properly re-throw an exception in C#, and is it different from Java?</p>
<p>In fact, despite that many similarities between Java and C# as managed languages, how to re-throw exceptions makes a profound difference between them.</p>
<p>As you might remember, in Java, re-throwing a exception can be easily done&nbsp;by:</p>
<pre class="scroll"><code class="java">try{<br /> ... // code that may throw exception<br /> } catch (Exception e) {<br /> <span style="background-color: #ffff00;">throw e;</span><br /> }</code></pre>
<p>The only line you need to look at is "throw e;", which is really not very interesting in the context of Java, but works entirely differently in C# if you write the same line:</p>
<pre class="scroll"><code class="csharp">try<br /> { <br /> ... // code that may throw exception <br /> } <br /> catch (Exception e) <br /> { <br /> <span style="background-color: #ffff00;">throw e;</span> <br /> }</code></pre>
<p>The difference is:&nbsp;In Java, when you do "throw e;", you are&nbsp;essentially re-throwing that&nbsp;exception "as-is", which means&nbsp;the original stack trace -&nbsp;the most important&nbsp;thing here -&nbsp;is preserved. While in C# (or VB and other .NET languages I assume), the same syntax will cause the stack trace to be overridden by the line that re-throw the exception, which is very unlikely what you want.</p>
<p>So, to answer the question of "How to properly re-throw exceptions in C#", you have two options:</p>
<p>1. Instead of "throw e;" just use "throw;", which works like a charm.</p>
<p>2. Create a new exception with the information of old one, such as "new newExp ( "Exception happened when I do bla-bla-bla", e )". This is usually more suitable for the case when you want to add some additional information about the error.</p>
<p>Hope this will help some other new C#ers as well! :)</p>
<p>&nbsp;</p>
<p>One more thing, if you've been wondering what to do with an exception (especially those unchecked ones), here's a blogpost on "Coding Horror" by Jeff Atwood which contains a handy guideline for exception handling: <a href="https://www.codinghorror.com/blog/2004/07/rethrowing-exceptions.html">https://www.codinghorror.com/blog/2004/07/rethrowing-exceptions.html</a>&nbsp;</p>
<p>* But note that author made a mistake&nbsp;about the&nbsp;difference I talked above and he clarified later in the comment. Don't be tricked :P&nbsp;&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<pre class="scroll"><code class="csharp"></code>&nbsp;</pre>]]></content:encoded>
    </item>
    <item>
      <title>[Fiddler] Fix "The proxy server isn't responding" Problem Caused by Fiddler</title>
      <link>https://docs.microsoft.com/archive/blogs/tfang/fiddler-fix-the-proxy-server-isnt-responding-problem-caused-by-fiddler</link>
      <pubDate>Wed, 18 Jul 2012 15:20:00 GMT</pubDate>
      <dc:creator><![CDATA[Tyler Fang]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/tfang/2012/07/18/fiddler-fix-the-proxy-server-isnt-responding-problem-caused-by-fiddler/</guid>
      <description><![CDATA[Very occasionaly I ran into the problem of losing network connection after using Fiddler for...]]></description>
      <content:encoded><![CDATA[<p>Very occasionaly I ran into the problem of losing network connection after using Fiddler for debugging with web traffic. To be more precise, this problem usually happens after closing the Fiddler application, and it seems more reporducible when using the FiddlerCore API(However, this is very likely because&nbsp;my code&nbsp;didn't&nbsp;release Fiddler&nbsp;in the correct way...) After this problem happens, and when I open IE, I see the error message of "The proxy server isn't responding" as follow:</p>
<p>This is caused by, as far as I see it, that Fiddler sometimes fails to unregister itself as the system proxy upon on quitting. In that case you will be redirecting all your HTTP traffic to a proxy that doesn't exist, and thus gives you the error shown above.</p>
<p>Easy way to fix this, is to reluanch Fiddler application which will act as system proxy again (make sure you set the option in "Fiddler Options" - "WinINET Connetions"). Close the application gracefully and it should fix the connetion path for you.</p>
<p>Or, you could manually clear the proxy settings by going to Internet Options, select "LAN setting" in "Connections" tab, and uncheck the option of "Use a proxy server for your LAN". (In which you should see a proxy like 127.0.0.1:8888, which is the default listening port for Fiddler.)</p>
<p></p>
<p></p>
<p></p>]]></content:encoded>
    </item>
    <item>
      <title>[Windows] Win8 Failed Booting after Installation, Stuck on "Getting Devices Ready"</title>
      <link>https://docs.microsoft.com/archive/blogs/tfang/windows-win8-failed-booting-after-installation-stuck-on-getting-devices-ready</link>
      <pubDate>Sun, 08 Jul 2012 17:54:17 GMT</pubDate>
      <dc:creator><![CDATA[Tyler Fang]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/tfang/2012/07/08/windows-win8-failed-booting-after-installation-stuck-on-getting-devices-ready/</guid>
      <description><![CDATA[Recently I ran into this problem after installed a Windows 8 RC build on my test machine. The...]]></description>
      <content:encoded><![CDATA[<p>Recently I ran into this problem after installed a Windows 8 RC build on my test machine. The instaillation ran though without a problem, however,&nbsp;when the machine rebooted afer&nbsp;the installaition (when it supposed to load up the welcome message and prompt you for a computer name), it gave a message of "Getting Devices Ready", and stopped at&nbsp;some random percentages. The next&nbsp;thing happened&nbsp;was that&nbsp;I always ended up with black screen after&nbsp;rebooting.</p>
<p>At some&nbsp;points I was almost&nbsp;certian that this is&nbsp;a hardware problem, as I tried re-installing repeatedly and the problem resisted. However,&nbsp;the actual problem was caused by connecting my&nbsp;machine to two monitors. The root&nbsp;cause was clear: the driver for the graphic card got flushed during the re-installation of WIndows, and thus it won't know how to support dual-display. Apparently Windows 8 doesn't handle&nbsp;this problem gracefully but rather dumping a black screen.</p>
<p>So,&nbsp;if you run into the same problem,&nbsp;it's most likely&nbsp;because you computer was connected to some external device that it doesn't suppport(at that sepcial point). You should disconnect the&nbsp;devices while leaving only one monitor. It should solve the problem for you.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>]]></content:encoded>
    </item>
    <item>
      <title>[C#] Two Easy Tricks to Play with Byte Array Using Buffer.BlockCopy</title>
      <link>https://docs.microsoft.com/archive/blogs/tfang/c-two-easy-tricks-to-play-with-byte-array-using-buffer-blockcopy</link>
      <pubDate>Wed, 04 Jul 2012 18:40:35 GMT</pubDate>
      <dc:creator><![CDATA[Tyler Fang]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/tfang/2012/07/04/c-two-easy-tricks-to-play-with-byte-array-using-buffer-blockcopy/</guid>
      <description><![CDATA[A lot of times when I have to feed some APIs with byte array(byte[]), I got myself...]]></description>
      <content:encoded><![CDATA[<p>A lot of times when I have to feed some APIs with byte array(byte[]), I got myself confused&nbsp;during converting what I have in hand(usually a string or even, a string array) to what I want it to be. There are&nbsp;certainly a lot of ways you could conduct this convention, and most of them works fairly well in terms of performance and space consumption. While putting these two criteria's aside, here're&nbsp;two simple and self-explained helper methods that will do the trick:</p>
<pre class="scroll"><code class="csharp">static byte[] ConvertToBytes(string inputString)<br /> {<br /> byte[] outputBytes = new byte[inputString.Length * sizeof(char)];<br /> Buffer.BlockCopy(inputString.ToCharArray(), 0, outputBytes, 0, outputBytes.Length);<br /> return outputBytes;<br /> }</code></pre>
<p>In some other cases when you want to concatenate two byte arrays, while you don't want to bother doing the convention between strings. Here's a easy way of doing it, which also employs Buffer.BlockCopy. Note that it appends the Byte[] arrayB to the end of Byte[] arrayA:</p>
<pre class="scroll"><code class="csharp">static byte[] AppendTwoByteArrays(byte[] arrayA, byte[] arrayB)<br /> {<br /> byte[] outputBytes = new byte[arrayA.Length + arrayB.Length];<br /> Buffer.BlockCopy(arrayA, 0, outputBytes, 0, arrayA.Length);<br /> Buffer.BlockCopy(arrayB, 0, outputBytes, arrayA.Length, arrayB.Length);<br /> return outputBytes;<br /> }</code></pre>
<p>&nbsp;&nbsp;</p>]]></content:encoded>
    </item>
    <item>
      <title>[C#] A quick (and easy) way to deserialize a XML file into List&lt;T&gt;</title>
      <link>https://docs.microsoft.com/archive/blogs/tfang/c-a-quick-and-easy-way-to-deserialize-a-xml-file-into-listt</link>
      <pubDate>Mon, 02 Jul 2012 19:00:00 GMT</pubDate>
      <dc:creator><![CDATA[Tyler Fang]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/tfang/2012/07/02/c-a-quick-and-easy-way-to-deserialize-a-xml-file-into-listt/</guid>
      <description><![CDATA[I spent more time than I expected to find a easy and intuitive way to quickly transform the data...]]></description>
      <content:encoded><![CDATA[<p>I spent more time than I expected to find a easy and intuitive way to quickly transform the data contained by a&nbsp;XML file into a List object in C#. And here it is:</p>
<p>&nbsp;</p>
<p>Suppose you have a XML file which looks like following:</p>
<pre class="scroll"><code class="html">&lt;?xml version="1.0" encoding="utf-8" ?&gt;<br /> &lt;list&gt;<br /> &lt;title&gt;FeaturedMovies&lt;/title&gt;<br /> &lt;movie&gt;<br /> &lt;name&gt;Grey&lt;/name&gt;<br /> &lt;/movie&gt;<br /> &lt;movie&gt;<br /> &lt;name&gt;Haywire&lt;/name&gt;<br /> &lt;/movie&gt;<br /> &lt;movie&gt;<br /> &lt;name&gt;Harry Potter&lt;/name&gt;<br /> &lt;/movie&gt;<br /> &lt;/list&gt;</code></pre>
<p>Ideally after parsing, we should have a list of objects which simply contains a string filed, the "name". There are no attributes involved in this file, just for the purpose of demonstrating the simplest case. Here's the code that will properly decode it:</p>
<pre class="scroll"><code class="csharp"> using System;<br /> using System.Xml;<br /> using System.Collections.Generic;</code></pre>
<pre class="scroll"><code class="csharp"> class Movie<br /> {<br /> public string name;<br /> public string searchToken;<br /> public string guid;<br /> }</code><code class="csharp"><br /> static void Main(string[] args)<br /> {<br /> XmlTextReader textReader = new XmlTextReader("C:\\MoviesList.xml");<br /> <br /> List&lt;Movie&gt; movieList = new List&lt;Movie&gt;();<br /> <br /> while (textReader.Read())<br /> {<br /> Movie movie = new Movie();<br /> <br /> if (textReader.MoveToContent() == XmlNodeType.Element &amp;&amp; textReader.Name == "name")<br /> movie.name = textReader.ReadElementString();<br /> if (textReader.MoveToContent() == XmlNodeType.Element &amp;&amp; textReader.Name == "searchToken")<br /> movie.searchToken = textReader.ReadElementString();<br /> <br /> if (!String.IsNullOrEmpty(movie.name))<br /> movieList.Add(movie);<br /> }<br /> }<br /> </code></pre>
<p>You might notice that none of the movies in the XML actually have a "searchToken", while this will still work fine. But since we want the "name" to be mandatory, we do check for it before adding that into the list.</p>
<p>&nbsp;</p>
<p>Hope this will save you sometime! :)</p>]]></content:encoded>
    </item>
  </channel>
</rss>