<?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 Felipe Gonçalves Marques on Medium]]></title>
        <description><![CDATA[Stories by Felipe Gonçalves Marques on Medium]]></description>
        <link>https://medium.com/@marques.goncalves.felipe?source=rss-38935e863dd5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*6xwP4VdQZ1BLwpWMhVpLOw.jpeg</url>
            <title>Stories by Felipe Gonçalves Marques on Medium</title>
            <link>https://medium.com/@marques.goncalves.felipe?source=rss-38935e863dd5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 05:46:38 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@marques.goncalves.felipe/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[My thoughts on types]]></title>
            <link>https://medium.com/taqtilebr/my-thoughts-on-types-c77d89422687?source=rss-38935e863dd5------2</link>
            <guid isPermaLink="false">https://medium.com/p/c77d89422687</guid>
            <category><![CDATA[code]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Felipe Gonçalves Marques]]></dc:creator>
            <pubDate>Tue, 03 Jul 2018 11:06:25 GMT</pubDate>
            <atom:updated>2018-07-03T11:06:25.356Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*T355La32-3h11sgb8gZ2mQ.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/j0g8taxHZa0?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Samuel Zeller</a> on <a href="https://unsplash.com/search/photos/abstract?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>My two last posts were focused specifically in Angular. So I decided to write this one to talk about a more general software development concept : Types. But the examples will still be in Typescript. ;)</p><h4>TL;DR;</h4><p>Functions and class fields should always be typed. This includes both functions arguments and returns values, also public and private fields.</p><h4>Introduction</h4><p>Types allows us to specify what we expect a variable to be. This brings two benefits:</p><ul><li>Errors are thrown when this expectation is broken:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/509/1*zCP0mRU8kIQ5wtYFFv9-Kw.png" /></figure><ul><li>Our code conveys more information about its intent, being easier to understand</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/219844df0de8a047dda843d4e2ac3488/href">https://medium.com/media/219844df0de8a047dda843d4e2ac3488/href</a></iframe><p>But sometimes, typing everything can be really annoying and might not bring enough benefits (Java feelings…).</p><p>Lots of modern languages as Python, Ruby and Javascript uses dynamic typing which frees us from having to specify every variable type. Other languages uses static typing with types inference as TypeScript. It allows us to specify types to our variables, but if we don&#39;t, it&#39;ll try to infere it.</p><p>So as we can see, there is a trade-off between using or not type checking. Currently, I have the following opinion about it:</p><h4>When to use types?</h4><p><strong>#1 Types should be used whenever we have data coming from outside or going outside of the application</strong></p><p>If we are using a library, fetching information from an API through HTTP or answering requests in the server, we should use types.</p><p>When we have data coming and going from the application, we have expectation about it. Types help make those expectations clear.</p><p>They define what information we expect to receive and what information we will be sending. This allows anyone using the application to understand its interface and, if you have some <a href="https://8thlight.com/blog/christoph-gockel/2017/06/14/api-assumption-tests.html">type of test</a> that checks your expectations, it is easy to find breaking changes.</p><p>Lot of companies are using tools as Apollo GraphQL, Apache Thrift, etc, because those tools allow us to keep the API models synchronised through client and server. They align the expectation between the communication models — they also do a lot of other stuff.</p><p>Also the Typescript community puts a lot of effort in keeping Types definition for Javascript libraries consistent and working in the <a href="https://github.com/DefinitelyTyped/DefinitelyTyped">DefinitelyTyped repository</a>.</p><p>Personally, I think that if we begin using types in those entrypoints of data, will be much easier to keep using types inside the application.</p><p>PS: In the <a href="https://8thlight.com/blog/uncle-bob/2012/08/13/the-clean-architecture.html">clean architecture</a>, every layer frontier is an entrypoint of data, so it should be typed.</p><p><strong>#2 Types should always be used whenever we have class fields or functions</strong></p><p>Class fields (both private and public) and functions are used distant from its definition (can be a few lines or a few files away). So to guarantee that it is being used correctly, we should use types.</p><p>It allows us to catch mistakes at compile-time and convey information about the contract that this artefact defines.</p><p>For example, suppose we have the following method to fetch users:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5e0e6a06f5f2a85d1b8826a73048e4a2/href">https://medium.com/media/5e0e6a06f5f2a85d1b8826a73048e4a2/href</a></iframe><p>When we use it at some point at our application, we get no information from its signature about the params it expects. We may check its documentation or its implementation — hopping it is simple enough to understand at a glance. But, by lack of attention, we may do a mistake and end up using it like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/046e285901c3586728eee3dab199609a/href">https://medium.com/media/046e285901c3586728eee3dab199609a/href</a></iframe><p>The result will not be as expected and we&#39;ll have to check the documentation or the implementation again…</p><p>But, if we modify our method to the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/840535525060389d6dc32909fd17afec/href">https://medium.com/media/840535525060389d6dc32909fd17afec/href</a></iframe><p>There will be an warning in compile-time or even in the Text Editor, saving some time and some users from a bad experience.</p><p><strong>#3 It is ok to not use types in small scopes with variables that will not be returned or passed away.</strong></p><p>If you are implementing a small function and in its implementation you use some variables, it might be ok to not type it. Since this will be restricted to these scopes, we don&#39;t face the problem of it being misused somewhere else.</p><p>For example, if I have the following code, it is ok to not type the private variables in these scope.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b4cfb11e55d8db7d48704adb89839a59/href">https://medium.com/media/b4cfb11e55d8db7d48704adb89839a59/href</a></iframe><p>Both the status and the emails variable are implicit defined in the User and UserFilter interface, so it is ok to not type then and let Typescript infere its type.</p><h4>Conclusion</h4><p>A good rule of thumb for defining when to use types is: &quot;How far is this going to be used from its definition?&quot;.</p><p>If it is a more then a few lines from the definition, it should be typed.</p><p>There are lot of tools that can be used to enforce typing:</p><ul><li><a href="https://8thlight.com/blog/christoph-gockel/2017/06/14/api-assumption-tests.html">API Assumption Test — 8th Light Blog</a></li><li><a href="https://flow.org/en/">Flow Type Checker for Javascript</a></li><li><a href="https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html">Typescript declaration files</a></li></ul><p>And almost every language has functionalities as <a href="https://www.typescriptlang.org/docs/handbook/generics.html">generics</a>, <a href="http://www.typescriptlang.org/docs/handbook/advanced-types.html">partial</a>, <a href="https://www.typescriptlang.org/docs/handbook/enums.html">enums.</a></p><p>So, remember the old saying: Better (type-)safe than sorry. ;)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c77d89422687" width="1" height="1" alt=""><hr><p><a href="https://medium.com/taqtilebr/my-thoughts-on-types-c77d89422687">My thoughts on types</a> was originally published in <a href="https://medium.com/taqtilebr">TaqtileBR</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Injecting Context through Structural Directives in Angular]]></title>
            <link>https://medium.com/taqtilebr/injecting-context-through-structural-directives-in-angular-218ff35645c7?source=rss-38935e863dd5------2</link>
            <guid isPermaLink="false">https://medium.com/p/218ff35645c7</guid>
            <category><![CDATA[code]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Felipe Gonçalves Marques]]></dc:creator>
            <pubDate>Tue, 19 Jun 2018 22:40:07 GMT</pubDate>
            <atom:updated>2018-06-19T22:40:07.120Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HKcGP1PeBgVBCPy8wZGY0Q.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/AZK6C3jKZEQ?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Daniel von Appen</a> on <a href="https://unsplash.com/search/photos/pattern?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>In my <a href="https://medium.com/taqtilebr/splitting-data-and-presentation-components-in-angular-7e20ad5631c1">previous post</a>, I show how we can use <strong>ng-template </strong>with <strong>ngTemplateRouterOutlet </strong>to separate data fetching logic from presenting the data, without polluting other classes. But, specifically for that example, there was a lot of overhead: the data component did not apply any presentation logic (a loading, a show/hide feature, etc), so we have an unnecessary <strong>ng-container</strong>.</p><p>A simpler result can be obtained using Structural Directive, although it is against the Angular recommendation:</p><blockquote>&quot;Structural directives are responsible for <strong>HTML layout</strong>. They <strong>shape</strong> or <strong>reshape</strong> the <strong>DOM’s <em>structure</em></strong>, typically by adding, removing, or manipulating elements.&quot; <a href="https://angular.io/guide/structural-directives">Angular Guide</a></blockquote><p>But, I think it brings a nice readable layout and provides the functionality we want without too much overhead. Before presenting the solution, let me explain what are Structural Directives.</p><h4>Structural Directives</h4><p>This directives are applied to an HTML tag (which can be native or an Angular Component). Its constructor receives a <strong>TemplateRef, </strong>consisting of the element it is being applied to, and a <strong>ViewContainerRef, </strong>that represents the view where the component would be rendered. (Actually, Angular replaces the element with an <strong>ng-template </strong>and uses it to render the component).</p><p>This is why this kind of directive are used mainly for HTML layout. The responsibility of rendering the component is passed to them, so we can use only one of these per tag. Examples of this directives are: <strong>*ngFor, *ngIf, etc.</strong></p><p>The <strong>ViewContainerRef </strong>allows you to control what is rendered, and inject a context object. In our case, we will be using the following method:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2618bebd13daaca5c2e4a5481fc141d1/href">https://medium.com/media/2618bebd13daaca5c2e4a5481fc141d1/href</a></iframe><p>In other words, we will inject the exactly same context object as before!</p><h4>The code</h4><p>So let&#39;s take a look in how we can implemente this.</p><p>This is the code of the directive. As you can see, it doesn&#39;t change much from what we did before:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f77877ffa64a6f62a62357d77b5dd835/href">https://medium.com/media/f77877ffa64a6f62a62357d77b5dd835/href</a></iframe><p>The difference is that instead of having a <strong>ContentChild, </strong>we receive the <strong>TemplateRef</strong> on the constructor. Also, there is no *<strong>ngTemplateOutlet.</strong> We just call the <strong>viewContainerRef</strong> method: <strong>createEmbeddedView </strong>to render the TemplateRef.</p><p>In this case, we call it on the <strong>ngOnInit, </strong>because the presentation does not change, only the context. In <strong>ngFor </strong>and <strong>ngIf, </strong>the interaction with the<strong> viewContainerRef </strong>may<strong> </strong>happen in different points, since what is rendered depends on what is passed as input to the directive.</p><p>To use this directive we must do:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fb263551f430db976731f13c512caa9d/href">https://medium.com/media/fb263551f430db976731f13c512caa9d/href</a></iframe><p>As you can see, we use a syntax similar to the <strong>*ngFor. </strong>We are basically doing the same variables binding that we did in the last post with <strong>ng-template</strong>. Actually, it is exactly the same, but Angular allows us to use a sugar syntax. This brings us the last topic of the post.</p><h4><strong>Structural Directive Microsyntax</strong></h4><p>Angular allows us to use this very semantic syntax with the directive, because it uses something called Microsyntax. It takes the the string you passed to the directive and transform it in other directives. I think it is more clear if we see some examples. The <strong>*ngFor </strong>works like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fdbb32ea49ab36b8383578ec606880db/href">https://medium.com/media/fdbb32ea49ab36b8383578ec606880db/href</a></iframe><p>For our custom directive, it works like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/672cb84e544821a8a1a2b208361687f3/href">https://medium.com/media/672cb84e544821a8a1a2b208361687f3/href</a></iframe><p>The transformation it does is:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3861e0f047a9ac3f43e010594938bb2d/href">https://medium.com/media/3861e0f047a9ac3f43e010594938bb2d/href</a></iframe><p>So, for the <strong>let </strong>statement, it transform in variables binding. For other directives (as <strong>of</strong> and <strong>trackBy</strong>), it transforms in Input directives by <em>title-casing</em> the statement and <em>prefixing</em> it with the the directive name. And thats all!</p><p>Structural directive are a powerful tool for reusability in Angular and are great for improvement code readability with its Microsyntax.</p><p>Hope you gained more understanding of <strong>Structural Directives </strong>and how to use it as building block for some reusable components. Take a look below to get some links to learn more!</p><p>If you want to know more about structural directives, take a look at <a href="https://angular.io/guide/structural-directives#microsyntax">Angular guide about it.</a></p><p>The guide suggests to take a look at <strong>*ngFor </strong>source code, but I got confuse trying to understand it together with the <strong>IterableDiffer </strong>class. So, I think it is better to take a loot at <a href="https://github.com/angular/angular/blob/6.0.5/packages/common/src/directives/ng_template_outlet.ts#L10-L100">*ngTemplateOutlet source code</a>.</p><p>Finally, this article was inspired by <a href="https://angular-2-training-book.rangle.io/handout/advanced-angular/directives/providing_context_variables_to_directives.html">this tutorial</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=218ff35645c7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/taqtilebr/injecting-context-through-structural-directives-in-angular-218ff35645c7">Injecting Context through Structural Directives in Angular</a> was originally published in <a href="https://medium.com/taqtilebr">TaqtileBR</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Splitting Data and Presentation Components in Angular]]></title>
            <link>https://medium.com/taqtilebr/splitting-data-and-presentation-components-in-angular-7e20ad5631c1?source=rss-38935e863dd5------2</link>
            <guid isPermaLink="false">https://medium.com/p/7e20ad5631c1</guid>
            <category><![CDATA[code]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Felipe Gonçalves Marques]]></dc:creator>
            <pubDate>Mon, 11 Jun 2018 18:07:31 GMT</pubDate>
            <atom:updated>2019-02-25T15:31:44.032Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XHVhBv7qx1RYrJ1Iyl_Sew.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/4wDPAZ9k8C4?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Dmitri Popov</a> on <a href="https://unsplash.com/?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Split the logic of fetching data from how it is presented is a great step towards separation of concerns. It gives us flexibility to change any of those parts without breaking the other, as long as they respect their interfaces.</p><p>Also, sometimes this splitting is what we need to reduce that big classes that represents a screen.</p><p>For example, suppose we have an user registration form and it has an Autocomplete field that looks for Star Wars characters to be selected. The user can choose as many as he wants. Besides that, the form also has other fields as Name, Email, Telephone, etc. We can also add some conditional fields: if the user is a student, we ask for the university otherwise, we can ask for the company.</p><p>The Component representing this form might become really big and any reduction, respecting good practices, is an improvement.</p><p>Supposing we add all the dynamic part of the autocomplete in the Component, we would have something like this (you can also check the <a href="https://stackblitz.com/edit/autocomplete-before-refactor-angular">stackblitz</a>):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9fe822d831d7e087c6d82d4144c8f3ff/href">https://medium.com/media/9fe822d831d7e087c6d82d4144c8f3ff/href</a></iframe><p>The data and presentation are separated here, because the presentation is done by the Autocomplete component. But every time we would like to use the Star Wars Characters Autocomplete, we would have to reimplement the event listeners, the call to the service, the unsubscribe, etc. This can become very repetitive and error prone. So we build a component that handles this!</p><p>The solution here is inspired in <a href="https://blog.angularindepth.com/use-ng-template-c72852c37fba">Isaac Mann article</a> about the Angular equivalent of React renderProps pattern.</p><p>We will build a component that provides the autocomplete with the options and the callbacks through a context object.</p><p>This component is called StarWarsCharactersAutocomplete. Its HTML would look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/59eedc5570e114788dd55ec1eb0d6533/href">https://medium.com/media/59eedc5570e114788dd55ec1eb0d6533/href</a></iframe><p>Here we use <strong>ngTemplateOutlet</strong>. It allows us to render a <strong>TemplateRef</strong> providing some <strong>context</strong> to it. So, the layoutTemplate here can access the variables that are in the context object. In this case we have the <strong>options</strong> and the <strong>search callback</strong>. This directive would be used as:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b742dac94529b5fcc7fdb27924dd79d3/href">https://medium.com/media/b742dac94529b5fcc7fdb27924dd79d3/href</a></iframe><p>Here, we get the two variables we need to bind to the component. Finally, let&#39;s see how the component works:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/64ccf6349761e68a3d1f887af38b09fd/href">https://medium.com/media/64ccf6349761e68a3d1f887af38b09fd/href</a></iframe><p>Here we use <strong>ContentChild</strong> to find the <strong>TemplateRef</strong> that we will pass to the <strong>ngTemplateRouterOutlet</strong>. We also define the variables for the context object and, finally, bind the changes to calls to the Star Wars Service.</p><p>This way we can reduce the AppComponent:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0b8207d8b1368cfd26f97c073dfcaa36/href">https://medium.com/media/0b8207d8b1368cfd26f97c073dfcaa36/href</a></iframe><p>We have isolated both the data fetch service and the autocomplete component. As a result we have much simpler, flexible and reusable components!</p><p>Compared to the React implementation of this pattern, the Angular equivalent has some boilerplate and is not very clear how it works. But it is better then polluting other classes with this logic every time we want to add an autocomplete with this data.</p><p>Finally, I think it is possible to simplify this with just a structural directive, although this is not <a href="https://angular.io/guide/structural-directives">the common usage of this directives as Angular suggest</a>.</p><p>You can check the final code <a href="https://stackblitz.com/edit/autocomplete-after-refactor-angular?file=app%2Fapp.component.ts">here</a>.</p><p>The ideas here are also inspired by <a href="https://medium.com/@dan_abramov/smart-and-dumb-components-7ca2f9a7c7d0">Dan Abramov Presentational and Container Components</a>.</p><p>This is my first medium article! :)</p><p>If you have any suggestions or questions, please let me know!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7e20ad5631c1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/taqtilebr/splitting-data-and-presentation-components-in-angular-7e20ad5631c1">Splitting Data and Presentation Components in Angular</a> was originally published in <a href="https://medium.com/taqtilebr">TaqtileBR</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>