<?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"><channel><title><![CDATA[RSS Feed]]></title><description><![CDATA[This is where I'll share my thoughts]]></description><link>https://alabeduarte.com</link><generator>GatsbyJS</generator><lastBuildDate>Thu, 08 Jan 2026 00:57:19 GMT</lastBuildDate><item><title><![CDATA[Context Engineering: How I've Been Using Claude Code in My Development Workflow]]></title><description><![CDATA[Context engineering with AI assistants is evolving rapidly, and everyone seems to have their own approach. This article shares a workflow I…]]></description><link>https://alabeduarte.com/context-engineering-with-claude-code-my-evolving-workflow/</link><guid isPermaLink="false">https://alabeduarte.com/context-engineering-with-claude-code-my-evolving-workflow/</guid><pubDate>Sat, 13 Sep 2025 09:31:28 GMT</pubDate><content:encoded>&lt;p&gt;Context engineering with AI assistants is evolving rapidly, and everyone seems to have their own approach. This article shares a workflow I’ve been experimenting with over a couple of months using Claude Code: a practical method for maintaining context across sessions, validating understanding, and turning AI collaboration into well-documented, testable code.&lt;/p&gt;
&lt;p&gt;There are heaps of excellent resources on context engineering out there: &lt;a href=&quot;https://www.anthropic.com/engineering/claude-code-best-practices&quot;&gt;Claude Code Best Practices&lt;/a&gt; from Anthropic, &lt;a href=&quot;https://youtu.be/IS_y40zY-hc?si=tgSP87N5SYlrMDnD&quot;&gt;Simon Willison’s exploration of Claude Code&lt;/a&gt;, and &lt;a href=&quot;https://www.youtube.com/watch?v=fHWFF_pnqDk&quot;&gt;Ethan Mollick’s deep dive into working with AI tools&lt;/a&gt; to name a few. Every day brings new articles with tips and tricks for using AI assistants effectively, it can be overwhelming, even repetitive at times. Some are genuinely insightful, others feel like variations on the same theme. What I want to share here is something different: my personal journey over the last few months. Not another prescriptive guide, but the workflow that’s actually stuck and made a tangible difference to how I write code.&lt;/p&gt;
&lt;p&gt;I won’t give you prescriptive prompts or a &lt;code class=&quot;language-text&quot;&gt;CLAUDE.md&lt;/code&gt; file to copy and paste. Instead, I want to explain the workflow and reasoning behind it, so you can adapt it to your own context.&lt;/p&gt;
&lt;h2&gt;Building Context Through Dialogue&lt;/h2&gt;
&lt;p&gt;Working with large codebases presents a unique challenge: features rarely live in isolation. They span multiple files, interact with various systems, and carry implicit knowledge that’s often scattered across team members’ heads and outdated documentation.&lt;/p&gt;
&lt;p&gt;What I found that worked great was starting with a simple premise: I already understand the code I’m working with, but I want to validate that understanding and create a shared context with Claude Code.&lt;/p&gt;
&lt;p&gt;Here’s an example of how this works. I start by describing what I know about how a feature works in my own words. I’m not asking Claude to explain the code to me, I’m explaining it myself and asking for validation. I’ll typically mention an entry point where the logic begins, something like: “The search feature starts in &lt;code class=&quot;language-text&quot;&gt;search/handler.go&lt;/code&gt; and I believe it parses the query, builds the database filters, executes the search, and returns paginated results.”&lt;/p&gt;
&lt;p&gt;Claude Code then gives me its understanding based on the actual code. This is where the magic happens: it’s not about getting an explanation, it’s about calibrating our shared understanding. If there’s a mismatch, I’ll follow up with corrections or clarifications until we’re aligned.&lt;/p&gt;
&lt;p&gt;Once I’m happy with our shared understanding, I ask Claude Code to write this down in a markdown file; let’s call it &lt;code class=&quot;language-text&quot;&gt;SEARCH_FEATURE.md&lt;/code&gt;. Now I have a structured, written form of how this particular feature works. Remember, I’m documenting a specific feature, not the entire codebase. This focused approach keeps things manageable and relevant.&lt;/p&gt;
&lt;h2&gt;From Understanding to Planning&lt;/h2&gt;
&lt;p&gt;With our context documented, I start fresh with a &lt;code class=&quot;language-text&quot;&gt;/clear&lt;/code&gt; command. This clean slate is intentional: it forces me to be explicit about what context is needed for the next phase.&lt;/p&gt;
&lt;p&gt;Now I can say: “Based on @SEARCH_FEATURE.md, I want to add pagination to our search results that currently return everything.”&lt;/p&gt;
&lt;p&gt;But here’s where I add my own thinking to the mix. I’ll usually have ideas about implementation approaches, so I’ll include them: “We could use offset-based pagination which is simple, or cursor-based pagination which handles data changes better. Offset is easier to implement and gives page numbers, but cursor pagination performs better with large datasets. What’s the right choice here?”&lt;/p&gt;
&lt;p&gt;I do this in plan mode (rotate shift + tab in Claude Code), which helps maintain focus on planning rather than jumping straight to implementation. The back-and-forth here is crucial, I challenge Claude Code with questions, explore edge cases, and refine until I’m confident we have not only a direction I’m happy with but also documented reasoning about alternatives considered.&lt;/p&gt;
&lt;h3&gt;Iterative Planning and Documentation&lt;/h3&gt;
&lt;p&gt;Claude Code tends to create plans with multiple phases, and I often tweak these. Sometimes it over-engineers things, so I’ll consolidate or remove phases. This granular control helps break down complex problems into manageable chunks, and I can git commit after each successful phase implementation.&lt;/p&gt;
&lt;p&gt;Depending on the complexity, I’ll either append to the existing markdown file or create a new one for the plan. The key is maintaining that written record of decisions and approach.&lt;/p&gt;
&lt;h3&gt;Using Tests as Validation Checkpoints&lt;/h3&gt;
&lt;p&gt;Here’s a critical addition to my workflow that’s saved me countless hours: I ask Claude Code to write tests first, then use them as validation checkpoints. Instead of me manually checking if the implementation works, Claude runs the tests to verify correctness.&lt;/p&gt;
&lt;p&gt;This TDD approach does consume more tokens due to iterative test-fail-implement-pass cycles and context accumulation. But here’s the thing: I’d write tests anyway, with or without AI. Tests are living documentation that verify the code works as intended. While code remains the source of truth, tests ensure that truth aligns with expectations. The token investment essentially automates what I’d do manually, with the added benefit of reduced debugging cycles and higher confidence in the implementation.&lt;/p&gt;
&lt;p&gt;This creates a powerful feedback loop. The tests become the specification, and Claude Code iterates on the implementation until they pass.&lt;/p&gt;
&lt;h2&gt;Implementation with Precision&lt;/h2&gt;
&lt;p&gt;Time for another &lt;code class=&quot;language-text&quot;&gt;/clear&lt;/code&gt;, starting fresh for the implementation phase. This separation between planning and implementation has been crucial for keeping the context manageable.&lt;/p&gt;
&lt;p&gt;My editor of choice is Neovim with the &lt;a href=&quot;https://github.com/coder/claudecode.nvim&quot;&gt;claudecode.nvim plugin&lt;/a&gt;, which has been particularly helpful for maintaining focus. Instead of saying “implement phase 2”, I can select specific lines from the plan and send them directly with references like &lt;code class=&quot;language-text&quot;&gt;@SEARCH_FEATURE.md#L7-17&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;This approach has helped me explore code more effectively. When I need to understand a particular section, I can select it and ask targeted questions about its behaviour or edge cases. Being able to provide precise context has made these conversations much more productive.&lt;/p&gt;
&lt;p&gt;Most of the time, I prefer to handle the git workflow myself, staging files as chunks of work are completed so I know exactly what’s being added. But I let Claude Code write the commit messages. It’s genuinely excellent at understanding what was done and writing comprehensive commit messages that capture both the what and the why.&lt;/p&gt;
&lt;p&gt;This also helps with continuity. When I return to a feature hours later, instead of providing the entire plan again, I can just give Claude Code a couple of recent commits to read and it picks up right where we left off.&lt;/p&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;My development workflow is evolving, and I’m still figuring it out. The approach I’ve described here is what’s been working for me lately, but it’s constantly changing as I try to keep up with how quickly these tools are improving.&lt;/p&gt;
&lt;p&gt;The biggest shift for me has been becoming more pragmatic. I still care deeply about clean APIs, good abstractions, and code that expresses clear intent, sometimes I find myself arguing with Claude about these things. But now that AI generates code for me, I’m learning to pick my battles. The craft still matters, but I’m less precious about being the one who types it all out.&lt;/p&gt;
&lt;p&gt;Building context through documentation has become central to how I work, though I’m not committing these context files to git yet. These artifacts are still personal tools, they help me maintain continuity between sessions and occasionally help when explaining my thinking to teammates. But there’s a journey ahead in figuring out how this fits with how everyone else on the team works.&lt;/p&gt;
&lt;p&gt;This way of working is still evolving. The tools keep changing, my approach keeps adapting, and I’m still discovering what works and what doesn’t. But for now, this structured context-building has shifted development from wrestling with complexity alone to having a conversation about solving problems, even if sometimes that conversation involves me shouting at Claude about proper error handling.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Retrospectives - Roles and Expectations]]></title><description><![CDATA[In the team that I work with, we run retrospective
ceremonies
every fortnight so that we can reflect on the work we’ve delivered by praising…]]></description><link>https://alabeduarte.com/retrospectives/roles-and-expectations/</link><guid isPermaLink="false">https://alabeduarte.com/retrospectives/roles-and-expectations/</guid><pubDate>Sat, 11 Dec 2021 03:04:14 GMT</pubDate><content:encoded>&lt;p&gt;In the team that I work with, we run &lt;a href=&quot;https://www.thoughtworks.com/en-au/insights/blog/5-things-you-need-know-facilitate-retrospective#:~:text=A%20Retrospective%20is%20a%20ceremony,evaluate%20its%20past%20working%20cycle.&amp;#x26;text=Finally%2C%20retrospective%20is%20a%20moment,improve%20things%20identified%20as%20negative.&quot;&gt;retrospective
ceremonies&lt;/a&gt;
every fortnight so that we can reflect on the work we’ve delivered by praising
our doings and see how we can improve our work or processes. Recently I had the
opportunity to run one of these meetings and this post is to share the format
I’ve used and how things went in general.&lt;/p&gt;
&lt;h3&gt;Formats&lt;/h3&gt;
&lt;p&gt;I reckon that the most popular retrospective format is the triade &lt;em&gt;“What worked,
What we can keep doing and What didn’t work”&lt;/em&gt; or any variation of that (i.e.
smile, sad and neutral faces, etc).&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 630px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/306584caaf316dcb9d27df823366ec8a/0d1a4/3-faces.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 29.746835443037973%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAYAAADDl76dAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAw0lEQVR42mP4T2XAgGD+A5N/X239/+de9///P1+B+X/eHvv/62rZ/79vj0L4707//3m15P/v59uh2v5hM/AfxLhfL/4/3ur0//IC5f8/784Ay3w9mfD/6Wql/9/PpoL5H09l/r8yT/r/qz2eQN4PqDP+4TDw/6//9w43/j+yOOz/7zenwCK/n6z+/2ZvIJBeA1H5fP3/d/s8//+5P5WQl2E+/w3E39AF0VjfUcTwGvgPGCaQYPkHDyMwHx5W/5C8iWkoAKv5ykvPW1ouAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;What worked, What we can keep doing and What didn&amp;#39;t work&quot;
        title=&quot;&quot;
        src=&quot;/static/306584caaf316dcb9d27df823366ec8a/f058b/3-faces.png&quot;
        srcset=&quot;/static/306584caaf316dcb9d27df823366ec8a/c26ae/3-faces.png 158w,
/static/306584caaf316dcb9d27df823366ec8a/6bdcf/3-faces.png 315w,
/static/306584caaf316dcb9d27df823366ec8a/f058b/3-faces.png 630w,
/static/306584caaf316dcb9d27df823366ec8a/40601/3-faces.png 945w,
/static/306584caaf316dcb9d27df823366ec8a/0d1a4/3-faces.png 1036w&quot;
        sizes=&quot;(max-width: 630px) 100vw, 630px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;This format is great, but there are times where it can be quite challenging if
