<?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: patrick0806</title>
    <description>The latest articles on DEV Community by patrick0806 (@patrick0806).</description>
    <link>https://dev.to/patrick0806</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%2F754616%2Fa53c75b0-634d-4796-998e-a03118540893.jpeg</url>
      <title>DEV Community: patrick0806</title>
      <link>https://dev.to/patrick0806</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/patrick0806"/>
    <language>en</language>
    <item>
      <title>Why You Should Never Use a Random UUID as an Idempotency Key in Payment APIs</title>
      <dc:creator>patrick0806</dc:creator>
      <pubDate>Fri, 27 Jun 2025 14:33:32 +0000</pubDate>
      <link>https://dev.to/patrick0806/why-you-should-never-use-a-random-uuid-as-an-idempotency-key-in-payment-apis-3jei</link>
      <guid>https://dev.to/patrick0806/why-you-should-never-use-a-random-uuid-as-an-idempotency-key-in-payment-apis-3jei</guid>
      <description>&lt;p&gt;Imagine you have a saas and your user clicks "Pay", but the network times out, so they hit the button again.&lt;/p&gt;

&lt;p&gt;Your backend processes the same request &lt;strong&gt;twice&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;💥 Result? Two charges. Angry user. Support ticket. Refund dance.&lt;/p&gt;

&lt;p&gt;Recently, I started building a personal project using a &lt;strong&gt;monolithic architecture&lt;/strong&gt; and integrated &lt;strong&gt;Stripe&lt;/strong&gt; for billing. As expected, YouTube began recommending all sorts of payment integration videos.&lt;/p&gt;

&lt;p&gt;One of them caught my attention — and nearly made me choke on my dinner.&lt;/p&gt;

&lt;p&gt;In the video, the creator was integrating with &lt;strong&gt;Mercado Pago&lt;/strong&gt; and explained the use of the &lt;code&gt;X-Idempotency-Key&lt;/code&gt; header by saying:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"Just use &lt;code&gt;randomUUID()&lt;/code&gt; from Node here. It just needs to be unique."&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Seems harmless, right? But that one line can &lt;strong&gt;break the whole concept of idempotency&lt;/strong&gt; especially in payment systems.&lt;/p&gt;




&lt;h2&gt;
  
  
  🧠 What Is Idempotency, Really?
&lt;/h2&gt;

&lt;p&gt;Idempotency ensures that &lt;strong&gt;multiple identical requests&lt;/strong&gt; only have &lt;strong&gt;one effect&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If the first request goes through and the second one is retried — whether due to a flaky network, user impatience, or frontend retry logic — the backend &lt;strong&gt;should return the same result&lt;/strong&gt;, not perform the operation again.&lt;/p&gt;

&lt;h3&gt;
  
  
  ✅ Example:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;User tries to subscribe to your service.&lt;/li&gt;
&lt;li&gt;You send a request to Mercado Pago → an invoice is created.&lt;/li&gt;
&lt;li&gt;User refreshes the page and submits again.&lt;/li&gt;
&lt;li&gt;If the request includes the &lt;strong&gt;same idempotency key&lt;/strong&gt;, the same invoice is returned — &lt;strong&gt;no duplicate charge&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That’s the power of headers like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;X-Idempotency-Key: invoice-1234-user-5678
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  ❌ The Problem with &lt;code&gt;UUID.randomUUID()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;If you generate a new UUID for &lt;strong&gt;each retry&lt;/strong&gt;, the server can't detect that it's the same operation.&lt;br&gt;&lt;br&gt;
Each request looks &lt;strong&gt;completely unique&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So instead of being safe and idempotent, you get:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;❌ First request → Charge created
&lt;/li&gt;
&lt;li&gt;❌ Second request → Another charge created
&lt;/li&gt;
&lt;li&gt;💣 Third request → Yep... you get the idea&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  ✅ What You Should Do Instead
&lt;/h3&gt;

&lt;p&gt;An &lt;strong&gt;idempotency key must be deterministic&lt;/strong&gt;:&lt;br&gt;&lt;br&gt;
the &lt;strong&gt;same operation&lt;/strong&gt; should &lt;strong&gt;always produce the same key&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  You can use:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;A combination of &lt;code&gt;userId + invoiceId&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;traceId&lt;/code&gt; or &lt;code&gt;sessionId&lt;/code&gt; from the frontend&lt;/li&gt;
&lt;li&gt;A fingerprint of the payload (e.g. hash of the JSON body)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Examples:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const key = "invoice-" + userId + "-" + invoiceId;

or something like

const key = sha256(JSON.stringify({
  userId: "1234",
  plan: "pro",
  price: "49.90"
}));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This ensures &lt;strong&gt;idempotency across retries&lt;/strong&gt;, across network hops, even across frontend reloads.&lt;/p&gt;




&lt;h3&gt;
  
  
  🛡️ Backend Handling
&lt;/h3&gt;

&lt;p&gt;On the server side, the logic is straightforward:&lt;/p&gt;

&lt;p&gt;Receive the X-Idempotency-Key header&lt;/p&gt;

&lt;p&gt;Check if that key already exists in storage&lt;/p&gt;

&lt;p&gt;If yes → return the saved response&lt;/p&gt;

&lt;p&gt;If no → process, store the response + key&lt;/p&gt;

&lt;p&gt;Storage options:&lt;br&gt;
Relational DB with unique constraint on the key&lt;/p&gt;

&lt;p&gt;Redis with TTL&lt;/p&gt;

&lt;p&gt;Distributed cache or NoSQL with atomic writes&lt;/p&gt;

&lt;p&gt;Bonus tip: log the idempotency key with your trace logs to simplify debugging.&lt;/p&gt;

&lt;h3&gt;
  
  
  💡 Real-World Examples: Stripe &amp;amp; Mercado Pago
&lt;/h3&gt;

&lt;p&gt;Both Stripe and Mercado Pago rely heavily on idempotency keys to avoid duplicate charges.&lt;/p&gt;

&lt;p&gt;Their APIs require that you set this key once per operation, and they cache the result server-side.&lt;/p&gt;

&lt;p&gt;If you change the key on each retry — like using a UUID.randomUUID() — you lose all protection.&lt;/p&gt;

