<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[The ⓝⓞⓞⓑ Code Chronicles - Medium]]></title>
        <description><![CDATA[Insights on code, coding pain and coding pleasure from n00bs. Full of non expert language, thoughts and examples. That does not mean they are incorrect — we have a curation guy with a PhD in Computer Science. - Medium]]></description>
        <link>https://medium.com/the-n00b-code-chronicles?source=rss----e5b2de76615f---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>The ⓝⓞⓞⓑ Code Chronicles - Medium</title>
            <link>https://medium.com/the-n00b-code-chronicles?source=rss----e5b2de76615f---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 04:21:38 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/the-n00b-code-chronicles" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Avoiding common errors during SageMaker deployment]]></title>
            <link>https://medium.com/the-n00b-code-chronicles/avoiding-common-errors-during-sagemaker-deployment-598453e81cb1?source=rss----e5b2de76615f---4</link>
            <guid isPermaLink="false">https://medium.com/p/598453e81cb1</guid>
            <category><![CDATA[aws-lambda]]></category>
            <category><![CDATA[sentiment-analysis]]></category>
            <category><![CDATA[lambda-function]]></category>
            <category><![CDATA[sagemaker]]></category>
            <category><![CDATA[udacity]]></category>
            <dc:creator><![CDATA[Tanya Gupta]]></dc:creator>
            <pubDate>Sun, 07 Jun 2020 16:02:47 GMT</pubDate>
            <atom:updated>2021-01-18T14:49:17.471Z</atom:updated>
            <content:encoded><![CDATA[<h3>Avoiding common SageMaker errors: Udacity’s Deep Learning Nanodegree Sentiment Analysis Project</h3><p><strong><em>Disclaimer: Consistent with the honor code, no solutions are provided here, only hints to help troubleshoot two common errors</em></strong></p><p>While working on the Deep Learning capstone “Deploying a Sentiment Analysis Model” project, most of my time was spent troubleshooting two common errors. They were <em>ResourceLimitExceeded </em>error and a CORS error of <em>CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.</em></p><h3>Resource Limit Error</h3><p>The error I got was <em>“ResourceLimitExceeded: An error occurred (ResourceLimitExceeded) when calling the CreateTrainingJob operation: The account-level service limit ‘ml.p2.xlarge for training job usage”</em></p><p><strong>Fix</strong></p><p>You need to apply for a separate instance for training and deployment. <a href="https://udacity.zendesk.com/hc/en-us/articles/360037696812-How-do-I-request-a-limit-increase-for-my-p2-xlarge-instance-">This</a> Udacity resource suggests it does not matter whether you choose SageMaker Training or SageMaker Deployment</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/732/1*wzS6Ve2r53eSiKCYT6KJXA.png" /></figure><p>It DOES matter. If you are doing the Udacity exercise for training (as below) you need to request the SageMaker Training Resource Type and not SageMaker Hosting.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/954/1*QQKyIU3La0AgJNw1J3FNZA.png" /></figure><p>The training and hosting(deployment) instances have to be requested separately even if it’s the same instance, for example, ml.m4.xlarge, you need to request twice and specify training and hosting.</p><p>My own error occurred because I had requested SageMaker Hosting and was trying to use it for training. After contacting AWS and consulting <a href="https://github.com/awslabs/amazon-sagemaker-examples/issues/307">online resources</a> I was able to resolve it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/987/1*cvCIy4Q-Ll8z19raGxq4hA.png" /></figure><h3>CORS error</h3><p>The error I got was <em>AWS sagemaker error has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. </em>However the error is NOT because we need to change a CORS setting</p><p><strong>Potential Fixes and hints:</strong></p><ol><li>For <em>Step 7 (again): Use the model for the web app </em>complete *all* the steps at one go, as many of the steps that follow interdependent</li><li>The endpoint needs to be deployed and running, if it is not running or not deployed, it will not work</li><li>The method used to execute the Lambda function is the endpoint created using API Gateway. This endpoint will be a URL that listens for data to be sent to it. So if the URL provided is not correct, an error is generated. Make sure you have the right endpoint in the lambda code. This was what was causing my error.</li></ol><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F52c8qYZj6Cc%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D52c8qYZj6Cc&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F52c8qYZj6Cc%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/ccbdab507c69d87df1fee2f051526ee2/href">https://medium.com/media/ccbdab507c69d87df1fee2f051526ee2/href</a></iframe><h3>Additional Resources</h3><ul><li><a href="http://francescopochetti.com/is-this-movie-a-thrillerinvoking-a-sagemaker-deep-learning-model-in-an-end-to-end-serverless-web-application/">Is this movie a thriller</a>? Exposing a SageMaker Deep Learning Model in an end to end serverless web application</li><li><a href="https://github.com/awslabs/amazon-sagemaker-examples/issues/307">Resource limit when deploying</a></li></ul><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FcPZgR4gSyXwzCVBCCz%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fsagemaker-amazon-cPZgR4gSyXwzCVBCCz&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/dcec5c3939972d824eb26f8b6f2669bd/href">https://medium.com/media/dcec5c3939972d824eb26f8b6f2669bd/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=598453e81cb1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-n00b-code-chronicles/avoiding-common-errors-during-sagemaker-deployment-598453e81cb1">Avoiding common errors during SageMaker deployment</a> was originally published in <a href="https://medium.com/the-n00b-code-chronicles">The ⓝⓞⓞⓑ Code Chronicles</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The 5 super powers of ES6]]></title>
            <link>https://medium.com/the-n00b-code-chronicles/the-5-super-powers-of-es6-84e62875eed6?source=rss----e5b2de76615f---4</link>
            <guid isPermaLink="false">https://medium.com/p/84e62875eed6</guid>
            <category><![CDATA[udacity]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[google]]></category>
            <category><![CDATA[es6]]></category>
            <dc:creator><![CDATA[Tanya Gupta]]></dc:creator>
            <pubDate>Fri, 16 Mar 2018 23:57:04 GMT</pubDate>
            <atom:updated>2018-03-16T23:57:03.991Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*tyWFEAMlSCY7o9nndoykUg.png" /></figure><p>This blog is about new ES6 features. Learning about ES6 is probably pretty straightforward for experienced folks. It wasn’t for me. As a part of <a href="https://www.udacity.com/grow-with-google">Udacity’s Grow with Google challenge</a> cohort where ES6 was covered, I see that I wasn’t alone. So here are 5 new ES6 features written by a newbie for other newbies.</p><h3>Default function parameters</h3><p><strong>Before we start:</strong> You know what a function and a parameter is</p><p><strong>What it does</strong> The default function parameter allows you to set a default value for your parameter(s)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*wGM9VDPLonSNxvpuD3wtog.jpeg" /></figure><p><strong>Why is it useful ? </strong>Not all parameters are created equal. Sometimes if a parameter value is missing the code can crash badly. default allows the programmers a nice safety net.</p><p><strong>An example: </strong>Say you are writing a function that will perform a division by the value of one of its parameters. To avoid a division by zero you will have to do some detailed error checking.</p><pre>function divide_old(a, b) {<br>  /* We have to make sure we have the right number of arguments and their values are not 0. We are simplifying here and assuming we are dealing only with positive numbers<br>*/<br>  if (arguments.length===2 &amp;&amp; arguments[0]&gt;0 &amp;&amp; arguments[1]&gt;0){<br>    return arguments[0]/arguments[1]<br>  }<br>  else if(arguments.length===1 &amp;&amp; arguments[0]&gt;0){<br>    return arguments[0]/1<br>  }<br>}</pre><p>With the default value for the parameters in ES6 you can now write code as follows</p><pre>function divide(a, b = 1) {<br>  return a / b;<br>}</pre><p>This makes life a whole lot easier for both the user/caller of a function, who does not have to worry and of course for the function author.</p><p>For more see the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters">MDN page</a></p><h3>Fetch</h3><p><strong>Before we start: </strong>you need to know what is a call to a REST API means and what JSON data is, and what JSON data processing is.</p><p><strong>What it does: </strong>Provides programmers with a really simple way to make calls to a REST API</p><p><strong>Why is it useful: </strong>REST APIs give you access to databases with rich information that your program can use. Before ES6 the most common ways of doing this was to use a third party library, with the most popular being jQuery’s <a href="http://api.jquery.com/jquery.ajax/">ajax</a>.</p><p><strong>An example: </strong>The example shows the mechanics of a fetch call and getting some JSON data. <strong>If you have done things the old way </strong>I am sure you can immediately appreciate the benefits.</p><pre>function fetch_simple (url) {<br>  fetch(url)<br>  .then (response =&gt; {return response.json()}) // JSON parsing done for you<br>  .then(data =&gt; {<br>  	// do whatever you want to do with the data<br>  })<br>  .catch (err =&gt; {<br>    console.log(&quot;===we have an error===&quot;)<br>    console.log(err.stack)<br>  })<br>}</pre><p>For more see the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API">MDN page</a></p><h3>Sets</h3><p><strong>Before we start:</strong> You need to know arrays, and objects</p><p><strong>What it does:</strong> It’s an iterable collection of unique elements. However this is not about what it is, but rather what it can help you do.</p><p><strong>Why it is useful: </strong>You can very quickly remove duplicates from existing arrays.</p><h4>An example (and more):</h4><p>To remove duplicates just create a set from an array and then convert that set back to an array.</p><pre>const nums_with_duplicates = [1,5,7,8,5,8,23,45];</pre><pre>a_set = new Set(nums_with_duplicates);</pre><pre>const nums_unique = [a_set]// the [] changes the set back to an array </pre><p>While both objects and the new ES6 Map also provide the support for unique elements, Set preserves the iteration order of its elements. So when we need a collection where the order of iteration <em>and</em> uniqueness matter, Set comes upon top.</p><p>The pattern [...new Set (array_with_duplicates)] will always give you an array with the duplicates removed. Nice, right?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/513/1*VwSJ5JqmQhcP9gcmTdJ0hQ.png" /><figcaption>Basic operations with a set</figcaption></figure><p>The code to walk a set is also nice and compact</p><pre>for (const month of months) {<br>	console.log(month)<br>}</pre><p>For more see the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set">MDN page</a></p><h3>Rest parameter and Spread operator</h3><p><strong>Before we start: </strong>you need to know what arrays and parameters are. Also an iterable object</p><p><strong>What it does: </strong><br>If you have been reading about ES6 and looking at code written in it I am sure you have seen the curious ... three dots popping up everywhere. These three dots have two names and two very different meaning in ES6. The naming and the meaning depends on what section of the code they appear:</p><ul><li>... is called a<strong> <em>rest parameter</em> </strong>when used in function declaration, for example, in function foo (a, ...b){//do stuff}, the dots <em>make</em> b a rest parameter. The dots also mean that the second parameter is an array with an unspecified number of elements.</li><li>Any other place you see those dots they are <strong><em>spread operator</em>s</strong>. The spread operators most commonly appear in array literals like this: [...a, 4, 5,6] Here also the dots represent an array of unspecified number of elements.</li></ul><p>The a must be an iterable object like arrays, strings and the ES6&#39;s new Maps and Sets.</p><p><strong>Why is it useful: </strong>When used as rest parameters, the dots “pack” many variables in to an array. So in the function function foo (a, ...b){//do stuff} if we call foo with foo(5,6,9), this would mean that a is 5 and b, because of the three dots is an array[6,9]</p><p>When used as a spread operator the dots “unpack” variables. See the following function:</p><pre>function bar (a, ...b){ <br>  console.log(bar); </pre><pre>/* [6,9,10] Note it only starts from the second number because the first parameter is not part of the spread operator */</pre><pre>  console.log(...b);// 6 9 10 //notice it unpacks [6,9,10] </pre><pre> }<br>bar(5,6,9,10)</pre><p>The first three dots are rest parameters and “pack” the variables into an array. The second set of three dots “unpack” the array into the individual elements. This makes it easier to work with arrays in functions.</p><p><strong>An example</strong></p><pre>function foo (a, ...rest) {<br>  console.log(&quot;\n\nvalue of the first param is  &quot; + a)<br>  console.log(&quot;the rest of the params are in an array: &quot;)<br>  console.log(rest)<br>  console.log(&quot;unpack them to a list as follows:&quot;)<br>  console.log(...rest) <br>  return &quot;Done&quot;<br>}<br><br>foo (5,6,9,11) // a will be assigned 5 and the rest will be [6,9,11]<br>foo (5,6,9,11, 2) // a will be assigned 5 and the rest will be [6,9,11, 12]<br>foo (5) // a will be assigned 5 and the rest will be empty<br>foo (5,6,[9,11]) // will be assigned 5 and the rest will be [6,[9,11]]</pre><p>Copy and paste the above to the console to see how it works</p><p>Feel free to dig deep into many interesting <a href="https://dmitripavlutin.com/how-three-dots-changed-javascript/">usecases for rest parameters and spread operators</a>.</p><p>But as a newbie I find the following two patterns very useful:</p><blockquote><em>The spread operator make array concatenation much more natural.</em></blockquote><p>joined_array = [...first_array, ...second_array, ...third_array]</p><blockquote><em>You can combine it with ES6 Sets to remove duplicate from arrays</em></blockquote><pre>const unique = [...new Set(non_unique)];</pre><p>For more see the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax">MDN page</a> and this <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters">MDN page</a> too</p><h3>Destructuring</h3><p><strong>Before we start:</strong> You need to know sets, arrays, parameters</p><p><strong>What it does: </strong>Destructuring allows a programmer to break a compound structure like an array or an object down to its elements (or properties in case of objects) and assign those “destructured” pieces to variables. [a,b] = [1,2] is now a valid Javascript statement, which assigns 1 to a, and 2 to b.</p><p><strong>Why it is useful: </strong>It makes code simpler and short. See example for more</p><h4><strong>An example (and more):</strong></h4><p>Get rid of the pesky temp variable and three lines of code and swap variables in one shot.</p><pre>var a = 1;<br>var b = 3;<br>[a, b] = [b, a]; // <br>console.log(a); // 3<br>console.log(b); // 1</pre><h4>Parse function returns like a Pro</h4><pre>function f() {<br>  return [1, 2];<br>}</pre><pre>var a, b; <br>[a, b] = f(); <br>console.log(a); // 1<br>console.log(b); // 2</pre><h4>Remove duplicates from Arrays</h4><p>Create a duplicate free arrays in one shot. This one needs to know Sets too!</p><pre>const unique = [...new Set(array_with_duplicates)];</pre><ol><li>Create a set from the array_with_duplicates array, this will remove the duplicates</li><li>Use the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax">spread</a> syntax to unpack the set elements</li><li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment">“destructure”</a> the unpacked element back into an array</li></ol><p>For more see the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment">MDN page</a></p><h3>And finally putting everything together..</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*QpBKmH4C41FMuJS0E69xdg.jpeg" /><figcaption>Javascript takes on the big guns</figcaption></figure><p>Many languages (such as Perl) are famous for allowing programmers to express fairly complicated commands in just one line. Javascript was never known for this. With ES6 it is slowly getting there.</p><p>Consider the following array that contains the month names when the temperature dipped below a threshold.</p><p>const low_temp_months = [&#39;January&#39;,&#39;January&#39;, &#39;February&#39;, &#39;March&#39;,&#39;November&#39;, &#39;December&#39;,&#39;December&#39;];</p><p>As you notice there are some duplicates perhaps caused by multiple readings submitted for the same month. If you want the month names to be unique you can do the following:</p><ol><li>Create a set from the low_temp_months array, this will remove the duplicates</li><li>Use the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax">spread</a> syntax to unpack the set elements</li><li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment">“Destructure”</a> the unpacked element back into an array</li></ol><p>All the three aspect can be implemented in a very nice one liner!</p><pre>const winter_months = [...new Set(low_temp_months)];</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=84e62875eed6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-n00b-code-chronicles/the-5-super-powers-of-es6-84e62875eed6">The 5 super powers of ES6</a> was originally published in <a href="https://medium.com/the-n00b-code-chronicles">The ⓝⓞⓞⓑ Code Chronicles</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[My Udacity experience and the wonderful strange world of open source]]></title>
            <link>https://medium.com/the-n00b-code-chronicles/my-udacity-experience-and-the-wonderful-strange-world-of-open-source-3301464e7ec7?source=rss----e5b2de76615f---4</link>
            <guid isPermaLink="false">https://medium.com/p/3301464e7ec7</guid>
            <category><![CDATA[google]]></category>
            <category><![CDATA[udacity]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[education]]></category>
            <category><![CDATA[open-source]]></category>
            <dc:creator><![CDATA[Tanya Gupta]]></dc:creator>
            <pubDate>Fri, 16 Mar 2018 23:55:53 GMT</pubDate>
            <atom:updated>2018-03-16T23:55:53.378Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://www.udacity.com/">Udacity</a> sponsors a three month first round “<a href="https://www.udacity.com/grow-with-google">Grow with Google Challenge Scholarship</a>”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/422/1*9Epbk5AIJD1B3snK78JblA.png" /></figure><p>When I applied to the Mobile Web Specialist track I was not sure what I would get out of Udacity. I considered myself a middling self-taught web programmer. (Here’s a <a href="https://medium.com/@tanyagupta/i-dropped-my-cs-major-because-i-couldnt-do-a-bubble-sort-126e1242d86e">blog that documents my learning journey</a> and here’s the links (<a href="https://gsuite.google.com/marketplace/app/word_cloud_generator/360115564222?pann=gam">1 </a>and <a href="https://chrome.google.com/webstore/detail/word-cloud-generator/alhnlhbhnklajhmccemipdbaifocepab?authuser=0">2</a>) for my most popular app with over half million users). If I could learn anything I wanted to on my own, what would be Udacity’s value proposition for me ? So my expectations were fairly low even when I heard I got the scholarship. Fast forward three months.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*1TaF81cuT9YPOvdVEa96pQ.png" /></figure><blockquote><strong>I am blown away by the experience.</strong></blockquote><p>Why ? Two words — <strong><em>community and open source.</em></strong></p><p>I quickly became part of a rich evolving learning community.</p><h3>Four specific experiences were invaluable:</h3><ul><li>For the first time in my life I worked on not just one but two open source projects, one of which I initiated both built around maps. One was the <a href="https://github.com/TheDevPath/googleMaps-offline-navigator">Navi project</a> with a very <a href="https://github.com/TheDevPath/googleMaps-offline-navigator/graphs/contributors">large team</a> and a great lead <a href="https://github.com/motosharpley">Bryan</a> who was very helpful. Another one was <a href="https://github.com/gwg-women/gwg-women-techmakers">Mappa</a> an <a href="https://github.com/gwg-women/gwg-women-techmakers/graphs/contributors">all women</a> project, one that I initiated just with <a href="https://github.com/khusbuchandra">Khusbu</a> but quickly grew to include some very talented and hardworking <a href="https://github.com/khusbuchandra">women</a>. Mappa started off with some of the code I used for a Udacity course using knockout js. Halfway through the project we switched to React, another interesting experience of changing track midway and managing the implications on the project timeline. On Navi, I focused on making documentation easy to read and understand. In the process I became familiar with git, github, learned to read other people’s code, realized the importance of documentation and much more.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*8FZh_o2-5iik1g3PWmS40w.jpeg" /></figure><ul><li>Working with students from different programming backgrounds taught me a lot. There were absolute beginners, those with some experience, and others that with significant experience in open source and programming. With such a diversity of experience, collaboration is the only way the community moves together as a whole. The more experienced ones helped the less experienced ones, and the less experienced ones helped the more experienced by asking questions that need to be answered.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*W97RMZ6AjIn5x6_UzzrOHg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*UZBnnzA9Hv2dD82pLiHzug.png" /></figure><ul><li>A community needs tools to grow and to communicate. Udacity chose Slack and Forums. What even Udacity could not predict was how the tools would be used. The organic growth and innovative use of the tools provided (Slack channel, forums) and tools not provided (Google Hangouts, opened issues on Github) was fascinating and could probably be a PhD in itself. Some folks started a list of useful resources, I wrote a tool to capture video captions from Udacity lessons and many others organized meetups and study sessions.</li><li>I was exposed to the advantages and disadvantages of the democratic experience where coders of different ability often have the same voice, and work together in a transparent atmosphere. You can join any channel you want, most ask for help when they need it without shame, and people are empowered to work on projects even if they don’t have much experience.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*fGlLyeLDC9LkhBYPihko1A.jpeg" /></figure><p>This led me to immediately contrast this way of working to the typical workplace that I am so used to — hierarchical, top down management structure, and with many black boxes.</p><p>Working on open source made me think about and reaffirm the importance of transparency, collaboration, balancing top-down design with bottom-up innovations in code and in life</p><p>Given the diversity of programming backgrounds, and seeing the difficulty in communication between those who knew a lot and those that were beginners, I have been inspired to tackle the gap: to really understand a topic in depth and create accessible materials that explain the topic to beginners or those with relatively less experience. This was the single biggest impact of the Udacity/Grow with Google experience on my life.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*g0m5nonGeGid9GOEm2hWJQ.jpeg" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3301464e7ec7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-n00b-code-chronicles/my-udacity-experience-and-the-wonderful-strange-world-of-open-source-3301464e7ec7">My Udacity experience and the wonderful strange world of open source</a> was originally published in <a href="https://medium.com/the-n00b-code-chronicles">The ⓝⓞⓞⓑ Code Chronicles</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to get multipage JSON results from Google and Zendesk APIs]]></title>
            <link>https://medium.com/the-n00b-code-chronicles/how-to-get-multipage-results-from-web-apis-767df696923?source=rss----e5b2de76615f---4</link>
            <guid isPermaLink="false">https://medium.com/p/767df696923</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[learning-to-code]]></category>
            <category><![CDATA[api]]></category>
            <dc:creator><![CDATA[Tanya Gupta]]></dc:creator>
            <pubDate>Mon, 02 Jan 2017 20:17:19 GMT</pubDate>
            <atom:updated>2017-01-13T18:26:48.792Z</atom:updated>
            <content:encoded><![CDATA[<h4>This post is about two ways to handle pagination (getting multipage results) in response to queries on Google and Zendesk APIs</h4><p>While most Web API provides multi page results to a call, most API documentation will only talk about how to get the <strong>first page</strong> of those results.</p><h3>It is almost impossible to find guides to pagination!</h3><h4>Zendesk gets a B</h4><p>Here is an example from <a href="https://developer.zendesk.com/rest_api/docs/core/tickets">Zendesk</a>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/675/1*CxEcEOuCGm5k88WHzSA6jg.png" /></figure><p>If at all they have instructions on pagination, it will be hidden away somewhere in the API documentation</p><p>Here’s the documentation on <a href="https://developer.zendesk.com/rest_api/docs/core/introduction#pagination">pagination</a> on Zendesk. This one was hard to find but not impossible.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/262/1*86oRSjCgSIBYvc8gp5Wu8w.png" /></figure><h4>Google gets an F</h4><p>Sometimes the instructions may be hidden way deeper than necessary. See below for a search on “pagination” on <a href="http://developers.google.com/">http://developers.google.com</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1tATGyC50p5QEr04f10ghA.png" /></figure><p>I am assuming the documentation people leave the details out because for most professional developers this is trivial.</p><p><strong>Not so for a newbie. </strong><br>If you are a code n00b, you are have struggled with the stubborn OAuth, pored over API docs to get the perfect call that will get you the data for your app, and finally late in the evening, you are getting the data back! The last thing you want is to cross yet another hurdle.</p><p>So here’s what I have: <strong>There are two common ways API designers tell you that there are more pages of results.</strong></p><p><strong>One is via a next page field in the result</strong>, which will contain the url for the next page. If there are no next pages the field will not be present in the result. <a href="https://developer.zendesk.com/rest_api/docs/core/introduction#pagination">Zendesk</a> uses this approach.</p><p><strong>The other way is to return the token for the next page</strong>, which you append to your next call. Most Google APIs uses this approach. <a href="https://dev.twitter.com/ads/basics/pagination">Twitter</a> uses a similar next cursor key.</p><h4>In either case the solution boils down to the following</h4><ol><li>Have an array to store <strong>all</strong> the results</li><li>Make a call and get the result and push them to the array</li><li>Enter a loop if the next page field or page token is present</li><li>Make the new call either using the next page URL or the token</li><li>Get the result</li><li>Optionally — give the server a few milliseconds break. Be nice.</li><li>Here’s what I did for Zendesk. I used a do-while loop, but while-do works just fine of course.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/020fc472c0edf7718ed76f83d229e5a9/href">https://medium.com/media/020fc472c0edf7718ed76f83d229e5a9/href</a></iframe><p>8. Here’s what I did for a Google API call</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/020fc472c0edf7718ed76f83d229e5a9/href">https://medium.com/media/020fc472c0edf7718ed76f83d229e5a9/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=767df696923" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-n00b-code-chronicles/how-to-get-multipage-results-from-web-apis-767df696923">How to get multipage JSON results from Google and Zendesk APIs</a> was originally published in <a href="https://medium.com/the-n00b-code-chronicles">The ⓝⓞⓞⓑ Code Chronicles</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to deploy a Node.js web app using Express in Cloud9]]></title>
            <link>https://medium.com/the-n00b-code-chronicles/how-to-deploy-a-node-js-web-app-using-express-in-cloud9-91e73910293f?source=rss----e5b2de76615f---4</link>
            <guid isPermaLink="false">https://medium.com/p/91e73910293f</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[learning-to-code]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[expressjs]]></category>
            <category><![CDATA[cloud9]]></category>
            <dc:creator><![CDATA[Tanya Gupta]]></dc:creator>
            <pubDate>Mon, 05 Dec 2016 00:12:51 GMT</pubDate>
            <atom:updated>2016-12-07T10:48:06.637Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GRRMJOQi5HQK7YX0KYZO9A.png" /></figure><h4>This post is about how simple it is to run a basic web app on <a href="https://c9.io/">Cloud9</a> if you can get past the jargon-heavy documentation on the Web. Read this “dejargonified” article, save time and hit the ground running right away.</h4><h3>Prerequisites</h3><ul><li>You are familiar with basic Node (i.e. you know loops, if-thens etc.) and understand the basic <a href="https://github.com/FreeCodeCamp/FreeCodeCamp/wiki/JS-Callback-Functions">callback</a> pattern.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/824/1*yN8gVxoFgewJP2N30qIJdA.png" /></figure><blockquote>For example, you are good to go if you have done (or can do) the Node challenges in <a href="https://medium.com/u/17756313f41a">Quincy Larson</a>’s <a href="https://medium.com/u/8b318225c16a">Free Code Camp</a></blockquote><ul><li>You are NOT an experienced programmer. You will be bored :-)</li><li>You have an account on <a href="https://medium.com/u/f487850c8667">Cloud9</a> and you have <a href="https://docs.c9.io/v1.0/docs/create-a-workspace">created</a> a workspace on <a href="https://medium.com/u/f487850c8667">Cloud9</a>.</li></ul><h3>Terms</h3><ol><li><strong>Web Server:</strong> The Web is vast and complex but its core functionality is strikingly simple. The Web is built around two classes of programs: servers and requesters. Servers run continuously (unless they are “down”) and listen for requests for web pages. When it receives a request it provides a copy of that page (if it has access to it) to the requester.</li><li><strong>HTTP protocol: </strong>The communication convention these two classes of program use to talk to each other is the famous http protocol.</li><li><strong>Requesters, browsers and a web page: </strong>The requesters are programs that can ask for a specific web page from a server. A requester that can display the web page received from a server is what we know now as browsers. All browsers are requesters but not all requesters are browsers. For example, when you write some code to access and process <a href="https://medium.com/@tanyagupta/da10e81a035c#.quw44dphf">JSON data using web API</a>, your code is a requester and not a browser.</li><li><strong>Express:</strong> Express is a program that makes setting up your own web server very easy. It is a written in Node. Anything you can do in Express you can also do it in pure Node <a href="https://gist.github.com/tanyagupta/87bc01b561df839a275456c2183b1f68">but it is a lot of work</a>. Besides <a href="https://medium.com/u/bbb3c7ccb0a0">TJ Holowaychuk</a> and <a href="https://medium.com/u/96cd9a1fb56">Node.js Foundation</a> <a href="https://github.com/expressjs/express/graphs/contributors">contributors</a>, have already solved the problem for us.</li></ol><h3>Setting up express, creating an app and running it in Cloud9</h3><ol><li><a href="https://expressjs.com/en/starter/installing.html">Install express</a> in a directory of your choosing (see link for details)</li></ol><p>$ npm install express --save</p><p>2. Create a new javascript file</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*pfKyqJJVvUBhXefQdv7FLg.gif" /></figure><p>3. Use the code below</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e9fb4c91224f3b8398988c4978091de4/href">https://medium.com/media/e9fb4c91224f3b8398988c4978091de4/href</a></iframe><p>4. One challenge I had was that I did not know where the app was running. The typical <a href="http://localhost:8080/"><em>http://localhost:&lt;port number&gt;/</em></a><em> </em>as <a href="https://expressjs.com/en/starter/hello-world.html">referenced</a> in various express resources is not going to work since the app is running on Cloud9 servers and the command above works only to test apps running locally.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/601/1*j8VY3gjWmASlVeH1vNJ5lA.png" /></figure><p>On Cloud9 it will be http://&lt;<em>workspace name</em>&gt;-&lt;<em>user name</em>&gt;. If you’ve forgotten what these are, you can check in your browser window. For example in the picture here, the user name is <em>tanyagupta</em> (me) and the workspace name is <em>test</em>. Therefore in this case you would go to <a href="http://test-tanyagupta.c9users.io/">http://test-tanyagupta.c9users.io</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*ZXkRNkjmyTF25FY_d7yTUA.gif" /></figure><p>You can also use the preview button to do so.</p><p><strong>NOTE: </strong>The app “runs” only while you are logged in.</p><p>If you wanted to get into the details of the code, here is the same code, heavily marked up with comments:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5aade72019f1c6db67140452a0ff4b90/href">https://medium.com/media/5aade72019f1c6db67140452a0ff4b90/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/744/1*VGC5W5Ntv1AhnSLte_XLdw.gif" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=91e73910293f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-n00b-code-chronicles/how-to-deploy-a-node-js-web-app-using-express-in-cloud9-91e73910293f">How to deploy a Node.js web app using Express in Cloud9</a> was originally published in <a href="https://medium.com/the-n00b-code-chronicles">The ⓝⓞⓞⓑ Code Chronicles</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to efficiently handle irregular JSON data:]]></title>
            <link>https://medium.com/the-n00b-code-chronicles/a-n00ber-pattern-for-taming-the-wild-data-web-da10e81a035c?source=rss----e5b2de76615f---4</link>
            <guid isPermaLink="false">https://medium.com/p/da10e81a035c</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[json]]></category>
            <category><![CDATA[learning-to-code]]></category>
            <category><![CDATA[education]]></category>
            <dc:creator><![CDATA[Tanya Gupta]]></dc:creator>
            <pubDate>Mon, 05 Dec 2016 00:11:37 GMT</pubDate>
            <atom:updated>2017-01-02T20:27:41.477Z</atom:updated>
            <content:encoded><![CDATA[<h3>Using Javascript logical operators &amp;&amp; and ||</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6Fw37wv47djcDjmbmeRhjQ.png" /><figcaption>JSON</figcaption></figure><h4><strong>This post is about how I had been processing JSON data from Web API calls, why the approach I chose was “poor taste” code, and what I did to fix it.</strong></h4><h3>Prerequisites</h3><ul><li>You are familiar with basic Javascript (i.e. you know loops, if-thens etc.).</li></ul><blockquote>For example, you are good to go if you have done (or can do) the Basic JavaScript challenges in <a href="https://medium.com/u/17756313f41a">Quincy Larson</a>’s <a href="https://medium.com/u/8b318225c16a">Free Code Camp</a> (<a href="https://www.freecodecamp.com/map-aside#nested-collapseBasicJavaScript">link</a>)</blockquote><ul><li>You have cursory knowledge of web APIs. (i.e. you <strong>know</strong> what they are used for). You know a bit about relational database and SQL.</li></ul><blockquote>This does not mean you have actually used them in your code (although that would be nice).</blockquote><ul><li>You are NOT an experienced programmer. You will be bored :-)</li></ul><h3>Preamble (a.k.a. why this post?)</h3><p>The inspiration for this post comes from <a href="https://medium.com/u/394a2b0ef92f">Brian Barto</a>’s thought provoking and popular (2.1 k recommends and counting) <a href="https://medium.com/@bartobri/applying-the-linus-tarvolds-good-taste-coding-requirement-99749f37684a#.xnta2qxv0">article</a> on “good taste” coding. <a href="https://medium.com/u/394a2b0ef92f">Brian</a> used an example given by Linus Torvalds (<em>you know that guy who single handedly wrote the kernel of one of the world’s most robust, efficient and elegant operating system and gave it away for free</em>) in an interview to drive home some software development best practices. While I encourage everyone to read the article, my post here is about what <strong>I </strong>ended up doing after reading his article.</p><p>I am a <a href="https://medium.com/@tanyagupta/i-dropped-my-cs-major-because-i-couldnt-do-a-bubble-sort-126e1242d86e#.l6rxigrr0">self-taught</a> newbie hobbyist programmer. And my partner (in both code and in life) who is trained in “legacy” CS, says I am bit of a “code brat.” I code first, think (of algorithm and stuff) later. I code for my pleasure (i.e. not for money, for now). Once I get an app working and tested, I deploy it and never look back (well I do listen to user complains and fix them, but that is about it). Brian’s article nudged me to take a <strong>critical</strong> look at the code I have written over the last two years (as I learned Google Apps Script and built Google Doc/sheet Add-ons and web apps using Google App script).</p><p>It is humbling to find examples of “poor taste” programming in one’s own code. But it is fun to fix them and talking about them is almost therapeutic. In this post I will describe one such fix (the fancy word for it is refactoring).</p><h3>On processing the (data) web using APIs and JSON</h3><p>Web APIs give you access to databases with rich information that your program can use. Here’s an <a href="http://www.programmableweb.com/news/how-to-get-most-out-new-york-times-api-suite/how-to/2012/01/04">introduction with detailed example using the New York Times Web API.</a> Most modern Web APIs return data in JSON format. <a href="http://www.json.org/">JSON</a> is a subset of Javascript and parsing a string to a JSON (i.e. a Javascript) object is trivial.</p><p>var data = JSON.parse(string_result_of_api_call)</p><p>Boom! The Javascript object datanow contains all the information that was sent back in response to the API call.</p><p><em>Things get a bit messy from here.</em></p><h4>The “mess” explained</h4><p>The use of Web API to get data from a web service is conceptually very similar to making a query into a database. For example, a SQL query select * from cars where price &lt;10K (get me all the cars priced &lt;10K), will return a table with rows that match the query criteria.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/456/1*Hvm4835Ansoku3NmMdCTMQ.png" /></figure><p>In JSON the results would look a bit different.</p><p>[{make:&#39;Toyota&#39;, model:&#39;Camry&#39;, year:1999, price:7K, doors:&#39;2DR&#39;},{make:&#39;Nissan&#39;, model:&#39;Sentra&#39;, year:1988, price:3K, doors:&#39;2DR&#39;},{make:&#39;Jeep&#39;, model:&#39;Cherokee&#39;, year:1988, price:7K, doors:&#39;4DR&#39;}{make:&#39;Jeep&#39;, model:&#39;Wrangler&#39;, year:1984, price:9K, doors:&#39;2DR&#39;}]</p><ol><li>The result is a valid Javascript object (an array in this example)</li><li>Each row of the result is represented as an element of an array and the fields name are given for every value.</li></ol><p>Although the <strong>look</strong> is different, the underlying structure is still not <em>that</em> different.</p><p>Now let’s bring the Web’s messiness. What if some of the fields are absent from some of the rows. This usually does not happen in professionally built and curated in-house databases but irregularities like this is the norm when it comes to the Web and results from the Web API calls are no different.</p><p>If the results were returned in an SQL style rows they would look like the following (where we are missing information about the Nissan’s year and its drive train type the model for one of the two Jeep).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/466/1*JVj7d2kUh0TmCjoNnKXnDw.png" /></figure><p>Processing this data is fairly straight forward. We loop through the data, for each row, we check if a value is null (or blank) and do what we need to do with the data that <em>is</em> available. Our code does not break, just because there is a null or an empty value.</p><p>In JSON the data would be something like</p><p>[{make:&#39;Toyota&#39;, model:&#39;Camry&#39;, year:1999, price:7K, doors:&#39;2DR&#39;}, {make:&#39;Nissan&#39;, model:&#39;Sentra&#39;, price:3K},{make:&#39;Jeep&#39;, year:1988, price:7K, doors:&#39;4DR&#39;}, {make:&#39;Jeep&#39;, model:&#39;Wrangler&#39;, year:1984, price:9K, doors:&#39;2DR&#39;}]</p><p>For Nissan, as expected there is no key for year and doors. For one of the Jeeps the model is missing.</p><p>My n00b inclination was that the processing of this data is still quite simple. I will need to add an additional check to see if a field is present before accessing its value. I will access the key and see if accessing the key returns <em>undefined</em>. If it does not, then I capture the value. In our Nissan example, trying to access the key <em>year</em> will return undefined. So my pattern was</p><p>if (key != undefined) {do_some_thing(object[key]}</p><p><strong>It worked well until I encountered complex (i.e. real life) JSONs.</strong></p><p>JSON is a nested tree like structure, rather than a table structure. While the keys of a JSON object has to be a string or a number, the values can be objects. The values of the keys of these <em>child</em> objects can themselves again be objects. Many real world JSONs can be 8 or 9 level deep. Using my pattern this would mean that I would have a 8<em> or 9 level </em><strong><em>nested</em></strong><em> if-thens.</em></p><p><strong><em>Try debugging that.</em></strong></p><h3>“Poor taste” a detailed example</h3><p>We continue with the car domain. Let’s say we are tasked to print the values of certain fields from the inventory. We will display the value of the fields if they are available. If the fields are not there or if the value of a field is not present we will not display anything (i.e. write a blank).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/11d408e431c166e3894a91a40553fb94/href">https://medium.com/media/11d408e431c166e3894a91a40553fb94/href</a></iframe><p>Checking for <em>model</em>, <em>trim</em> and <em>year</em> is easy because the keys are at the first level. But <em>wheel_size </em>and<em> wheel_metal </em>are nested fields. We need to first check if the parent field (in this case <em>wheel</em>) exists before we access them. You can see how the complexity increases in line 21-29. Again imagine processing fields that are 8 or 9 level deep.</p><p>Before I describe my refactoring let’s look at an interesting feature that is available in many programming languages.</p><h3>Short-circuiting — booleans</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A__QHnkb9jvGQX0hY0U9Zg.jpeg" /></figure><p>As logical expressions are evaluated left to right, they are tested for possible “short-circuit” opportunities using the following <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators">rule</a>s:</p><p>1. false &amp;&amp; (<em>anything)</em> is short-circuit evaluated to false</p><p>2. true || (<em>anything)</em> is short-circuit evaluated to true</p><p>Here’s a useful partial truth table</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/247/1*W6-4oIkiJGdGIPAveVxgsA.png" /><figcaption>A partial <a href="https://en.wikipedia.org/wiki/Truth_table">truth table</a></figcaption></figure><p>Taking advantage of rule # 1 if you write cars[&quot;wheel&quot;] &amp;&amp; cars[&quot;wheel&quot;][&quot;wheel_size&quot;] <strong>AND</strong> if cars[&quot;wheel&quot;]<em> </em>does not exist (i.e. it is false in Javascript) the rest never gets evaluated.</p><p>Basically the short-circuiting can convert a <strong>nested</strong> if else error checking for undefined fields to a simple single line of code. The depth of a field will not increase the complexity it will just add more clauses with &amp;&amp;s.</p><p>Finally, here’s the cleaned up code</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fd698dc71fd8adde046ca8c0c638b9f3/href">https://medium.com/media/fd698dc71fd8adde046ca8c0c638b9f3/href</a></iframe><p><strong>TL;DR:</strong> Use short circuit of &amp;&amp; to check for keys in a deep JSON instead of a nested if-else. This will significantly reduce your code complexity. Thread together all the keys from the top level until you reach the key whose value you want with bunch &amp;&amp;s.</p><blockquote>k_0:{k_1: {k_2:{k3: … kn}} should become k_0 &amp;&amp; k_0[k_1] &amp;&amp; k_0[k_1][k_2]&amp;&amp; … &amp;&amp;k_0[k_1][k_2]….[kn]</blockquote><p>In the last two years I have spent 2300 hours in coding and have written 23,342 lines of code. In that code I have made 349 real world JSON calls followed by nested if-then-else. Say it took me on the average 1 hour to code/debug each of them. That is still 350 hours or 15% of my coding time.</p><p>This pattern literally takes a minute or less to write and it is mostly bug proof. That is 350 minutes or about 6 hours.Go figure!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*b668vo6eFZFmONYKXoytxw.gif" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/744/1*clb5mwXJxwPxfpizxvWwiQ.gif" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=da10e81a035c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-n00b-code-chronicles/a-n00ber-pattern-for-taming-the-wild-data-web-da10e81a035c">How to efficiently handle irregular JSON data:</a> was originally published in <a href="https://medium.com/the-n00b-code-chronicles">The ⓝⓞⓞⓑ Code Chronicles</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Tracking the *real* number of users in Google Add-Ons using Google Analytics]]></title>
            <link>https://medium.com/the-n00b-code-chronicles/tracking-the-real-number-of-users-in-google-add-ons-using-google-analytics-fa91d447879c?source=rss----e5b2de76615f---4</link>
            <guid isPermaLink="false">https://medium.com/p/fa91d447879c</guid>
            <category><![CDATA[google-analytics]]></category>
            <category><![CDATA[learning-to-code]]></category>
            <category><![CDATA[education]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[analytics]]></category>
            <dc:creator><![CDATA[Tanya Gupta]]></dc:creator>
            <pubDate>Mon, 05 Dec 2016 00:10:29 GMT</pubDate>
            <atom:updated>2017-02-01T21:38:12.876Z</atom:updated>
            <content:encoded><![CDATA[<p>I developed my first add-on <a href="https://chrome.google.com/webstore/detail/word-cloud-generator/alhnlhbhnklajhmccemipdbaifocepab?authuser=0">Word Cloud Generator for Google Docs</a> without knowing much about who was using the Add-On and how they were using it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/558/1*gKBztL3wI3kXtmoF6_1znA.png" /></figure><p>Over time I introduced enhancements such as enabling downloadable word clouds but did not know if those enhancements were indeed being used. I googled the <a href="https://support.google.com/analytics/answer/1032385?hl=en">essentials</a>, <a href="https://support.google.com/analytics/answer/1008080?hl=en">set up</a> <a href="https://developers.google.com/analytics/">Analytics</a> tracking, and included the basic tracking <a href="https://developers.google.com/analytics/devguides/collection/analyticsjs/">code</a> in my script file.</p><p>This basic tracking code is well explained on the <a href="https://medium.com/u/991272e72e68">Google Developers</a> <a href="https://developers.google.com/analytics/devguides/collection/analyticsjs/">analytics.js page</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/724/1*1hwMzrroLGaYGKkstxflZg.png" /></figure><p>I quickly realized that the basic set up did not result in useful information on usage for Add-Ons as the counts were vastly exaggerated (tracking was done using cookies that were not persistent and resulted in information per <strong>use</strong>, and not per <strong>user</strong>). (<em>The number of installs displayed in the developer dashboard was not much help for analytics). </em>Then I found an Analytics <a href="https://developers.google.com/analytics/devguides/collection/analyticsjs/cookies-user-id">resource</a> that presented the code to be used and a <a href="https://gsuite-developers.googleblog.com/2016/01/google-apps-script-tracking-add-on.html">blog</a> with a discussion of the cookie issue by <a href="https://medium.com/u/b7a188dea249">Romain Vialard</a>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/878/1*mr3C3_HlNdyrxrKDxRkE2Q.png" /></figure><p>This (ultimately) with a lot of trial and error, finally worked.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*imn5kiubzGf3DWwMgiv6eQ.png" /></figure><p>However it took me a <strong>very long time to understand what this code was doing and how to use it.</strong> Not a fun experience. So in case there is somebody out there looking for a step by step explanation of why the above works and what it means, I posted my code on gist with a LOT of comments. I hope it helps someone to save some time and get to tracking Add-On usage rather than googling!</p><p>Leave a message if you have any additional tips or comments</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/16ba450e59ee9ae5deac3bf27061d48d/href">https://medium.com/media/16ba450e59ee9ae5deac3bf27061d48d/href</a></iframe><h3>GA concepts:</h3><p><strong>Tracker objects</strong>: A JS object created by GA that can collect and store data and then send that data to Google Analytics.</p><p><strong>Tracking id:</strong> A string like UA-000000–01. It is included in tracking code to tell Google Analytics which account and property to send data to.</p><p><strong>Client id:</strong> The client id is a unique, randomly generated string that usually gets stored in the browsers cookies, so subsequent visits are associated with the same user.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/744/1*ZC7o-BJbg517v1sUb0f81w.gif" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fa91d447879c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-n00b-code-chronicles/tracking-the-real-number-of-users-in-google-add-ons-using-google-analytics-fa91d447879c">Tracking the *real* number of users in Google Add-Ons using Google Analytics</a> was originally published in <a href="https://medium.com/the-n00b-code-chronicles">The ⓝⓞⓞⓑ Code Chronicles</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Encountering Javascript’s “weird” parts as a noob and how I dealt with it]]></title>
            <link>https://medium.com/the-n00b-code-chronicles/a-n00ber-hack-for-solving-needle-haystack-problems-when-the-haystack-is-an-array-33dc54d0501b?source=rss----e5b2de76615f---4</link>
            <guid isPermaLink="false">https://medium.com/p/33dc54d0501b</guid>
            <category><![CDATA[education]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[how-to]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Tanya Gupta]]></dc:creator>
            <pubDate>Mon, 05 Dec 2016 00:08:57 GMT</pubDate>
            <atom:updated>2017-01-08T21:28:02.664Z</atom:updated>
            <content:encoded><![CDATA[<h4>Array.indexOf() and converting an array to an object literal</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6GkHk6EX6tsF9gfFDcujlw.png" /></figure><p>The <strong>weirdness</strong> of Javascript is hard enough to deal with if you are an experienced programmer. It is much harder for a new programmer.</p><p>My first encounter with Javascript’s strangeness was with the <em>in</em> operator. The <em>in</em> operator is a nifty tool to check if a specified property is present in a specified object. For example we can check if a certain key is present in a object that has several key value pairs.</p><pre>var mycar = {make: &quot;Honda&quot;, model: &quot;Accord&quot;, year: 1998};<br>&quot;make&quot; in mycar  // returns true</pre><pre>&quot;mileage&quot; in mycar  // returns false</pre><p><strong>Unfortunately the <em>in</em> does not work in an intuitive (and useful) way when used with arrays.</strong></p><p>In the example below, I would really like it if Javascript’s <em>in</em> would work like this:</p><pre>var trees = [&quot;redwood&quot;, &quot;bay&quot;, &quot;cedar&quot;, &quot;oak&quot;, &quot;maple&quot;];</pre><pre>&quot;bay&quot; in trees    // returns false</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*HdrD9MA4NQy_jvYGMg7E4g.png" /></figure><p>The reason it does not work (and it should not) is the particular way Javascript’s array objects are implemented. They are internally viewed as a key value pairs of index and the corresponding element. For example the <em>trees</em> above is stored internally as something like:</p><pre>{0:&quot;redwood&quot;, 1:&quot;bay&quot;, 2:&quot;cedar&quot;, 3:&quot;oak&quot;, 4:&quot;maple&quot;}</pre><p>And yes, 1 in trees will return true, but that is not useful to me as a programmer.</p><p>I am relatively new to programming (<a href="https://medium.com/@tanyagupta/i-dropped-my-cs-major-because-i-couldnt-do-a-bubble-sort-126e1242d86e#.aohwtloii">here’s my story in medium</a>) and I do not code professionally. But even I encounter several scenarios almost every day where I need to check if some item is in an array or not. Naturally this weirdness in Javascript puzzled me a bit. From what I found out, this is available in other languages (Python, Ruby) so — why not Javascript? I googled a bit and found <a href="https://www.tutorialspoint.com/data_structures_algorithms/linear_search_algorithm.htm">this</a> on how to implement a “linear search” in Javascript. Which I did.</p><blockquote>To check if an item is in an array, I have to iterate through the array elements until the item is found in the array.</blockquote><pre>function inArray (item, theArray) {<br>  var found = false;<br>  var i=0;<br>  while (!found &amp;&amp; i in theArray) {<br>    found =(item === theArray[i]);<br>    i++;<br>  }<br>  return found;</pre><pre>}</pre><p>But it was not satisfying. I could not tell then what exactly about it dissatisfied me — at that point it was just something I did not think was cool. I now know better. Please read on.</p><h3>1) Array.indexOf()</h3><p>The <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf">Array.indexOf() method</a> returns the first index at which a given item can be found in the array. If it is not present it returns -1. Thanks to <a href="https://medium.com/u/72236815cb91">Nikolay Digaev</a> for pointing me to this (another is Array.includes() — which unfortunately is not currently supported by Google Apps Script where I mostly work on these days. The Array.includes() <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes">method</a> returns true or false based on whether an array includes a certain element.)</p><p>Here is a simple use case for Array.indexOf():</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e833ebe7874e295301e9f4c58beca4d5/href">https://medium.com/media/e833ebe7874e295301e9f4c58beca4d5/href</a></iframe><h3>2) Convert the array to an object literal and use the “in” operator</h3><p>I figured out a way to use the <em>in</em> operator in my use cases. I started converting my arrays to objects. I would store each (unique) item as a key and just put a blank in the corresponding value.</p><blockquote>The Hack: convert the array to a typical object with items as keys and empty values</blockquote><pre><strong>Creating the object:</strong></pre><pre>  var hack_object_literal = {};<br>  for (var i in test_data) {<br>    hack_object_literal[test_data[i]]=&quot;&quot;;<br>      <br>  }</pre><p>Careful readers may notice that my hack ignores duplicate items when building the object. Which is fine since all we want to know if an item is present. Not how many of them are present.</p><p>Now I can use the <em>in</em> operator to check if the item is present in the object which in effect tells me if the item is present in my array.</p><pre>function inArrayMyHack (item, arrayDataAsObject) { <br>  return item in arrayDataAsObject; <br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/575/1*Dn_ZfRcUQo4wMmAmWCIeQQ.png" /></figure><h4>Recommendation: use Array.indexOf() — easier andshorter</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*YJmKqYHIMiiPcMBDtuG5hw.jpeg" /><figcaption>Courtesy: <a href="https://pixabay.com/en/binary-system-code-computer-files-1543168/">Pixabay</a></figcaption></figure><h3>An experiment to determine speed</h3><p>Computer science purists believe that data structures should be used for the tasks they are originally designed to do, as otherwise, it results in unmaintainable code. One may deviate from this principle only if there is a compelling reason to do so. Leaving the values empty in a collection of key-value pairs seem to be a major deviation. So I decided to see if there is a compelling reason for my “hack.” And I found it.</p><p>I implemented the three functions: the linear search function as inArray, Array.indexOf() and inArrayMyHack</p><ol><li>For every size of arrays that I created:</li></ol><p>a) I sorted the array element and created a sorted version of the array</p><p>b) For every element in the sorted array I called both the functions giving them the element and the array. So for a 200 size array both the functions will be called 200 times each.</p><p>I recorded the time it took for them to complete.</p><p><em>Note for inArrayMyHack I also had to convert the array to an object before I made the call. I added that time to the performance time of inArrayMyHack — but there is a catch to that to0, read on..</em></p><h3>Try { there are significant efficiency gains}</h3><p>The graph and the table below shows that either of the two methods are way faster than the linear search.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/575/1*Jdd52crrvWZyTUJXlnsF4w.png" /></figure><p><strong>The hack and Array.indexOf() </strong>are close initially but the Array.indexOf() methods slows down once size increases. I talked to some coder friends who told me that the reason for this performance is the <em>in</em> operation uses a very fast algorithmic technique called hashing. This technique produces a performance that is (almost) constant time regardless of the data size. Linear search time increases as the data size increase. The data conversion time is very negligible compared to the speed gained by hashing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/586/1*ByfOArSgO57mPbYg11NY4w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/405/1*4r8VjIFdMZ3oCfKV93B_Zg.png" /><figcaption>Speed test</figcaption></figure><h3>Catch with the hack{We bomb if the arrays are not known at coding time}</h3><p>There can be situations where I do not know about the arrays at coding time. In such case I will not be able to convert the arrays to objects <strong>before</strong> making the call. Things will slow down if I have to do a lot of searches each time with a different unknown array.</p><h4>Bottom line: Use Array.indexOf() — will fit for most use cases unless you are dealing with large sizes and you have some knowledge of the arrays at coding time.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/744/1*ZC7o-BJbg517v1sUb0f81w.gif" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=33dc54d0501b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-n00b-code-chronicles/a-n00ber-hack-for-solving-needle-haystack-problems-when-the-haystack-is-an-array-33dc54d0501b">Encountering Javascript’s “weird” parts as a noob and how I dealt with it</a> was originally published in <a href="https://medium.com/the-n00b-code-chronicles">The ⓝⓞⓞⓑ Code Chronicles</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>