we want to touch on certain topics. For example, if I want to assess how aligned
we all are with regards to the role each one of us has to play in the Software
Development, how can I discuss that if all I have is “What worked”, “What didn’t
work”, etc?&lt;/p&gt;
&lt;p&gt;In our team, there are a quite diverse number of roles, which is actually true
in most of Software Development teams nowadays. We have people that are more
focused on the product direction, people that are looking into how the user
experience should be and Software Developers/Engineers like myself.&lt;/p&gt;
&lt;h2&gt;How the activity went out&lt;/h2&gt;
&lt;p&gt;The retrospective was divided into 2 steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Bullet point gathering from each participant and then, translated them into
sticky notes trying to form a timeline from the &lt;strong&gt;Idea&lt;/strong&gt; to &lt;strong&gt;Working Software&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Asked the participants to fill out the &lt;strong&gt;Roles and Expectations&lt;/strong&gt; matrix&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;based on the bullet points above.&lt;/p&gt;
&lt;h4&gt;First step: bullet points from Idea to Software lifecycle&lt;/h4&gt;
&lt;p&gt;The first step was to gather bullet points from everyoneasking &lt;strong&gt;what are the
steps to deliver a software, starting from the idea until the actual software in
the hands of a customer&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;I wanted to avoid any kind of bias, so I did this &lt;strong&gt;anonymously&lt;/strong&gt;.  Once I had
all the data from everyone, I’ve added sticky notes to represent the different
steps provided by everyone in some sort of timeline.&lt;/p&gt;
&lt;p&gt;Here’s an example:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 630px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/60a56c968bddc261b2f8521a3c83d421/1843f/retro-step-1.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 28.48101265822785%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAYAAADDl76dAAAACXBIWXMAAAsTAAALEwEAmpwYAAABMUlEQVR42l2Q6U7CUBCF+3g+kfE1fBV/mJi4xuBC0IgLpJDQAgXF2hawLeVun4O4oCc5mTuZM+dOjscm3JpusxEsrcVY9ytyX/zR2S86vLLM+Y9vbTgvuIvfOAqHtKSuYH+MNmFkqfp8ef2gJgYFxfyRQUcxiirqSZ9ZOSJ47XHUfiKLD2mGNYK0xKkJy+YD6r2kVDOqckAR71IkO2JX4L0XU8w8Iz07JWzekrb2OBm0hD5xcs7lVYC//0KtHjIIGpT+Aap1QCMMOe535bKEcXubqLklhgpvdeYiXzDp9Enf2uTPN/SGEddBRBL7zLMxvXaGfz8m7d4QX9RIpvJZNiGd5sxmOUZXWLNYZ7jOxaKtQWnLUuIwxuCk19IrbbBWi0bmxqFlZrQTSjUapZZ/0vwAKeXH1XksBeQAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;From Idea to Software lifecycle&quot;
        title=&quot;&quot;
        src=&quot;/static/60a56c968bddc261b2f8521a3c83d421/f058b/retro-step-1.png&quot;
        srcset=&quot;/static/60a56c968bddc261b2f8521a3c83d421/c26ae/retro-step-1.png 158w,
/static/60a56c968bddc261b2f8521a3c83d421/6bdcf/retro-step-1.png 315w,
/static/60a56c968bddc261b2f8521a3c83d421/f058b/retro-step-1.png 630w,
/static/60a56c968bddc261b2f8521a3c83d421/40601/retro-step-1.png 945w,
/static/60a56c968bddc261b2f8521a3c83d421/1843f/retro-step-1.png 1186w&quot;
        sizes=&quot;(max-width: 630px) 100vw, 630px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;h4&gt;Second step: fill out the “Roles and Expectations” matrix&lt;/h4&gt;
&lt;p&gt;The second step was to discuss more in-depth roles and expectations. To do that,
we created a matrix where the number of rows represents the many roles we wanted
to discuss and two columns saying &lt;strong&gt;“I expect that…”&lt;/strong&gt; and &lt;strong&gt;“I don’t expect
that…”&lt;/strong&gt;.  This idea was born from a combination of &lt;a href=&quot;https://www.funretrospectives.com/role-expectations-matrix/&quot;&gt;Role Expectations
Matrix&lt;/a&gt; and &lt;a href=&quot;https://www.funretrospectives.com/is-is-not-does-does-not/&quot;&gt;Is -
Is not - Does - Does
not&lt;/a&gt; exercises from
&lt;a href=&quot;https://www.funretrospectives.com&quot;&gt;FunRetrospectives&lt;/a&gt; website.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;a href=&quot;https://www.funretrospectives.com&quot;&gt;FunRetrospectives&lt;/a&gt; is an excellent source
of different activities for running retros, so if you’re looking for different
activities to run in your team, please check them out!&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 630px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/3997d19675b69a172f0bb1d398340c7f/b9024/retro-step-2.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 68.9873417721519%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAYAAAAvxDzwAAAACXBIWXMAAAsTAAALEwEAmpwYAAACQklEQVR42o1UXU/bQBDM//8hfan6wBNSJV6KIoSoSoC2SZsQ4pBwtu/DPp/P3/Z070xQSlHblVayVuudnZm1J/jPKMsSfd//s28yDAMOeYi275DVOUxjULWVryVJgrZtQVPRW4s+z9FlGXpjfO1l4GsE95IMV9ixNdZhiEhyFEUBxhikShDyBKkQ0HGMhGpJFCGVEhkNdktNMkKRVOCco6zcNgPan+9g2QzvVxF22nig3GgCMPhwrVC240bDIY8YTpwuXde9ZE5UrDWwhUWcJsht4bdWSsHWJWWNmoCbukLtnp+zcrWmGSkfi11VFio6Bws2mJ0XUNxQYw0uOHY3Idic4+SaYbaWqIscmhg6llprL83EoYeklaM8DidDplcQn6fYBbe0SefrJjeQ8wz5tsVG3CMU85H28MplPGvgwlHmPMZuucV+s4SQC0rh6W6DADyhZxOSCUso8QBFzifP6Xr8ho6308251JAWPQ0/3c/xXT2gzK5JAunBdKZRk5568xGFuR+3IzbHZ+dddmKmaeqzqsabi6yETKbo6g01teNAAqxIlubiAh3p9Sbf13fozElpfSEkHp8CrJ4YAhZDSYb1Zg22XCKe3ULTPSYyRBwH/uRcCrpNQ6CT45M5uP1DaZys9jjbhrjhEVr7BTxlsItvKD+d4pIp3IULNPbOS/Tbhk5I57BLh+KiIF334op02+PyLIdkGqmmm8wf6Zu+w1euEGTu4Pu3Xf4jqKtvYzroHPypQ1UM6MiAvmvJiPqvP4dfG5s15rYPNaoAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Roles and Expectations matrix&quot;
        title=&quot;&quot;
        src=&quot;/static/3997d19675b69a172f0bb1d398340c7f/f058b/retro-step-2.png&quot;
        srcset=&quot;/static/3997d19675b69a172f0bb1d398340c7f/c26ae/retro-step-2.png 158w,
/static/3997d19675b69a172f0bb1d398340c7f/6bdcf/retro-step-2.png 315w,
/static/3997d19675b69a172f0bb1d398340c7f/f058b/retro-step-2.png 630w,
/static/3997d19675b69a172f0bb1d398340c7f/40601/retro-step-2.png 945w,
/static/3997d19675b69a172f0bb1d398340c7f/b9024/retro-step-2.png 1203w&quot;
        sizes=&quot;(max-width: 630px) 100vw, 630px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;For this activity, I asked each member to add sticky notes with a short summary