&lt;h3&gt;
  
  
  🧵 Pro Tip: Where to Generate the Key?
&lt;/h3&gt;

&lt;p&gt;Ideally, the client(frontend, api caller) should generate the key once per operation and persist it until it's confirmed successful.&lt;/p&gt;

&lt;p&gt;If you use traceId or a sessionId, make sure it persists across retries (not regenerated).&lt;/p&gt;

&lt;p&gt;On mobile apps, you can store it in local storage or memory.&lt;/p&gt;

&lt;p&gt;The important thing is: same intent → same key.&lt;/p&gt;

&lt;h3&gt;
  
  
  📌 Final Thoughts
&lt;/h3&gt;

&lt;p&gt;Idempotency isn't about "not crashing on retry" —&lt;br&gt;
it's about not duplicating irreversible effects.&lt;/p&gt;

&lt;p&gt;Random UUIDs are great for correlation IDs or primary keys —&lt;br&gt;
but not for identifying business operations.&lt;/p&gt;

&lt;p&gt;So next time you're building a billing integration or an API that creates side effects, ask yourself:&lt;/p&gt;

&lt;p&gt;“What happens if this request is sent twice?”&lt;/p&gt;

&lt;p&gt;And remember:&lt;/p&gt;

&lt;p&gt;🧨 Random UUIDs ≠ Idempotency&lt;/p&gt;

&lt;p&gt;💬 Have you ever seen this bug happen in production?&lt;/p&gt;

&lt;p&gt;Or used UUID.randomUUID() without thinking twice?&lt;/p&gt;

&lt;p&gt;Drop a comment below — let’s share some war stories 😅&lt;/p&gt;

</description>
      <category>backend</category>
      <category>stripe</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Do you know the difference between Kafka and RabbitMQ... or did you just put it on your resume?</title>
      <dc:creator>patrick0806</dc:creator>
      <pubDate>Wed, 18 Jun 2025 14:29:09 +0000</pubDate>
      <link>https://dev.to/patrick0806/do-you-know-the-difference-between-kafka-and-rabbitmq-or-did-you-just-put-it-on-your-resume-2bp</link>
      <guid>https://dev.to/patrick0806/do-you-know-the-difference-between-kafka-and-rabbitmq-or-did-you-just-put-it-on-your-resume-2bp</guid>
      <description>&lt;p&gt;Since last year, one of my responsibilities has been to interview those who apply for positions at the company they work for. What often happens is the following: many do not have resumes with tools like Kafka and RabbitMQ — which are present in most modern systems — but when I ask the classic question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"What is the difference between these messaging services? And why did you use both?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The answer almost always comes out as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"I don't know, I just used them."&lt;/li&gt;
&lt;li&gt;"They're all queues, right?"&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;💣 Spoiler: they're not.&lt;/p&gt;

&lt;p&gt;If you really want to understand how these tools work and when to use each one, this article is for you. Let's talk about:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The difference between a queue and a stream&lt;/li&gt;
&lt;li&gt;Why RabbitMQ is push-based and Kafka is pull-based&lt;/li&gt;
&lt;li&gt;When to use each approach&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What is a queue? (RabbitMQ):
&lt;/h2&gt;

&lt;p&gt;A queue is a FIFO (First In, First Out) data structure. When we talk about messages, the flow looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Producer ➡️ Queue ➡️ Consumer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or, in more detail:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[1] The producer sends a message to the queue
[2] The broker delivers that message to a consumer
[3] The message is removed from the queue after consumption
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;If processing fails, the message can be resent — usually through a dead-letter queue.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Guaranteed processing order (generally)&lt;/li&gt;
&lt;li&gt;Single consumer per message&lt;/li&gt;
&lt;li&gt;Transient storage&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Examples of queue-based brokers:&lt;br&gt;
RabbitMQ&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Amazon SQS&lt;/li&gt;
&lt;li&gt;Redis Streams (FIFO mode)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  What is a stream? (Kafka)
&lt;/h2&gt;

&lt;p&gt;A stream is an ordered, immutable record of events. Unlike a queue, messages are not discarded after consumption. Instead, the consumer reads the message from a given offset, and this log can be suspended for days or weeks (or indefinitely).&lt;/p&gt;

&lt;p&gt;Typical flow:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Producer ➡️ Broker (log) ⬅️ Consumer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The consumer asks:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Is there anything new for me after offset 128?”&lt;br&gt;
And then he reads the message directly.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Immutability: messages are not deleted immediately&lt;/li&gt;
&lt;li&gt;Configurable retention (e.g. 7 days by default in Kafka)&lt;/li&gt;
&lt;li&gt;Multiple consumers can read the same message independently&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Examples beyond Kafka:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Amazon Kinesis&lt;/li&gt;
&lt;li&gt;Apache Pulsar&lt;/li&gt;
&lt;li&gt;Redpanda&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Push vs Pull: Who controls the pace?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;RabbitMQ(push)&lt;br&gt;
The broker pushes messages to consumers as soon as they are available.&lt;br&gt;
✅ Lower latency&lt;br&gt;
⚠️ Risk of overload if the consumer is not contagious&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Kafka (pull)&lt;br&gt;
The consumer pulls messages according to its own pace and offset.&lt;br&gt;
✅ Easier reprocessing&lt;br&gt;
⚠️ Require more control and logic on the consumer side&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  When to use each? RabbitMQ:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Guaranteed and immediate delivery&lt;/li&gt;
&lt;li&gt;&lt;p&gt;TTL or disposable message after consumption&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Not ideal for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reprocessing messages&lt;/li&gt;
&lt;li&gt;Multiple consumers reading the same data&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  When to use each? RabbitMQ:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Storing and reprocessing events&lt;/li&gt;
&lt;li&gt;Multiple concurrent consumers&lt;/li&gt;
&lt;li&gt;Processing large volumes with horizontal scalability&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Implementing event sourcing or stream processing&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Not ideal when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each message must be processed only once and summarized&lt;/li&gt;
&lt;li&gt;You need advanced routing logic on the broker&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Kafka ≠ Queue. This is the trick:&lt;br&gt;
Kafka is not a queue.&lt;br&gt;
Kafka is a distributed log with read control on the consumer.&lt;br&gt;
RabbitMQ is a traditional message broker, based on delivery and removal.&lt;/p&gt;

