<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Alejandro Barba</title>
    <description>The latest articles on DEV Community by Alejandro Barba (@alejandro742).</description>
    <link>https://dev.to/alejandro742</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F574452%2F35b63cff-6fa1-4e01-9682-eb58b7809beb.jpeg</url>
      <title>DEV Community: Alejandro Barba</title>
      <link>https://dev.to/alejandro742</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alejandro742"/>
    <language>en</language>
    <item>
      <title>How Changing Requirements Shape the Infrastructure of a Software Project</title>
      <dc:creator>Alejandro Barba</dc:creator>
      <pubDate>Tue, 25 Feb 2025 05:14:02 +0000</pubDate>
      <link>https://dev.to/alejandro742/how-changing-requirements-shape-the-infrastructure-of-a-software-project-h89</link>
      <guid>https://dev.to/alejandro742/how-changing-requirements-shape-the-infrastructure-of-a-software-project-h89</guid>
      <description>&lt;h2&gt;
  
  
  🎬 Introduction
&lt;/h2&gt;

&lt;p&gt;Since few weeks ago I'm learning about AWS SAM so I decided to create a small project to put in practice what I've been learning.&lt;/p&gt;

&lt;p&gt;The project is a wallet app with features we all know: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Register incomes and outcomes.&lt;/li&gt;
&lt;li&gt;Categorize the incomes and outcomes.&lt;/li&gt;
&lt;li&gt;Label the incomes and outcomes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An important requirement of this dummy wallet app was to allow users to create and manage their own categories for tracking incomes and outcomes. But, these categories needed to be private to each user meaning that a category created by one user could not be shared with another.&lt;/p&gt;

&lt;h2&gt;
  
  
  💪 Hands To Work
&lt;/h2&gt;

&lt;p&gt;So I started designing the DB using this &lt;a href="https://drawsql.app/" rel="noopener noreferrer"&gt;cool tool&lt;/a&gt;. The project has 2 tables, &lt;code&gt;users&lt;/code&gt; and &lt;code&gt;categories&lt;/code&gt; . The user can create many categories as he wants so the first approach I took was creating a third table, a union table to store &lt;code&gt;user_id&lt;/code&gt; and &lt;code&gt;category_id&lt;/code&gt;.&lt;br&gt;
With this solution the users are able to create &lt;code&gt;x&lt;/code&gt; numbers of categories and we can see assign the category to the user.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Example:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv4zowgcju61lgu1q094z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv4zowgcju61lgu1q094z.png" alt="db_diagram" width="794" height="362"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  ⚠️The problem
&lt;/h2&gt;

&lt;p&gt;Technically, this approach works but something smells weird here. While it correctly assigns categories and users, the current design allows to assign a category created by User A to User B. &lt;br&gt;
 For example if user Alejandro creates the "Health" category, this category can be assigned to user Edith by simply creating the record in &lt;code&gt;user_categories&lt;/code&gt; table. But the requirement is clear: &lt;em&gt;A user can create as many categories as they want, but these categories must not be shared. Categories created by a specific user should remain tied to that user only&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  🤓 The Solution
&lt;/h2&gt;

&lt;p&gt;Although we can enforce this rule both on the backend and frontend, it’s generally a good practice to align all components with the project’s core requirements to maintain clarity and consistency.&lt;/p&gt;

&lt;p&gt;So at the end, I've changed the design to this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1huoecr0pn1zuldzr43a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1huoecr0pn1zuldzr43a.png" alt="db_diagram" width="785" height="249"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This aligns with the given requirement and reduces the complexity of the database.&lt;/p&gt;

&lt;h2&gt;
  
  
  🧠 Conclusions
&lt;/h2&gt;

&lt;p&gt;I know this is a very basic example but its clear and concise. While coding is a crucial (and funny) part of the job, a software engineer’s role is to think critically about the design choices, taking into account factors like maintainability, security, and scalability.&lt;/p&gt;

</description>
      <category>softwareengineering</category>
      <category>sideprojects</category>
      <category>management</category>
      <category>database</category>
    </item>
    <item>
      <title>How the Strategy Pattern Can Simplify Complex Logic</title>
      <dc:creator>Alejandro Barba</dc:creator>
      <pubDate>Thu, 13 Feb 2025 04:53:51 +0000</pubDate>
      <link>https://dev.to/alejandro742/how-the-strategy-pattern-can-simplify-complex-logic-49na</link>
      <guid>https://dev.to/alejandro742/how-the-strategy-pattern-can-simplify-complex-logic-49na</guid>
      <description>&lt;h2&gt;
  
  
  🧠 Introduction