of what was the understanding of the expectation of each role, adding stickies
for &lt;strong&gt;“I expect that…”&lt;/strong&gt; and also adding stickies for &lt;strong&gt;“I don’t expect
that…”&lt;/strong&gt;. I also asked the participants to avoid the use of “+1” or any kind
of emoji that would make others biased. The idea here was to have equal weight
for every sticky note since we would have a &lt;strong&gt;separate&lt;/strong&gt; moment for voting on
the themes that we wanted to discuss and then, &lt;strong&gt;“+1“‘s&lt;/strong&gt; would be
appropriate.&lt;/p&gt;
&lt;h4&gt;No action items, for now…&lt;/h4&gt;
&lt;p&gt;It is usually expected that &lt;strong&gt;“Action Items”&lt;/strong&gt; should be one of the outcomes
from retrospectives and who owns these action items. For this retro though, I
wanted to focus on the &lt;strong&gt;discussion&lt;/strong&gt; instead of focusing on action items.&lt;/p&gt;
&lt;p&gt;I’ve planned having 45 minutes just debating the “Roles and Expectations” sticky
notes, appreciating &lt;strong&gt;how different views we might have in the team about the
certain role and how we can agree on something together&lt;/strong&gt;. With that, I tried to
have 10 minutes time slot for the top voters, trying to cover as many roles as
possible.&lt;/p&gt;
&lt;h3&gt;Takeaways from this particular retro&lt;/h3&gt;
&lt;p&gt;I’ve got feedback from the participants that the retro was quite insightful,
which inspired me to write this post. On the occasion of this particular retro,
as it might occur with many, there were some loose ends; &lt;strong&gt;we couldn’t figure
everything out in one go&lt;/strong&gt;. However, I believe we had an opportunity to speak
freely about what things some believed that a Software Engineer &lt;strong&gt;should not
do&lt;/strong&gt;, for example, and appreciate that different people thought exactly opposite
things.&lt;/p&gt;
&lt;h2&gt;Thank you for reading&lt;/h2&gt;
&lt;p&gt;I hope you enjoy this post, if you have any feedback or questions, hit me up on
&lt;a href=&quot;mailto:alabeduarte@gmail.com&quot;&gt;alabeduarte@gmail.com&lt;/a&gt;, I’d be happy to hear your thoughts and be better next
time!&lt;/p&gt;
&lt;p&gt;&lt;em&gt;All the images were generated from &lt;a href=&quot;https://excalidraw.com/&quot;&gt;excalidraw.com&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;Other References&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://www.thoughtworks.com/en-au/insights/blog/5-things-you-need-know-facilitate-retrospective#:~:text=A%20Retrospective%20is%20a%20ceremony,evaluate%20its%20past%20working%20cycle.&amp;#x26;text=Finally%2C%20retrospective%20is%20a%20moment,improve%20things%20identified%20as%20negative.&quot;&gt;Retrospective
Ceremony&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.funretrospectives.com/role-expectations-matrix/&quot;&gt;Role Expectations
Matrix&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.funretrospectives.com/is-is-not-does-does-not/&quot;&gt;Is - Is not - Does - Does
not&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded></item><item><title><![CDATA[The semantics of Marshalling in Go]]></title><description><![CDATA[In this post, I’d like to discuss how Marshalling/Unmarshalling process is
usually applied in Go programs. I’ll give some examples
of how it…]]></description><link>https://alabeduarte.com/the-semantics-of-marshalling-in-go/</link><guid isPermaLink="false">https://alabeduarte.com/the-semantics-of-marshalling-in-go/</guid><pubDate>Fri, 26 Mar 2021 10:38:35 GMT</pubDate><content:encoded>&lt;p&gt;In this post, I’d like to discuss how Marshalling/Unmarshalling process is
usually applied in &lt;a href=&quot;https://golang.org/&quot;&gt;Go&lt;/a&gt; programs. I’ll give some examples
of how it is used in practice with some code examples (i.e. data parsing) and
then expand on other usages when we have the need for transforming data across
different domains.&lt;/p&gt;
&lt;p&gt;Before discussing the term
&lt;a href=&quot;https://en.wikipedia.org/wiki/Marshalling_(computer_science)&quot;&gt;Marshalling&lt;/a&gt;, I’d
like to start with the word &lt;em&gt;data&lt;/em&gt; in the context of programming languages. More
often than not we programmers have a task that involves in some shape or form
interacting with data represented in a format and then we need to transform this
data into a different representation.&lt;/p&gt;
&lt;p&gt;A few use-cases that come to mind:&lt;/p&gt;
&lt;hr&gt;
&lt;blockquote&gt;
&lt;p&gt;Fetch data from this HTTP API and convert it into my own type to then do
something…&lt;/p&gt;
&lt;/blockquote&gt;
&lt;hr&gt;
&lt;blockquote&gt;
&lt;p&gt;As soon as this “data” arrives, transform them into something else, which is
what our system understands…&lt;/p&gt;
&lt;/blockquote&gt;
&lt;hr&gt;
&lt;blockquote&gt;
&lt;p&gt;In order to integrate with certain third-party API, I need to convert this
“data” into something that the third-party API understands to then send the
data…&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;What should we call this?&lt;/h3&gt;
&lt;p&gt;When it comes to changing data’s representation into another representation in
Software, I would imagine some of these terms would ring a bell:
&lt;em&gt;Encode/Decode&lt;/em&gt;, &lt;em&gt;Marshal/Unmarshal&lt;/em&gt;, &lt;em&gt;Map&lt;/em&gt;, &lt;em&gt;Normalise (also Normalize)&lt;/em&gt;,
&lt;em&gt;Parse&lt;/em&gt;, &lt;em&gt;Reduce&lt;/em&gt;, &lt;em&gt;Serialise (also Serialize)&lt;/em&gt;, &lt;em&gt;Transform&lt;/em&gt;, &lt;em&gt;etc.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;You might notice that different programming languages use one more than another
and, in Go, the terms &lt;code class=&quot;language-text&quot;&gt;Marshal&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;Unmarshal&lt;/code&gt; are usually preferred. In this
post, I’d like to explore or try to guess why this is the case and what all of
these terms have in common.&lt;/p&gt;
&lt;h2&gt;The term Marshalling&lt;/h2&gt;
&lt;p&gt;In the context of computer science and programming languages, the term
&lt;em&gt;marshalling&lt;/em&gt; is the process of transforming a data representation into a
suitable format that will be used by a different Software Component.&lt;/p&gt;
&lt;p&gt;This process is quite interesting because it enables interoperability between
different Softwares. Consider &lt;em&gt;Software X&lt;/em&gt; and &lt;em&gt;Software Y&lt;/em&gt;, they are totally
independent of one another. However, they can communicate with each other via
message passing and they can exchange data, as long as they establish a common
contract or &lt;a href=&quot;https://en.wikipedia.org/wiki/Interface_(computing)&quot;&gt;interface&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;i.e.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;Software X –&gt; Marshal data &quot;abc&quot; into &quot;&amp;lt;abc&gt;&quot;
Software Y –&gt; Unmarshal data &quot;&amp;lt;abc&gt;&quot; into &quot;A, B, C&quot;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;How Marshal/Unmarshal can be used in Go&lt;/h2&gt;
&lt;p&gt;In Go, one of the most famous examples you might find is converting data from a
JSON structure into Go code (&lt;em&gt;in other words, JSON “parsing”&lt;/em&gt;).&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;What about the term Parsing?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;There is a quite popular term that can be seen as &lt;em&gt;similar&lt;/em&gt;, but don’t assume
that just yet, which is &lt;a href=&quot;https://en.wikipedia.org/wiki/Parsing&quot;&gt;parsing&lt;/a&gt;. I
would say that the &lt;strong&gt;semantics of parsing&lt;/strong&gt; is more applicable when the data to
be processed are &lt;em&gt;&lt;strong&gt;strings/symbols&lt;/strong&gt;&lt;/em&gt; that will then be transformed into a
custom representation throughout the programming language in question.&lt;/p&gt;
&lt;p&gt;While terms like Marshalling and Encoding offer a bit more scope to work with.
Given you need to transform &lt;em&gt;type A&lt;/em&gt; to &lt;em&gt;type B&lt;/em&gt; while &lt;em&gt;type A&lt;/em&gt; is a &lt;strong&gt;binary&lt;/strong&gt;,
I personally think that calling a “parser” doesn’t feel right.&lt;/p&gt;
&lt;h3&gt;Back to Marshalling and Unmarshalling&lt;/h3&gt;
&lt;p&gt;It took a bit longer but the term “Marshalling” clicked for me, eventually. When
I first came to learn Go, call the method &lt;code class=&quot;language-text&quot;&gt;Unmarshal&lt;/code&gt; to “parse” JSON felt
really weird and too much low-level.&lt;/p&gt;
&lt;p&gt;However, Marshal/Unmarshal can and should be used as a generic way to transform
types between boundaries regardless of the level of abstraction (i.e. network,
application, domain, platform).&lt;/p&gt;
&lt;h3&gt;Converting from one format to another&lt;/h3&gt;
&lt;p&gt;When dealing with &lt;a href=&quot;https://en.wikipedia.org/wiki/Distributed_computing&quot;&gt;Distributed
Systems&lt;/a&gt;, converting
different representations from one domain to another can be quite common. And
I’m not only referring to business domains (i.e. Shopping cart, Billing,
Invoice) but also to different transport layers (i.e. Network, Protocols).&lt;/p&gt;
&lt;h4&gt;Data format transformations (i.e. JSON, XML, etc)&lt;/h4&gt;
&lt;p&gt;We can unmarshal from &lt;em&gt;type A&lt;/em&gt; to &lt;em&gt;type B&lt;/em&gt; when transporting data over
the wire or when converting from a generic data representation to a
domain-specific representation.&lt;/p&gt;
&lt;p&gt;So let’s imagine we are building a system that manages user’s accounts and we
have the following use-cases to cover:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Convert from &lt;a href=&quot;https://www.w3schools.com/xml/xml_whatis.asp&quot;&gt;XML&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Convert from &lt;a href=&quot;https://www.json.org/json-en.html&quot;&gt;JSON&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Convert from &lt;a href=&quot;https://developers.google.com/protocol-buffers&quot;&gt;Protocol Buffers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;etc.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&quot;/1f37e8bda1d594b050535961a1de6a60/marshal-unmarshal-data-format.svg&quot; aria-label=&quot;Here we have a
diagram with 3 marshallers: XML, JSON and protobuf. Each of them marshals data
into its respective format. Then, we have Account with the Unmarshal method
which will take a single argument as input and based on the format, it will
figure out how to Unmarshal to an Account&quot; width=&quot;100%&quot;&gt;&lt;/p&gt;
&lt;h3&gt;Code examples&lt;/h3&gt;
&lt;details&gt;
&lt;summary&gt;Unit tests (click to expand)&lt;/summary&gt;
&lt;p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre class=&quot;language-go&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Test_Unmarshal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;t &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;testing&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;T&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	tests &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		name &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
		in   &lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
		out  Account
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
			name&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;should unmarshal Account from XML&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
			in&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;`
			&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
			&amp;lt;User&gt;
				&amp;lt;Name&gt;Mary&amp;lt;/Name&gt;
				&amp;lt;Type&gt;Gold&amp;lt;/Type&gt;
			&amp;lt;/User&gt;
			`&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
			out&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Account&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
				Name&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Mary&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
			&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
		&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;

		&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
			name&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;should unmarshal Account from JSON&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
			in&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;`
			{
				&quot;user&quot;: {
					&quot;name&quot;: &quot;John&quot;,
					&quot;type&quot;: &quot;Premium&quot;,
					&quot;created_at&quot;: &quot;2020-01-01&quot;
				}
			}
			`&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
			out&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Account&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
				Name&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;John&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
			&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
		&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;

		&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
			name&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;should unmarshal Account from protobuf&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
			in&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; ProtobufAccount&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
				AccountName&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Bob&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
			&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
			out&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Account&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
				Name&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Bob&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
			&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
		&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;

		&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
			name&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;should not unmarshal from CSV format since it doesn&apos;t support yet&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
			in&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;   &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;`Name,Mary`&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
			out&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;  Account&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
		&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;

		&lt;span class=&quot;token comment&quot;&gt;// ...&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

	&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tt &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;range&lt;/span&gt; tests &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		t&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Run&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;tt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;name&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;t &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;testing&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;T&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; a Account
			err &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Unmarshal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;tt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;in&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
			&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
				expectedError &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; fmt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Errorf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;unable to unmarshal %v, format not supported.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;in&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

				&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; err&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Error&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; expectedError&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Error&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
					t&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Errorf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;unexpected error while trying to unmarshal account: %v&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; err&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
				&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
			&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

			&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; tt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; a &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
				t&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Errorf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;test failed. wanted: %v, got: %v&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
			&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
		&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;/details&gt;
&lt;p&gt;&lt;/p&gt;
&lt;details&gt;
&lt;summary&gt;Implementation (click to expand)&lt;/summary&gt;
&lt;p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre class=&quot;language-go&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// xmlPayload represents the XML payload with user data&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; xmlPayload &lt;span class=&quot;token keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	Name &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;`xml:User&gt;Name`&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// jsonPayload represents the JSON payload wiht user data&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; jsonPayload &lt;span class=&quot;token keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	User &lt;span class=&quot;token keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		Name &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;`json:&quot;name&quot;`&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;`json:&quot;user&quot;`&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// ProtobufAccount represents a generated Go code from a protobuf definition&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// More details: https://developers.google.com/protocol-buffers/docs/reference/go-generated&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; ProtobufAccount &lt;span class=&quot;token keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	AccountName &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;`protobuf:&quot;bytes,1,opt,name=acount_name,proto3&quot;`&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Account is the domain representation of an account in the system. It is the&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// main entity for the example we&apos;re using.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; Account &lt;span class=&quot;token keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	Name &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;Account&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;unmarshalXML&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;src &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;error&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; res xmlPayload
	err &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; xml&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Unmarshal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;src&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;res&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
	a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;Account&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		Name&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; res&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Name&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; err
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;Account&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;unmarshalJSON&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;src &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;error&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; res jsonPayload
	err &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; json&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Unmarshal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;src&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;res&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
	a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;Account&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		Name&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; res&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;User&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Name&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; err
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;Account&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;unmarshalProtobuf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;src ProtobufAccount&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;error&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;Account&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		Name&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; src&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;AccountName&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

	&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Unmarshal takes an interface{} and try to convert into Account type&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;Account&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Unmarshal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;src &lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;error&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;switch&lt;/span&gt; src&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
		b &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; src&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

		&lt;span class=&quot;token comment&quot;&gt;// naive logic to determine if it is XML&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; strings&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Contains&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;&amp;lt;?xml&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;unmarshalXML&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

		&lt;span class=&quot;token comment&quot;&gt;// naive logic to determine if it is JSON&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; strings&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Contains&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; `&lt;span class=&quot;token string&quot;&gt;&quot;user&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;`&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;unmarshalJSON&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; ProtobufAccount&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;unmarshalProtobuf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;src&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ProtobufAccount&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

	&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; fmt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Errorf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;unable to unmarshal %v, format not supported.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; src&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;/details&gt;