&lt;p&gt;🧠 Conclusion&lt;br&gt;
Kafka and RabbitMQ solve different problems.&lt;br&gt;
Using both is not wrong. Using them without understanding why is.&lt;br&gt;
Kafka is about streaming, retention, performance and multiple consumers&lt;br&gt;
RabbitMQ is about control, reliability and simplicity in routing&lt;br&gt;
It is not enough to know how to use it.&lt;br&gt;
Understand why.&lt;br&gt;
📌 Messaging is an architecture, not a library.&lt;/p&gt;

&lt;p&gt;💬 What about you?&lt;br&gt;
Have you ever fallen into the trap of treating Kafka as a queue?&lt;br&gt;
Have you ever had a project that used both... and didn't need either?&lt;br&gt;
Tell us what you think, and share it with that friend who swears that Kafka is the best.&lt;/p&gt;

</description>
      <category>kafka</category>
      <category>eventdriven</category>
      <category>architecture</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Monoliths aren't dead. You just forgot how they work.</title>
      <dc:creator>patrick0806</dc:creator>
      <pubDate>Fri, 13 Jun 2025 14:03:17 +0000</pubDate>
      <link>https://dev.to/patrick0806/simplification-is-an-underrated-skill-56fk</link>
      <guid>https://dev.to/patrick0806/simplification-is-an-underrated-skill-56fk</guid>
      <description>&lt;p&gt;Over the past few years, I've worked with robust and complex architectures: microservices, Kubernetes, message queues, millions of requests per day. Naturally, when I started a new personal project, my mind went straight to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Separate API and frontend&lt;/li&gt;
&lt;li&gt;Different deployments&lt;/li&gt;
&lt;li&gt;Observability, alerts, scalability from day 0&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But is this the right path for an MVP?&lt;/p&gt;

&lt;p&gt;The answer I found was: &lt;strong&gt;not necessarily&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  A new experiment: less complexity, more delivery
&lt;/h2&gt;

&lt;p&gt;For my new SaaS, I decided to try the opposite:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;strong&gt;Next.js&lt;/strong&gt; not only as a frontend, but taking advantage of its full stack capabilities&lt;/li&gt;
&lt;li&gt;I centralized the logic in Server Actions&lt;/li&gt;
&lt;li&gt;For checkout, I adopted &lt;strong&gt;Stripe Checkout&lt;/strong&gt; out of the box, without reinventing the wheel&lt;/li&gt;
&lt;li&gt;No queues, workers or distributed services — everything in a single deployment&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Benefits I noticed
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;🚀 Fast delivery: less configuration, more code that matters&lt;/li&gt;
&lt;li&gt;🧩 Simple maintenance: everything in one place&lt;/li&gt;
&lt;li&gt;💡 Focus on the product, not on the infrastructure&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And most importantly: &lt;strong&gt;I moved forward&lt;/strong&gt;. I took the idea off the drawing board and started testing it with real users.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;If scaling becomes a problem in the future, great — that's the kind of problem you want to have.&lt;/p&gt;

&lt;p&gt;But until then, think twice before applying complex solutions to simple problems. Sophistication can wait. Delivery can’t.&lt;/p&gt;




&lt;p&gt;Have you also experienced this when building your own projects? How did you find the balance between robustness and simplicity?&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>productivity</category>
      <category>discuss</category>
      <category>career</category>
    </item>
    <item>
      <title>Big O Notation: A Simple Guide</title>
      <dc:creator>patrick0806</dc:creator>
      <pubDate>Tue, 29 Oct 2024 14:12:17 +0000</pubDate>
      <link>https://dev.to/patrick0806/big-o-notation-a-simple-guide-543j</link>
      <guid>https://dev.to/patrick0806/big-o-notation-a-simple-guide-543j</guid>
      <description>&lt;p&gt;Big O Notation is a mathematical concept used to describe the performance or complexity of an algorithm in terms of time and space as the input size grows. It helps us understand how the runtime of an algorithm increases with larger inputs, allowing for a more standardized comparison of different algorithms.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Use Big O Notation?
&lt;/h2&gt;

&lt;p&gt;When comparing algorithms, relying solely on execution time can be misleading. For example, one algorithm might process a massive dataset in one hour, while another takes four hours. However, the execution time can vary based on the machine and other running processes. Instead, we use Big O Notation to focus on the number of operations performed, which provides a more consistent measure of efficiency.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example: Summing Numbers
&lt;/h3&gt;

&lt;p&gt;Let’s explore two ways to calculate the sum of all numbers from 1 to n:&lt;/p&gt;

&lt;h3&gt;
  
  
  Option 1: Using a Loop
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;addUpTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;total&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;
  
  
  Option 2: Using a Formula
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;addUpTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&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;
  
  
  Analyzing the Complexity
&lt;/h3&gt;

&lt;p&gt;In Option 1, if n is 100, the loop runs 100 times. In contrast, Option 2 always executes a fixed number of operations (multiplication, addition, and division). Thus:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Option 1&lt;/strong&gt; is O(n): The time complexity grows linearly with n.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Option 2&lt;/strong&gt; is O(1): The time complexity remains constant, regardless of the input size.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Disclaimer&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;While Option 2 involves three operations (multiplication, addition, division), we focus on the general trend in Big O analysis. Thus, instead of expressing it as O(3n), we simplify it to O(n). Similarly, O(n+10) simplifies to O(n), and O(n^2 + 5n + 8) simplifies to O(n^2). In Big O Notation, we consider the worst-case scenario, where the highest-order term has the greatest impact on performance.&lt;/p&gt;

&lt;p&gt;There are other forms of notation beyond the common complexities listed above, such as logarithmic time complexity expressed as O(log n).&lt;/p&gt;

&lt;h2&gt;
  
  
  What Is Big O Notation?
&lt;/h2&gt;