&lt;/h2&gt;

&lt;p&gt;The Strategy pattern is a behavioral design pattern that allows you to define multiple ways to perform an operation by encapsulating them in separate classes. This makes behaviors interchangeable, following the Open/Closed Principle: open for extension but closed for modification.&lt;/p&gt;

&lt;h3&gt;
  
  
  🙋 Why it’s useful and in what scenarios it’s applied.
&lt;/h3&gt;

&lt;p&gt;This pattern is useful when an application needs to perform the same operation using different approaches. Instead of cluttering your code with long if-else or switch statements, you encapsulate each variation in its own class, making the code more maintainable and scalable.&lt;/p&gt;

&lt;h3&gt;
  
  
  😵‍💫 Common misconceptions or confusion around it.
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Strategy vs. Factory Pattern&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; ❗&lt;strong&gt;Misconception:&lt;/strong&gt; "Strategy and Factory are the same."&lt;/li&gt;
&lt;li&gt; ✅ &lt;strong&gt;Reality:&lt;/strong&gt; The &lt;strong&gt;Factory Pattern&lt;/strong&gt; is about &lt;strong&gt;creating&lt;/strong&gt; objects, while the &lt;strong&gt;Strategy Pattern&lt;/strong&gt; is about &lt;strong&gt;choosing a behavior&lt;/strong&gt; at runtime.

&lt;ul&gt;
&lt;li&gt; You can use a &lt;strong&gt;Factory&lt;/strong&gt; to instantiate the correct &lt;strong&gt;Strategy&lt;/strong&gt;, but they serve different purposes.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Thinking It's Only for Algorithms&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;❗&lt;strong&gt;Misconception:&lt;/strong&gt; "Strategy is only for sorting or mathematical algorithms."&lt;/li&gt;
&lt;li&gt;✅&lt;strong&gt;Reality:&lt;/strong&gt; It applies to &lt;strong&gt;any scenario where behavior varies dynamically&lt;/strong&gt;, such as:

&lt;ul&gt;
&lt;li&gt;Payment methods 💰&lt;/li&gt;
&lt;li&gt;Logging mechanisms 👨‍💻&lt;/li&gt;
&lt;li&gt;Compression formats (ZIP, GZIP, etc.) 🗜️&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Overcomplicating Small Problems&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;❗&lt;strong&gt;Misconception:&lt;/strong&gt; "Strategy should be used everywhere!"&lt;/li&gt;
&lt;li&gt;✅&lt;strong&gt;Reality:&lt;/strong&gt; If you only have &lt;strong&gt;two or three simple cases&lt;/strong&gt;, a basic function or if-else might be enough.

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Good Use:&lt;/strong&gt; Multiple payment gateways with different API calls.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bad Use:&lt;/strong&gt; A function that just switches between two database queries.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Forgetting Dependency Injection&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;❗&lt;strong&gt;Misconception:&lt;/strong&gt; "Each Strategy should be instantiated manually."&lt;/li&gt;
&lt;li&gt;✅&lt;strong&gt;Reality:&lt;/strong&gt; It’s better to use &lt;strong&gt;Dependency Injection&lt;/strong&gt; or a &lt;strong&gt;Factory Pattern&lt;/strong&gt; to select and provide the strategy dynamically.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;📌 Problem Example&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;You launch an app in Mexico, and it quickly becomes a hit! 🎉 At first, you integrate &lt;strong&gt;Conekta&lt;/strong&gt;, a popular Mexican payment gateway.&lt;/p&gt;

&lt;p&gt;As time goes on, users start requesting &lt;strong&gt;PayPal&lt;/strong&gt;, so you add support for it. Then, your app gains traction in the &lt;strong&gt;USA&lt;/strong&gt;, and you integrate &lt;strong&gt;Stripe&lt;/strong&gt; to accept USD payments.&lt;/p&gt;

&lt;p&gt;Later, your app expands to &lt;strong&gt;Colombia&lt;/strong&gt;, where you realize that using a &lt;strong&gt;local payment gateway&lt;/strong&gt; reduces transaction fees compared to Stripe. To cut costs and keep users happy, you integrate yet another payment provider.&lt;/p&gt;