&lt;h3&gt;Converting from one domain to another&lt;/h3&gt;
&lt;p&gt;In this particular case, let’s imagine we have an e-commerce system that goes
from adding item to a cart, placing an order and sending an invoice to the
customer.&lt;/p&gt;
&lt;p&gt;Once the customer selects an item and adds it to her/his cart, the item will be
part of an order that will also be listed in the invoice afterwards. However,
the “item” might not have the same meaning across the system.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;I would suggest a read on &lt;a href=&quot;https://martinfowler.com/bliki/BoundedContext.html&quot;&gt;Bounded
Context&lt;/a&gt; for more details.
It is described in more depth in the book
&lt;a href=&quot;https://www.amazon.com/gp/product/0321125215?ie=UTF8&amp;#x26;tag=martinfowlerc-20&amp;#x26;linkCode=as2&amp;#x26;camp=1789&amp;#x26;creative=9325&amp;#x26;creativeASIN=0321125215&quot;&gt;Domain-Driven-Design&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Without further ado, let’s check how &lt;code class=&quot;language-text&quot;&gt;Marshal&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;Unmarshal&lt;/code&gt; can be used to
translate different data representations across different domains:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/00ef3c2ecfb4063dcdeb3887c4a9e4c1/marshal-unmarshal-domains.svg&quot; aria-label=&quot;Here we have three
circles, which one representing a Software COmponent. On each circle, we have
boxes that represent actions, events and entities. Every component communicates
with each other via events and the data is translated between circles throughout
Marshalling and Unmarshalling&quot; width=&quot;100%&quot;&gt;&lt;/p&gt;
&lt;p&gt;Because now the components are now communicating via a common interface
(&lt;code class=&quot;language-text&quot;&gt;Marshal&lt;/code&gt;/&lt;code class=&quot;language-text&quot;&gt;Unmarshal&lt;/code&gt;), the data format that is used to transfer between
components became merely an implementation detail. It no longer matters if an item,
when unmarshalled to an Order, will be a JSON payload, protobuf or a just data
transformation between &lt;a href=&quot;https://golang.org/pkg/go/types/&quot;&gt;go types&lt;/a&gt;. As the
system supports different ways of data transformation (new domains being
introduced, new formats, new integrations), the core flow doesn’t need to
change, because now &lt;code class=&quot;language-text&quot;&gt;Marshal&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;Unmarshal&lt;/code&gt; abstracts that away.&lt;/p&gt;
&lt;h2&gt;Final thoughts&lt;/h2&gt;
&lt;p&gt;Although the &lt;em&gt;Marshalling&lt;/em&gt; terminology might sound too low level (i.e. feels
more about computation, less about business domain), it is a &lt;strong&gt;consistent way&lt;/strong&gt;
to transform data from one format to another, where this transformation can be
from low-level bytes into an “object” or it can be used to translate from one
domain representation to another.&lt;/p&gt;
&lt;p&gt;One might argue that having a single interface to transform data between domains
is nothing novel. However, having this &lt;strong&gt;semantics&lt;/strong&gt; established is quite useful
so developers won’t need to come up with new conventions all over again.
Although I appreciate always having new ways to express real-life problems in
form of code, I believe that data transformation, most of the time, are stepping
stones for problem-solving, not the end goal. So if there is a standard way
to deal with this mundane task, I found it very welcome.&lt;/p&gt;
&lt;p&gt;But hey, this is only my opinion at the time of this post! If you read this far,
I hope I didn’t waste your time!&lt;/p&gt;
&lt;h3&gt;Thank you for reading&lt;/h3&gt;
&lt;p&gt;I hope you enjoy this post, if you have any feedback or questions, hit me up on
&lt;a href=&quot;mailto:alabeduarte@gmail.com&quot;&gt;alabeduarte@gmail.com&lt;/a&gt;, I’d be happy to hear your thoughts and be better next
time!&lt;/p&gt;
&lt;h2&gt;References&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Marshalling_(computer_science)&quot;&gt;Marshalling (computer science)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Interface_(computing)&quot;&gt;Interface (computing)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Parsing&quot;&gt;Parsing&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://gobyexample.com/json&quot;&gt;Go By Example: JSON&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Distributed_computing&quot;&gt;Distributed Systems&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.w3schools.com/xml/xml_whatis.asp&quot;&gt;XML&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.json.org/json-en.html&quot;&gt;JSON&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://developers.google.com/protocol-buffers&quot;&gt;Protocol Buffers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://martinfowler.com/bliki/BoundedContext.html&quot;&gt;Bounded Context by Martin Fowler&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.amazon.com/gp/product/0321125215?ie=UTF8&amp;#x26;tag=martinfowlerc-20&amp;#x26;linkCode=as2&amp;#x26;camp=1789&amp;#x26;creative=9325&amp;#x26;creativeASIN=0321125215&quot;&gt;Domain-Driven Design by Eric Evans&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded></item><item><title><![CDATA[3 approaches to consider when rendering conditional content in React]]></title><description><![CDATA[In this post, I’d like to share alternative approaches for dealing with
conditional rendering in
React. I will try to list out the pros and…]]></description><link>https://alabeduarte.com/3-approaches-conditional-rendering-react/</link><guid isPermaLink="false">https://alabeduarte.com/3-approaches-conditional-rendering-react/</guid><pubDate>Fri, 19 Feb 2021 07:54:24 GMT</pubDate><content:encoded>&lt;p&gt;In this post, I’d like to share alternative approaches for dealing with
&lt;a href=&quot;https://reactjs.org/docs/conditional-rendering.html&quot;&gt;conditional rendering&lt;/a&gt; in
&lt;a href=&quot;https://reactjs.org/&quot;&gt;React&lt;/a&gt;. I will try to list out the pros and cons about 3
different approaches (doesn’t mean couldn’t be more!) and hopefully this can be
useful for you someday.&lt;/p&gt;
&lt;h2&gt;Use-case&lt;/h2&gt;
&lt;p&gt;Let’s consider we have a React component that is called &lt;code class=&quot;language-text&quot;&gt;AccountCard&lt;/code&gt;. The
responsibility of this component is to display the user’s first name, last name and
the avatar.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 630px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/2929574b839204879d75b55434212841/5fada/user-card.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 41.139240506329116%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAABYlAAAWJQFJUiTwAAAA/ElEQVR42qWR62qEMBCFff+3K11aql0TjMLaNYIYo+Z2mpmyFxa2P9qBT4/j5OREC+SKKdKVSZkQwp9IKaGIMWA4TzifbMZAfy3Y950HvPcMaeccc9E0c4Ge13VlXSQEdMLg+BpQHxyaD4/VrpjnmYcIY8w1hc3v6P5Y1LsZSgNx8BBvHqoK0HqElBJt26Lve5RliWmasvECIRssi/3dsBUzPl98Zod8p+M4CFFDKcWGVVWxoc1plep4IVV6Zth3Bt3Roat3nBrH320cRzahI2utc6qFNxoGjW3bnicMMfIi+kOkY4b0Y933SN9DRSF+EuaGtZZ3/Q8Uiky/ActPbf5cm6rqAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;User Card&quot;
        title=&quot;&quot;
        src=&quot;/static/2929574b839204879d75b55434212841/f058b/user-card.png&quot;
        srcset=&quot;/static/2929574b839204879d75b55434212841/c26ae/user-card.png 158w,
/static/2929574b839204879d75b55434212841/6bdcf/user-card.png 315w,
/static/2929574b839204879d75b55434212841/f058b/user-card.png 630w,
/static/2929574b839204879d75b55434212841/40601/user-card.png 945w,
/static/2929574b839204879d75b55434212841/78612/user-card.png 1260w,
/static/2929574b839204879d75b55434212841/5fada/user-card.png 1706w&quot;
        sizes=&quot;(max-width: 630px) 100vw, 630px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre class=&quot;language-typescript&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;AccountCardProps&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  firstName&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  lastName&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  avatarURL&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; AccountCard&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; React&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;FunctionComponent&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;AccountCardProps&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  firstName&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  lastName&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  avatarURL&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;img src&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;avatarURL&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;label&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;First name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;label&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;span&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;firstName&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;span&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;

      &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;label&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;Last name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;label&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;span&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;lastName&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;span&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// usage&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// ...&lt;/span&gt;

&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Account
  firstName&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;John&quot;&lt;/span&gt;
  lastName&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Doe&quot;&lt;/span&gt;
  avatarURL&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;https:/example.com/slug-123.png&quot;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// ...&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;New requirements&lt;/h2&gt;
&lt;p&gt;Let’s imagine we’ve got a new requirement:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Premium Accounts&lt;/em&gt; should have a small badge indicating that the account is
&lt;em&gt;premium&lt;/em&gt;.  As for &lt;em&gt;regular&lt;/em&gt; accounts, the user card should stay the same.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th align=&quot;center&quot;&gt;Regular Account&lt;/th&gt;
&lt;th align=&quot;center&quot;&gt;Premium Account&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td align=&quot;center&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 630px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/2929574b839204879d75b55434212841/5fada/user-card.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 41.139240506329116%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAABYlAAAWJQFJUiTwAAAA/ElEQVR42qWR62qEMBCFff+3K11aql0TjMLaNYIYo+Z2mpmyFxa2P9qBT4/j5OREC+SKKdKVSZkQwp9IKaGIMWA4TzifbMZAfy3Y950HvPcMaeccc9E0c4Ge13VlXSQEdMLg+BpQHxyaD4/VrpjnmYcIY8w1hc3v6P5Y1LsZSgNx8BBvHqoK0HqElBJt26Lve5RliWmasvECIRssi/3dsBUzPl98Zod8p+M4CFFDKcWGVVWxoc1plep4IVV6Zth3Bt3Roat3nBrH320cRzahI2utc6qFNxoGjW3bnicMMfIi+kOkY4b0Y933SN9DRSF+EuaGtZZ3/Q8Uiky/ActPbf5cm6rqAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Account Card&quot;
        title=&quot;&quot;
        src=&quot;/static/2929574b839204879d75b55434212841/f058b/user-card.png&quot;
        srcset=&quot;/static/2929574b839204879d75b55434212841/c26ae/user-card.png 158w,
/static/2929574b839204879d75b55434212841/6bdcf/user-card.png 315w,
/static/2929574b839204879d75b55434212841/f058b/user-card.png 630w,
/static/2929574b839204879d75b55434212841/40601/user-card.png 945w,
/static/2929574b839204879d75b55434212841/78612/user-card.png 1260w,
/static/2929574b839204879d75b55434212841/5fada/user-card.png 1706w&quot;
        sizes=&quot;(max-width: 630px) 100vw, 630px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/td&gt;
&lt;td align=&quot;center&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 630px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/c8ba7a3dbaafb87d452a3dcedc972bda/2aa89/user-card-premium.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 42.405063291139236%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAABYlAAAWJQFJUiTwAAABT0lEQVR42pVSW07DMBDM0TkFB+CbM4DEJxLviipqU6Bpk7YkTdw4cfwc7C2JCuKHlcYveWdmvY6sNbBOw8EQrDPQWhOMMT/w19kpnHOIWt5i9cawem+RJhzlrgOJWEcE1lqCUmpMDOsRUkL7WQhB+6jrBJJJh8VEY/YoUeQW+32B7XZHl5qmQVVVRFrXDOxwICe/I4h/E/ZYTDt8TA2SF4n9xqBmFebzOYqiwHK5RJIklFBVNdZZDu1dDuEU94M9JRSYPXEkzwrxfY8iC4kl4jhGWZZI05TIlVbIvevVOqMnCaGyW8jXc5jsxouEZ5GIhOjxmQlfqsZuJcFZUFNoDg0pcs7BGKMyg4u2bceSm7tL1FdnaB8uoGXv7+vgsPPqPXU3dNsYNTZjmANBIKc9NUX6HO+o3UDm11BNik70R4dD14av8j8EUe/cHN2Hb/MFmxBqDhlOuIIAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Premium Account Card&quot;
        title=&quot;&quot;
        src=&quot;/static/c8ba7a3dbaafb87d452a3dcedc972bda/f058b/user-card-premium.png&quot;
        srcset=&quot;/static/c8ba7a3dbaafb87d452a3dcedc972bda/c26ae/user-card-premium.png 158w,
/static/c8ba7a3dbaafb87d452a3dcedc972bda/6bdcf/user-card-premium.png 315w,
/static/c8ba7a3dbaafb87d452a3dcedc972bda/f058b/user-card-premium.png 630w,
/static/c8ba7a3dbaafb87d452a3dcedc972bda/40601/user-card-premium.png 945w,
/static/c8ba7a3dbaafb87d452a3dcedc972bda/78612/user-card-premium.png 1260w,
/static/c8ba7a3dbaafb87d452a3dcedc972bda/2aa89/user-card-premium.png 1698w&quot;
        sizes=&quot;(max-width: 630px) 100vw, 630px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Basically, we need to add a new &lt;em&gt;badge&lt;/em&gt; only if the user’s account is &lt;em&gt;premium&lt;/em&gt;,
otherwise, we just render the component as it was before. It seems straightforward.&lt;/p&gt;
&lt;p&gt;To make things more interesting and to &lt;strong&gt;justify why I’m writing this post&lt;/strong&gt;,
I’d like to consider the possibility where &lt;strong&gt;the current component &lt;code class=&quot;language-text&quot;&gt;&amp;lt;AccountCard
/&gt;&lt;/code&gt; could be in use by other teams and different places in the app&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;With that said, in order to make the changes, we’ll need to make, we will need to
consider our strategy carefully since we don’t want to break compatibility with
other teams/areas where this component is already being used.&lt;/p&gt;
&lt;h2&gt;Approaches&lt;/h2&gt;
&lt;p&gt;I can imagine 3 approaches (probably there are more) and I’ll try to reason
about the pros and cons of each one.&lt;/p&gt;
&lt;h3&gt;1. Adding a new prop with boolean values&lt;/h3&gt;
&lt;p&gt;Our goal is to display additional information on top of what is already
there, so one way to approach this is to add a new prop to our component that
indicate that the account is &lt;em&gt;premium&lt;/em&gt;. In this case, we can simply use a
&lt;code class=&quot;language-text&quot;&gt;boolean&lt;/code&gt; value to indicate whether the account is &lt;em&gt;premium&lt;/em&gt; or not:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre class=&quot;language-typescript&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Let&apos;s pretend we have a component that displays the &quot;premium&quot; badge&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token function-variable function&quot;&gt;PremiumBadge&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;PREMIUM&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;AccountCardProps&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  firstName&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  lastName&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  avatarURL&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  isPremium&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// new prop&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; Account&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; React&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;FunctionComponent&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;AccountCardProps&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  firstName&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  lastName&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  avatarURL&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  isPremium&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// new prop&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;img src&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;avatarURL&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;label&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;First name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;label&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;span&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;firstName&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;span&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;

      &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;label&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;Last name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;label&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;span&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;lastName&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;span&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;/* if the value of isPremium is true, display the Premium badge */&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;isPremium &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;PremiumBadge &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// usage&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// ...&lt;/span&gt;