&lt;p&gt;Big O Notation allows us to formalize the growth of an algorithm’s runtime based on input size. Rather than focusing on specific operation counts, we categorize algorithms into broader classes including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Constant Time&lt;/strong&gt;: O(1) - The algorithm's performance does not change with the input size.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Linear Time&lt;/strong&gt;: O(n) - The performance grows linearly with the input size.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Quadratic Time&lt;/strong&gt;: O(n^2) - The performance grows quadratically as the input size increases.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example of O(n^2)
&lt;/h3&gt;

&lt;p&gt;Consider the following function, which prints all pairs of numbers from 0 to n:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;printAllPairs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&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="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;j&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="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, the function has two nested loops, so when nnn increases, the number of operations increases quadratically. For n= 2, there are 4 operations, and for n=3, there are 9 operations, leading to O(n^2).&lt;/p&gt;

&lt;h3&gt;
  
  
  Another Example: Count Up and Down
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;countUpAndDown&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Going up!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&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="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;At the top!&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;Going down...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;)&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="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;);&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Back down. Bye!&lt;/span&gt;&lt;span class="dl"&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;p&gt;At first glance, one might think this is O(n^2) because it contains two loops. However, both loops run independently and scale linearly with n. Thus, the overall time complexity is O(n).&lt;/p&gt;

&lt;h3&gt;
  
  
  Simplifying the Analysis
&lt;/h3&gt;

&lt;p&gt;Analyzing every aspect of code complexity can be complex, but some general rules can simplify things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Arithmetic operations are considered constant time.&lt;/li&gt;
&lt;li&gt;Variable assignments are constant time.&lt;/li&gt;
&lt;li&gt;Accessing elements in an array (by index) or object (by key) is constant time.&lt;/li&gt;
&lt;li&gt;For a loop, the complexity is the length of the loop multiplied by the complexity of what happens inside the loop.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Space Complexity
&lt;/h3&gt;

&lt;p&gt;While we've focused on time complexity, it's also possible to calculate space (memory) complexity using Big O. Some people include input size in their calculations, but it’s often more useful to focus solely on the space required by the algorithm itself.&lt;/p&gt;

&lt;h3&gt;
  
  
  Rules for Space Complexity (based on JavaScript):
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Most primitive values (booleans, numbers, etc.) are constant space.&lt;/li&gt;
&lt;li&gt;Strings require O(n) space (where n is the string length).&lt;/li&gt;
&lt;li&gt;Reference types (arrays, objects) are generally O(n), where n is the length of the array or the number of keys in the object.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;An Example&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;total&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;p&gt;In this function, the space complexity is O(1) because we use a constant amount of space (two variables) regardless of the input size.&lt;/p&gt;

&lt;p&gt;For a function that creates a new array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;double&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;newArr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
    &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;lenght&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nx"&gt;newArr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;newArr&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;p&gt;Here, the space complexity is O(n) because we allocate space for a new array that grows with the size of the input array.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Big O Notation provides a framework for analyzing the efficiency of algorithms in a way that is independent of hardware and specific implementation details. Understanding these concepts is crucial for developing efficient code, especially as data sizes grow. By focusing on how performance scales, developers can make informed choices about which algorithms to use in their applications.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Choosing the Best Messaging System: Practical Guide</title>
      <dc:creator>patrick0806</dc:creator>
      <pubDate>Mon, 27 May 2024 18:16:34 +0000</pubDate>
      <link>https://dev.to/patrick0806/choosing-the-best-messaging-system-practical-guide-286m</link>
      <guid>https://dev.to/patrick0806/choosing-the-best-messaging-system-practical-guide-286m</guid>
      <description>&lt;p&gt;When it comes to communication between microservices, choosing the right messaging system is crucial. These systems guarantee the independence of services by storing events in a broker to be consumed when the services are available. However, with a variety of options on the market, such as Kafka, RabbitMQ and SQS, how do you decide which one to use? In this article, we'll explore the basics of queuing and the key differences between some of the major players.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Message Ordering&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;It is important to understand how messages are ordered in queuing systems. The most common methods are FIFO (First In, First Out) and LIFO (Last In, First Out). Additionally, some brokers offer priority queues, allowing important messages to be consumed first, regardless of the default order.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Types of Queues: Pull vs. Push&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Another crucial aspect is the type of queue: Pull or Push. In a push queue, messages are automatically sent to consumers regardless of their status. This can lead to problems if consumers cannot process the volume of incoming messages. On the other hand, in a pull queue, consumers request new messages from the broker, ensuring that they can process them correctly, although this may be slower.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foqeyjnt4reszbcvqqct3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foqeyjnt4reszbcvqqct3.png" alt="Image description" width="731" height="126"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Few5x9esqjl291v4v8ny1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Few5x9esqjl291v4v8ny1.png" alt="Image description" width="731" height="127"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Difference between Messaging and Stream&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Messaging enables asynchronous and reliable communication between systems, useful when message order is not critical and data does not need to be delivered in real time. On the other hand, streaming services deal with continuous streams of data in real time, maintaining the order of records. This is ideal for processing large volumes of data in real time where order is crucial.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Amazon SNS (simple notification service)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Advantages:&lt;/strong&gt; Self-managed service that supports sending messages to multiple destinations, integrated with AWS services, facilitating the construction of cloud-based applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Disadvantages:&lt;/strong&gt; May be more expensive for applications with high message load and may not offer granular control over message consistency.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Amazon SQS (simple queue service)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Advantages:&lt;/strong&gt; Managed and fault-tolerant queuing service, with automatic replication of messages between clusters.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Disadvantages:&lt;/strong&gt; Message size limit and possibility of receiving duplicate messages due to replication.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Redis&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Advantages:&lt;/strong&gt; Simple configuration and quick access to data due to its memory storage.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Disadvantages:&lt;/strong&gt; Lack of guarantees on message delivery order and latency for larger messages.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;RabbitMQ&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Advantages:&lt;/strong&gt; Granular control over message consistency, support for multiple protocols and messaging models such as pub/sub and FIFO ordering.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Disadvantages:&lt;/strong&gt; Complex configuration for scalability beyond a high rate of requests per second.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Apache Kafka&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Advantages:&lt;/strong&gt; Highly scalable, fault tolerant and suitable for handling large volumes of data in real time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Disadvantages:&lt;/strong&gt; Complexity in configuration and lack of integrated tools for management and monitoring.&lt;/p&gt;

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