&lt;p&gt;Your app is a commercial success... but your code is a &lt;strong&gt;nightmare&lt;/strong&gt;. 😱 Maintaining multiple payment gateway integrations has become a tangled mess, full of &lt;strong&gt;if-else statements&lt;/strong&gt; and duplicated logic.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;💡 Solution: The Strategy Pattern&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Instead of hardcoding payment logic, the &lt;strong&gt;Strategy Pattern&lt;/strong&gt; offers a cleaner approach:&lt;/p&gt;

&lt;p&gt;✅ &lt;strong&gt;Encapsulate payment algorithms&lt;/strong&gt; in separate classes, called &lt;strong&gt;strategies&lt;/strong&gt;. Each class represents a different payment gateway (e.g., &lt;code&gt;ConektaStrategy&lt;/code&gt;, &lt;code&gt;PayPalStrategy&lt;/code&gt;, &lt;code&gt;StripeStrategy&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;✅ &lt;strong&gt;Create a &lt;code&gt;Context&lt;/code&gt; class&lt;/strong&gt; that decides which strategy to use at runtime. The context &lt;strong&gt;doesn't need to know the details&lt;/strong&gt; of each payment method—it simply delegates the work to the selected strategy.&lt;/p&gt;

&lt;p&gt;✅ &lt;strong&gt;Define a common interface&lt;/strong&gt; for all payment strategies. This ensures consistency in your code and makes it easy to add new payment gateways in the future&lt;/p&gt;

&lt;p&gt;Here’s a TypeScript example demonstrating the &lt;strong&gt;Strategy Pattern&lt;/strong&gt; for handling multiple payment gateways dynamically:&lt;/p&gt;

&lt;h3&gt;
  
  
  👨‍💻 Code Example
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Step 1: Define a Common Interface&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Each payment method should follow the same structure.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// PaymentStrategy.ts&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;PaymentStrategy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;pay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Step 2: Create Payment Strategies&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Each payment gateway gets its own class implementing &lt;code&gt;PaymentStrategy&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ConektaStrategy.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;PaymentStrategy&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./PaymentStrategy&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ConektaStrategy&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;PaymentStrategy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;pay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Processing payment of $&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; MXN via Conekta.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// PayPalStrategy.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;PaymentStrategy&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./PaymentStrategy&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PayPalStrategy&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;PaymentStrategy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;pay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Processing payment of $&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; USD via PayPal.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// StripeStrategy.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;PaymentStrategy&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./PaymentStrategy&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;StripeStrategy&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;PaymentStrategy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;pay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Processing payment of $&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; USD via Stripe.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// ColombianGatewayStrategy.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;PaymentStrategy&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./PaymentStrategy&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ColombianGatewayStrategy&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;PaymentStrategy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;pay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Processing payment of $&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; COP via Colombian Gateway.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Step 3: Implement the Context Class&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This class selects the payment strategy at runtime.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// PaymentContext.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;PaymentStrategy&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./PaymentStrategy&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PaymentContext&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;strategy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;PaymentStrategy&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;strategy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;PaymentStrategy&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;strategy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;strategy&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;setStrategy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;strategy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;PaymentStrategy&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;strategy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;strategy&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;processPayment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;strategy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Step 4: Use the Strategy Pattern in Your App&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Now, we can dynamically switch payment methods:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// main.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;PaymentContext&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./PaymentContext&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ConektaStrategy&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./ConektaStrategy&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;PayPalStrategy&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./PayPalStrategy&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;StripeStrategy&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./StripeStrategy&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ColombianGatewayStrategy&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./ColombianGatewayStrategy&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Select initial payment method&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;paymentContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PaymentContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ConektaStrategy&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="nx"&gt;paymentContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;processPayment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Processing payment of $500 MXN via Conekta.&lt;/span&gt;

&lt;span class="c1"&gt;// Switch to PayPal&lt;/span&gt;
&lt;span class="nx"&gt;paymentContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setStrategy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PayPalStrategy&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="nx"&gt;paymentContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;processPayment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Processing payment of $100 USD via PayPal.&lt;/span&gt;

&lt;span class="c1"&gt;// Switch to Stripe&lt;/span&gt;
&lt;span class="nx"&gt;paymentContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setStrategy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;StripeStrategy&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="nx"&gt;paymentContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;processPayment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Processing payment of $200 USD via Stripe.&lt;/span&gt;

&lt;span class="c1"&gt;// Switch to Colombian Gateway&lt;/span&gt;
&lt;span class="nx"&gt;paymentContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setStrategy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ColombianGatewayStrategy&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="nx"&gt;paymentContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;processPayment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;300000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Processing payment of $300000 COP via Colombian Gateway.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  🚀 &lt;strong&gt;Benefits of This Approach&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;✅ &lt;strong&gt;Scalability:&lt;/strong&gt; Easily add new payment gateways without modifying existing code.&lt;br&gt;&lt;br&gt;
✅ &lt;strong&gt;Clean Code:&lt;/strong&gt; No more long &lt;code&gt;if-else&lt;/code&gt; statements for payment processing.&lt;br&gt;&lt;br&gt;
✅ &lt;strong&gt;Flexibility:&lt;/strong&gt; Switch payment methods dynamically at runtime.&lt;/p&gt;

&lt;p&gt;Would you like me to add any refinements, such as using dependency injection or a factory to select the strategy automatically? 😃&lt;/p&gt;

&lt;h2&gt;
  
  
  🤔 &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;Strategy Pattern&lt;/strong&gt; is a powerful tool for managing complex or varying behaviors in your application. In the context of payment gateways, it allows you to encapsulate different payment methods in separate classes, making your code more maintainable and scalable. By using the &lt;strong&gt;Context&lt;/strong&gt; class to delegate the logic and &lt;strong&gt;Dependency Injection (DI)&lt;/strong&gt; to manage your strategies, you ensure that your app is flexible enough to handle future changes or new payment methods seamlessly.&lt;/p&gt;

&lt;p&gt;Remember, the Strategy Pattern helps avoid long chains of &lt;code&gt;if-else&lt;/code&gt; or &lt;code&gt;switch&lt;/code&gt; statements, making your code cleaner and easier to extend. Whether you’re building a payment processing system or dealing with other dynamic behaviors, this pattern can simplify your design and reduce code duplication.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;😁 Let me know your thoughts&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;What do you think? Have you implemented the &lt;strong&gt;Strategy Pattern&lt;/strong&gt; in your own projects? If so, how did it help simplify your code? Feel free to share your thoughts or any questions you might have about this pattern in the comments below!&lt;/p&gt;

&lt;p&gt;Let’s keep the conversation going! 👇&lt;/p&gt;

</description>
      <category>programming</category>
      <category>designpatterns</category>
      <category>payments</category>
      <category>javascript</category>
    </item>
    <item>
      <title>From Zero to Developer: My Journey into the World of Code</title>
      <dc:creator>Alejandro Barba</dc:creator>
      <pubDate>Tue, 04 Feb 2025 05:13:16 +0000</pubDate>
      <link>https://dev.to/alejandro742/from-zero-to-developer-my-journey-into-the-world-of-code-5hd8</link>
      <guid>https://dev.to/alejandro742/from-zero-to-developer-my-journey-into-the-world-of-code-5hd8</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Hi, I’m Alejandro, a 26-year-old software engineer, and this is my journey (so far).&lt;/p&gt;

&lt;p&gt;Right now (new Date() JS joke here), I’m living a great life doing what I love (sometimes 😜). But it wasn’t always this way. Four years ago, I made the decision to leave college and start working as a programmer. Five years ago, I was considering leaving college altogether to pursue a career in gastronomy. And even further back, I dreamed of becoming a doctor. Life has a funny way of taking unexpected turns, doesn’t it?&lt;/p&gt;

&lt;p&gt;In this post, I’ll share how I went from being unsure about my future to building a career I’m passionate about—and the lessons I learned along the way.&lt;/p&gt;

&lt;h2&gt;
  
  
  The beginning
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;How did I start?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;My first “Oh boy, this is what I love!” moment came when I was 17, studying programming in high school. The topics of the class were Visual Basic, Excel, and C++. I loved the feeling of solving problems and seeing my code actually do something—exactly what I wanted it to do. But what I loved even more was that my friends paid me to do their projects for them. (Hey, it was my first taste of freelancing!)&lt;/p&gt;

&lt;p&gt;After high school, I went to college to study computer science. Back then, my goal was to get a PhD (naive me, right?). But reality hit hard. Being a software engineer was much harder than I thought. Looking back, it makes sense—in high school, the most complex thing I had to build was a calculator. But in college, I was learning things like Dijkstra’s algorithm. My younger self thought I’d spend my life making calculators in C++. Little did I know, there was a whole world of complexity waiting for me.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Imposter Syndrome and the (First)Turning Point&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;At that time, I met one of my best friends—Imposter Syndrome. This little f*&lt;em&gt;er&lt;/em&gt; made me (and sometimes still does) think that coding wasn’t my thing, that I wasn’t smart enough, and that I didn’t work hard enough to pass my classes. It got so bad that I started thinking about leaving college altogether and pursuing something else, like gastronomy. I began skipping classes, missing exams, and as a result, I failed some subjects—like data structures.&lt;/p&gt;

&lt;p&gt;That period was awful. I cried a lot. I was scared of the future and felt like I was stuck in a never-ending loop of failure. But one day, something changed. I decided to take matters into my own hands. I went to the library, picked up a book on data structures and algorithms in C++, and started reading and practicing on my own. Slowly but surely, something clicked in my head. I began to understand the concepts that had once seemed impossible. My grades improved, I passed the subjects I had previously failed, and for the first time, I started to see the light at the end of the tunnel.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Second Turning Point&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;After that awful period, I went through another one—lol—but this time, it was lighter. The main problem was that I had no idea how to use my super strong, almighty, powerful programming skills in real life. All I knew was C++, so once again, the idea of becoming a chef started creeping back into my mind.&lt;/p&gt;

&lt;p&gt;After a few (and by "a few," I mean a lot) of days researching, I came across a Facebook ad for a bootcamp called DevF. It focused on web development, and at first, it seemed amazing—until I saw the price. So, I took the subjects from their syllabus and started learning on my own.&lt;/p&gt;

&lt;p&gt;At the beginning, it was all a mess. Concepts like Node.js, CSS, JS, HTML, MongoDB, MySQL, middleware, JWT, Express, deploy, branches—a whole lot of WTF to be honest. I was overwhelmed. I didn’t know where to start, which path to take, or how to put everything together.&lt;/p&gt;

&lt;p&gt;One day, while searching for courses on Udemy, I found Juan Pablo De la Torre Valdez. This guy saved my life (not literally, but you get it). The courses I bought from him gave me much-needed clarity. I started building a simple page with HTML, CSS, and JS, and suddenly, frontend development started making sense. I began to understand the technologies, how they connected, and what role each played.&lt;/p&gt;

&lt;p&gt;But there was still a missing piece—the backend. I knew absolutely nothing about it. So, I made a bold decision: I enrolled in the DevF bootcamp. To afford it, I sold my one-week-old PS4 (painful, but worth it).&lt;/p&gt;

&lt;p&gt;The bootcamp gave me the final clarity I needed. I finally understood how frontend and backend worked together. And at that moment, I knew—software engineering was what I wanted to do for the rest of my life.&lt;/p&gt;

&lt;p&gt;A few months (and a lot of learning) later, I made another big decision: I dropped out of college to focus on my first programming jobs. Spoiler alert: they were absolute trash. But that's a story for another post.&lt;/p&gt;

&lt;h2&gt;
  
  
  Advice for Others
&lt;/h2&gt;

&lt;p&gt;I have some advice for anyone starting this amazing career. These tips come from my own experience—they worked for me and still do:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Repeat, repeat, repeat, repeat...&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I can’t even count how many to-do lists I built. One with MongoDB, one with MySQL, one with Python, one with Node.js, one with Vue, one with React, one with PHP, one with Laravel… you get the idea. I made tons of login pages, CRUDs, and React/Vue/Angular components.&lt;/p&gt;

&lt;p&gt;The key? Practice. A lot.&lt;br&gt;
Don’t think that just because you did something once, you’re an expert. There’s always room to learn and improve.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Don’t be afraid to ask&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Back in college, I was struggling with data structures, and one guy approached me and helped me with my homework. He showed me what I was doing wrong and saved me a ton of time.&lt;/p&gt;

&lt;p&gt;People always have different perspectives—take advantage of that. Asking for help is not a weakness, it’s a shortcut to learning.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Have someone to talk to&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I’ll never forget the people who supported me during my toughest moments. Thanks to them, I became the professional I am today. Having people who understand your struggles and can offer guidance makes a huge difference.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Have a Plan B&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I dropped out of college, but only after I had a job lined up. If you’re going to take a big risk, make sure you have a backup plan.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Never stop learning&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You don’t know what you don’t know. Keep exploring, keep building, and keep improving. The tech world moves fast—stay curious and stay hungry.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Have fun&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Life is a balance.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>programming</category>
      <category>devjournal</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