&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Account
  firstName&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;John&quot;&lt;/span&gt;
  lastName&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Doe&quot;&lt;/span&gt;
  avatarURL&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;https:/example.com/slug-123.png&quot;&lt;/span&gt;
  isPremium&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// ...&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h4&gt;Pros&lt;/h4&gt;
&lt;blockquote&gt;
&lt;p&gt;Easy&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;It is an easy change that gets the job done. If there are no many people
depending on this component and the impact of doing changes is low, I’d consider
this approach in a heartbeat.&lt;/p&gt;
&lt;h4&gt;Cons&lt;/h4&gt;
&lt;blockquote&gt;
&lt;p&gt;Our component will be subject to change every time we get a new requirement&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The example I gave earlier of having to display a &lt;em&gt;premium&lt;/em&gt; badge is simple
because either we show a new content or not.  However, software tends to evolve
over time and I wouldn’t be surprised if we get to support new use cases in the
future such as:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;display this icon when the user is online.&lt;/li&gt;
&lt;li&gt;display date of birth if a certain condition is met, etc.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Then, every time we get a new requirement will be a new prop and a new “if”
tucked in in the component and every change needs to be well considered given
there is now the risk of &lt;strong&gt;breaking the existing behaviour and impacting other
teams&lt;/strong&gt;.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;It can break compatibility with existing code&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;If our component is being reused by other areas in the codebase, &lt;strong&gt;we will need
to change everywhere&lt;/strong&gt; to make sure the new prop is being passed correctly. We
also would need to watch out for regression.&lt;/p&gt;
&lt;p&gt;One way to mitigate the risk of breaking things or introducing breaking changes
to other teams is to change the newly added prop to be set as &lt;em&gt;optional&lt;/em&gt; and set
its default value to be always &lt;code class=&quot;language-text&quot;&gt;false&lt;/code&gt; so that only the new usage of the code
that needs the new prop &lt;code class=&quot;language-text&quot;&gt;isPremium&lt;/code&gt; will need to change passing &lt;code class=&quot;language-text&quot;&gt;false&lt;/code&gt; or
&lt;code class=&quot;language-text&quot;&gt;true&lt;/code&gt; while places where this component is already in use don’t need to change
a thing.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre class=&quot;language-typescript&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;AccountCardProps&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  firstName&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  lastName&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  avatarURL&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  isPremium&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// new prop now marked as &quot;optional&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; Account&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; React&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;FunctionComponent&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;AccountCardProps&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  firstName&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  lastName&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  avatarURL&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  isPremium &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// new prop&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// ...&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;2. Adding a new prop with an enum&lt;/h3&gt;
&lt;p&gt;As mentioned above, adding new props for every new use case will increase the
the complexity of the component and it will require larger coordination between
developers that already use it.&lt;/p&gt;
&lt;p&gt;With that said, let’s try changing our data structure in a way that allows the
users of our code to pass down what type of &lt;em&gt;account&lt;/em&gt; will be displayed and we
can write our component in a way that we will know what to do based on that.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre class=&quot;language-typescript&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// AccountType represents all possible types of account&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;enum&lt;/span&gt; AccountType &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  Regular&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  Trial&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  Premium&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  Gold&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// accountTypeComponentMap takes an AccountType as input and returns a React&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// component based on the account type&lt;/span&gt;
&lt;span class=&quot;token class-name&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;/span&gt; accountTypeComponentMap &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
  accountType&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; AccountType
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; React&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;FunctionComponent &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; map &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;AccountType&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Regular&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// for &quot;regular&quot; accounts, display nothing&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;AccountType&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Trial&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;TRIAL&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ACCOUNT&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;AccountType&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Premium&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;PREMIUM&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;AccountType&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Gold&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;GOLD&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;token comment&quot;&gt;// Fetch the component based on the account type&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// return nothing (null) if can&apos;t find it.&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; map&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;accountType&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;AccountCardProps&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  firstName&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  lastName&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  avatarURL&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  accountType&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; AccountType&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// type that represents the account type (i.e.  premium, regular)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; Account&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; React&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;FunctionComponent&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;AccountCardProps&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  firstName&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  lastName&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  avatarURL&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  accountType&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// Assign the corresponding component based on the account type&lt;/span&gt;
  &lt;span class=&quot;token class-name&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;/span&gt; BadgeComponent &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;accountTypeComponentMap&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;accountType&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;img src&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;avatarURL&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;label&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;First name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;label&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;span&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;firstName&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;span&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;

        &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;label&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;Last name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;label&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;span&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;lastName&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;span&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;/* displays the content */&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;BadgeComponent &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// usage&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// ...&lt;/span&gt;

&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Account
  firstName&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;John&quot;&lt;/span&gt;
  lastName&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Doe&quot;&lt;/span&gt;
  avatarURL&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;https:/example.com/slug-123.png&quot;&lt;/span&gt;
  accountType&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;AccountType&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Premium&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// ...&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h4&gt;Pros&lt;/h4&gt;
&lt;blockquote&gt;
&lt;p&gt;Extensible&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This solution opens the possibilities to be able to accommodate new
use cases that are related to different types of accounts. As we have new
types, all we have to do is to include in the list of supported types and add a
a new entry in the map, then add the corresponding rendering logic to it:&lt;/p&gt;
&lt;p&gt;i.e.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre class=&quot;language-typescript&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;enum&lt;/span&gt; AccountType &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// ...&lt;/span&gt;
  Diamond&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// new type&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// accountTypeComponentMap takes an AccountType as input and returns a React&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// component based on the account type&lt;/span&gt;
&lt;span class=&quot;token class-name&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;/span&gt; accountTypeComponentMap &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
  accountType&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; AccountType
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; React&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;FunctionComponent &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; map &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// ...&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;AccountType&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Diamond&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;token comment&quot;&gt;// Fetch the component based on the account type&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// return nothing (null) if can&apos;t find it.&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; map&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;accountType&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h4&gt;Cons&lt;/h4&gt;
&lt;blockquote&gt;
&lt;p&gt;It can also break compatibility with existing code&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;As we are adding a new prop, every consumer will need to update the code where
this component is being used to accommodate the &lt;code class=&quot;language-text&quot;&gt;accountType&lt;/code&gt;. Perhaps now the
situation can be trickier because other teams when updating the code, will also
need to know what would be the &lt;em&gt;default&lt;/em&gt; account type to be used since depending
on what you pass in, it might render additional content.&lt;/p&gt;
&lt;p&gt;To mitigate that, we would need to make sure the documentation is in place and
perhaps have a pre-selected &lt;code class=&quot;language-text&quot;&gt;accountType&lt;/code&gt; if none is given. However, having this
implicit might lead to confusion in the future so I’d choose this approach
carefully.&lt;/p&gt;
&lt;p&gt;i.e.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre class=&quot;language-typescript&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;AccountCardProps&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// ...&lt;/span&gt;
  accountType&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; AccountType&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// accountType now is optional&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; Account&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; React&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;FunctionComponent&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;AccountCardProps&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  firstName&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  lastName&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  avatarURL&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  accountType &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; AccountType&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Regular&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// account type will always be &quot;regular&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// ...&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;3. Using composition&lt;/h3&gt;
&lt;p&gt;The implementation of this approach consists of having separate components that
can be combined in order to achieve a broader “concept”.&lt;/p&gt;
&lt;p&gt;The idea is to extend basic components into more specific ones as needed so that
these components can be opted-in by developers as new features are introduced or
tweaked. This principle is also called &lt;a href=&quot;https://en.wikipedia.org/wiki/Open%E2%80%93closed_principle#:~:text=In%20object%2Doriented%20programming%2C%20the,without%20modifying%20its%20source%20code.&quot;&gt;Open-closed
principle&lt;/a&gt;.
To illustrate this principle applied to our situation, our component is now
&lt;strong&gt;closed for modification&lt;/strong&gt;. Meaning that we don’t add new code, or at least we
minimise the changes to it. However, we will now make it &lt;strong&gt;open for extension&lt;/strong&gt;,
meaning that whenever new requirements come, we extend our component into
something new based on the original one as we would do with &lt;em&gt;lego bricks&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Let’s see how our code would look like with this approach:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre class=&quot;language-typescript&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;AccountCardProps&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  firstName&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  lastName&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  avatarURL&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; AccountCard&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; React&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;FunctionComponent&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;AccountCardProps&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  firstName&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  lastName&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  avatarURL&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;React&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Fragment&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;/* replace &amp;lt;div&gt; by &amp;lt;React.Fragment&gt; */&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;img src&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;avatarURL&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;label&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;First name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;label&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;span&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;firstName&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;span&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;

      &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;label&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;Last name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;label&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;span&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;lastName&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;span&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;React&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Fragment&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; PremiumAccountCard&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; React&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;FunctionComponent&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;AccountCardProps&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  firstName&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  lastName&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  avatarURL&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;React&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Fragment&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;AccountCard
      firstName&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;firstName&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
      lastName&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;lastName&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
      avatarURL&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;avatarURL&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;PREMIUM&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;React&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Fragment&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// usage&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// ...&lt;/span&gt;

&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;PremiumAccountCard
  firstName&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;John&quot;&lt;/span&gt;
  lastName&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Doe&quot;&lt;/span&gt;
  avatarURL&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;https:/example.com/slug-123.png&quot;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// ...&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h4&gt;Pros&lt;/h4&gt;
&lt;blockquote&gt;
&lt;p&gt;Flexible&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This approach is arguable the most flexible of all, but it can also be
considered as over-engineering. All depends on the problem at hand.&lt;/p&gt;
&lt;p&gt;Considering our use case, if the requirement is to just display a new badge when
the user’s account is &lt;em&gt;premium&lt;/em&gt;, breaking the component into a separate
component might be too much. However, this approach can be really powerful
because the original component doesn’t need to be changed necessarily,
therefore, the codebase won’t be affected as we evolve our software.&lt;/p&gt;
&lt;h4&gt;Cons&lt;/h4&gt;
&lt;blockquote&gt;
&lt;p&gt;Risk of introducing the wrong abstraction&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Although I really like this approach, applying it too soon might hurt the code
design since there is a risk of creating wrong abstractions for something we
don’t fully understand yet. If that happens, might be hard to go back to the
simplest state and there is a risk of the “composed” component become a hybrid
beast (generic, but specific, which is pretty much unfortunate, no one wants
that).&lt;/p&gt;
&lt;p&gt;More about that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://wiki.c2.com/?PrematureAbstraction&quot;&gt;Premature Abstraction&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://martinfowler.com/bliki/Yagni.html&quot;&gt;Yagni&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Thank you for reading&lt;/h2&gt;
&lt;p&gt;If you read this far, I hope I didn’t waste your time.&lt;/p&gt;
&lt;p&gt;I’ve talked about 3 approaches that I often consider in situations like this and
I don’t believe there is a right or wrong approach to follow but I thought would
be beneficial listing a few that came to mind here so it can be referenced later
on as a head start in the case you – &lt;em&gt;or future me&lt;/em&gt; – face similar challenges.&lt;/p&gt;
&lt;p&gt;I hope you enjoy this post, if you have any feedback or questions, hit me up on
&lt;a href=&quot;mailto:alabeduarte@gmail.com&quot;&gt;alabeduarte@gmail.com&lt;/a&gt;, I’d be happy to hear your thoughts and be better next
time!&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Multiple .gitconfig files]]></title><description><![CDATA[What  is for? When you set up a new code and uses Git to manage your
version control, unless you are only reading a code of a repository…]]></description><link>https://alabeduarte.com/multiple-git-config/</link><guid isPermaLink="false">https://alabeduarte.com/multiple-git-config/</guid><pubDate>Wed, 20 Jan 2021 07:16:30 GMT</pubDate><content:encoded>&lt;h2&gt;What &lt;code class=&quot;language-text&quot;&gt;.gitconfig&lt;/code&gt; is for?&lt;/h2&gt;
&lt;p&gt;When you set up a new code and uses &lt;a href=&quot;https://git-scm.com/&quot;&gt;Git&lt;/a&gt; to manage your
version control, unless you are only reading a code of a repository that you
&lt;a href=&quot;https://git-scm.com/docs/git-clone&quot;&gt;cloned&lt;/a&gt;, you will have to add some
configuration to start interacting with Git, which is mainly to tell Git “who
you are” since Git’s configurations are per-user.&lt;/p&gt;
&lt;p&gt;For this post, let’s use the infamous &lt;a href=&quot;https://en.wikipedia.org/wiki/John_Doe&quot;&gt;John
Doe&lt;/a&gt; as an example. I believe would be
easier if we do some role play/story tell this because we will be discussing
emails, directory names, company names and I’d like to avoid any situation where
could lead you – the reader – to trouble (i.e. wrong filename, etc).&lt;/p&gt;
&lt;p&gt;Say John Doe wants to set up Git and have more than one email (personal and work)
and John Doe wants to make sure that the commits are being referenced properly
so that other people might reach out to him directly in case of questions with
changes and all. The challenge is that &lt;strong&gt;the company that John Doe works for
requires every employee to use their work-email&lt;/strong&gt; so John Doe needs to pay
attention to his commits.&lt;/p&gt;
&lt;h3&gt;Getting started&lt;/h3&gt;
&lt;p&gt;For a complete guide on how to setup Git, I’d recommend the &lt;a href=&quot;https://git-scm.com/book/en/v2/Getting-Started-First-Time-Git-Setup&quot;&gt;official
guide&lt;/a&gt;, but
to summarise, running the following commands will get you ready to go:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;$ git config --global user.name &quot;John Doe&quot;
$ git config --global user.email johndoe@example.com&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The command above tells Git about your &lt;strong&gt;identity&lt;/strong&gt; which is represented by your
&lt;strong&gt;name&lt;/strong&gt; and your &lt;strong&gt;email&lt;/strong&gt;. This is important because every Git commit uses
this information.&lt;/p&gt;
&lt;p&gt;The &lt;code class=&quot;language-text&quot;&gt;--global&lt;/code&gt; flag means that these configurations will be stored in your
system which will be placed in a file located at &lt;code class=&quot;language-text&quot;&gt;~/.gitconfig&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;NOTE: without the flag &lt;code class=&quot;language-text&quot;&gt;--global&lt;/code&gt;, the configuration provided will only be
applied to the current directory.&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;Checking Your Settings&lt;/h3&gt;
&lt;p&gt;You can check your current Git settings by typing &lt;code class=&quot;language-text&quot;&gt;git config&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;$ git config --list
user.name=John Doe
user.email=johndoe@example.com&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You also can verify the &lt;code class=&quot;language-text&quot;&gt;~/.gitconfig&lt;/code&gt; file:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;$ cat ~/.gitconfig