&lt;p&gt;Choosing the right messaging system depends on the specific needs of your project. Consider factors such as scalability, consistency, cost, and configuration complexity when making your decision. Carefully evaluate each option and choose the one that best meets your system requirements.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>messaging</category>
      <category>microservices</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Understanding Providers and Dependency Injection in NestJS</title>
      <dc:creator>patrick0806</dc:creator>
      <pubDate>Thu, 30 Mar 2023 18:53:54 +0000</pubDate>
      <link>https://dev.to/patrick0806/understanding-providers-and-dependency-injection-in-nestjs-248e</link>
      <guid>https://dev.to/patrick0806/understanding-providers-and-dependency-injection-in-nestjs-248e</guid>
      <description>&lt;p&gt;NestJS is a Node.js framework that allows us to build scalable and well-structured web applications using service-oriented architecture approach. One of the core features of NestJS is dependency injection, which is a common design pattern in object-oriented applications that allows you to create and manage instances of objects within an application environment.&lt;/p&gt;

&lt;p&gt;To use dependency injection in NestJS, it is necessary to create and configure service providers, which are responsible for managing and providing object instances to other application components. In this article, we'll explore the concepts of providers and dependency injection in NestJS, as well as show you how to create and use custom providers and share instances of providers between modules.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What are providers and why are they important in NestJS?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;A provider is an object that provides an instance of a service to other application components. In other words, a provider is responsible for creating, managing, and providing instances of objects to other application components. Providers are important in NestJS because they allow the developer to create and manage services in a modular and reusable way, making it easier to maintain code and create automated tests.&lt;/p&gt;

&lt;p&gt;Providers are created through classes that have the &lt;strong&gt;&lt;code&gt;@Injectable()&lt;/code&gt;&lt;/strong&gt; decorator and are registered in modules through the &lt;strong&gt;&lt;code&gt;providers&lt;/code&gt;&lt;/strong&gt; method. For example, suppose we want to create a service that performs user validation in our application. We can create an &lt;strong&gt;&lt;code&gt;AuthService&lt;/code&gt;&lt;/strong&gt; provider as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Injectable&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="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Injectable&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;AuthService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;validateUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Lógica de validação de usuário aqui&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&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;p&gt;The &lt;strong&gt;&lt;code&gt;AuthService&lt;/code&gt;&lt;/strong&gt; class has the &lt;strong&gt;&lt;code&gt;@Injectable()&lt;/code&gt;&lt;/strong&gt; decorator, indicating that it can be injected into other application components. In addition, the class has a &lt;strong&gt;&lt;code&gt;validateUser()&lt;/code&gt;&lt;/strong&gt; method that implements user validation logic.&lt;/p&gt;

&lt;p&gt;To use the &lt;strong&gt;&lt;code&gt;AuthService&lt;/code&gt;&lt;/strong&gt; provider in other application components, we need to register it in a module. This can be done as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Module&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="s1"&gt;@nestjs/common&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;AuthService&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="s1"&gt;./auth.service&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;providers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;AuthService&lt;/span&gt;&lt;span class="p"&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;AuthModule&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;&lt;code&gt;AuthModule&lt;/code&gt;&lt;/strong&gt; module registers the &lt;strong&gt;&lt;code&gt;AuthService&lt;/code&gt;&lt;/strong&gt; provider in the &lt;strong&gt;&lt;code&gt;providers&lt;/code&gt;&lt;/strong&gt; array, indicating that it can be injected into other application components that depend on that service.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Dependency injection in NestJS&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Dependency injection is a design pattern that allows objects to be created and managed within an application environment. In NestJS, dependency injection is used to provide instances of services to other application components.&lt;/p&gt;

&lt;p&gt;To inject a service into an application component, you must use the &lt;strong&gt;&lt;code&gt;@Inject()&lt;/code&gt;&lt;/strong&gt; annotation. For example, suppose we want to inject the &lt;strong&gt;&lt;code&gt;AuthService&lt;/code&gt;&lt;/strong&gt; provider into a controller that handles user authentication. We can do this as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Body&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="s1"&gt;@nestjs/common&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;AuthService&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="s1"&gt;./auth.service&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;auth&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;AuthController&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="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;authService&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;AuthService&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="nd"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;login&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;login&lt;/span&gt;&lt;span class="p"&gt;(@&lt;/span&gt;&lt;span class="nd"&gt;Body&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;password&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isValid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&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;authService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;validateUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;isValid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BadRequestException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Invalid credentials&lt;/span&gt;&lt;span class="dl"&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;// JWT token generation logic here&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;p&gt;In the &lt;strong&gt;&lt;code&gt;AuthController&lt;/code&gt;&lt;/strong&gt; class, we use the constructor to inject the &lt;strong&gt;&lt;code&gt;EmailService&lt;/code&gt;&lt;/strong&gt; provider, indicating that this service is necessary so that the class can send registration confirmation emails.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Sharing provider instances between modules&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;To share the same instance of a provider across multiple application modules, you need to import the module containing the provider into all modules that want to use that provider. This can be done as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Module&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="s1"&gt;@nestjs/common&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;EmailModule&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="s1"&gt;./email.module&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;AuthModule&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="s1"&gt;./auth.module&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
   &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;EmailModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;AuthModule&lt;/span&gt;&lt;span class="p"&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;AppModule&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, the module &lt;strong&gt;&lt;code&gt;AppModule&lt;/code&gt;&lt;/strong&gt; imports the modules &lt;strong&gt;&lt;code&gt;EmailModule&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;AuthModule&lt;/code&gt;&lt;/strong&gt;, which contain the providers &lt;strong&gt;&lt;code&gt;EmailService&lt;/code&gt;&lt;/strong&gt; and *&lt;em&gt;&lt;code&gt;AuthService&lt;/code&gt;&lt;/em&gt; *, respectively. That way, all instances of these providers share the same dependency injection scope.&lt;/p&gt;

