<?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[Stories by Diego Balduini on Medium]]></title>
        <description><![CDATA[Stories by Diego Balduini on Medium]]></description>
        <link>https://medium.com/@mut_e?source=rss-6ecbf115969f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*XFIGmZbOqwljjBI5W8qxeA.png</url>
            <title>Stories by Diego Balduini on Medium</title>
            <link>https://medium.com/@mut_e?source=rss-6ecbf115969f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 20:09:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mut_e/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Record Linkage em Big Data na Loft]]></title>
            <link>https://medium.com/loftbr/record-linkage-em-big-data-na-loft-80c02ec97026?source=rss-6ecbf115969f------2</link>
            <guid isPermaLink="false">https://medium.com/p/80c02ec97026</guid>
            <category><![CDATA[engineering]]></category>
            <category><![CDATA[big-data]]></category>
            <category><![CDATA[record-linkage]]></category>
            <category><![CDATA[entity-resolution]]></category>
            <category><![CDATA[data-matching]]></category>
            <dc:creator><![CDATA[Diego Balduini]]></dc:creator>
            <pubDate>Wed, 21 Sep 2022 15:03:14 GMT</pubDate>
            <atom:updated>2022-09-21T15:03:14.074Z</atom:updated>
            <content:encoded><![CDATA[<p>Na Loft, nós do squad de Market Data realizamos record linkage em grande escala com Spark em nosso pipeline de dados, para deduplicar entidades semelhantes entre as diversas fontes de dados.</p><p><strong>Record Linkage</strong>, também conhecido como <strong>Data Matching </strong>ou <strong>Entity Resolution,</strong> é o processo de encontrar itens similares que representam a mesma entidade entre diferentes fontes de dados.</p><p>Existem dois tipos de Record Linkage: Determinístico e Probabilístico.</p><p>No determinístico, usamos diversas regras e heurísticas para relacionar duas entidades. Por exemplo, sabemos que a entidade Pessoa é a mesma se o CPF for idêntico.</p><p>Já no probabilístico, conhecido também como fuzzy-matching, dependemos da probabilidade de duas entidades serem similares. Aqui são usados métodos como por exemplo: Levenshtein, Jaro–Winkler, Fellegi Sunter, entre outros.</p><p>O processo de comparação (matching) executado durante o record linkage é custoso, possuindo a complexidade de um algoritmo de produto cartesiano. Supondo que temos dois conjuntos de dados, cujos tamanhos são dados por <strong>M</strong> e <strong>N</strong> respectivamente, então a complexidade assintótica seria de <strong>O(M*N).</strong></p><p>A seguir veremos um pouco de como funciona o pipeline responsável pelo match de dados, que é composto por dois jobs principais: o de <em>match</em> e do <em>grouping</em>.</p><h3>Matching com SQL</h3><p>O <em>match</em> é responsável por comparar uma entidade com todas as outras, e levantar um <em>score</em> do quão parecidas elas são. Uma das formas de realizar essa comparação é através de um produto cartesiano, o qual pode ser feito com SQL usando o CROSS JOIN.</p><p>A <strong>Tabela 1</strong> foi gerada com o <strong>CROSS JOIN</strong>. Durante o join, é computado o valor da coluna <strong>Score</strong>, a qual indica a probabilidade de semelhança entre <strong>Entidade</strong> e <strong>Matches</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AdQBTI37r2dUWXv6cxEAMQ.png" /></figure><p>Logo abaixo, a <strong>Tabela 1</strong> foi transformada para uma versão mais compacta, exibida na <strong>Tabela 2</strong>. Em ambas versões evitamos a comparação entre entidades idênticas (A com A), contudo, na nova versão, também evitamos comparações comutativas (A com B, B com A) — mais em notas de rodapé ₁.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7ocTMzmAfeKjG1lfm50AIQ.png" /></figure><p>É importante ressaltar que B não dá match com C. Isso pode ser por diversos motivos. No exemplo, para efeitos de demonstração, foi escolhido o score mínimo de 0.73 para ser um match com confiança alta. Ou seja, tudo abaixo deste threshold deve ser considerado como entidades distintas, e tudo acima como sendo a mesma entidade.</p><p>Isso completa a etapa de comparação. Entretanto, ainda é preciso um passo a mais para agrupar ou deduplicar as entidades.</p><h4>Notas de rodapé</h4><p>₁ Esta mudança afeta apenas o resultado final, que fica mais compacto para a etapa de agrupamento: reduzimos 6 linhas para 3 linhas. Contudo, não há melhorias relacionadas com a performance do algoritmo. Ou seja, tanto a <strong>Tabela 1</strong> quanto a <strong>Tabela 2 </strong>são resultados de um produto cartesiano.</p><p>A query da <strong>Figura 1</strong> mostra como gerar uma tabela parecida com a <strong>Tabela 2</strong>. Na <strong>Figura 2 </strong>podemos verificar o operador <em>CartesianProduct</em> no <em>Query Plan</em> que o <em>Spark</em> executou.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1reVHzFl1SK9iQ3Ok0U13g.png" /><figcaption>Figura 1 — Código para gerar Tabela 2</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rqhTtx2R1TcF6wZrjMaJog.png" /><figcaption>Figura 2 — Spark Query Plan</figcaption></figure><h3>Agrupamento</h3><p>O objetivo do processo de agrupamento é o de juntar todas as entidades semelhantes em grupos distintos. A definição de semelhança vai depender de qual método foi usado durante a etapa de comparação: probabilístico ou determinístico.</p><p>No método determinístico é mais fácil agrupar itens semelhantes, já que a definição de semelhança é binária: a entidade é ou não é a mesma. Já no probabilístico, é necessário definir um threshold de certeza sobre o score. A escolha de um threshold ótimo envolve técnicas de ciência de dados e aprendizado de máquina, como matriz de confusão e métricas como acurácia e precisão. Dada a complexidade do tema, isso não será discutido neste artigo.</p><h4>Agrupamento V1.0</h4><p>Nossa primeira versão do pipeline de agrupamento rodava em cima da <strong>Tabela 1</strong>. A <strong>Figura 3</strong> mostra uma versão simplificada de como o agrupamento era feito com uma query SQL.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UGMnb0PJJu5OdEieI4c6jQ.png" /><figcaption>Figura 3 — Agrupamento feito com SQL</figcaption></figure><p>O resultado deste agrupamento são três grupos, um para cada entidade. Sendo eles:</p><ul><li><strong>K</strong> = {A,B,C}</li><li><strong>N</strong> = {A,B}</li><li><strong>M</strong> = {A,C}</li></ul><p>Aqui já podemos notar dois problemas. O primeiro é que existem subgrupos, <strong>N</strong> ⊆ <strong>K</strong> e <strong>M</strong> ⊆ <strong>K</strong>. O segundo problema é que B e C foram agrupados, mas de acordo com nossa regra de threshold de score, eles deviam estar separados, já que não são itens semelhantes.</p><blockquote><strong>Relação com Grafos</strong></blockquote><blockquote>Uma observação interessante sobre este agrupamento: o grupo formado é composto pela entidade e todos os seus vizinhos. Isso porque, <strong>K</strong> é formado pelas arestas B&lt;-A-&gt;C, <strong>N</strong> por B-&gt;A e <strong>M</strong> por C-&gt;A.</blockquote><h4>Modelando o Problema como um Grafo</h4><p>Com o insight do agrupamento V1.0, ao observar novamente a <strong>Tabela 2</strong>, notamos que ela tem a propriedade de ser uma lista de arestas não direcionadas.</p><p>Sabendo disso, passamos a usar esta versão mais compacta do resultado do record linkage para remodelar o problema usando um <strong>grafo</strong> como estrutura de dados.</p><p>A <strong>Figura 4 </strong>mostra como a <strong>Tabela 2</strong> ficaria modelada como um grafo. Lembrando que B perdeu a aresta com C por causa do filtro de threshold de score que havíamos mencionado.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*7hF5c4q2RY-7JCy1UsECTA.png" /><figcaption>Figura 4</figcaption></figure><p>Com as entidades ligadas por arestas, formando um grafo não-direcionado, agora temos algumas formas possíveis de agrupamento, cada uma resultando em grupos diferentes.</p><p>Segue lista de possíveis algoritmos de agrupamento de subgrafos que consideramos:</p><ul><li>Neighbors</li><li>Connected Components</li><li>Max Cliques</li></ul><h4>Neighbors (Vizinhos)</h4><p>Como já discutimos anteriormente na versão 1, este algoritmo trouxe alguns problemas nos grupos, como a formação de subgrupos, e itens não semelhantes dentro do mesmo conjunto.</p><h4>Agrupamento V2.0 — Connected Components</h4><p>O algoritmo <strong>Connected Components</strong> (componentes conexas) agrupa todos os vértices que possuem alguma conexão, independente do grau, e que não fazem parte de um de um subgrafo maior.</p><p>A <strong>Figura 5</strong> mostra quatro diferentes exemplos de arranjamentos de arestas entre os vértices {A, B, C, D}, e como eles seriam agrupados. Se os arranjamentos da figura fizessem parte do mesmo grafo, ou seja, cada arranjamento fosse entre diferentes vértices, teríamos então quatro grupos como resultado do algoritmo.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OueM5xsJcQk2VRzunlF6MQ.png" /><figcaption>Figura 5 — Connected Components</figcaption></figure><p>Apesar deste algoritmo não gerar subgrupos como o de <strong>Neighbors</strong>, ele também tem o problema de agrupar itens não semelhantes. Dado esse comportamento, decidimos ir com a versão 3, usando o algoritmo de <strong>Max Cliques</strong>.</p><h4>Agrupamento V2.1 — Max Cliques</h4><p>Em determinados contextos, para que os vértices sejam considerados uma mesma entidade é necessário que todos os elementos relacionados sejam suficientemente parecidos com todos os outros elementos do mesmo grupo.</p><p>De acordo com a teoria dos grafos, um <strong>clique </strong>(conhecido também como grafo completo) é um grafo ou subgrafo em que todo mundo se conhece, ou seja, existe uma aresta entre todos os pares contidos naquele conjunto. Um <strong>clique máximo</strong>, ou <strong>max clique</strong>, é simplesmente um clique que não é um subconjunto de nenhum outro clique.</p><p>A <strong>Figura 6</strong> mostra os mesmos arranjamentos de arestas entre os vértices {A, B, C, D} da <strong>Figura 5</strong>, mas agora os agrupamentos foram formados de acordo com o algoritmo de <strong>max cliques</strong>. O resultado é a formação de nove grupos — cinco a mais do que o visto anteriormente.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PDF7zFj8CKV2frGZmlkfag.png" /><figcaption>Figura 6 — Max Cliques</figcaption></figure><h3>Conclusão</h3><p>O processo de record linkage pode ser determinístico ou probabilístico. Na etapa seguinte da comparação, o agrupamento precisa ser realizado, A criação de grupos pode ser feita de diversas formas, sendo que aqui neste artigo foram apresentadas três, sendo elas: vizinhos, componentes conexas, clique máximo.</p><p>Como mencionado acima, aqui na Loft realizamos record linkage em grande escala com Spark em nosso pipeline de dados, para deduplicar entidades semelhantes entre as diversas fontes de dados.</p><p>Assim como é feito no aprendizado de máquina, também separamos nosso dataset em dois, um para treino e outro para testes, durante a evolução do nosso modelo de record linkage e agrupamento. Isso foi muito útil para saber onde estávamos, e aonde gostaríamos de chegar.</p><p>Foram diversos experimentos. A cada iteração, utilizamos métricas como acurácia, precisão e matriz de confusão para medir os resultados. Além disso, observamos também estatísticas em relação à formação dos grupos, como o tamanho médio, diâmetro e densidade.</p><p>Por fim, tivemos os melhores resultados com a mudança de match probabilístico para determinístico, e com a alteração do agrupamento para usar o algoritmo de clique máximo. O resultado final foi uma grande melhoria na deduplicação, que passou de 72% para até 98% de acurácia.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=80c02ec97026" width="1" height="1" alt=""><hr><p><a href="https://medium.com/loftbr/record-linkage-em-big-data-na-loft-80c02ec97026">Record Linkage em Big Data na Loft</a> was originally published in <a href="https://medium.com/loftbr">Loft</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Testing a Facebook chatbot with Nightmare.js [pt. 1]]]></title>
            <link>https://medium.com/hackernoon/testing-a-facebook-chatbot-with-nightmare-js-pt-1-d2fc1501113e?source=rss-6ecbf115969f------2</link>
            <guid isPermaLink="false">https://medium.com/p/d2fc1501113e</guid>
            <category><![CDATA[chatbots]]></category>
            <category><![CDATA[bots]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Diego Balduini]]></dc:creator>
            <pubDate>Fri, 26 Oct 2018 16:17:00 GMT</pubDate>
            <atom:updated>2019-05-21T11:39:18.834Z</atom:updated>
            <content:encoded><![CDATA[<h3>cTesting a Facebook chatbot with Nightmare.js [pt. 1]</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pSHz3NSk4ok6LJXkcUtRXg.png" /></figure><p>On this tutorial we will see how to create an end-to-end test with <a href="https://github.com/segmentio/nightmare">Nightmare.js</a> to test a <a href="https://hackernoon.com/tagged/facebook">Facebook</a> <a href="https://hackernoon.com/tagged/chatbot">chatbot</a>.</p><p>It is assumed that you already have a Facebook chatbot running, because we will not see how to build one here.</p><p>On this article, the <strong>first part, </strong>we will focus on creating a <em>test user </em>and a <em>test page</em> on Facebook. This <em>test user</em> will have the capabilities to login into Facebook and chat with our BOT. For that, we will use a <em>test page</em>, so that our chatbot can send and receive messages through it.</p><p>Optionally, on the end of this tutorial, we will see how to configure the Azure Bot Service to connect the BOT with the the <em>test page</em>.</p><p>When i finish writing the <strong>second part</strong>, i will post the link <a href="#">here</a>. It will be the last part, where we will write a simple question &amp; answer api on Nightmare.js to test the chatbot.</p><h4>Index:</h4><ol><li>Create a Test User</li><li>Create a Test Page</li><li>Link the Test Page with the BOT application</li><li>(Optional) Connect the BOT with the Test Page on Azure</li></ol><p>Lets get started…</p><h3>Create a Test User</h3><p>According to the Facebook, a Test User is described as:</p><blockquote>Test Users are special Users you can use to test your app. They are <a href="https://developers.facebook.com/docs/apps/test-users#rules">hidden from real User accounts</a>, and any data you generate with a Test User will only be visible to other Test Users on that app, or to real Users who have a Admin, Developer, or Tester role on the app. Test Users are also exempt from our spam and fake account detection systems, so they won’t be disabled when you use them to test your app.</blockquote><p><em>To know more about Test Users you can access </em><a href="https://developers.facebook.com/docs/apps/test-users"><em>this link.</em></a></p><p>Since we are trying to test our BOT, it is very likely that we had to set up a Facebook Application so the BOT could trade messages with Messenger.</p><p>We can create Test Users on the Facebook application page, which can be accessed on the <a href="https://developers.facebook.com/apps/">apps dashboard</a>.</p><p>On the App Page, click on <em>Functions</em> to open a submenu, and next on <em>Test Users</em>. This will open the page that allow us to create test users, as shown on <strong>Figure 1</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kOOmJyoZFVmNpDdsA02maA.png" /><figcaption>Figure 1 —Test Users page</figcaption></figure><p>Click on Add to open a modal, as shown at <strong>Figure 2</strong>. Them, select “Yes” for “Authorize Test Users for This App”.</p><p>It is necessary to give the Test User the right permissions to create and send messages to the Test Page that we will be creating. The Login Permissions are: <em>manage_pages</em>, <em>pages_show_list </em>and <em>pages_messaging</em>.</p><p>We can leave the other options with their default values.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/411/1*suR-Yfot0JfMKJBWB-GvBw.png" /><figcaption>Figure 2 — Creating the Test User</figcaption></figure><p>Next we need to set a password. Otherwise we can’t login on Facebook (or Messenger) with the Test User. It is important to set the password before getting the token because every time the password is changed we need to generate a new token.</p><p>Click on <em>Edit </em>to open a little menu, then click on “Change the name or password for this test user”, as shown on <strong>Figure 3</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/930/1*5aK22IPbu0CPm4iSzybnVQ.png" /><figcaption>Figure 3 — Setting the test user password</figcaption></figure><p>After changing the user’s password, click again on <em>Edit</em>. This time, click on “Get an access token for this test user” as shown on <strong>Figure 4</strong>. Don’t forget to save the generated token for later.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/938/1*wzh0XFEH-pIK1AMdkATjzw.png" /><figcaption>Figure 4— Getting an access token for the test user</figcaption></figure><p>Now that the Test User is set up. We can now login on <a href="https://www.messenger.com/">Facebook Messenger</a> with his Email and Password. Try it out, because that is how we will be logging in with the <strong>Nightmare.js </strong>application.</p><p>Notice that this user can’t send messages yet. We also cannot send messages to the BOT with him. This is the reason we will be creating a Test Page on the next step.</p><p>The BOT needs to be configured to send and receive messages from the Test Page too. On the end, we will have two (or maybe more) pages connected with our BOT application.</p><h3><strong>Create a Test Page</strong></h3><p>Test pages allow us to test the BOT’s application without using our production page. Creating a Test Page is very straightforward, we just need to send a POST request using the <a href="https://developers.facebook.com/tools/explorer">Facebook Graph API</a>.</p><pre>POST /v3.1/:testUserId/accounts</pre><p><strong>Application</strong>: Select your BOT application name</p><p><strong>Access Token</strong>: Paste the Test User access token you got on the previous step.</p><p><strong>Query Params</strong> (required):</p><ul><li>name: The page name.</li><li>category_enum: The page category.</li><li>about: The page’s about.</li><li>picture: Some valid picture for the page.</li><li>cover_photo: Some valid cover foto</li></ul><p>You can check on the <em>Links </em>section for more information about this API. Now, we will use the following query parameters to create the Test Page.</p><ul><li>name: Jackson Albiejejcdjhd Liescu Personal Blog</li><li>category_enum: PERSONAL_BLOG</li><li>about: Jackson Albiejejcdjhd Liescu test page</li><li>picture: <a href="https://avatars1.githubusercontent.com/u/149531?v=3&amp;s=460">https://avatars1.githubusercontent.com/u/149531?v=3&amp;s=460</a></li><li>cover_photo:{“url”:”https://orig00.deviantart.net/7db9/f/2016/303/7/5/nature_beautiful_wheat_field___facebook_cover_by_tweakertea-damsgob.jpg”}</li></ul><pre>POST /v3.1/100470524281527/accounts?name=Jackson Albiejejcdjhd Liescu Personal Blog&amp;category_enum=PERSONAL_BLOG&amp;about=Jackson Albiejejcdjhd Liescu test page&amp;picture=<a href="https://avatars1.githubusercontent.com/u/149531?v=3&amp;s=460&amp;cover_photo={">https://avatars1.githubusercontent.com/u/149531?v=3&amp;s=460&amp;cover_photo={</a>&quot;url&quot;:&quot;<a href="https://orig00.deviantart.net/7db9/f/2016/303/7/5/nature_beautiful_wheat_field___facebook_cover_by_tweakertea-damsgob.jpg">https://orig00.deviantart.net/7db9/f/2016/303/7/5/nature_beautiful_wheat_field___facebook_cover_by_tweakertea-damsgob.jpg</a>&quot;}</pre><p>The <strong>Figure 5</strong> shows the result after we send the POST request to the Facebook Graph API. If it was successful, the Test Page ID will be returned. Save it for later.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1003/1*Xr-TtMn2rG3gc6PO3qkLHw.png" /><figcaption>Figure 5 — Creating a Test Page</figcaption></figure><h3>Link the Test Page with the BOT application</h3><p>To make the chatbot receive and send messages through the Test Page, we need to get an access token for the page. However, the access token registered to the page by default is a short-term access token, meaning that it expires in a very brief period of time.</p><p>What we will do next is to “transform” this access token into a long-term access token, which has a lifetime of 60 days. We can always use the default (short-term) access term to refresh our long-term access token.</p><h4>Retrieving the Test Page’s <em>short-term</em> Page Access Token</h4><pre>GET /v3.1/me/accounts</pre><p><strong>Application</strong>: Select your BOT application name</p><p><strong>Access Token</strong>: The test user access token</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1008/1*Fw6_SnJdN9_YNZTdKSF1Eg.png" /><figcaption>Figure 6</figcaption></figure><p>To subscribe the page we just need to send another POST request. But first, change the Access Token with the Test Page access token. This will subscribe the app on the current selected application. If all permissions was defined correctly, we should receive a <em>success</em> message.</p><pre>POST /me/subscribed_apps</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DKzieaHq3hnvsGhAZ6cqvA.png" /><figcaption>Figure 7</figcaption></figure><h4>Get a <em>long-term</em> Page Access Token for the Test Page</h4><p>We can retrieve our long-term access token with another API call. However, this time we will use the <em>CURL </em>command, because this API cannot be accessed on the Graph API Explorer.</p><p>If you don’t have CURL, you can choose whatever application you wish other that can send POST requests.</p><p>The following <strong>curl </strong>command query params:</p><ul><li>client_id: Facebook Application Id</li><li>client_secret: Facebook Application Secret</li><li>fb_exchange_token: The Page token</li></ul><pre>curl -X GET &quot;https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&amp;client_id=&amp;client_secret=&amp;fb_exchange_token=&quot;</pre><pre>----------------------------------------</pre><pre>Response:<br>{<br>  &quot;access_token&quot;: &quot;EAAcJ5P3IBycBAEencptoDloIKrAfDP0WZCTl3zMKCSCUiZBD14hja7DhGczPWdDDXVYKesZABugggCrg0AkUYHiySb1dm3VzHRuwURxHIHM1373KFFiPPW7U6Xse3tAGOYYAecaaHjiKl0XmR5NxfE3v40vrZCKFtby253wvcotO8QueTLuEZBVVl38ZD&quot;,<br>  &quot;token_type&quot;: &quot;bearer&quot;,<br>  &quot;expires_in&quot;: 5184000<br>}</pre><p>Now, if we try to login into Facebook Messenger we can see that it is possible to send messages to Test Page we’ve just created.</p><p>For that, just search for the Test Page name on the Search bar and the Page should appear on the results.<strong> Figure 8 </strong>shows the final result of this tutorial.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wWBnZesRDQx0O6-nDcDIhQ.png" /><figcaption>Figure 8 — Chatting with the Test Page</figcaption></figure><h3>(Optional) Connect the BOT with the Test Page on Azure</h3><p>This step assumes that you are using Azure Bot Service to create chatbots.</p><p>On the <em>Bot Channels Registration</em> resource, click on <em>Channels</em> &gt; <em>Facebook Messenger</em>. This page is where you might have linked your BOT application to the Facebook Messenger.</p><p>To connect the BOT with the Test Page, click on “+ Add a new page” and insert the Test Page ID and the Test Page long-term Access Token. Now you should have two pages linked with your bot: the original and the test page.</p><p>The chatbot should be linked to your Test Page now. To check that, login on Messenger with the Test User credentials and try to send a message to the Test Page. If everything went correctly, the bot will send an answer back. An example is presented on <strong>Figure 9</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/287/1*4jHYaFQ_z5UTYxQA8-UKCA.png" /><figcaption>Figure 9 — BOT chatting with the Test Page</figcaption></figure><p>We have successfully created a Test User and a Test Page. Now we have in hands all we need to create the end-to-end test for our chatbot: the Test User’s email and password, and a Test Page where the BOT can chat with the user.</p><p>On the second part of this tutorial we will see how to use what we’ve done here to test our chatbot integration.</p><p>See you later!</p><h4>Links:</h4><ul><li><a href="https://developers.facebook.com/docs/apps/test-users">https://developers.facebook.com/docs/apps/test-users</a></li><li><a href="https://developers.facebook.com/docs/apps/test-pages">https://developers.facebook.com/docs/apps/test-pages</a></li><li><a href="https://developers.facebook.com/docs/graph-api/reference/page-category/">https://developers.facebook.com/docs/graph-api/reference/page-category/</a></li><li><a href="https://developers.facebook.com/tools/explorer">https://developers.facebook.com/tools/explorer</a></li><li><a href="https://www.messenger.com/">https://www.messenger.com</a></li><li><a href="https://azure.microsoft.com/pt-br/services/bot-service/">https://azure.microsoft.com/pt-br/services/bot-service/</a></li></ul><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Fdde502%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2Fhackernoon%2F&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href">https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d2fc1501113e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/testing-a-facebook-chatbot-with-nightmare-js-pt-1-d2fc1501113e">Testing a Facebook chatbot with Nightmare.js [pt. 1]</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Clean Code Smells And Heuristics]]></title>
            <link>https://medium.com/@mut_e/clean-code-smells-and-heuristics-9080d9ab67c1?source=rss-6ecbf115969f------2</link>
            <guid isPermaLink="false">https://medium.com/p/9080d9ab67c1</guid>
            <category><![CDATA[diary]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[best-practices]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Diego Balduini]]></dc:creator>
            <pubDate>Tue, 11 Jul 2017 20:47:58 GMT</pubDate>
            <atom:updated>2017-07-12T00:11:58.335Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>A list of language agnostic rules from the <a href="https://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882/ref=sr_1_1?ie=UTF8&amp;qid=1499804149&amp;sr=8-1&amp;keywords=clean+code">Clean Code book</a>, with commentaries.</blockquote><p>Writing is the best technique to memorize things. This post is meant to be a reference for developers, including myself, to quick consult code smells and heuristics, following best practices from people with extensive experience in software development, like Uncle Bob (the author of Clean Code), Kent Beck and Martin Fowler, all of them very often mentioned on the book.</p><p><a href="https://cleancoders.com/episode/clean-code-episode-1/show">Clean Coders: Clean Code, Episode 1: Clean Code (Remake!)</a></p><p>Disclosure: The titles are composed by a list of smells and heuristics which can easily be found on the Internet. All content (commentaries) is purely formed by my own opinion, based on my own experience.</p><p>I intend to update the post content every time i learn a new thing, or when i think it is necessary.</p><p><strong>Last update:</strong> 07 July 2017</p><p><em>Index:</em></p><p><strong>The Clean Code</strong></p><ul><li>Comments</li><li>Environment</li><li>Functions</li><li>General</li><li>Names</li></ul><h3>The Clean Code</h3><h4>Comments</h4><p>Never use //TODO. Use kanban instead (Trello ❤).</p><p>Don’t put sensitive information on your comments, mainly if it is a code that will be visible to the client.</p><p><em>Javascript:</em></p><ul><li>gulp-uglify removes commentaries from the code by default.</li></ul><p><strong>C1: Inappropriate Information</strong></p><p><strong>C2: Obsolete Comment</strong></p><p><strong>C3: Redundant Comment</strong></p><pre>// The age of the person<br>var personAge;</pre><p><strong>C4: Poorly Written Comment</strong></p><p><strong>C5: Commented-Out Code</strong></p><p>Just remove it. Git keeps the history.</p><h4>ENVIRONMENT</h4><p>One command to rule then all.</p><p>Use continuous integration and tests automation.</p><p><strong>E1: Build Requires More Than One Step</strong></p><pre>./build</pre><p><strong>E2: Tests Require More Than One Step</strong></p><pre>npm test</pre><h4>FUNCTIONS</h4><p><strong>F1: Too Many Arguments</strong></p><pre>// No<br>function createReport(name, type, outputFile, verbose)</pre><pre>// Yes<br>function createCsvReport(name, options)</pre><p><strong>F2: Output Arguments</strong></p><p>It is ok only if you don’t update things implicitly.</p><p><strong>F3: Flag Arguments</strong></p><pre># No<br>def update(data, replace=False)</pre><pre># Yes<br>def replace(data)<br>def update(data)</pre><p><strong>F4: Dead Function</strong></p><p>Remove all unused functions. Git keeps the history.</p><h4>GENERAL</h4><p><strong>G1: Multiple Languages in One Source File</strong></p><p><strong>G2: Obvious Behavior Is Unimplemented</strong></p><p><strong>G3: Incorrect Behavior at the Boundaries</strong></p><p><strong>G4: Overridden Safeties</strong></p><p><strong>G5: Duplication</strong></p><p>Im my opinion this is the most important rule.</p><p>Follow the Dry principle (Don’t Repeat Yourself).</p><p><strong>G6: Code at Wrong Level of Abstraction</strong></p><p><strong>G7: Base Classes Depending on Their Derivatives</strong></p><p><strong>G8: Too Much Information</strong></p><p>Limit what you expose.</p><p><strong>G9: Dead Code</strong></p><p>Don’t catch exceptions that will never be thrown.</p><p>Else blocks that will never be reached.</p><p><strong>G10: Vertical Separation</strong></p><p>Create a project standard for vertical separation and follow it.</p><p>For example: All functions declaration before their usage. Or, declare the function right after its first usage.</p><p><strong>G11: Inconsistency</strong></p><p>Things that have the same meaning should have consistent conventions.</p><p>Method names:</p><p><em>Java:</em></p><pre>String getUser()<br>String findPerson()<br>String retrieveEmployee()</pre><pre>// Choose only one naming convention<br>String getUser()<br>String getPerson()<br>String getEmployee()</pre><p>Arguments:</p><p><em>Javascript:</em></p><pre><br>function route1(url, (req, res) =&gt; {})<br>function route2(url, (request, response) =&gt; {})<br>function route3(url, (req, resp) =&gt; {})</pre><pre>// Much better using the same name convention as the language design<br>function route1(url, (req, res) =&gt; {})<br>function route2(url, (req, res) =&gt; {})<br>function route3(url, (req, res) =&gt; {})</pre><p><strong>G12: Clutter</strong></p><p><strong>G13: Artificial Coupling</strong></p><p><strong>G14: Feature Envy</strong></p><p><a href="https://sourcemaking.com/refactoring/smells/feature-envy">https://sourcemaking.com/refactoring/smells/feature-envy</a></p><p><strong>G15: Selector Arguments</strong></p><p><strong>G16: Obscured Intent</strong></p><p>Don’t make me think. Be expressive.</p><p><strong>G17: Misplaced Responsibility</strong></p><p><strong>G18: Inappropriate Static</strong></p><p><em>Javascript:</em> inappropriate <strong>global</strong>.</p><p><strong>G19: Use Explanatory Variables</strong></p><p>The far the variable is from where it is used, the better (and probably also bigger) its name should be.</p><p><strong>G20: Function Names Should Say What They Do</strong></p><p><strong>G21: Understand the Algorithm</strong></p><p>Know exactly how your code works.</p><p>TDD will help you.</p><p>Care when copy pasting from stack overflow.</p><p><strong>G22: Make Logical Dependencies Physical</strong></p><p><strong>G23: Prefer Polymorphism to If/Else or Switch/Case</strong></p><p><strong>G24: Follow Standard Conventions</strong></p><p>Use a linter.</p><p>When working in a team, the whole team should follow a pre-defined set of code conventions.</p><p>I am currently using <a href="https://standardjs.com/">standard</a> for Javascript projects.</p><p><strong>Tabs vs Spaces</strong>? Use the language convention.</p><blockquote>“Use 4 spaces per indentation level” — Python PEP8</blockquote><blockquote>“uses tabs (width = 8) for indentation and blanks for alignment” — Go Gofmt</blockquote><p><strong>G25: Replace Magic Numbers with Named Constants</strong></p><pre># Bad<br>def radius(circumference):<br>  return circumference / (2.0 * 3.14)</pre><pre># Good<br>def radius(circumference):<br>  return circumference / (2.0 * <strong>math.pi</strong>)</pre><p><strong>G26: Be Precise</strong></p><p>Always handle your errors.</p><p><em>Java:</em> Use BigDecimal when dealing with currency.</p><p><strong>G27: Structure over Convention</strong></p><p>Prefer polymorphism, interfaces, High-order-functions over if/else and switch/case.</p><p><strong>G28: Encapsulate Conditionals</strong></p><p><strong>G29: Avoid Negative Conditionals</strong></p><pre>// bad<br>if !list.IsEmpty()</pre><pre>// good<br>if list.HasItems()</pre><p><strong>G30: Functions Should Do One Thing</strong></p><p>Follow the Single responsibility principle.</p><p>More on <a href="https://en.wikipedia.org/wiki/SOLID_(object-oriented_design)">SOLID</a>.</p><p><strong>G31: Hidden Temporal Couplings</strong></p><p><strong>G32: Don’t Be Arbitrary</strong></p><pre>// bad<br>if () {<br>}<br>else {<br>}</pre><pre>// good<br>if () {<br>} else {<br>}</pre><p><strong>G33: Encapsulate Boundary Conditions</strong></p><p><strong>G34: Functions Should Descend Only One Level of Abstraction</strong></p><p><strong>G35: Keep Configurable Data at High Levels</strong></p><p><em>Go:</em></p><pre>var port int</pre><pre>func main () {<br>  flag.Parse()<br>}</pre><pre>func init() {<br>  flag.IntVar(&amp;port, &quot;port&quot;, 8080, &quot;The server port&quot;)<br>}</pre><p><strong>G36: Avoid Transitive Navigation</strong></p><p>Know what is Law of Demeter (principle of least knowledge).</p><h3>Names</h3><h4>N1: Choose Descriptive Names</h4><p>Take your time to think in good name, it is one of the most important things in a software.</p><h4>N2: Choose Names at the Appropriate Level of Abstraction</h4><h4>N3: Use Standard Nomenclature Where Possible</h4><p><em>Python:</em></p><pre>def __repr__():<br>  pass</pre><pre>def __str__():<br>  pass</pre><h4>N4: Unambiguous Names</h4><h4>N5: Use Long Names for Long Scopes</h4><h4>N6: Avoid Encodings</h4><h4>N7: Names Should Describe Side-Effects</h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9080d9ab67c1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hacking Meteor DDP]]></title>
            <link>https://medium.com/hackernoon/hacking-meteor-ddp-9da37790b37b?source=rss-6ecbf115969f------2</link>
            <guid isPermaLink="false">https://medium.com/p/9da37790b37b</guid>
            <category><![CDATA[ddp]]></category>
            <category><![CDATA[meteor]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[functional-testing]]></category>
            <dc:creator><![CDATA[Diego Balduini]]></dc:creator>
            <pubDate>Mon, 02 Jan 2017 17:57:29 GMT</pubDate>
            <atom:updated>2019-05-21T11:39:20.556Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iO6nOcogAGL7brbqVol5cQ.jpeg" /><figcaption>the meteor by ready2freeze</figcaption></figure><blockquote>TD;DR, Track all messages going through the DPP connection of your Meteor application by monitoring the <strong>Meteor.connection._stream</strong>.</blockquote><p>Creating functional tests on Meteor can be a pain. It’s reactivity is great for UX, but things can get complicated when we are talking about functional testing.</p><p>On this project i’ve been working on, we were using <a href="http://nightwatchjs.org/">Nightwatch</a> to write our functional tests. This <a href="https://hackernoon.com/tagged/tool">tool</a> runs browser testing in a <strong>Selenium</strong> server and allows us to write test specifications with <strong>Nodejs</strong>.</p><p>Some weeks ago i was trying to figure out how to wait for the result of a Meteor’s Method before triggering a <em>click</em> command on some button in one page of the project’s web site.</p><p>There wasn’t an easy way of waiting for this result. Also, we try to avoid using the <a href="http://nightwatchjs.org/api#pause"><em>pause</em> command</a> that Nightwatch provides.</p><p>A lot of bad things can happen when someone infers how much time is necessary to wait for a computation to be completed, e.g., waiting for some CSS class is added to a <em>div</em> after the form is submitted.</p><p>Since <em>pause</em> was not an option, i needed another way of waiting for the result of this Method. After some investigation, i’ve come up with the idea of listening to <strong>DDP</strong> messages.</p><h4>Meteor’s DDP</h4><blockquote>DDP is Meteor’s built-in pub/sub and RPC protocol</blockquote><p>There are already many articles on the internet which will give you a nice introduction to the DDP protocol. You can also read more about DDP on the Meteor’s documentation at <a href="https://guide.meteor.com/accounts.html#userid-ddp">this link</a>.</p><p>In short, DDP is a JSON based protocol which <a href="https://hackernoon.com/tagged/meteor">Meteor</a> had implemented on top of <strong>SockJS</strong>. It serves to create a full duplex communication between client and server where it can change data and react to its changes.</p><blockquote>SockJS is a Javascript library that emulates WebSockets</blockquote><p>When you use <em>Meteor.method, Meteor.call</em>, <em>Meteor.publish</em> and <em>Meteor.subscribe</em>, Meteor is using DDP underneath.</p><p><strong>Listening to DDP messages</strong></p><p>It is possible to find the DDP connection created by Meteor on the client-side by accessing a variable called <strong>_stream</strong>, on the Meteor global object:</p><pre>Meteor.connection._stream</pre><p>With this object we can send and listen to any message we want on the client side. The following <em>gist</em> shows an example of a very simple code to do that:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7791e62cb404f8df1bcd6cfa2a636a39/href">https://medium.com/media/7791e62cb404f8df1bcd6cfa2a636a39/href</a></iframe><p>First we save the reference for the original send method.</p><p>Then we override the <em>send</em> method with the desired function. On the example, it just print the message after sending it to the server by the original <em>send</em> method.</p><p>Finally, we register a new listener for <strong>message</strong> events, which also will just print everything it receives from the server. All listeners previously registered will not be affected.</p><p>If you have a Meteor application, you can copy and paste this snippet on your browser’s console and it will start tracking everything going through the DDP.</p><p>The first thing you might notice is the ping/pong messages. Meteor keep sending these messages after some interval of time to check if the connection is still alive.</p><pre>&gt; {&quot;msg&quot;:&quot;pong&quot;}<br>&gt; {&quot;msg&quot;:&quot;ping&quot;}</pre><p><strong>Waiting for a Method response</strong></p><p>There are my types of messages you might find in the stream. You can check this out by navigating through your application and monitoring what is happening in the console.</p><p>I used the message with type <strong>method </strong>to remove the dependency of the <strong>pause</strong> command from my test. The test now waits until the <strong>result</strong> message arrives, searching for it in the browser console during some intervals.</p><pre>Meteor.call(&#39;foo&#39;);</pre><pre>&gt; {&quot;msg&quot;:&quot;method&quot;, &quot;method&quot;:&quot;foo&quot;, &quot;params&quot;:[], &quot;id&quot;:&quot;42&quot;}</pre><pre>&gt; {&quot;msg&quot;:&quot;result&quot;, &quot;id&quot;:&quot;42&quot;, &quot;result&quot;:[{&quot;a&quot;:1}, {&quot;b&quot;:2}]}</pre><p>The <strong>result</strong> does not include the name of the method, but it can be found on the <strong>method</strong> message as value of the <em>method</em> property. They are linked by the <strong>id</strong> property.</p><p>The same applies to waiting for a <em>subscription</em> to be ready.</p><pre>Meteor.subscribe(&#39;bar&#39;);</pre><pre>&gt; {&quot;msg&quot;:&quot;sub&quot;, &quot;id&quot;:&quot;abc1def3&quot;, name:&quot;bar&quot;, params:[]}</pre><pre>&gt; {msg:&quot;ready&quot;, subs:[&quot;abc1def3&quot;]}</pre><h4>Conclusion</h4><p>Besides functional testing, some other cool stuffs you can do with this Meteor Hack are:</p><ul><li>Debugging</li><li>Find memory Leak</li><li>Analyse security issues</li></ul><h4>References</h4><ul><li><a href="https://meteorhacks.com/introduction-to-ddp/">https://meteorhacks.com/introduction-to-ddp/</a></li><li><a href="https://docs.meteor.com/api/connections.html">https://docs.meteor.com/api/connections.html</a></li><li><a href="https://github.com/sockjs/sockjs-node">https://github.com/sockjs/sockjs-node</a></li><li><a href="http://docs.seleniumhq.org/">http://docs.seleniumhq.org/</a></li></ul><figure><a href="http://bit.ly/HackernoonFB"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0hqOaABQ7XGPT-OYNgiUBg.png" /></a></figure><figure><a href="https://goo.gl/k7XYbx"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vgw1jkA6hgnvwzTsfMlnpg.png" /></a></figure><figure><a href="https://goo.gl/4ofytp"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gKBpq1ruUi0FVK2UM_I4tQ.png" /></a></figure><blockquote><a href="http://bit.ly/Hackernoon">Hacker Noon</a> is how hackers start their afternoons. We’re a part of the <a href="http://bit.ly/atAMIatAMI">@AMI</a> family. We are now <a href="http://bit.ly/hackernoonsubmission">accepting submissions</a> and happy to <a href="/service/mailto:partners@amipublications.com">discuss advertising &amp; sponsorship</a> opportunities.</blockquote><blockquote>If you enjoyed this story, we recommend reading our <a href="http://bit.ly/hackernoonlatestt">latest tech stories</a> and <a href="https://hackernoon.com/trending">trending tech stories</a>. Until next time, don’t take the realities of the world for granted!</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*35tCjoPcvq6LbB3I6Wegqw.jpeg" /></figure><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Fdde502%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2Fhackernoon%2F&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href">https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9da37790b37b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/hacking-meteor-ddp-9da37790b37b">Hacking Meteor DDP</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Lazy Evaluation in Javascript]]></title>
            <link>https://medium.com/hackernoon/lazy-evaluation-in-javascript-84f7072631b7?source=rss-6ecbf115969f------2</link>
            <guid isPermaLink="false">https://medium.com/p/84f7072631b7</guid>
            <category><![CDATA[lazy-evaluation]]></category>
            <category><![CDATA[call-by-name]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Diego Balduini]]></dc:creator>
            <pubDate>Tue, 20 Dec 2016 17:41:36 GMT</pubDate>
            <atom:updated>2019-05-21T11:39:22.325Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>TD;DR, Lazy Evaluation in Javascript object properties can be easily performed with Object.defineProperty().</blockquote><p>Some time ago i was searching for a way to reach some kind of <strong>lazy evaluation</strong> in <strong>node.js</strong>.</p><p>I was building an object that must have an attribute containing an instance of its same kind (recursively). This could be achieved through a function instead of an attribute, but this was a very specific scenario.</p><p>So what i really needed was way to run this instantiation only when the object property was called, and not during the object construction.</p><p>Even tough i found some Javascript articles about <em>call-by-name or call-by-need</em> strategies on <em>stackoverflow</em>, nothing was working for what i was trying to do.</p><p>Most articles, code and libraries i came across was achieving <strong>lazy evaluation</strong> through a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy">Proxy</a>. But this kind of object is not available in older versions of <a href="https://hackernoon.com/tagged/nodejs">node.js</a>, which was my case.</p><p>In brief, Lazy Evaluation, also known as call-by-need, is defined as:</p><blockquote><em>Call by need</em> is a <a href="https://en.wikipedia.org/wiki/Memoization">memoized</a> version of call by name.</blockquote><p>But what is a call by name? And what about memoized?</p><h4>Call by Name &amp; Call by Value</h4><p>First, lets examine these two functions:</p><pre>// Evaluates with call-by-name strategy<br>1 function callByName (a, b) {<br>2  if (a === 1) {<br>3    return 10<br>4  }<br>5  return a + b<br>6 }</pre><pre>// Evaluates with call-by-value strategy<br>1 function callByValue (a, b) {<br>2  if (a === 1) {<br>3    return 10<br>4  }<br>5  return a + b<br>6 }</pre><p>Keep in mind that this is not Javascript. The first function evals through call-by-name, and the second one through call-by-value.</p><p>To understand the difference between both types of evaluation strategies, we can look line by line of some pseudocode of how the execution might be executed.</p><pre>1 &gt; callByName (1, 2 + 3)<br>2 &gt; a === 1<br>3 &gt; return 10</pre><pre>1 &gt; callByValue(1, 2 + 3)<br>1 &gt; callByValue(1, 5)<br>2 &gt; a === 1<br>3 &gt; return 10</pre><p>You might seem familiar with the second one. This is because Javascript only passes arguments as values (or by reference if it is an object).</p><p>In a <strong>call-by-value</strong> function (or <em>Eager Evaluation</em>) the numbers 2 and 3 are first summed up and then they are passed as function argument with the value 5.</p><p>On the other hand, in the <strong>call-by-name </strong>function (or <em>Lazy Evaluation</em>) showed in the example, the second parameter <strong>b </strong>is not even evaluated since the function returns 10 before <strong>b</strong> is used by the function.</p><h4>Memoization</h4><p>In short, <em>memoization</em> is an optimization technique to store pre-computed results (cache), to avoid recomputation for the same inputs.</p><p>In a more formal way, this means that if an idempotent function</p><pre><strong>f(x) = g(x) + 1</strong></pre><p>will<strong> </strong>always output some value <strong>y </strong>when it receives an input <strong>v</strong>, then <strong>f(v) = y</strong>.</p><p>So if <strong>g(v)</strong> is very <em>expensive</em> in terms of time or machine resources, we can store the result <strong>y</strong> in a cache to be returned when <strong>v</strong> is the input, such that we don’t have to recompute <strong>g(v) </strong>again.</p><pre><strong>cache = {}</strong></pre><pre><strong>cache[v] = y</strong></pre><h4>The Infinite Recursion Problem</h4><p>Now we will go through some very straightforward code of a recursive object which does lazy evaluation to avoid infinite recursion.</p><p>We will analyse an infinite data structure called Stream, very common on Functional <a href="https://hackernoon.com/tagged/programming">Programming</a> languages.</p><p>What would happen if we try to run this code?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c186b1f9a0e951d9cf035cb9656d4e8d/href">https://medium.com/media/c186b1f9a0e951d9cf035cb9656d4e8d/href</a></iframe><pre>var stream = new Stream(10);</pre><pre>&gt; RangeError: Maximum call stack size exceeded</pre><p>As expected, it would fail with a <strong>RangeError</strong>.</p><p>The problem is in the line of code below, where we are doing an <em>Eager loading</em> of the <strong>next </strong>attribute<strong>.</strong></p><pre>this.next = new Stream(value + 1);</pre><p>Let’s change the Stream to be <em>Lazy</em> instead.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f8134722296bce3dc539068af4cfa807/href">https://medium.com/media/f8134722296bce3dc539068af4cfa807/href</a></iframe><p>Note that i also added a <strong>takeUntil</strong> method to the Stream. Lets move on and try to run this code.</p><pre>var stream = new Stream(10);</pre><pre>console.log(stream.takeUntil(20))</pre><pre>&gt; [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]</pre><p>Now, not only we can build the Stream, but it is also possible to run <em>infinite </em>algorithms like <strong>takeUntil</strong> with this implementation.</p><p>You may be thinking: Why we didn’t use <em>memoization </em>here?</p><p>Well, we could, but this <em>optimization</em> is no worthy since we are doing just a basic math operation of summing two operands. This is not expensive computationally like it would be, for example, to calculate if <strong>value </strong>is a prime number.</p><p>References:</p><ul><li><a href="http://stackoverflow.com/questions/6605640/javascript-by-reference-vs-by-value#6605700">http://stackoverflow.com/questions/6605640/javascript-by-reference-vs-by-value#6605700</a></li><li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty</a></li><li><a href="https://en.wikipedia.org/wiki/Evaluation_strategy">https://en.wikipedia.org/wiki/Evaluation_strategy</a></li><li><a href="https://en.wikipedia.org/wiki/Memoization">https://en.wikipedia.org/wiki/Memoization</a></li></ul><blockquote><a href="http://bit.ly/Hackernoon">Hacker Noon</a> is how hackers start their afternoons. We’re a part of the <a href="http://bit.ly/atAMIatAMI">@AMI</a>family. We are now <a href="http://bit.ly/hackernoonsubmission">accepting submissions</a> and happy to <a href="/service/mailto:partners@amipublications.com">discuss advertising &amp;sponsorship</a> opportunities.</blockquote><blockquote>To learn more, <a href="https://goo.gl/4ofytp">read our about page</a>, <a href="http://bit.ly/HackernoonFB">like/message us on Facebook</a>, or simply, <a href="https://goo.gl/k7XYbx">tweet/DM @HackerNoon.</a></blockquote><blockquote>If you enjoyed this story, we recommend reading our <a href="http://bit.ly/hackernoonlatestt">latest tech stories</a> and <a href="https://hackernoon.com/trending">trending tech stories</a>. Until next time, don’t take the realities of the world for granted!</blockquote><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Fdde502%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2Fhackernoon%2F&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href">https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=84f7072631b7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/lazy-evaluation-in-javascript-84f7072631b7">Lazy Evaluation in Javascript</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>