[user]
  name = John Doe
  email = johndoe@example.com&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Dealing with multiple configurations&lt;/h2&gt;
&lt;p&gt;Git’s API is big so I might not cover all options here, meaning there might be
other ways to achieve the same thing wich is: &lt;strong&gt;be able to commit code using
appropriate identifications&lt;/strong&gt; without the hassle of manually change/check them.&lt;/p&gt;
&lt;p&gt;The approach I’d like to share is to use
&lt;a href=&quot;https://git-scm.com/docs/git-config#_includes&quot;&gt;includeIf&lt;/a&gt;.  What &lt;code class=&quot;language-text&quot;&gt;includeIf&lt;/code&gt;
does is to &lt;strong&gt;append&lt;/strong&gt; a new peace of config from a different path
&lt;strong&gt;conditionally&lt;/strong&gt; so that way, if the conditions are met, an addition to the
existing Git config will be considered when interacting with Git. This is based
on which directory you’re on so in order to make the most use of &lt;code class=&quot;language-text&quot;&gt;includeIf&lt;/code&gt; it
is recommended to keep all repositories within a single folder.  For example:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;- my-workspace/
  - xyz/
    - repository-1/
    - repository-2/
    (...)&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Considering John Doe works at the company &lt;em&gt;xyz&lt;/em&gt; and his email is
&lt;em&gt;&lt;a href=&quot;mailto:j.doe@xyz.com&quot;&gt;j.doe@xyz.com&lt;/a&gt;&lt;/em&gt;, let’s create a &lt;strong&gt;new file&lt;/strong&gt; containing the additional info that
should only be used for work-related repositories:&lt;/p&gt;
&lt;h4&gt;1. Create a new file:&lt;/h4&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;$ touch ~/my-workspace/xyz/.gitconfig-work&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h4&gt;2. Add the work-related info to the newly created file:&lt;/h4&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;[user]
  name = John Doe
  email = j.doe@xyz.com&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h4&gt;3. Add conditional config to the global Git config&lt;/h4&gt;
&lt;p&gt;Now we need to tell Git that, if John Doe is within the work-specific directory
(&lt;code class=&quot;language-text&quot;&gt;~/my-workspace/xyz/&lt;/code&gt; on this case), Git should use a different configuration
instead.&lt;/p&gt;
&lt;p&gt;To make that happen, let’s change the &lt;strong&gt;system Git config file&lt;/strong&gt; located on
&lt;code class=&quot;language-text&quot;&gt;~/.gitconfig&lt;/code&gt; and edit it with the following code:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;[user]
  name = John Doe
  email = johndoe@example.com
[includeIf &quot;gitdir:~/my-workspace/xyz/&quot;]
  path = ~/my-workspace/xyz/.gitconfig-work&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Dealing with different signature keys&lt;/h2&gt;
&lt;p&gt;Git is cryptographically secure and John Doe does not mess around, he wants to
make sure that every commit is from a trusted source. Git provides a mechanism
where you can &lt;a href=&quot;https://git-scm.com/book/en/v2/Git-Tools-Signing-Your-Work&quot;&gt;sign your
commits&lt;/a&gt; but now
that John Doe has different Git configurations, we need to find a way to also
setup different signatures depending on the identification (i.e. one signature
for his personal email, another signature for his work’s email).&lt;/p&gt;
&lt;p&gt;If you’re curious to know how to set up a GPG key with git, please read the
&lt;a href=&quot;https://git-scm.com/book/en/v2/Git-Tools-Signing-Your-Work&quot;&gt;official Git Tools
documentation&lt;/a&gt;.
Additionally, I’d recommend the following &lt;a href=&quot;https://docs.github.com/en/github/authenticating-to-github/telling-git-about-your-signing-key&quot;&gt;this
tutorial&lt;/a&gt;
published by GitHub.&lt;/p&gt;
&lt;p&gt;Similarly to the steps, we followed to set up different configs based on the
directory, let’s start:&lt;/p&gt;
&lt;h4&gt;1. Create a new file with “personal” config:&lt;/h4&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;$ touch ~/my-workspace/side-projects/.gitconfig-personal&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h4&gt;2. Add the signature (i.e. in our case, the signature is &lt;em&gt;SECRET&lt;/em&gt;) to the newly created file:&lt;/h4&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;[user]
  signingkey = SECRET&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h4&gt;3. Add your work-related signature (i.e. &lt;em&gt;SECRETWORK&lt;/em&gt; on this case) to your the work-related Git config&lt;/h4&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;[user]
  name = John Doe
  email = j.doe@xyz.com
  signingkey = SECRETWORK&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h4&gt;4. Now let’s configure our global &lt;code class=&quot;language-text&quot;&gt;~/.gitconfig&lt;/code&gt; with the following:&lt;/h4&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;[includeIf &quot;gitdir:~/my-workspace/xyz/&quot;]
  path = ~/my-workspace/.gitconfig-work&quot;
[includeIf &quot;gitdir:~/my-workspace/side-projects/&quot;]
  path = ~/my-workspace/side-projects/.gitconfig-personal&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h4&gt;5. All set!&lt;/h4&gt;
&lt;p&gt;Now, John Doe can commit freely between personal/work-related with ease!&lt;/p&gt;
&lt;h2&gt;Thank you for reading&lt;/h2&gt;
&lt;p&gt;I hope you enjoy this post, if you have any feedback or questions, hit me up on
&lt;a href=&quot;mailto:alabeduarte@gmail.com&quot;&gt;alabeduarte@gmail.com&lt;/a&gt;, I’d be happy to hear your thoughts and be better next
time!&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Setting up a new machine]]></title><description><![CDATA[Dang! Got a new laptop and I have to set up everything… again… I always find it exciting when I get something new, such as a new laptop…]]></description><link>https://alabeduarte.com/new-env-setup/</link><guid isPermaLink="false">https://alabeduarte.com/new-env-setup/</guid><pubDate>Fri, 08 Jan 2021 03:58:16 GMT</pubDate><content:encoded>&lt;h2&gt;Dang! Got a new laptop and I have to set up everything… again…&lt;/h2&gt;
&lt;p&gt;I always find it exciting when I get something new, such as a new laptop. When
buying one, changing jobs (assuming the employer provides me one) or replacing
old by new.&lt;/p&gt;
&lt;p&gt;The first thing I like to do is to assess the machine’s power, see what is made
in terms of memory and disk space. Then, I see how far I can get in terms of
privileges (i.e. can I install/uninstall whatever I want?).&lt;/p&gt;
&lt;p&gt;This is important to me because I like to keep things clean and tidy so,
eventually, I can start making my own mess by installing my stuff. During my
career I kind of got a mental list of software I lean on, things that I really
need, other things that I &lt;em&gt;might&lt;/em&gt; need and others that I heard that are nice but
I never really used it.&lt;/p&gt;
&lt;p&gt;As I became to customise my settings more and more, &lt;em&gt;having a new machine wasn’t
that exciting anymore&lt;/em&gt;. I not only have to remember to install all of these
things that I need, but it became also tedious and demanded a large cognitive
effort.  Also, I got frustrated many times because I thought I was all set up
until I realised I forgot to install a particular tool.&lt;/p&gt;
&lt;h2&gt;Consistent environment&lt;/h2&gt;
&lt;p&gt;The way I now approach this is to automate my setup as much as I can so that
every time I set up a new machine, my development environment will be consistent
each time. In order to make this happen, I like to structure my environment
setup with the following categories:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Tools&lt;/li&gt;
&lt;li&gt;Access&lt;/li&gt;
&lt;li&gt;Configuration&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h3&gt;Tools&lt;/h3&gt;
&lt;p&gt;I like to go with an approach of running a single command and install everything
I need at once. The operating system that I’ve been using is macOS but most of
the things that I’ll mention here also applies for Linux. In terms of Windows,
probably you will find an equivalent.  What is important here is to stick to the
concept of automation.&lt;/p&gt;
&lt;p&gt;What I use to install the tools that I rely on is &lt;a href=&quot;https://brew.sh/&quot;&gt;Homebrew&lt;/a&gt;
and it is simple as that:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;$ brew install git&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;em&gt;The command above installs &lt;code class=&quot;language-text&quot;&gt;git&lt;/code&gt;, as an example.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;You also can use &lt;code class=&quot;language-text&quot;&gt;brew&lt;/code&gt; to install macOS apps (i.e. browser, code editor, image
visualisation, etc) via &lt;a href=&quot;https://formulae.brew.sh/cask/&quot;&gt;Homebrew Cask&lt;/a&gt;&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;$ brew install --cask firefox&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;When you install something via &lt;code class=&quot;language-text&quot;&gt;brew&lt;/code&gt; (a.k.a Homebrew), the software is now
managed by it. Meaning that whenever you need to upgrade, reinstall or delete,
you can do via homebrew as follows:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;$ brew upgrade git&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The good thing about this is that you can now dump all your software list you
ever installed via brew into one file and that can be used to set up a whole new
machine with a single command. You can achieve that with &lt;a href=&quot;https://github.com/Homebrew/homebrew-bundle&quot;&gt;Homebrew
Bundle&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Please follow the &lt;a href=&quot;https://github.com/Homebrew/homebrew-bundle&quot;&gt;official
documentation&lt;/a&gt; to install it
properly.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Once you are all set, you can run the following command that will consolidate
all the things you have installed into a file usually named &lt;code class=&quot;language-text&quot;&gt;Brewfile&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;$ brew bundle dump --file=Brewfile&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Then, the &lt;code class=&quot;language-text&quot;&gt;Brewfile&lt;/code&gt; will look like this:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;tap &quot;github/gh&quot;
tap &quot;homebrew/bundle&quot;
tap &quot;homebrew/cask&quot;
tap &quot;homebrew/cask-fonts&quot;
tap &quot;homebrew/cask-versions&quot;
tap &quot;homebrew/core&quot;
tap &quot;homebrew/services&quot;
brew &quot;git&quot;
brew &quot;go&quot;
brew &quot;make&quot;
brew &quot;neovim&quot;
brew &quot;tmux&quot;
brew &quot;unzip&quot;
cask &quot;buttercup&quot;
cask &quot;docker&quot;
cask &quot;firefox&quot;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, imagine you do that now, save this file somewhere (i.e. cloud). When you
receive a new machine or even format your current machine, you can run &lt;code class=&quot;language-text&quot;&gt;brew
bundle&lt;/code&gt; to restore all the software that you used to have. See the command
below:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;$ brew bundle --file=homebrew/Brewfile&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The above will install everything as if you were running &lt;code class=&quot;language-text&quot;&gt;brew install git&lt;/code&gt;,
&lt;code class=&quot;language-text&quot;&gt;brew install go&lt;/code&gt;, etc.&lt;/p&gt;
&lt;h3&gt;Access&lt;/h3&gt;
&lt;p&gt;Now you have your stuff in place, there will be a “necessary” pain of
authenticating with most of them (i.e. your cloud storage, your music app). The
way I like to approach this is to get handy a password manager.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;PS: I usually use brew cask to install mine so I don’t have to install it
manually ever again&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;In case you don’t have a password manager, I’d consider downloading one now,
here’s
&lt;a href=&quot;https://www.troyhunt.com/password-managers-dont-have-to-be-perfect-they-just-have-to-be-better-than-not-having-one/&quot;&gt;why&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;A few options to consider:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://1password.com/&quot;&gt;1Password&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://buttercup.pw&quot;&gt;Buttercup&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Configuration&lt;/h3&gt;
&lt;p&gt;This category is pretty much to make sure once you are all set up, your tools
are the way you left them before changing machines (or before you restored your
current machine). I personally don’t bother too much about browser config
because I have personal accounts that bring my config from the browser’s cloud,
but editor configuration is something that is really close to my workflow and if
I don’t see my editor or my terminal the way I remembered, I might have a Hard
time trying to remember where things were before becoming productive.&lt;/p&gt;
&lt;p&gt;About code editors, you might have seen around some GitHub repo called
&lt;code class=&quot;language-text&quot;&gt;dotfiles&lt;/code&gt;. The reason for this name is because most of the configs are files
that have &lt;code class=&quot;language-text&quot;&gt;.&lt;/code&gt; in front of its name (i.e. &lt;code class=&quot;language-text&quot;&gt;.gitconfig&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;.vim&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;.zshrc&lt;/code&gt;,
&lt;code class=&quot;language-text&quot;&gt;.idea&lt;/code&gt;, etc).&lt;/p&gt;
&lt;p&gt;So what most people do, they create a git repository somewhere (i.e. GitHub,
GitLab, Bitbucket, etc) and consolidate all configs into one place so whenever
you clone your repo (from a brand-new machine), you can run a couple of commands
and have your original config in place.&lt;/p&gt;
&lt;p&gt;In addition, you can also add customisable scripts to speed up your initial
setting. As an example, VSCode is an editor that you can install a number of
plugins to support your day-to-day development. So, although I’m not a heavy
VSCode user, I use it sometimes and I have a few plugins that help during these
occasions. So I came up with a script that will read all my current plugins and
store them into a file so whenever I set up a new environment, the script will
install all of these plugins so that when I open VSCode, the &lt;code class=&quot;language-text&quot;&gt;settings.json&lt;/code&gt;
will contain all plugins that I used to have in my previous environment. That’s
was pretty much inspired by &lt;a href=&quot;https://github.com/Homebrew/homebrew-bundle&quot;&gt;Homebrew
Bundle&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Here’s a snippet:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;#!/bin/bash