&lt;p&gt;It is important to note that when sharing the same instance of a provider across multiple modules, care must be taken to avoid concurrency issues. For example, if multiple controllers modify the state of a provider simultaneously, problems with data inconsistency can occur. Therefore, it is recommended that providers be designed to be thread-safe, that is, to support concurrent access by multiple threads without generating data consistency problems.&lt;/p&gt;

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

&lt;p&gt;In summary, providers and dependency injection are important features of NestJS that allow you to create modular, flexible and scalable applications. Using providers, we can encapsulate the application's business logic in independent, reusable components, which can be transparently injected into other components. Additionally, dependency injection allows us to automatically manage dependencies between application components, reducing code complexity and making maintenance and testing easier.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>node</category>
      <category>nestjs</category>
    </item>
    <item>
      <title>What is OAuth2 ?</title>
      <dc:creator>patrick0806</dc:creator>
      <pubDate>Sat, 15 Oct 2022 12:51:52 +0000</pubDate>
      <link>https://dev.to/patrick0806/what-is-oauth2--1p3i</link>
      <guid>https://dev.to/patrick0806/what-is-oauth2--1p3i</guid>
      <description>&lt;p&gt;OAuth2 is an authorization framework that allows applications — such as Google, Facebook, and GitHub — to gain limited access to user accounts over an HTTP service. It works by delegating user authentication to the service that hosts a user account and authorizing third-party applications to access that user account. OAuth2 provides authorization flows for web and desktop applications as well as mobile devices.&lt;/p&gt;

&lt;p&gt;You must have already used a button that says &lt;em&gt;“Log in with your google account”&lt;/em&gt; when you were using some other application to avoid having to do all the registration by hand. In this case you are giving permission to this third-party application to use the resources of your application, in the example cited by Google, these applications have limited access to your information through the HTTP protocol.&lt;/p&gt;

&lt;h3&gt;
  
  
  Roles
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Resource Owner:&lt;/strong&gt; Person or entity that allows access to your data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Client:&lt;/strong&gt; It is the application that interacts with the Resource Owner, such as the browser, speaking in the case of a web application.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Resource Serve:&lt;/strong&gt; The API that is exposed on the internet and needs data protection. To gain access to information, a token is required, which is issued by the authorization server.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Authorization Server:&lt;/strong&gt; Responsible for authenticating the user and issuing access tokens, it is the one who has the resource owner information (the user), authenticates and interacts with the user after the client is identified.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How works ?
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--m_rIh0FW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/azgue6k67nmm90x8u3a3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--m_rIh0FW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/azgue6k67nmm90x8u3a3.png" alt="OAuth2 abstract flow" width="838" height="537"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the image above we can see how the authorization flow usually works:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Authorization Request:&lt;/strong&gt; In this part, the client (application) requests authorization to access the user's server resources.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Authorization Grant:&lt;/strong&gt; If the user authorizes the application, he receives an authorization grant.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Authorization Granting:&lt;/strong&gt; The client requests an access token from the authorization server (API) by authenticating its own identity and granting authorization.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Access token:&lt;/strong&gt; If the application's identity is authenticated and the authorization grant is valid, the authorization server issues an access token to the application. The client will already have an access token to manage and the authorization at this stage is already complete.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Access Token:&lt;/strong&gt;  When the client needs to request a resource from the resource server, just present the access token.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Protected resource:&lt;/strong&gt; The resource server provides the resource to the client if its access token is valid.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The authorization server is responsible for SSO (Single Sign On), which centralizes user access credentials and authenticates, manages user permissions and issues access tokens.&lt;/p&gt;

&lt;p&gt;The client (application that runs on the user's machine) is defined through two types of application:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Confidential:&lt;/strong&gt; clients who are able to maintain the confidentiality of their credentials.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Public :&lt;/strong&gt; who are unable to maintain the confidentiality of their credentials&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Continuing with the authorization flows, there are 4 ways to work with this integration:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Implicit -&lt;/strong&gt; is a simplified authorization flow, optimized for web clients. When issuing an access token, the authorization server does not authenticate the client. It is widely used in SPAs and MVC applications.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Authorization Code -&lt;/strong&gt; is achieved by using an authorization server as an intermediary between the client and the user. The client redirects the user to an authorization server. This type of client is used in third-party applications, that is, untrusted.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Resource Owner Password Credentials -&lt;/strong&gt; used when the customer requests the username and password directly, which is already used in so-called trusted applications, such as the company's own applications.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Client Credentials -&lt;/strong&gt; can be used when the client application is protected, which are used in system integrations.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>security</category>
      <category>webdev</category>
    </item>
    <item>
      <title>What is Apache Kafka</title>
      <dc:creator>patrick0806</dc:creator>
      <pubDate>Sun, 25 Sep 2022 14:21:07 +0000</pubDate>
      <link>https://dev.to/patrick0806/what-is-apache-kafka-2327</link>
      <guid>https://dev.to/patrick0806/what-is-apache-kafka-2327</guid>
      <description>&lt;p&gt;Recently I had to mess with Kafka in my work, it's something I've never done and I had some difficulty understanding, so I'm thinking of bringing a series of posts about it.&lt;br&gt;
In this first post I want to give a little introduction about what Kafka is and how it works&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Kafka?
&lt;/h2&gt;

&lt;p&gt;Apache Kafka is an open-source, high-performance, real-time messaging platform. It stands out for being extremely fast and scalable, it is widely used with microservices.&lt;/p&gt;

&lt;p&gt;After this more technical description let's go to what it actually does, in today's world we have systems communicating with each other all the time, but this may not always be the best option.&lt;/p&gt;

&lt;p&gt;Let's imagine that I have a System A and a System B, if system A sends a message and system B is down, this message will be lost, so that doesn't happen, we put an intermediary for this communication that stores and sends these messages when system B comes back.&lt;/p&gt;

&lt;h2&gt;
  
  
  Important terms
&lt;/h2&gt;

&lt;p&gt;When we talk about Kafka we have some terms that we use, and without understanding them it would be difficult to understand how it works, so here comes a list explaining these terms:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Broker&lt;/strong&gt;: in computing, a broker is the server for sending messages, it is the one who triggers and receives the messages and it is he who owns the topics and partitions&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Topics&lt;/strong&gt;: a topic is an identifier, Through this identifier it is possible to connect with the broker and send or receive messages. (For me this guy is basically like the “database table” is where our data is)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Microservices&lt;/strong&gt;: Microservices are a software architecture model that aims to separate a system into several smaller systems to improve performance and availability.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Partition&lt;/strong&gt;: Subdivision of a topic, partition is a resource for load balancing&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cluster&lt;/strong&gt;: In short, a cluster is a set of brokers and it contains the servers and the main instance of Kafka.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How does Kafka work ?
&lt;/h2&gt;

&lt;p&gt;Kafka was written in Java and Scala, it performs data transmission asynchronously. In its structure, Kafka has consumers and producers.&lt;/p&gt;

&lt;p&gt;Producers are those who send and distribute the data, while consumers are those who subscribe to a topic and listen to the messages that will arrive.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vl7OOKwZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jizjynlr3si5la88vral.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vl7OOKwZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jizjynlr3si5la88vral.png" alt="Kafka Architecture" width="880" height="487"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Kafka Architecture described above allows for consistency in sending messages. Brokers are designed to withstand outages and failures, when a broker goes down and stays out for some reason, a backup broker takes over the activities and maintains delivery and availability without losing any information, the same thing goes for topics that have multiple partitions that are replicas of the primary. These replicas are often scattered on other machines.&lt;/p&gt;

&lt;p&gt;Another very interesting thing about Kafka is that unlike some other messaging systems, even after the consumer reads the message, it is not thrown away, so if we have a new consumer who wanted to read the data from the beginning, he can, but of course, if desired, it is possible to configure the time this data will be available before being deleted.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final considerations
&lt;/h2&gt;

&lt;p&gt;Apache Kafka is really a fantastic system for you to be able to work with messaging tools, including working with Pub/Sub, for example.&lt;/p&gt;

&lt;p&gt;Therefore, everyone reads a channel and a producer produces there on that channel, that is, in this topic everyone can read these messages.&lt;/p&gt;

&lt;p&gt;If you are thinking of some solution for you to make your system communicate securely and efficiently and you still need to process these messages Apache Kafka is a great solution for you to consider.&lt;/p&gt;

</description>
      <category>opensource</category>
      <category>tutorial</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Clean Code where to start</title>
      <dc:creator>patrick0806</dc:creator>
      <pubDate>Fri, 08 Jul 2022 00:35:44 +0000</pubDate>
      <link>https://dev.to/patrick0806/clean-code-where-to-start-2lpm</link>
      <guid>https://dev.to/patrick0806/clean-code-where-to-start-2lpm</guid>
      <description>&lt;p&gt;Recently I started to read Clean Code, it is a well-known book in our area in which Uncle Bob tries to give us what he considers clean code, that is, a simple code to read and also to maintain.&lt;/p&gt;

&lt;p&gt;In this article I want to focus on the first three chapters of the book where he talks about variables and functions. But this seems so common, that maybe you think if just paying attention to these two points can really improve the quality of my code?&lt;/p&gt;

&lt;p&gt;The answer is yes and a lot, if we think carefully about the names of our variables and those names will be clear and will tell you what values are stored there and help in our understanding of what is going on in the code.&lt;/p&gt;

&lt;p&gt;With an easier reading and understanding of the code, it is also easier to understand where to move and what to change, thus also helping in the maintainability of the software.&lt;/p&gt;

&lt;p&gt;See an example with bad variable names:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Patrick&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;yb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2019&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ln&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Nicezi&lt;/span&gt;&lt;span class="dl"&gt;"&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;ln&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; nasceu em&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;yb&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Result Patrick Nicezi was born in 1998&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's see the same example with well-named variables:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Patrick&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;yearOfBirth&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2019&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;lastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Nicezi&lt;/span&gt;&lt;span class="dl"&gt;"&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;lastname&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; nasceu em&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;yearOfBirth&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Result Patrick Nicezi was born in 1998&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, the second one is much easier to understand because each variable says exactly what it stores and when it is used in conjunction with others it is much easier to understand what is happening. And this choice of names applies to everything you name during development like class names, functions...&lt;/p&gt;

&lt;p&gt;But not only giving a good name to the variable gives a clear naming pattern it's also important to create a naming pattern, it helps a lot the next person who reads your code to understand it. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;alterUser&lt;/span&gt;&lt;span class="p"&gt;(){...};&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;changeAddress&lt;/span&gt;&lt;span class="p"&gt;(){...};&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;modifyUserDate&lt;/span&gt;&lt;span class="p"&gt;(){....};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When reading the code above, although I know that they are all functions that change an object, I still wonder if they have the same purpose, because with the different names I feel that they are doing different things&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;updateUser&lt;/span&gt;&lt;span class="p"&gt;(){...}&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;updateAddress&lt;/span&gt;&lt;span class="p"&gt;(){...}&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;updateUserDate&lt;/span&gt;&lt;span class="p"&gt;(){...}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now in code with a naming pattern we can clearly see just by reading the function name what their purpose is and that they are similar purposes, and with this pattern it is much easier for someone who has never read a code to know what any function that starts with update does.&lt;/p&gt;

&lt;p&gt;Speaking of functions, we have some very important things to focus on when creating one, the first one a function must do only one thing and the second it must not be too big, because apart from making it difficult to read if the function is very large it probably does more than one thing.&lt;/p&gt;

&lt;p&gt;See an example of this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;returnFormatedProducts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;amountOfPassengers&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;adults&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;childrens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CRUISE&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nx"&gt;adults&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cruiser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;persons&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;adults&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;childrens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cruiser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;persons&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;cruiser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;persons&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;infants&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nx"&gt;amountOfPassengers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;adults&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;childrens&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;HOTEL&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
     &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;passengersPerRoom&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;hotel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;rooms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;room&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;childrensInRoom&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;adultsInRoom&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
         &lt;span class="nx"&gt;passengers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;passenger&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
              &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;passenger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ADULT&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nx"&gt;adults&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
              &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nx"&gt;childrens&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
              &lt;span class="p"&gt;}&lt;/span&gt;

          &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;adults&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;childrens&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;

        &lt;span class="nx"&gt;amountOfPassengers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;passengersPerRoom&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
          &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;totalPassengers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;passengersInRoom&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
              &lt;span class="na"&gt;adults&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;totalPassengers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;adults&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;passengersInRoom&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;adults&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
              &lt;span class="na"&gt;childrens&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;totalPassengers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;childrens&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;passengersInRoom&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;childrens&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="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;adults&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;childrens&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&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="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;passengers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;passenger&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;passenger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ADULT&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;adults&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;childrens&lt;/span&gt;&lt;span class="o"&gt;++&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="nx"&gt;amountOfPassengers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;adults&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;childrens&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;sellDate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sellDate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;route&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;route&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;startDate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;startDate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;endDate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;endDate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;$&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;R$ &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
      &lt;span class="na"&gt;paxChd&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;amountOfPassengers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;adults&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; / &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;amountOfPassengers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;childrens&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code becomes much clearer when we separate this large function into smaller functions and each with their own responsibility it becomes clear what the function does just by reading its name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;returnFormatedProducts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;amountOfPassengers&lt;/span&gt; &lt;span class="o"&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;getPassengersAmount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;sellDate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sellDate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;route&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;route&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;startDate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;startDate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;endDate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;endDate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;$&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;R$ &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
      &lt;span class="na"&gt;paxChd&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;amountOfPassengers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;adults&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; / &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;amountOfPassengers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;childrens&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&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="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getPassengersAmount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;CRUISE&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;getPassengersFromCruiser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;HOTEL&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;getPassengersFromHotel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;countPassengers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;passengers&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getPassengersFromCruiser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cruiser&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;adults&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cruiser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;persons&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;adults&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;childrens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cruiser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;persons&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;cruiser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;persons&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;infants&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;adults&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;childrens&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getPassengersFromHotel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;hotel&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;passengersPerRoom&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;hotel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;rooms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;room&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&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;countPassengers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;room&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;passengers&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;amountOfPassengers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;passengersPerRoom&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;totalPassengers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;passengersInRoom&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;adults&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;totalPassengers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;adults&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;passengersInRoom&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;adults&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
          &lt;span class="na"&gt;childrens&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;totalPassengers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;childrens&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;passengersInRoom&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;childrens&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="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;adults&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;childrens&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;amountOfPassengers&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;p&gt;With this little code refactoring we can read it more smoothly, when we see the returnFormatedProduct function, we can understand what it does and if we need more details on how the number of passengers are taken, we can look at the specific functions for that.&lt;/p&gt;