###
# Creates symbolic link from this dir into where vscode is installed
# then reads VSCodeExtensionsFile and install each extension one by one
###
bundle()
{
  # Creates symbolic link
  rm -rf ~/Library/Application\ Support/Code/User/settings.json || true
  ln -s `pwd`/vscode/settings.json ~/Library/Application\ Support/Code/User/settings.json

  # Install extensions
  while read extension; do
    code --install-extension $extension
  done &amp;lt; vscode/VSCodeExtensionsFile

  echo &quot;Done.&quot;
}

###
# List vscode extensions and place into VSCodeExtensionsFile file
###
dump()
{
  extensions=&quot;vscode/VSCodeExtensionsFile&quot;
  echo &quot;Reading existing extensions from VSCode onto $extensions&quot;
  echo &quot;&quot;

  code --list-extensions | tee $extensions

  echo &quot;Done.&quot;
}

$*&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;— Edited on Wed 20 Jan&lt;/p&gt;
&lt;p&gt;If you want to use the the script above, you can copy its content into your
machine with the following steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Create a file on your &lt;code class=&quot;language-text&quot;&gt;/usr/local/bin&lt;/code&gt; so that you can execute from anywhere&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;$ touch /usr/local/bin/vs-code-bundler&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;ol start=&quot;2&quot;&gt;
&lt;li&gt;Grant permission to the script to being executed&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;$ chmod +x /usr/local/bin/vs-code-bundler&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;ol start=&quot;3&quot;&gt;
&lt;li&gt;
&lt;p&gt;Open the file we created and paste the script&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Test the script&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Run the command below to get your existing extensions and dump into a file.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;$ vs-code-bundler dump&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Then check the content of &lt;code class=&quot;language-text&quot;&gt;vscode/VSCodeExtensionsFile&lt;/code&gt; and see if it matches
with your existing extensions:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;$ cat vscode/VSCodeExtensionsFile&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Save this file somewhere and next time you can run the &lt;code class=&quot;language-text&quot;&gt;bundle&lt;/code&gt; command to
install everything if you need to:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;$ vs-code-bundler bundle&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;hr&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;If you read until now, thank you very much, I hope this article help you somehow
or help someone you know!&lt;/p&gt;
&lt;p&gt;I’ll share below my personal &lt;code class=&quot;language-text&quot;&gt;dotfiles&lt;/code&gt; where I apply most of the things I’ve
discussed and feel free to copy &amp;#x26; paste and make your own config according to
what makes sense to you.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://github.com/alabeduarte/dotfiles&quot;&gt;https://github.com/alabeduarte/dotfiles&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;If you have any questions or feedback, feel free to email me on
&lt;a href=&quot;mailto:alabeduarte@gmail.com&quot;&gt;alabeduarte@gmail.com&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;A few shout outs to friends that helped me and inspired me to do this post:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/celsodantas&quot;&gt;Celso Dantas&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/fernando-alves&quot;&gt;Fernando Alves&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://about.me/gtramontina&quot;&gt;Guilherme Tramontina&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://marcovaltas.com/about.html&quot;&gt;Marco Valtas&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/narcisobenigno&quot;&gt;Narciso Benigno&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded></item><item><title><![CDATA[TDD (Test-Driven Development) - My personal journey]]></title><description><![CDATA[What is TDD? TDD stands for Test-Driven Development and it is a Software
Development’s process that was credited to Kent
Beck.  The idea…]]></description><link>https://alabeduarte.com/tdd/</link><guid isPermaLink="false">https://alabeduarte.com/tdd/</guid><pubDate>Fri, 01 Jan 2021 14:00:00 GMT</pubDate><content:encoded>&lt;p&gt;What is TDD? TDD stands for Test-Driven Development and it is a Software
Development’s process that was credited to &lt;a href=&quot;https://www.kentbeck.com/&quot;&gt;Kent
Beck&lt;/a&gt;.  The idea consists of writing unit tests for
your program, making them fail, then introducing the minimum amount of code that
is enough to make the test pass, then you continue writing more tests according
to your requirements at hand. By having tests in place, any change you do on
your main code (let’s call it production code from now) can be checked against
those tests and running them for every new change can be a way to get faster
feedback, as an alternative to boot your application (whatever that is), run by
each scenario and check if things are working as expected.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 630px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/98aa19d5bc7b830b8b483512c6e404c4/f3c07/traditional-approach.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 38.60759493670886%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAABYlAAAWJQFJUiTwAAAB80lEQVR42l3S30uTURzH8bPNpmKJYWJzmou6qIvqwssyN4OophO6sCwj1g8cGkKrzIzNiYy0UVuCz7TyosL9AUXNJt50labLmLrEKdlFSEGNIohu3p3nmYn1wIvzPed8zuHw5RFj75YIPX1NOPqG/ucTDMhReTGBImt17WEsvmYoNvUPbW00LnOTjL5dRP3E7gt9lJ7spUwqPx2gsK4by6kA5VLxcT+6yjbEAalKOuRF2HwZ1R7EwbbM/v6r5Dm6WP6SRmxzKxRfH8C0qvCKQlnHA0pkvUXW2S0hci6FMFwMYql3U3HGRUWjC/OJaxiaMnvG5hAb6v0klz8jdoy/oujnLKZvCbZKJekZbVSZ0glK5dzyO0nOdJxHnn0wJOC+oNdrJXc+wfZfc5i/z5LXHmYutYIwB56QG46Qr0TYtM7Gv/qHyR+MoL/1GLuzAZ/bhu+yDet5J1mBYQoGM7nscwGSH+QLNzu6EZXt6Gw3EdaONepcX71K1lmHZc/sPRhrgxhrgogjftlHmVPzVTcwHOvk/Ud54c6mPsRRD/q6LoTDh04SNZ0UNPRgdt7FdPYOe1oV9raG2eW6Jy/1YKj1opO0vHrO7qWo8Tafvv5AzMhGPhufZ2Rygeh6UwuMaFLE4ou8jKc00f9yakY9P720ov02fwAbKodJGZ1SPQAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Traditional Approach&quot;
        title=&quot;&quot;
        src=&quot;/static/98aa19d5bc7b830b8b483512c6e404c4/f058b/traditional-approach.png&quot;
        srcset=&quot;/static/98aa19d5bc7b830b8b483512c6e404c4/c26ae/traditional-approach.png 158w,
/static/98aa19d5bc7b830b8b483512c6e404c4/6bdcf/traditional-approach.png 315w,
/static/98aa19d5bc7b830b8b483512c6e404c4/f058b/traditional-approach.png 630w,
/static/98aa19d5bc7b830b8b483512c6e404c4/40601/traditional-approach.png 945w,
/static/98aa19d5bc7b830b8b483512c6e404c4/78612/traditional-approach.png 1260w,
/static/98aa19d5bc7b830b8b483512c6e404c4/f3c07/traditional-approach.png 2516w&quot;
        sizes=&quot;(max-width: 630px) 100vw, 630px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 630px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/27084a2e42d15111216f1114342fb272/fb371/tdd-approach.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 39.24050632911392%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAABYlAAAWJQFJUiTwAAACKklEQVR42k2PW0gTYBiGf3eyyLAulAiki5CgoBLCqKgkGU1KUzqhGIPhSorEY+nUqW1T8VBh4srMSDRFcJSoHUyn5rKNyqWBWpGFJUVedWFZ0tO/5YUXD9/7nf7v/cX8r990uidpdr6mZWCMu5K2oTdLjNM6OO7Xvtjqqy3ly/H1HM8m+DG/gLjT5yVMX014ylW2nK1l05kaghIsCG0+AbpCRIzkYMH/qFvCly9DFWNG7M3hWpcbUdUxLIsmgk6U+ll1zIoqvoSAIyWEGa6wXl9FuDyyLc1OZEY9q4/bEHHF/r4ithiF1OqjVkTUBaxtg/JBx4h0Y0GplZcP5KE6bENzqNSvFTozgQmVqGMuodCaWBFvRRNXgYg2yR0TythS/6xS/kbsycbWPoS4cd/JLr2BYFMtIsvO5uR0ItOyUeTXo06tZr/eSFiWdJXbQKi+CK3RSGBeHSLTztakc+zIyEVjvoVItGDzOXzU3Y7XHsL2eQ+qmXeUF0Th6IsjmFlCHvfgLgrBOF2H8u8c0ZdNfLoZyobFSZTTb6m/GEGTK5m1fEfcbsHS+ATR/dSNLuUka2zXEeZGIpJT2Zd5HmV5E5r0GnSnEtlYKB2WNbPOUEDC6URWlklHhY3sTDKwOycDdWUzIqWChgcvED//QM/YHL2uKQZGpuge/UKn5zNO1yROz3vujX7j4cgHBlwT9L76iOPlV/p9s8+n6PLO0uWZoX94Apd3moXFRf4B3saIVSBOBSUAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;TDD Approach&quot;
        title=&quot;&quot;
        src=&quot;/static/27084a2e42d15111216f1114342fb272/f058b/tdd-approach.png&quot;
        srcset=&quot;/static/27084a2e42d15111216f1114342fb272/c26ae/tdd-approach.png 158w,
/static/27084a2e42d15111216f1114342fb272/6bdcf/tdd-approach.png 315w,
/static/27084a2e42d15111216f1114342fb272/f058b/tdd-approach.png 630w,
/static/27084a2e42d15111216f1114342fb272/40601/tdd-approach.png 945w,
/static/27084a2e42d15111216f1114342fb272/78612/tdd-approach.png 1260w,
/static/27084a2e42d15111216f1114342fb272/fb371/tdd-approach.png 2512w&quot;
        sizes=&quot;(max-width: 630px) 100vw, 630px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;My journey&lt;/h2&gt;
&lt;p&gt;TDD (Test-Driven Development) is one of many techniques for developing software
and this article aims to share my personal experience with it. I hope that I can
clarify my thoughts on &lt;strong&gt;when I believe it can help&lt;/strong&gt; and when I believe we
should take a step back. I need to say that &lt;strong&gt;I found TDD pretty difficult and
abstract at first&lt;/strong&gt;. There will always be a space for discussion and that’s why
other processes such as pair-programming and code review are extremely
complementary to what I consider healthy software development processes. Let me
give you some context before I start, so then you can understand better where I
came from.&lt;/p&gt;
&lt;p&gt;I’ve started reading about TDD on the internet and what helped me the most in my
first steps were the articles from the &lt;a href=&quot;https://www.caelum.com.br&quot;&gt;Caelum&lt;/a&gt;
website. I found really nice examples of how to do it at the time through their
articles and further I found &lt;a href=&quot;https://alabeduarte.com/&quot;&gt;books&lt;/a&gt; which led me to
the formal definitions written by Kent Beck. Then I joined
&lt;a href=&quot;https://www.thoughtworks.com&quot;&gt;ThoughtWorks&lt;/a&gt; in 2011 and there I believe it was
a turning point for me. ThoughtWorks is famous for its revolutionary mindset
(look for &lt;a href=&quot;https://agilemanifesto.org&quot;&gt;the agile manifesto&lt;/a&gt;), the &lt;a href=&quot;https://www.thoughtworks.com/radar&quot;&gt;Tech
Radar&lt;/a&gt; publications and for caring about
code quality. However, the reason I believe working there was so important was
because of the people that I had a chance to work with at the time. They really
encouraged me to challenge the current state of the code and how it could be
improved. We also had a strong pair-programming culture which combined with
writing tests, challenging code maintainability became pretty natural.&lt;/p&gt;
&lt;h2&gt;Why do I believe TDD is important?&lt;/h2&gt;
&lt;h3&gt;Test-Driven Development enables refactoring.&lt;/h3&gt;
&lt;p&gt;As I mentioned above, having tests that exercise your code’s intention (function
or method) opens room for refactoring the code with stronger confidence. You can
move stuff around, you can rename things that express better what you are trying
to do, you can add an argument to a function or you can remove them all. If
after all of that you run your tests and they pass, chances are that you’ve done
everything right! The opposite is true as well. If you change things and after
running the tests some of them fail, it could be a sign that you’ve done
something wrong and needs to be fixed. While refactoring the code, if you notice
that one single change affects multiple tests, it could be a sign that the
coupling between components is high or the scope of the change/refactor is big.&lt;/p&gt;
&lt;p&gt;With time, &lt;strong&gt;I learned that TDD is not about testing first necessarily&lt;/strong&gt;. The
process of writing tests first, seeing them fail to then make them pass to be
able to refactor really helps but the key factor to me was let the tests drive
how I think so that my scope of changes can be expressed in the tests. I might
want to refactor something, but if my scenarios are pretty small, I can postpone
this decision for later.&lt;/p&gt;
&lt;h3&gt;Test-Driven Development favours design&lt;/h3&gt;
&lt;p&gt;TDD is all about letting your tests guide you about the problem you need to
solve. It is pretty common to use the baby steps terminology here. Basically,
you will start with really small and basic scenarios (for a given input, I
expect this output). With that in mind, your code design should be simple enough
to satisfy the requirements. Another aspect that can contribute to better design
is that your test is the first consumer of your code. There you will call the
function that you carefully named and thoroughly added certain arguments and you
will see if that really expresses what you want to do. You may ask: “Should I
really pass 4 arguments in here?” “Is this name good?” “Isn’t too complicated to
set up the initial state for just testing if this does what it needs to do?” If
the answer to this question doesn’t feel quite right, you can use this
opportunity to change your code and look at your unit tests again and imagine:
“how will someone call this code in real life? Is it easy to read?”.&lt;/p&gt;
&lt;p&gt;Sometimes, “For a given input, I expect this output” is not possible.  You could
argue that it is not that simple as sometimes it is not easy to structure input
and expected output. However, The fact that might not be possible all times
doesn’t change the fact that you can always think about it and make it as an
exercise. Challenge yourself often and see if this is really the case. If not,
try breaking down your code into smaller components and then ask this question
again.&lt;/p&gt;
&lt;h3&gt;Good code design is pretty subjective!&lt;/h3&gt;
&lt;p&gt;YES! That is it! If I’m coding all by myself I’m pretty sure I’ll understand it
all and every decision will make sense until I have to explain to someone.
That’s how pair programming and/or code review becomes crucial here. The process
of doing TDD evolves empathy as well. I tend to think that we need to solve some
problems right away, but often when I share my thoughts with someone I realise
that I’m thinking too far ahead. Conversations like that can help you to avoid
overengineering and make decisions prematurely. As we know by now, our tests are
the first consumers of our code, so lean on that and discuss with your peers
whether this is really a good design. Here are some questions that I often try
to ask myself:&lt;/p&gt;
&lt;ul class=&quot;contains-task-list&quot;&gt;
&lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; checked disabled&gt; Is my code easy to maintain?&lt;/li&gt;
&lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; checked disabled&gt; Is my code easy to change?&lt;/li&gt;
&lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; checked disabled&gt; Is my code easy to read?&lt;/li&gt;
&lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; checked disabled&gt; Is my code expressive enough?&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;TDD doesn’t mean bug-free code!&lt;/h3&gt;
&lt;p&gt;I’ve heard an argument before saying that if you’re doing TDD, your code
shouldn’t have bugs. Well, I believe this is a bold statement. Of course, my
code can have bugs. Test-Driven Development is a technique that requires
discipline and experience. If I don’t follow the steps, for example, if I never
see my tests failing, I can fall into the false positive trap. It is crucial to
write a test, see it fail, then make it pass! The tests should also be reliable.
Is part of the good design I mentioned above that our tests are also easy to
read and maintain. They should be predictable and clean. If there are too many
dependencies or setups, it could be a sign that my code is too coupled,
therefore, it will be hard to maintain and false positives can be introduced.
Besides, at this stage, you could argue if having tests is really adding value
given the amount of setup that it might require if you come to that point.&lt;/p&gt;
&lt;p&gt;There is another aspect to it, which is: we still can write tons of unit tests
following all the rules, but with the wrong assumptions. If we don’t understand
the problem we are trying to solve, we will end up having tests that will
exercise something that doesn’t make sense with tests passing.&lt;/p&gt;
&lt;h3&gt;Tooling&lt;/h3&gt;
&lt;p&gt;In the previous sections, I’ve described techniques, beliefs and discipline. To
make it stick and be pleasant, we need to have good tooling. The tests should be
fast to run. It should be quicker to run a test than open the application (i.e.
browser or mobile device) myself and test it out. While coding, it should be
quick to trigger the tests as well. Nowadays, most programming languages’
ecosystems contain some tool where it watches your code and whenever you save a
file, the tests relevant to that file will run. That is the sweet spot, in my
opinion.&lt;/p&gt;
&lt;h3&gt;Final thoughts and when doing TDD doesn’t help&lt;/h3&gt;
&lt;p&gt;I believe that Test-Driven Development doesn’t help when we are not quite
certain about what to do and what to expect. Sometimes, you might not be
familiar with the language, framework or platform and you might need some time
to play around with the available APIs and see what it is possible and what can
be a constraint.&lt;/p&gt;
&lt;p&gt;As the expected outcome might be unknown, having the development guided by tests
can be extremely difficult since TDD is all about defining the expectations and
figuring out how to meet them. So what usually works well for me is to do quick
experiments, writing code with no tests at all, see what can be done, explore
the limits of the tool/platform/API and then restart with TDD mindset using the
experiments as a reference/documentation. After that, I can safely throw the
experiment away and continue with the new code.&lt;/p&gt;
&lt;p&gt;Of course, this might not work for everyone, but as I mentioned a few times, it
is all about the exercise. If you try to do that, you might learn a thing or two
and next time you will perform better. All I want is the short feedback and the
ability to iterate over my understanding of the problem at hand often.&lt;/p&gt;
&lt;h3&gt;References&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://agilemanifesto.org&quot;&gt;Agile Manifesto&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.amazon.com.au/Test-Driven-Development-Kent-Beck/dp/0321146530&quot;&gt;Test-Driven Development by
Example by Kent Beck&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.amazon.com.au/Growing-Object-Oriented-Software-Guided-Tests/dp/0321503627&quot;&gt;Growing Object-Oriented Software, Guided by
Tests by Nat Pryce&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.amazon.com/Test-Driven-Development-Teste-Design-Portuguese-ebook/dp/B00WKMN24W&quot;&gt;Test-Driven Development: Teste e Design no Mundo Real (Portuguese
Edition) by Mauricio Aniche&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded></item><item><title><![CDATA[This Blog]]></title><description><![CDATA[I’ve had some opportunities to write technical blogs and documentation
throughout my career (in the context of my day-to-day job) but I…]]></description><link>https://alabeduarte.com/this-blog/</link><guid isPermaLink="false">https://alabeduarte.com/this-blog/</guid><pubDate>Fri, 01 Jan 2021 13:00:00 GMT</pubDate><content:encoded>&lt;p&gt;I’ve had some opportunities to write technical blogs and documentation
throughout my career (in the context of my day-to-day job) but I always deferred
the moment to atually write my own stuff, my own experiences you know?&lt;/p&gt;
&lt;p&gt;Now, I’m finally ready – I guess. The idea is document and share my learnings so
that we (you, dear reader, and myself) can come back later and check things out.&lt;/p&gt;
&lt;p&gt;I hope you enjoy the posts!&lt;/p&gt;</content:encoded></item><item><title><![CDATA[About me]]></title><description><![CDATA[Background Software Engineer since 2007, my professional career has given me a
great experience building applications in many different…]]></description><link>https://alabeduarte.com/about-me/</link><guid isPermaLink="false">https://alabeduarte.com/about-me/</guid><pubDate>Fri, 01 Jan 2021 12:00:00 GMT</pubDate><content:encoded>&lt;h2&gt;Background&lt;/h2&gt;
&lt;p&gt;Software Engineer since 2007, my professional career has given me a
great experience building applications in many different languages and domains
spanning health, energy/oil/gas, media and retail. I am a quality-focused
engineer with over 5 years of experience leading technical teams of 10+
including coaching and mentoring offshore teams.&lt;/p&gt;
&lt;p&gt;I have a solid understanding of object-oriented programming and functional
programming paradigms applied with extensive experience on test-driven
development (TDD) and pair programming techniques. I have delivered production
applications in many different languages and platforms including Go, Elixir,
Ruby, Javascript/Typescript, Scala, Java, Android and iOS.&lt;/p&gt;
&lt;h2&gt;Values&lt;/h2&gt;
&lt;p&gt;I believe in transparency, servant leadership and constant — two-way —
feedback. I’m constantly challenging user’s value and scope and I feel pretty
excited about social impact. I also believe that people can do amazing things
given the right opportunity, and I also believe that opportunities can be
created.&lt;/p&gt;
&lt;p&gt;I also enjoy speaking/attending conferences, coaching, sharing knowledge and
contributing back to the software community.&lt;/p&gt;
&lt;p&gt;Ways of working: I have led a number of programs on the path of continuous
delivery, with a focus on technical capability uplift and the realisation of
benefits including shorter cycle time of features released to end-users.&lt;/p&gt;
&lt;p&gt;I consider myself an easy-going person. I tend to avoid challenging the status
quo without building a rapport around the environment and I try to listen and
learn how things work first.&lt;/p&gt;
&lt;p&gt;I appreciate knowledge sharing, thoughtful code reviews and pair programming. I
wouldn’t be so confident and happy about my career if I didn’t have the input
from the people that I had the chance to work with.&lt;/p&gt;
&lt;p&gt;I like to apply TDD whenever is possible and I try to keep my changes small. I
also believe in discipline and code that favours the ability to refactor and
good design. I understand that good design is subjective, so I tend to ask some
key questions while I’m coding, such as:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Is my code easy to maintain?&lt;/li&gt;
&lt;li&gt;Is my code easy to change?&lt;/li&gt;
&lt;li&gt;Is my code easy to read?&lt;/li&gt;
&lt;li&gt;Is my code expressive enough?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When doing code review, I tend to be flexible by encouraging follow-up PRs to
address minor changes so that features can be unblocked as smaller PRs are being
shipped. That also means a smaller scope of changes and fewer risks of breaking
things.&lt;/p&gt;</content:encoded></item></channel></rss>