&lt;p&gt;And if you ever need to add a new shape or change an existing one, it won't impact the code as a whole and can be easily changed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Writing clean code is a matter of practice, training and paying attention to the small details. It's an ongoing process, don't be afraid to refactor your code and rename things if you find better names for them.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Como começar a aprender programação</title>
      <dc:creator>patrick0806</dc:creator>
      <pubDate>Fri, 24 Dec 2021 19:22:46 +0000</pubDate>
      <link>https://dev.to/patrick0806/como-comecar-a-aprender-programacao-55a1</link>
      <guid>https://dev.to/patrick0806/como-comecar-a-aprender-programacao-55a1</guid>
      <description>&lt;p&gt;Quando comecei a aprender programação me senti perdido sobre a enxurrada de conhecimento que eu tinha que absorver, e também a ordem em que eu deveria aprender as coisas.&lt;/p&gt;

&lt;p&gt;Então agora uns anos depois estou escrevendo esse artigo no intuito de tentar ajudar alguém que esteja começando a como ele pode começar a aprender e até apresentar alguns materiais ou aulas que podem ajudar. Vou me ater a conteúdos em português por questão de facilidade para quem está iniciando.&lt;/p&gt;

&lt;p&gt;Nesse artigo eu vou focar nas duas coisas que precisamos aprender para conseguir continuar os nossos estudos sem ficarmos batendo cabeça por ter pulado uma parte da base.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Logica de Programação&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A primeira coisa que precisamos para aprender programação é aprender lógica de programação, qual a utilidade dela ?&lt;/p&gt;

&lt;p&gt;Podemos descrever o computador como "burro" ele faz apenas oque é dito explicitamente para ele, a lógica de programação é a organização coesa de uma sequência de instruções voltadas à resolução de um problema, ou à criação de um software ou aplicação. Ela é a base porque é a partir dele que os aprendizados posteriores, como por exemplo o das linguagens de programação, fará sentido.&lt;/p&gt;

&lt;p&gt;Para aprender logica de programação aconselho o material do &lt;a href="https://www.youtube.com/watch?v=8mei6uVttho&amp;amp;list=PLHz_AreHm4dmSj0MHol_aoNYCSGFqvfXV&amp;amp;ab_channel=CursoemV%C3%ADdeo"&gt;Canal Curso em Vídeo&lt;/a&gt; sobre o tema.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Escolher uma linguagem de programação&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Após isso o próximo passo seria escolher uma área de estudo como Front-end, Back-end ou mobile, a partir disso escolher uma linguagem de programação e aprende-la. Mas independente da área escolhida, eu aprenderia o javascript pois ele é quase uma bala de prata é possível usa-lo em qualquer uma das áreas embora nem sempre o ideal. Com ele você pode aprender todos os conceitos que necessita.&lt;/p&gt;

&lt;p&gt;E após aprender esses conceitos trocar para outra linguagem especifica se necessário se torna muito mais simples, porque tirando a sintaxe que é como escrevemos o código em determinada linguagem os conceitos, são em grande parte reaproveitados por todas as linguagens, o curso em video também tem um otimo curso sobre &lt;a href="https://www.youtube.com/watch?v=1-w1RfGIov4&amp;amp;list=PLHz_AreHm4dlsK3Nr9GVvXCbpQyHQl1o1&amp;amp;ab_channel=CursoemV%C3%ADdeo"&gt;javascript&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Eu diria que esses são os dois pontos principais para um iniciante aprender, após eles você já deve ser capaz de ver com qual área tem mais afinidade e deseja trabalhar, e assim filtrar e pesquisar coisas mais focadas com área que deseja trabalhar.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
