<?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: Gaurang</title>
    <description>The latest articles on DEV Community by Gaurang (@gaurang847).</description>
    <link>https://dev.to/gaurang847</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%2F178012%2Ff2aae508-4a58-4fab-97ba-3cde1b71f25a.png</url>
      <title>DEV Community: Gaurang</title>
      <link>https://dev.to/gaurang847</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gaurang847"/>
    <language>en</language>
    <item>
      <title>ORMs: A Solution Looking for a Problem?</title>
      <dc:creator>Gaurang</dc:creator>
      <pubDate>Sun, 06 Jul 2025 10:13:53 +0000</pubDate>
      <link>https://dev.to/gaurang847/orms-are-annoying-until-you-try-living-without-one-14d6</link>
      <guid>https://dev.to/gaurang847/orms-are-annoying-until-you-try-living-without-one-14d6</guid>
      <description>&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;li&gt;My Initial Reaction - "These People are Mad!!"&lt;/li&gt;
&lt;li&gt;Then Came Sequelize... And Then Mongoose&lt;/li&gt;
&lt;li&gt;We Think It's Just Mapping&lt;/li&gt;
&lt;li&gt;
The Reality Without ORMs

&lt;ul&gt;
&lt;li&gt;Raw SQL Everywhere&lt;/li&gt;
&lt;li&gt;Conditional Joins Get Messy&lt;/li&gt;
&lt;li&gt;Transactions And Multi-Step Logic&lt;/li&gt;
&lt;li&gt;Input Validation And SQL Injection Risks&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

Lessons from Real Projects

&lt;ul&gt;
&lt;li&gt;Changing Column Names&lt;/li&gt;
&lt;li&gt;Environment-Specific Schema Hack&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

What I've come to realize

&lt;ul&gt;
&lt;li&gt;When you try to build your own mini-ORM&lt;/li&gt;
&lt;li&gt;The "ORM but still Raw SQL" Anti-Pattern&lt;/li&gt;
&lt;li&gt;Not Perfect, But Worth It&lt;/li&gt;
&lt;li&gt;The Real Value&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Summary&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;a id="introduction"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;When I first got into web development during my college days, the process was refreshingly straightforward. I would set up a PHP backend, execute my SQL queries using &lt;code&gt;mysqli_query&lt;/code&gt;, and that was it - no models, no layers, just raw queries and the data I needed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$conn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;mysqli_connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"localhost"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"root"&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="s2"&gt;"mydb"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;mysqli_query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"SELECT * FROM users WHERE id = 1"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then I was introduced to &lt;a href="https://hibernate.org/orm/" rel="noopener noreferrer"&gt;Hibernate ORM&lt;/a&gt; in Java. Later, &lt;a href="https://sequelize.org/" rel="noopener noreferrer"&gt;Sequelize ORM&lt;/a&gt; in Node.js. And eventually, &lt;a href="https://mongoosejs.com/" rel="noopener noreferrer"&gt;Mongoose ODM&lt;/a&gt; with MongoDB. Suddenly, everything was an object, everything had a schema, and everything required a model definition.&lt;/p&gt;

&lt;p&gt;For those unfamiliar, &lt;strong&gt;ORM&lt;/strong&gt; (Object Relational Mapping) and &lt;strong&gt;ODM&lt;/strong&gt; (Object Document Mapping) tools let you interact with your database using objects in your programming language instead of writing raw SQL or database commands. ORMs are typically used with relational databases like MySQL or PostgreSQL, while ODMs are used with document databases like MongoDB. So instead of crafting a SQL &lt;code&gt;SELECT&lt;/code&gt; statement, you call something like &lt;code&gt;User.findAll()&lt;/code&gt; or &lt;code&gt;User.save()&lt;/code&gt; in your code, and the ORM or ODM library handles the database part under the hood.&lt;/p&gt;

&lt;p&gt;&lt;a id="initial-reaction"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  My Initial Reaction: These People Are Mad!!
&lt;/h2&gt;

&lt;p&gt;I vividly remember my initial reaction while learning about Hibernate ORM during a Java crash course at my first job: &lt;em&gt;"WHHHATT?! These Java people are MAD!"&lt;/em&gt; Why does everything need to be a class, a model, or a POJO object? If I can retrieve my data with a one-line SQL query, why must I now split that across three files and define models, relationships, and repositories just to achieve the same result?&lt;/p&gt;

&lt;p&gt;It felt like some kind of object-worship cult. SQL a perfectly capable language was suddenly not allowed to sit next to Java. No, no. We must now jump through a flaming hoop of entity files and framework magic just to avoid writing SQL directly.&lt;/p&gt;

&lt;p&gt;&lt;a id="sequelize-mongoose"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Then Came Sequelize... And Then Mongoose
&lt;/h2&gt;

&lt;p&gt;When I switched to Node.js, we used &lt;a href="https://sequelize.org/" rel="noopener noreferrer"&gt;Sequelize ORM&lt;/a&gt;. Same story - more models, more boilerplate, more time spent reading docs just to figure out how to define a basic relationship between entities.&lt;/p&gt;

&lt;p&gt;Even after working with Sequelize for years, I would &lt;em&gt;still&lt;/em&gt; have to go back to their &lt;a href="https://sequelize.org/docs/v6/core-concepts/assocs/" rel="noopener noreferrer"&gt;documentation on associations&lt;/a&gt; to understand how to use &lt;code&gt;hasOne&lt;/code&gt;, &lt;code&gt;belongsTo&lt;/code&gt;, and &lt;code&gt;through&lt;/code&gt;. Every. Single. Time.&lt;/p&gt;

&lt;p&gt;Then came MongoDB and the &lt;a href="https://mongoosejs.com/" rel="noopener noreferrer"&gt;Mongoose ODM&lt;/a&gt;. And my confusion reached a whole new level. MongoDB is supposed to be &lt;em&gt;schema-less&lt;/em&gt;, right? And JavaScript isn't even a typed language. So why the heck are we defining schemas?&lt;/p&gt;

&lt;p&gt;People often say that ORMs make it easy to switch between relational databases - like swapping MySQL for PostgreSQL - because they abstract away the underlying database. Fair enough. But in the case of MongoDB, what exactly am I switching to? Sure, other document databases exist but Mongoose is tightly coupled to MongoDB, so the whole "you can switch databases" argument kind of falls apart here.&lt;/p&gt;

&lt;p&gt;And let's be real - no one's casually swapping out their database on a Tuesday afternoon. Even if you did want to, it's rarely seamless. You can't just unplug Postgres and plug in MySQL and expect things to work because &lt;em&gt;"we use an ORM"&lt;/em&gt;. If someone has actually done that flawlessly, please message me - I need to see this miracle firsthand.&lt;/p&gt;

&lt;p&gt;Out of a mix of curiosity and a bit of rebellion, I once wrote a quick script for a one-off task that just updated some data in MongoDB. I just used the native &lt;a href="https://www.npmjs.com/package/mongodb" rel="noopener noreferrer"&gt;MongoDB Node.js client&lt;/a&gt; — the same driver Mongoose uses under the hood — with no models, no schema validation, nothing. And it worked. Which left me thinking: if this gets the job done, why go through all the extra steps with Mongoose and its overhead?&lt;/p&gt;

&lt;p&gt;People also say that with an ORM, developers don't need to learn SQL - they can just stick to the programming language they already know. But here's the catch: you still have to learn how to use the ORM! And most of them have their own conventions, syntax, and rules. You didn't really avoid learning something new - you just traded SQL for something else. Sometimes, something even more confusing.&lt;/p&gt;

&lt;p&gt;In fact, I've lost count of how many times I had a perfectly valid SQL query - maybe something I got from a colleague or wrote for debugging - and then had to spend extra time figuring out how to express that same logic using the ORMs syntax. Sometimes it's straightforward, but other times it feels like translating from English to Klingon.&lt;/p&gt;

&lt;p&gt;&lt;a id="just-mapping"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  We Think It's Just Mapping
&lt;/h2&gt;

&lt;p&gt;I came across a line - maybe in a MongoDB blog or a talk (I honestly can't recall the exact source) - that really struck a chord and got me thinking:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"MongoDB is schema-less. But your data sure follows some schema."&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That line really made me pause. Just because MongoDB doesn't &lt;em&gt;enforce&lt;/em&gt; a schema doesn't mean your app doesn't &lt;em&gt;need&lt;/em&gt; one. Without some kind of structure, things quickly spiral into chaos - making the code harder to understand, maintain, and trust.&lt;/p&gt;

&lt;p&gt;And that's where tools like ORMs and ODMs come in. The phrase &lt;em&gt;Object Relational Mapping&lt;/em&gt; or &lt;em&gt;Object Document Mapping&lt;/em&gt; sounds so harmless. Just a mapping. No big deal.&lt;/p&gt;

&lt;p&gt;But we often lose context for &lt;em&gt;why&lt;/em&gt; that mapping matters.&lt;/p&gt;

&lt;p&gt;&lt;a id="reality-without-orm"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Reality Without ORMs
&lt;/h2&gt;

&lt;p&gt;Let's imagine a production-grade app built entirely without ORMs or ODMs.&lt;/p&gt;

&lt;p&gt;&lt;a id="sql-everywhere"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Raw SQL Everywhere
&lt;/h3&gt;

&lt;p&gt;Your queries end up scattered throughout the codebase - sometimes written inline alongside business logic, sometimes tucked away in helper functions. If your team is organized, maybe there's a dedicated file or folder just for SQL strings. But even then, they're just raw strings - meaning the compiler, linter, or even your IDEs IntelliSense can't catch errors, offer suggestions, or help you refactor them safely.&lt;/p&gt;

&lt;p&gt;And that's the real problem: if there's &lt;em&gt;any&lt;/em&gt; mistake in one of those strings - a wrong column name, a missing comma, &lt;code&gt;ORDER BY&lt;/code&gt; before &lt;code&gt;WHERE&lt;/code&gt; - you don't know until the query actually runs.&lt;/p&gt;

&lt;p&gt;And if the query is buried in a rarely-used API endpoint? That bug could sit unnoticed for months. Then one day, someone calls that API, and boom - SQL error in production.&lt;/p&gt;

&lt;p&gt;Now imagine someone renames a column or changes a data type in the DB. Good luck finding every instance of that column across your codebase. Miss one, and you'll find out the hard way.&lt;/p&gt;

&lt;p&gt;&lt;a id="conditional-joins"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conditional Joins Get Messy
&lt;/h3&gt;

&lt;p&gt;Let's say you're building an API to fetch employee details. If the employee is an intern, include the &lt;code&gt;university_address&lt;/code&gt; table. If they're a manager, include the &lt;code&gt;office_address&lt;/code&gt; table. Both have similar schemas, but they're still separate tables - so your joins and logic must adapt accordingly.&lt;/p&gt;

&lt;p&gt;You now have two options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Option 1:&lt;/strong&gt; Write separate SQL queries for each case. This often results in duplicated logic - like repeating the same &lt;code&gt;SELECT&lt;/code&gt;, &lt;code&gt;WHERE&lt;/code&gt;, &lt;code&gt;ORDER BY&lt;/code&gt; clauses or shared joins - across multiple queries.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Option 2:&lt;/strong&gt; Dynamically build the query with string concatenation. That reduces redundancy, but now no one knows what the final query looks like without running the code.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now imagine handling 68 optional joins like this. Yikes!&lt;/p&gt;

&lt;p&gt;&lt;a id="transactions"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Transactions and Multi-Step Logic
&lt;/h3&gt;

&lt;p&gt;Now imagine you need a transaction: create an employee, then insert into &lt;code&gt;employee_address&lt;/code&gt;, and based on role, insert into either &lt;code&gt;manager&lt;/code&gt;, &lt;code&gt;intern&lt;/code&gt;, or &lt;code&gt;director&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Without an ORM, you're manually building a query block like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;BEGIN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;employee&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;role&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'John Doe'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'manager'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;employee_address&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;employee_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;LAST_INSERT_ID&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="s1"&gt;'123 Office St'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;manager&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;employee_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;LAST_INSERT_ID&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="s1"&gt;'Engineering'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;COMMIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now imagine you have to add logic to conditionally insert into &lt;code&gt;intern&lt;/code&gt; or &lt;code&gt;director&lt;/code&gt; instead of &lt;code&gt;manager&lt;/code&gt;, handle rollbacks, and construct this dynamically in code using string concatenation - things get messy fast.&lt;/p&gt;

&lt;p&gt;In the previous section, we saw how conditional joins can complicate a single query. Now imagine chaining multiple such conditionally-executed queries into a single transaction. It becomes more difficult to read, understand or even predict what the final query will look like.&lt;/p&gt;

&lt;p&gt;&lt;a id="sanitization"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Input Validation and SQL Injection Risks&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;When you build queries by stitching strings together - especially with values coming from API requests - things can get dangerous fast. You have to be extremely cautious about validating and sanitizing every input, or you risk exposing your app to SQL injection attacks. ORMs take care of this for you by using parameter binding and escaping inputs safely by default. That means one less thing to worry about every time you write a query; and a big step toward keeping your APIs secure.&lt;/p&gt;

&lt;p&gt;&lt;a id="real-projects"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Lessons From Real Projects
&lt;/h2&gt;

&lt;p&gt;Here are two real work experiences that I've faced.&lt;/p&gt;

&lt;p&gt;&lt;a id="column-name-change"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Changing Column Names
&lt;/h3&gt;

&lt;p&gt;At work, we sometimes had to rename DB columns. With JPQL, all I had to do was update the field name in the entity class. Done. The app might throw a few syntax errors on startup, but once fixed, everything stays consistent.&lt;/p&gt;

&lt;p&gt;With native queries? Total pain. Every single query using that column had to be updated. And that could be in hundreds! Miss one, and it quietly fails later when someone happens to hit that endpoint.&lt;/p&gt;

&lt;p&gt;&lt;a id="env-schema-hack"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Environment-Specific Schema Hack
&lt;/h3&gt;

&lt;p&gt;Once, our client didn't want to set up a separate database instance for pre-prod environment. Instead, they asked us to create a different database schema in the same instance.&lt;/p&gt;

&lt;p&gt;Now, Spring Boot allows us to configure a custom strategy to set the schema name. But it only applies to the JPQL queries - the ones that are ORM-based. And since most of our queries were native, we had to create a Java file with the schema names, exclude it from Git, and manually place a different version on each environment's server. We also had to update all our queries to use the schema name from the Java file.&lt;/p&gt;

&lt;p&gt;It worked. But it was fragile, ugly, and easily avoidable if we had relied more on ORM features.&lt;/p&gt;

&lt;p&gt;&lt;a id="realization"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What I've come to realize
&lt;/h2&gt;

&lt;p&gt;I used to think that ORMs were just bloated frameworks trying to make simple things harder. But over time, I realized they're actually solving the problems you don't even know you'll have - until its too late.&lt;/p&gt;

&lt;p&gt;&lt;a id="rebuild-orm"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  🛠 When You Try to Build Your Own Mini ORM
&lt;/h3&gt;

&lt;p&gt;Let's be honest - if you're going to pull out query strings, schema names, and column names into separate files, and then write logic to stitch strings together based on conditions... aren't you basically building your own mini ORM? You're solving the exact problems that ORMs are designed to handle.&lt;/p&gt;

&lt;p&gt;Only now, its more error-prone, less generic, and tightly coupled to your project. ORMs already do this - and they do it better, more reliably, and in a way that works across projects and teams.&lt;/p&gt;

&lt;p&gt;&lt;a id="orm-but-sql"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  🚨 The ORM But Still Raw SQL Anti-Pattern
&lt;/h3&gt;

&lt;p&gt;Imagine you buy an expensive bed for your cat… and it still sleeps in a cardboard box.&lt;br&gt;
Or you visit a doctor, get prescribed medicine… and then never finish the course.&lt;br&gt;
Or you buy nice clothes, only to leave them unworn in the closet.&lt;/p&gt;

&lt;p&gt;Frustrating, right?&lt;/p&gt;

&lt;p&gt;That’s exactly what it’s like when a project includes an ORM — but developers still write most of the queries in raw SQL. You're adding the overhead, but not reaping any of the benefits.&lt;/p&gt;

&lt;p&gt;I've seen codebases where an ORM was technically in place, but nearly all queries were still written natively. In that case, you're getting the worst of both worlds - extra boilerplate from the ORM, and all the risks of raw SQL.&lt;/p&gt;

&lt;p&gt;To be clear, most ORMs do let you run raw queries — and that’s by design. It’s meant as a fallback, for when you have a complex SQL query that the ORM syntax can’t express easily. But that’s a backup plan, not the main plan. If you find yourself reaching for raw queries by default, you’re not really using the ORM — you’re just working around it.&lt;/p&gt;

&lt;p&gt;A lot of times, teams stick with raw queries because learning the ORM feels like extra work. Or they were told to use it, so they did - but without really getting why it matters. And in the end, they miss out on everything the ORM was supposed to help with.&lt;/p&gt;

&lt;p&gt;If you're using an ORM, native queries should be your last resort. The more you rely on raw queries, the more you defeat the purpose of using an ORM in the first place.&lt;/p&gt;

&lt;p&gt;As your project grows, you’ll eventually run into a moment where you think,&lt;br&gt;
“We already have the ORM. This would’ve been trivial if we just used it properly.”&lt;br&gt;
But by then, the ORM feels useless — not because it didn’t work, but because we never used it the way it was meant to be used. The tool had the power to help — and yet we couldn't tap into it when it mattered most.&lt;/p&gt;

&lt;p&gt;&lt;a id="imperfect-but-worth"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  🧩 Not Perfect, But Worth It
&lt;/h3&gt;

&lt;p&gt;Sure, they aren't perfect. Sometimes the queries these ORMs generate are messy. Sometimes they feel overkill for basic stuff. Sometimes, they get in the way. But they solve real problems. They handle transactions, conditional logic, rollbacks, query sanitization - all in your apps native language. You write clean logic. It handles the mess behind the scenes.&lt;/p&gt;

&lt;p&gt;&lt;a id="real-value"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  🧱 The Real Value
&lt;/h3&gt;

&lt;p&gt;They're not just "mappers". They're your defense against chaos that give you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;🔒 Safer queries&lt;/li&gt;
&lt;li&gt;🔧 Easier maintenance&lt;/li&gt;
&lt;li&gt;🔄 Smoother schema changes&lt;/li&gt;
&lt;li&gt;🔁 Better handling of relationships, conditional logic, and transactions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's not about being able to switch databases or not having to learn SQL. It's about building something that wont fall apart when things get complicated.&lt;/p&gt;

&lt;p&gt;&lt;a id="summary"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;ORMs and ODMs can feel like unnecessary overhead in small projects or one-off scripts - and in these cases, skipping them is perfectly fine. But as your codebase grows, raw queries introduce real risks: scattered queries, fragile string concatenation, and painful schema changes. ORMs and ODMs bring structure, safety, and consistency that scale with your application.&lt;/p&gt;

&lt;p&gt;This article isn't about always using ORMs - its about understanding why they matter.&lt;/p&gt;

&lt;p&gt;You can get things done without them - but when things get complex, you'll be glad you have them.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;P.S.&lt;/strong&gt; This turned out to be a long one - so if you made it all the way to the end, thank you! 🙌 I hope it gave you something to think about. If you have any questions or thoughts, feel free to leave a comment below. And if you found it helpful, a like or share would really mean a lot! 😊&lt;/p&gt;

</description>
      <category>java</category>
      <category>node</category>
      <category>hibernate</category>
      <category>programming</category>
    </item>
    <item>
      <title>[Node.js] How to Use Promises in a Callback-based Codebase</title>
      <dc:creator>Gaurang</dc:creator>
      <pubDate>Sun, 01 Jun 2025 05:58:21 +0000</pubDate>
      <link>https://dev.to/gaurang847/nodejs-how-to-use-promises-in-a-callback-based-codebase-1a07</link>
      <guid>https://dev.to/gaurang847/nodejs-how-to-use-promises-in-a-callback-based-codebase-1a07</guid>
      <description>&lt;p&gt;Hi! I'm Gaurang. I’ve worked extensively with Node.js in the past, and during that time, I spent a lot of effort wrangling with different async patterns — callbacks, promises, and async/await. This article is part of my series, &lt;a href="https://dev.to/gaurang847/series/22112"&gt;My guide to getting through the maze of callbacks, promises, and async-await&lt;/a&gt;, where I share lessons learned and best practices for writing asynchronous code in Node.js.&lt;/p&gt;

&lt;p&gt;This article assumes you’re familiar with basic Node.js concepts, especially asynchronous programming constructs like callbacks, promises, and async/await.&lt;/p&gt;




&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Converting a Promise-based Function to Use Callbacks&lt;/li&gt;
&lt;li&gt;Prefer Then-chaining over nesting&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Converting a Promise-based Function to Use Callbacks &lt;a&gt;
&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Often, you’ll find yourself working in older codebases where asynchronous functions are written using the callback pattern. Node.js also offers a built-in way to convert a Promise-returning function into a callback-based one using &lt;code&gt;util.callbackify()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Personally, I believe we should avoid writing new callbacks whenever possible. Even in a callback-heavy codebase, it's often worth introducing promises or async/await for better readability and maintainability.&lt;br&gt;
I’ve spent years digging through callback-infested codebases, and I’ll say it plainly: callbacks are obsolete. They had their time, but that time is over. Stop writing them. Let them die.&lt;/p&gt;

&lt;p&gt;Use this only when absolutely necessary — like when you're deep in a legacy callback jungle and need to drop in a Promise-based function without rewriting everything around it.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  Prefer Then-chaining over nesting &lt;a&gt;
&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;It's common to see people misuse promises by nesting &lt;code&gt;.then()&lt;/code&gt; calls instead of chaining them. This leads to "Promise hell", which is structurally similar to "callback hell."&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Callbacks are difficult to work with primarily because of their deeply nested structure. Promises were introduced to address this by enabling a more linear, manageable code flow.&lt;/p&gt;

&lt;p&gt;Of course, code with then-chaining may not be as readable and intuitive as synchronous code or code written with async-await. But it is more manageable than code with nested thens.&lt;/p&gt;

&lt;p&gt;For a deeper dive into why Promise hell is best avoided — and how to write cleaner promise chains — check out these excellent articles:&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/somedood" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F108756%2F1bb8b570-520b-4ad9-8a25-ee28c4d04a8d.jpeg" alt="somedood"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://dev.to/somedood/please-don-t-nest-promises-3o1o" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Please don't nest promises&lt;/h2&gt;
      &lt;h3&gt;Basti Ortiz ・ Jan 6 '20&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#javascript&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#node&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#programming&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#tips&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;



&lt;div class="ltag__link"&gt;
  &lt;a href="https://medium.com/@pyrolistical/how-to-get-out-of-promise-hell-8c20e0ab0513" class="ltag__link__link" rel="noopener noreferrer"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fv2%2Fresize%3Afill%3A64%3A64%2F1%2AXPYe-6twKe2WUT_EgdvOlQ.png" alt="Ronald Chen"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://medium.com/@pyrolistical/how-to-get-out-of-promise-hell-8c20e0ab0513" class="ltag__link__link" rel="noopener noreferrer"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;How to escape Promise Hell. Unlike Callback Hell, Promise Hell is… | by Ronald Chen | Medium&lt;/h2&gt;
      &lt;h3&gt;Ronald Chen ・ &lt;time&gt;Dec 15, 2018&lt;/time&gt; ・ 
      &lt;div class="ltag__link__servicename"&gt;
        &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.dev.to%2Fassets%2Fmedium-f709f79cf29704f9f4c2a83f950b2964e95007a3e311b77f686915c71574fef2.svg" alt="Medium Logo"&gt;
        Medium
      &lt;/div&gt;
    &lt;/h3&gt;
&lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;p&gt;Keep in mind that it’s not just &lt;code&gt;.then()&lt;/code&gt; chains you need to keep clean — &lt;strong&gt;don’t nest callback-based functions inside a &lt;code&gt;.then()&lt;/code&gt; either&lt;/strong&gt;. This is a common mistake that defeats the purpose of using Promises in the first place.&lt;/p&gt;

&lt;p&gt;If you’re already using Promises, don’t go back to callbacks mid-way. Promises are designed to give you a linear, manageable control flow — and the moment you reintroduce nested callbacks, you throw that benefit away. Whether you’re nesting callbacks or &lt;code&gt;.then()&lt;/code&gt; blocks, the result is the same: harder-to-read code.&lt;/p&gt;

&lt;p&gt;Instead, convert the callback-based function into a Promise using any of the methods discussed in my previous article "&lt;a href="https://dev.to/gaurang847/nodejs-using-callback-based-functions-when-the-rest-of-the-code-uses-promises-gl5"&gt;Using callback-based functions when the rest of the code uses Promises&lt;/a&gt;" and keep your chain linear.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Notice that &lt;code&gt;readFile2CB&lt;/code&gt; is a callback-based function. If you nest it (as shown in the bad-code example), any code that relies on &lt;code&gt;content2&lt;/code&gt; or &lt;code&gt;results&lt;/code&gt; must go &lt;em&gt;inside&lt;/em&gt; that nested block. Over time, this leads to deeply indented, harder-to-read code — exactly the kind of mess Promises were created to prevent.&lt;/p&gt;

&lt;p&gt;Once you start using Promises, commit to the pattern. Nesting defeats the purpose. Whether you’re nesting &lt;code&gt;.then()&lt;/code&gt; blocks or stuffing callbacks inside them, you’re trading away clarity and maintainability — the very reasons Promises exist.&lt;/p&gt;




&lt;p&gt;If you're stuck in a callback-heavy codebase, I feel your pain. Use these patterns sparingly, but push for modern async functions where you can — your future self (and your teammates) will thank you.&lt;/p&gt;

&lt;p&gt;I've put a lot of work into this article. And I hope that it has been helpful. If you have any questions, please feel free to leave a comment below. I'd really appreciate it if you could like and share the article! 😊&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>programming</category>
    </item>
    <item>
      <title>[Node.js] Using callback-based functions when the rest of the code uses Promises</title>
      <dc:creator>Gaurang</dc:creator>
      <pubDate>Sun, 21 May 2023 12:08:58 +0000</pubDate>
      <link>https://dev.to/gaurang847/nodejs-using-callback-based-functions-when-the-rest-of-the-code-uses-promises-gl5</link>
      <guid>https://dev.to/gaurang847/nodejs-using-callback-based-functions-when-the-rest-of-the-code-uses-promises-gl5</guid>
      <description>&lt;p&gt;Hey!&lt;/p&gt;

&lt;p&gt;My name is Gaurang and I've been working with Node.js for the past 5 years. This article is part of the "My Guide to Getting through the Maze of Callbacks and Promises" series. It's a 3-part series where I talk about writing asynchronous code with Node.js.&lt;/p&gt;

&lt;p&gt;The articles are intermediate-level and it is expected that the reader already has basic familiarity with Node.js syntax and the constructs related to asynchronous programming in it. Such as callbacks, promises, and async-await.&lt;/p&gt;




&lt;h1&gt;
  
  
  Table of Contents
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;li&gt;Using util.promisify&lt;/li&gt;
&lt;li&gt;Creating a new Promise&lt;/li&gt;
&lt;li&gt;Create a Promise/callback dual-behaviour wrapper&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;li&gt;Footnote&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Introduction &lt;a&gt;
&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;Promises were introduced in JavaScript in 2015 - just 8 years back from the date of writing this article. Async-await was introduced in 2017 - just 6 years back. That was not a very long time ago. (At least not enough to eradicate the use of callbacks 😢)&lt;/p&gt;

&lt;p&gt;As a working professional, you often need to work with legacy code. So as a JavaScript/Node.js developer, God forbid, you may have to work with callbacks at some point. And I'm assuming that you've had enough exposure to callbacks to understand the general dread that they bring.&lt;/p&gt;

&lt;p&gt;It takes time to get a hang of writing code with callbacks. For those without prior experience with asynchronous programming, it is no different than bullfighting for the first time. For this reason, I believe they try to stick to the sync version of async functions. If you've read my previous article in the series, you would know that this approach is more damaging than it is helpful.&lt;/p&gt;

&lt;p&gt;My personal opinion is that the best way to deal with callback-based functions is to convert them into promise-returning ones.&lt;/p&gt;

&lt;p&gt;Suppose, you're working on a Node.js-based project. You created new modules (it could be APIs, controllers, utility services, etc.); everything using promises and async-await. However, there are some legacy functions written using the callback approach. You need to call one such function but don't want to give birth to a callback hell.&lt;/p&gt;

&lt;p&gt;There are 3 ways of going ahead with this.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use &lt;code&gt;util.promisify&lt;/code&gt; (My favourite)&lt;/li&gt;
&lt;li&gt;Create a new promise.&lt;/li&gt;
&lt;li&gt;Create a smart wrapper that can get you either a callback or a Promise based on your requirement&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Using &lt;code&gt;util.promisify&lt;/code&gt; &lt;a&gt;
&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;A lot of people don't know about this gem that was introduced in Node.js version 8. If the legacy callback-based function uses the standard &lt;a href="https://fredkschott.com/post/2014/03/understanding-error-first-callbacks-in-node-js/" rel="noopener noreferrer"&gt;error-first callback style&lt;/a&gt; and the callback is the last argument, you can create a promise-returning function with minimal code using &lt;code&gt;util.promisify&lt;/code&gt;.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You have to be careful using it with functions that belong to a class/object though.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;If you're new to the bind function and are not sure what the object context is, here are a few great resources that may help:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind" rel="noopener noreferrer"&gt;MDN docs on bind function&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://youtu.be/GhbhD1HR5vk" rel="noopener noreferrer"&gt;MPJ's explainer video on bind and this&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.javascripttutorial.net/javascript-bind/" rel="noopener noreferrer"&gt;JavaScript Tutorial article on bind function&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One of the best things about &lt;code&gt;util.promisify&lt;/code&gt; is that it works flawlessly with Node.js native library methods such as functions belonging to modules: &lt;code&gt;fs&lt;/code&gt;, &lt;code&gt;child_process&lt;/code&gt;, etc. You can use it to promisify &lt;code&gt;setTimeout&lt;/code&gt; as well.&lt;/p&gt;

&lt;h1&gt;
  
  
  Creating a new Promise &lt;a&gt;
&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;If the callback-based function doesn't follow proper conventions. Or for some reason, &lt;code&gt;util.promisify&lt;/code&gt; doesn't work for you, you can always create a custom new Promise.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You can move the Promise-creation to a separate function if doing it inline looks shabby.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Note: Here, &lt;code&gt;promisifiedAsyncFunction&lt;/code&gt; does not use async-await. But, it returns a Promise. So, we have to use &lt;code&gt;await&lt;/code&gt; in the main function while calling &lt;code&gt;promisifiedAsyncFunction&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If the callback-based function belongs to a class/object, you'll need to take care of the context.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h1&gt;
  
  
  Create a Promise/callback dual-behaviour wrapper &lt;a&gt;
&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;If you have access to the source code of the original callback-based function. And the authority to modify it, you can create a wrapper around it. This wrapper will work as a callback-based function if a callback is provided. If not, it'll return a Promise.&lt;/p&gt;

&lt;p&gt;This dual-behaviour wrapper lets you work with Promises in new code that you write. Without disturbing the old code where the function is expected to work with a callback. Thus, it provides Promise support with backward compatibility for callback-based legacy code.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Previously, if the module exported the &lt;code&gt;callbackBasedAsyncFunc&lt;/code&gt; function, now it can export the &lt;code&gt;dualBehaviourWrapper&lt;/code&gt;.&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="c1"&gt;// Previously&lt;/span&gt;
&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;callbackBasedAsyncFunc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;callbackBasedAsyncFunc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Now&lt;/span&gt;
&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;callbackBasedAsyncFunc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;dualBehaviourWrapper&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thus, with minimal modification, you can add Promise support to a legacy module. And all the other existing modules, that are calling &lt;code&gt;callbackBasedAsyncFunc&lt;/code&gt; by passing a callback, remain unaffected. Win-win!&lt;/p&gt;

&lt;p&gt;You might have noticed that a lot of popular NPM libraries give similar dual-behaviour support for their functions where they use a callback if it is passed, and return a Promise if a callback is not passed. Their implementation may be different though. For example, &lt;a href="https://mochajs.org/#asynchronous-code" rel="noopener noreferrer"&gt;mocha.js&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion  &lt;a&gt;
&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;Overall, handling callback-based functions when the rest of your code uses promises can be a bit tricky. However, by following the tips in this article, you can make the process much easier. With a little practice, you'll be handling callback-based functions like a pro in no time!&lt;/p&gt;

&lt;p&gt;I've put a lot of work into this article. And I hope that it has been helpful. If you have any questions, please feel free to leave a comment below.&lt;br&gt;
I'd really appreciate it if you could like and share the article! 😊&lt;/p&gt;

&lt;h1&gt;
  
  
  Footnote &lt;a&gt;
&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;fs&lt;/code&gt; version 10 introduced Promises API which has promise-based versions of functions. Have a look at the &lt;a href="https://nodejs.org/docs/latest-v10.x/api/fs.html#fs_fs_promises_api" rel="noopener noreferrer"&gt;documentation&lt;/a&gt;. So you don't need to struggle with callbacks or manually convert &lt;code&gt;fs&lt;/code&gt; functions to Promise-based ones.&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>programming</category>
      <category>coding</category>
    </item>
    <item>
      <title>[Node.js] Why using sync versions of async functions is bad.</title>
      <dc:creator>Gaurang</dc:creator>
      <pubDate>Sun, 05 Mar 2023 15:08:14 +0000</pubDate>
      <link>https://dev.to/gaurang847/nodejs-why-using-sync-versions-of-async-functions-is-bad-586</link>
      <guid>https://dev.to/gaurang847/nodejs-why-using-sync-versions-of-async-functions-is-bad-586</guid>
      <description>&lt;p&gt;Hi!&lt;br&gt;
My name is Gaurang and I've been working with Node.js for the past 5 years.&lt;br&gt;
This article is part of the "My guide to getting through the maze of callbacks and promises" series. It's a 3-part series where I talk about writing asynchronous code with Node.js.&lt;/p&gt;

&lt;p&gt;The articles are intermediate-level and it is expected that the reader already has basic familiarity with Node.js syntax and the constructs related to asynchronous programming in it. Such as callbacks, promises, and async-await.&lt;/p&gt;


&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Sync of Async what?&lt;/li&gt;
&lt;li&gt;What's wrong with using sync versions of async functions?&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Sync of Async what? &lt;a&gt;
&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Before I go ahead and beef about synchronous functions, I should briefly describe synchronous as well as asynchronous functions.&lt;/p&gt;

&lt;p&gt;Many Node.js packages provide synchronous alternatives of their asynchronous functions.&lt;br&gt;
For example, the &lt;code&gt;fs&lt;/code&gt; package has &lt;a href="https://nodejs.org/api/fs.html#fsreadfilepath-options-callback" rel="noopener noreferrer"&gt;&lt;code&gt;fs.readFile&lt;/code&gt;&lt;/a&gt; (which works asynchronously) and &lt;a href="https://nodejs.org/api/fs.html#fsreadfilesyncpath-options" rel="noopener noreferrer"&gt;&lt;code&gt;fs.readFileSync&lt;/code&gt;&lt;/a&gt; (which works synchronously).&lt;/p&gt;

&lt;p&gt;The working of &lt;code&gt;fs.readFileSync&lt;/code&gt; is similar to traditional functions.&lt;br&gt;
Your program calls the method and waits for the file to be read. Once the file is completely read, it returns the contents. The program then resumes execution of the rest of the code.&lt;/p&gt;

&lt;p&gt;Now reading a file is a slow I/O operation that could take  a few seconds.&lt;br&gt;
So when our program calls the asynchronous &lt;code&gt;fs.readFile&lt;/code&gt; method, it does not wait for the method to read the entire file and return the contents. Our program moves ahead with the execution of the rest of the code immediately; while &lt;code&gt;fs.readFile&lt;/code&gt; continues with the reading task in parallel.&lt;/p&gt;

&lt;p&gt;If there's any code you want to execute after the file contents have been read, you need to wrap the code in a function and pass that function to &lt;code&gt;fs.readFile&lt;/code&gt; as a callback function.&lt;br&gt;
&lt;code&gt;fs.readFile&lt;/code&gt; will call the callback function as soon as the file contents have been read.&lt;br&gt;
This way of writing code is an engineering choice made by the creators of JavaScript/Node.js that allows it to be extremely fast despite being single-threaded.&lt;/p&gt;

&lt;p&gt;Here's a sample script demonstrating the difference between synchronous and asynchronous programming in JavaScript.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  What's wrong with using sync versions of async functions? &lt;a&gt;
&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;I often come across codebases where callbacks, promises, and async-await have been mixed together. Turning the code into a hotchpotch.&lt;br&gt;
I sympathize with the people that have to work with such codebases.&lt;br&gt;
Understanding callbacks and promises is difficult. Writing code with callbacks is difficult. Understanding the difference between synchronous and asynchronous functions is difficult.&lt;/p&gt;

&lt;p&gt;The advent of &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function" rel="noopener noreferrer"&gt;async-await&lt;/a&gt; has made writing code in JavaScript fairly straightforward. And so, most people learning JavaScript today do not know what a pain it used to be when using callbacks was the primary method of writing code. As a result, they often lack experience in handling callbacks.&lt;/p&gt;

&lt;p&gt;People often retreat to synchronous versions of library methods because they're more convenient. E.g. &lt;a href="https://nodejs.org/api/fs.html#fsreadfilesyncpath-options" rel="noopener noreferrer"&gt;&lt;code&gt;fs.readFileSync&lt;/code&gt;&lt;/a&gt;, &lt;code&gt;fs.writeFileSync&lt;/code&gt;, &lt;code&gt;fs.copyFileSync&lt;/code&gt;, &lt;code&gt;child_process.execSync&lt;/code&gt;, etc.&lt;br&gt;
In my opinion is one of the worst things to do to your project.&lt;/p&gt;

&lt;p&gt;People generally opt for Node.js due to its reputation for fast network and I/O operations. However, it's crucial not to overlook the fundamental reason behind this efficiency: Node.js's single-threaded, asynchronous execution model! Utilizing synchronous versions of I/O and network operations can significantly impair your application's performance compared to counterparts developed in alternative languages.&lt;/p&gt;

&lt;p&gt;Asynchronous functions are designed to be non-blocking and to avoid delays. As Node.js is single-threaded, just one long-running synchronous (blocking) task is enough to degrade the performance of the entire application. Using synchronous versions of asynchronous functions may seem like an easy solution at first, but it is the devil in disguise. It can cause delays all over the application; leading to frustration for developers and users alike.&lt;/p&gt;

&lt;p&gt;Of course, if you're writing a small independent one-time script, it may not matter much. But in large production-level applications, this is going to cause unnecessary bottlenecks.&lt;/p&gt;

&lt;p&gt;Here's a short demo of how a synchronous method call affects the rest of the application.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/xxKffpI7EIY"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;This script starts a barebones Node.js server that has three APIs.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;GET /quick-api&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;GET /slow-sync-api&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;GET /slow-async-api&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;quick-api&lt;/code&gt; API is supposed to return a response immediately.&lt;br&gt;
But, if you call &lt;code&gt;quick-api&lt;/code&gt; after calling &lt;code&gt;slow-sync-api&lt;/code&gt;, it does not return until &lt;code&gt;slow-sync-api&lt;/code&gt; returns. This is because Node.js is single-threaded. And &lt;code&gt;slow-sync-api&lt;/code&gt; calls a synchronous method that does not let the main thread do anything else till its execution is complete. It blocks all other requests.&lt;br&gt;
However, if you call &lt;code&gt;quick-api&lt;/code&gt; after &lt;code&gt;slow-async-api&lt;/code&gt;, the &lt;code&gt;quick-api&lt;/code&gt; works as expected. This is because &lt;code&gt;slow-async-api&lt;/code&gt; does not block the main thread till its execution is complete. The work of the asynchronous function is deferred to a worker thread and the main thread waits for the execution to complete. Meanwhile, the main thread is free to fulfil other requests.&lt;/p&gt;

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

&lt;p&gt;Asynchronous execution is integral to JavaScript. So, you'll find asynchronous functions everywhere. Transitioning from a traditional synchronous mindset to an asynchronous one takes some adjustment.&lt;/p&gt;

&lt;p&gt;However, using the synchronous versions of asynchronous functions is a bad practice. It can lead to unexpected behavior and performance bottlenecks. When performing asynchronous operations, it's best to use the asynchronous version of functions whenever possible. This will help you avoid any potential problems and ensure that your code is as efficient as possible.&lt;/p&gt;




&lt;p&gt;I've put a lot of work into this article. And I hope that it has been helpful. If you have any questions, please feel free to leave a comment below. I'd really appreciate it if you could like and share the article! 😊&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>learning</category>
      <category>career</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Might I introduce you to my friend TypeScript?</title>
      <dc:creator>Gaurang</dc:creator>
      <pubDate>Sun, 03 May 2020 11:04:24 +0000</pubDate>
      <link>https://dev.to/gaurang847/might-i-introduce-you-to-my-friend-typescript-e72</link>
      <guid>https://dev.to/gaurang847/might-i-introduce-you-to-my-friend-typescript-e72</guid>
      <description>&lt;h1&gt;
  
  
  Table of Contents
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;The lead-up&lt;/li&gt;
&lt;li&gt;Why interfaces in JavaScript make no sense&lt;/li&gt;
&lt;li&gt;Might I introduce you to my friend Typescript?&lt;/li&gt;
&lt;li&gt;Do I really need it though?&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  The lead-up
&lt;/h1&gt;

&lt;p&gt;A few days back, I wrote an &lt;a href="https://dev.to/gaurang847/do-you-really-understand-interfaces-1g7l"&gt;article&lt;/a&gt; explaining the concept of interfaces in Java (or object-oriented programming in general). In the process, I also debunked a popular myth that I found to exist among Java novices.&lt;br&gt;
I was glad to find that it was so well received. (Thanks all of you awesome people! You rock!! :D )&lt;br&gt;
I want to address this particular comment though.&lt;br&gt;
&lt;/p&gt;
&lt;div class="liquid-comment"&gt;
    &lt;div class="details"&gt;
      &lt;a href="/whiplash5057"&gt;
        &lt;img class="profile-pic" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F354209%2Fb0baf7b1-dacd-4732-90c5-eb0ccd95ace6.jpeg" alt="whiplash5057 profile image"&gt;
      &lt;/a&gt;
      &lt;a href="/whiplash5057"&gt;
        &lt;span class="comment-username"&gt;Richard Andrews&lt;/span&gt;
      &lt;/a&gt;
      &lt;span class="color-base-30 px-2 m:pl-0"&gt;•&lt;/span&gt;

&lt;a href="https://dev.to/whiplash5057/comment/mpai" class="comment-date crayons-link crayons-link--secondary fs-s"&gt;
  &lt;time class="date-short-year"&gt;
    Mar 23 '20
  &lt;/time&gt;

&lt;/a&gt;

    &lt;/div&gt;
    &lt;div class="body"&gt;
      &lt;p&gt;Great read. Love the analogies. But I'm a javascript developer and there is no support for interfaces in javascript currently. How would I create a contract for other developers to follow?&lt;/p&gt;


    &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;My answer to this (the short one) is "You can't. Nope. No. Nada. Zilch. Naught". You don't have interfaces in JavaScript.&lt;br&gt;
And I don't believe there exists any other way to enforce method contracts in JavaScript the way you do in Java with the help of interfaces.&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Why interfaces in JavaScript make no sense
&lt;/h1&gt;

&lt;p&gt;With the help of interfaces, you define three things in Java:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What methods should compulsory exist&lt;/li&gt;
&lt;li&gt;The input of these methods - how many parameters they will take and what shall be the data-type of each&lt;/li&gt;
&lt;li&gt;The output of these methods - what shall be the data-type of the returned value&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When you're talking about having interfaces in JavaScript, you're probably only thinking about point 1. But, what about points 2 and 3?&lt;br&gt;
&lt;/p&gt;
&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
function dummyMethod(definedParam1, definedParam2){
    // arguments is a special object that exists inside every JavaScript function.
    // It contains all the input-parameters passed to the function.
    console.log(arguments)
}

dummyMethod("a", "b");
dummyMethod();
dummyMethod("a", "b", "c", "d", "e");

  &lt;/code&gt;
&lt;/div&gt;
&lt;br&gt;
You can see that the function &lt;code&gt;dummyMethod&lt;/code&gt; is defined to take exactly 2 parameters as input.&lt;br&gt;
But, regardless of whether I pass less parameters or more, the function takes them without complaining.

&lt;p&gt;Not only does JavaScript lack the kind of type-checking that happens in Java. But also, there's no policing on the number of arguments of a function.&lt;br&gt;
Furthermore, JavaScript has no notion of 'return type of a function'.&lt;/p&gt;

&lt;p&gt;For a language like Java which is statically typed, it makes sense to have interfaces. There, things break if methods are called with the wrong parameters.&lt;br&gt;
But JavaScript follows duck-typing; which is considered to be the antithesis to interfaces.&lt;/p&gt;


&lt;div class="ltag__stackexchange--container"&gt;
  &lt;div class="ltag__stackexchange--title-container"&gt;
    
      &lt;div class="ltag__stackexchange--title"&gt;
        &lt;div class="ltag__stackexchange--header"&gt;
          &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.dev.to%2Fassets%2Fstackoverflow-logo-b42691ae545e4810b105ee957979a853a696085e67e43ee14c5699cf3e890fb4.svg" alt=""&gt;
          &lt;a href="https://stackoverflow.com/questions/4205130/what-is-duck-typing/40434829#40434829" rel="noopener noreferrer"&gt;
            &lt;span class="title-flare"&gt;answer&lt;/span&gt; re: What is duck typing?
          &lt;/a&gt;
        &lt;/div&gt;
        &lt;div class="ltag__stackexchange--post-metadata"&gt;
          &lt;span&gt;Nov  5 '16&lt;/span&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;a class="ltag__stackexchange--score-container" href="https://stackoverflow.com/questions/4205130/what-is-duck-typing/40434829#40434829" rel="noopener noreferrer"&gt;
        &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.dev.to%2Fassets%2Fstackexchange-arrow-up-eff2e2849e67d156181d258e38802c0b57fa011f74164a7f97675ca3b6ab756b.svg" alt=""&gt;
        &lt;div class="ltag__stackexchange--score-number"&gt;
          214
        &lt;/div&gt;
        &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.dev.to%2Fassets%2Fstackexchange-arrow-down-4349fac0dd932d284fab7e4dd9846f19a3710558efde0d2dfd05897f3eeb9aba.svg" alt=""&gt;
      &lt;/a&gt;
    
  &lt;/div&gt;
  &lt;div class="ltag__stackexchange--body"&gt;
    
&lt;h1&gt;Simple Explanation&lt;/h1&gt;
&lt;h3&gt;What is duck typing?&lt;/h3&gt;
&lt;p&gt;“If it walks like a duck and quacks like a.... ” - &lt;em&gt;YES, but what does that mean??!&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Short answer:&lt;/strong&gt; We're interested in what "objects" &lt;em&gt;&lt;strong&gt;can do&lt;/strong&gt;&lt;/em&gt;, rather than what they &lt;em&gt;&lt;strong&gt;are&lt;/strong&gt;&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Let's unpack it with an example:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://i.sstatic.net/DNeRD.jpg" rel="nofollow noreferrer noopener"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.sstatic.net%2FDNeRD.jpg" alt="Explanation of Duck Typing"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;See below for further…&lt;/p&gt;
    
  &lt;/div&gt;
  &lt;div class="ltag__stackexchange--btn--container"&gt;
    &lt;a href="https://stackoverflow.com/questions/4205130/what-is-duck-typing/40434829#40434829" class="ltag__stackexchange--btn" rel="noopener noreferrer"&gt;Open Full Answer&lt;/a&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;br&gt;
&lt;div class="ltag__stackexchange--container"&gt;
  &lt;div class="ltag__stackexchange--title-container"&gt;
    
      &lt;div class="ltag__stackexchange--title"&gt;
        &lt;div class="ltag__stackexchange--header"&gt;
          &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.dev.to%2Fassets%2Fstackoverflow-logo-b42691ae545e4810b105ee957979a853a696085e67e43ee14c5699cf3e890fb4.svg" alt=""&gt;
          &lt;a href="https://stackoverflow.com/questions/4205130/what-is-duck-typing/33631704#33631704" rel="noopener noreferrer"&gt;
            &lt;span class="title-flare"&gt;answer&lt;/span&gt; re: What is duck typing?
          &lt;/a&gt;
        &lt;/div&gt;
        &lt;div class="ltag__stackexchange--post-metadata"&gt;
          &lt;span&gt;Nov 10 '15&lt;/span&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;a class="ltag__stackexchange--score-container" href="https://stackoverflow.com/questions/4205130/what-is-duck-typing/33631704#33631704" rel="noopener noreferrer"&gt;
        &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.dev.to%2Fassets%2Fstackexchange-arrow-up-eff2e2849e67d156181d258e38802c0b57fa011f74164a7f97675ca3b6ab756b.svg" alt=""&gt;
        &lt;div class="ltag__stackexchange--score-number"&gt;
          47
        &lt;/div&gt;
        &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.dev.to%2Fassets%2Fstackexchange-arrow-down-4349fac0dd932d284fab7e4dd9846f19a3710558efde0d2dfd05897f3eeb9aba.svg" alt=""&gt;
      &lt;/a&gt;
    
  &lt;/div&gt;
  &lt;div class="ltag__stackexchange--body"&gt;
    
&lt;p&gt;Consider you are designing a simple function which gets an object of type &lt;code&gt;Bird&lt;/code&gt; and calls its &lt;code&gt;walk()&lt;/code&gt; method. There are two approaches you can think of:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;This is my function, and I must be sure that it only accepts the &lt;code&gt;Bird&lt;/code&gt; type or the code will not compile. If…&lt;/li&gt;
&lt;/ol&gt;
    
  &lt;/div&gt;
  &lt;div class="ltag__stackexchange--btn--container"&gt;
    &lt;a href="https://stackoverflow.com/questions/4205130/what-is-duck-typing/33631704#33631704" class="ltag__stackexchange--btn" rel="noopener noreferrer"&gt;Open Full Answer&lt;/a&gt;
  &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Might I introduce you to my friend Typescript?
&lt;/h1&gt;

&lt;p&gt;TypeScript is really just JavaScript + types. Or as they call it, the typed &lt;em&gt;super-set&lt;/em&gt; of JavaScript.&lt;br&gt;
So, remember how you write &lt;code&gt;int total&lt;/code&gt;, &lt;code&gt;boolean isEmpty&lt;/code&gt;, &lt;code&gt;String name&lt;/code&gt; in Java/C++. Yeah, Typescript is basically JavaScript with all that.&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/educative" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__org__pic"&gt;
      &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Forganization%2Fprofile_image%2F1212%2F416017e1-97b9-4ebe-9520-496352311e72.png" alt="Educative" width="750" height="750"&gt;
      &lt;div class="ltag__link__user__pic"&gt;
        &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F263040%2F2cae9736-9047-4d70-99d7-2f2955ced2b1.png" alt="" width="396" height="398"&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://dev.to/educative/typescript-tutorial-a-step-by-step-guide-to-learn-typescript-4g4p" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;TypeScript Tutorial: A step-by-step guide to learn TypeScript&lt;/h2&gt;
      &lt;h3&gt;Amanda Fawcett for Educative ・ Feb 17 '20&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#typescript&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#javascript&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#beginners&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#webdev&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;p&gt;TypeScript doesn't let you pass more parameters to a function, or less, than what has been defined.&lt;br&gt;
It also doesn't let you pass parameters of the wrong type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Source: https://www.typescriptlang.org/docs/handbook/functions.html&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;buildName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstName&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;lastName&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="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&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="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;result1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;buildName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bob&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;                  &lt;span class="c1"&gt;// error, too few parameters&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;result2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;buildName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bob&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="s2"&gt;Adams&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="s2"&gt;Sr.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// error, too many parameters&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;result3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;buildName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bob&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="s2"&gt;Adams&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;         &lt;span class="c1"&gt;// ah, just right&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now that we've introduced types to JavaScript, it makes lot more sense to have interfaces. Undoubtedly, TypeScript has them.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Source: https://devblogs.microsoft.com/typescript/walkthrough-interfaces/&lt;/span&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Greetable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&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="k"&gt;void&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="nf"&gt;helloEnglish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;g&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Greetable&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello there!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// OK&lt;/span&gt;
    &lt;span class="nx"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Not OK -- 42 is not a string&lt;/span&gt;
    &lt;span class="nx"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;greep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hi&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Not OK -- 'greep' is not a member of 'Greetable'&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Note that the type system of TypeScript is like an add-on over JavaScript. It is not a separate language.&lt;br&gt;
A script written in TypeScript is not runnable on its own.&lt;br&gt;
When you run the compiler over your &lt;code&gt;.ts&lt;/code&gt; file, it gets converted to a normal JavaScript file without types.&lt;br&gt;
You can even check out their &lt;a href="https://www.typescriptlang.org/play/index.html" rel="noopener noreferrer"&gt;playground&lt;/a&gt; and see it live how TypeScript gets converted to JavaScript. Check out their examples. It's fun! :D&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Do I really need this though?
&lt;/h1&gt;

&lt;p&gt;If you're an indie developer, who has spent considerable time working with vanilla JS, you might've grown fond of the type-less flexibility, and smartness of the language to dynamically infer the type of variables. Without bothering you, so that you can focus on writing your logic.&lt;/p&gt;

&lt;p&gt;You might be wondering whether this overhead of declaring types is really worth it or is just an unnecessarily glorified self-infliction.&lt;br&gt;
Maybe it's part of some grand propaganda conjured by Microsoft, spread by Java/C++ cult-followers, and backed by those pretentious developers who grill you for not following a certain coding standard, or ask a random language-quirk related question in interview and judge the candidate critically for not knowing the answer.&lt;/p&gt;

&lt;p&gt;Well... Your suspicions might as well be true. I have no proofs to tell you otherwise. ¯\_(ツ)_/¯&lt;br&gt;
But, here's the thing. Just by looking at the name, you can't tell if &lt;code&gt;total&lt;/code&gt; is a Number or String. You'll have to trace it back to where it's defined to know that. If it's a value being passed from another function, you'll need to trace it back there as well.&lt;br&gt;
The flexibility of JavaScript might feel like a boon when you're writing your software ground-up. But it feels like a curse when you're receiving a knowledge-transfer (KT) session from another developer.&lt;br&gt;
Larger the codebase and larger the number of people that have worked on it, the more difficult it is to understand it.&lt;/p&gt;

&lt;p&gt;Also, while writing your own feature, you'd obviously want your code to be resilient to the mistakes of previous code. So, you can't trust it. And you'll need to deliberately cast it &lt;code&gt;total = Number(total)&lt;/code&gt;.&lt;br&gt;
If you get a &lt;code&gt;Nan&lt;/code&gt;, you can handle it accordingly.&lt;br&gt;
Imagine if yours is an e-commerce website with the user-base of lakhs. You miss this said case and your code goes to production just before a major sale. A user clicks on checkout and boom! Server crashed!&lt;/p&gt;

&lt;p&gt;Forgive me if the scenario sounds a little presumptuous. But, at the very least, you must agree that it is plausible.&lt;br&gt;
Just Google "common JavaScript mistakes". Most results will list at least one mistake that is related to types.&lt;br&gt;
Specifically, the ones related to &lt;a href="http://www.javascriptgotchas.com/gotchas/values-plus.html" rel="noopener noreferrer"&gt;+ operator&lt;/a&gt; and &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators" rel="noopener noreferrer"&gt;strict/loose comparison&lt;/a&gt; or type-coercion.&lt;/p&gt;

&lt;p&gt;Consider this tweet:&lt;br&gt;
&lt;iframe class="tweet-embed" id="tweet-1161012147884707840-194" src="https://platform.twitter.com/embed/Tweet.html?id=1161012147884707840"&gt;
&lt;/iframe&gt;

  // Detect dark theme
  var iframe = document.getElementById('tweet-1161012147884707840-194');
  if (document.body.className.includes('dark-theme')) {
    iframe.src = "https://platform.twitter.com/embed/Tweet.html?id=1161012147884707840&amp;amp;theme=dark"
  }



&lt;br&gt;
Confused?&lt;br&gt;
Here, &lt;code&gt;+ "a"&lt;/code&gt; turns into &lt;code&gt;NaN&lt;/code&gt; (Not a Number. Yes, that's &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN" rel="noopener noreferrer"&gt;a thing in JavaScript&lt;/a&gt;) because + acts as the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Unary_plus" rel="noopener noreferrer"&gt;unary plus operator&lt;/a&gt; that tries to cast any value to a number. And "a" is literally not a number.&lt;br&gt;
Thus, &lt;code&gt;"b" + "a" + + "a" + "a"&lt;/code&gt; becomes &lt;code&gt;"ba" + NaN + "a"&lt;/code&gt;.&lt;br&gt;
Now, the NaN gets cast to a string and &lt;code&gt;"ba" + NaN + "a"&lt;/code&gt; transforms to &lt;code&gt;"baNaNa"&lt;/code&gt;.&lt;br&gt;
Convert it to lowercase and that's how you get &lt;code&gt;"banana"&lt;/code&gt;.&lt;/p&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
console.log(("b" + "a" + + "a" + "a").toLowerCase());
console.log(+"a");
console.log("ba" + NaN + "a");
console.log("baNaNa".toLowerCase());

  &lt;/code&gt;
&lt;/div&gt;



&lt;p&gt;With a type system in place, you just immediately know whether a variable is a string or a number or something else altogether. (Who knows.. It could be an object, for heaven's sake!)&lt;br&gt;
And say, if you're assigning a string to a number variable, the error will be caught immediately at compile time.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"I completely understand that if you're a single person writing a large JavaScript application where you know exactly what every object is. And  you've made it all. And it's beautiful. And it's fast. And you can change it into any other thing that you want, because you know everything about it in your head, right?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;But once you start programming larger apps in teams. Y'know, we just find that teams want to have the ability to agree upon [that] in between these two modules, we have to follow a contract. And they want to have things that can validate that they're adhering to the contract. Y'know, you can't just like run into each others offices and like compare code all the time.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;You want to have some sort of agreed upon way that you write this down. And you also want, when new people come in, something that tells you a bit about what this 100-1000 line codebase does. How am I supposed to understand what this paramteter &lt;code&gt;x&lt;/code&gt; is. I don't know. I don't know &lt;code&gt;x&lt;/code&gt;. Tell me! With an annotation, right?"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;— &lt;a href="https://www.hanselminutes.com/340/what-is-typescript-and-why-with-anders-hejlsberg" rel="noopener noreferrer"&gt;Anders Hejlsberg, What is TypeScript and why with Anders Hejlsberg [13:30]&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here's another cool thing.&lt;br&gt;
If you use Microsoft VS Code, and you've been using its IntelliSense features (code-completion, parameter info, member list, etc.), they are &lt;a href="https://code.visualstudio.com/docs/nodejs/working-with-javascript" rel="noopener noreferrer"&gt;powered by TypeScript&lt;/a&gt;.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fkj1oq5qya9lhn59ipfse.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fkj1oq5qya9lhn59ipfse.png" alt="Screenshot - parameter info pop-up" width="648" height="394"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzgnzlkc9r8hlgwlzzfv4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzgnzlkc9r8hlgwlzzfv4.png" alt="Screenshot - TypeScript file that helps provide IntelliSense" width="800" height="207"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Even with the absence of types and interfaces, I've seen great software written by people in vanilla JavaScript; mostly at startups with small teams though.&lt;br&gt;
So, if vanilla-JS is your thing, there's nothing wrong in using it.&lt;/p&gt;

&lt;p&gt;But, if you're working on larger projects, or/and you're bothered by the lack of interfaces in JavaScript (and by extension, lack of types), you should consider using &lt;a href="https://www.typescriptlang.org" rel="noopener noreferrer"&gt;TypeScript&lt;/a&gt;.&lt;br&gt;
It'll definitely increase your productivity and your team's.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>codenewbie</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Do you go back to edit your published articles?</title>
      <dc:creator>Gaurang</dc:creator>
      <pubDate>Thu, 09 Apr 2020 03:27:35 +0000</pubDate>
      <link>https://dev.to/gaurang847/do-you-go-back-to-edit-your-published-articles-1l9p</link>
      <guid>https://dev.to/gaurang847/do-you-go-back-to-edit-your-published-articles-1l9p</guid>
      <description>&lt;p&gt;Hey Dev's!&lt;/p&gt;

&lt;p&gt;So I've been wondering. I often spend days to write an article.&lt;br&gt;
'Cause I never feel the article is complete and ready to be published.&lt;br&gt;
Does the content make sense? Is it relatable? Did I spend all this time just rambling?&lt;br&gt;
Maybe there's a wrong assumption? Wrong fact? Did I make a googie?&lt;br&gt;
Maybe it's not relevant to other people? Maybe it's bland? Maybe I should add a diagram? Or an example?&lt;/p&gt;

&lt;p&gt;When I feel that the article is decent enough and I've gotten tired of editing it, I quickly publish it before I can change my mind.&lt;br&gt;
And being the self-obsessed person that I am, I often come back to reread them.&lt;br&gt;
And often I feel like, maybe I could add reference to that article that I recently read. That quote. Maybe this Codepen.&lt;br&gt;
Maybe active voice would've sounded better than passive here.&lt;/p&gt;

&lt;p&gt;Once it gets into my mind, it bothers me that something is amiss in my article. And I edit it.&lt;br&gt;
So yeah, here's my question.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Do you guys do it too? Do you edit your articles if you find something to add/change in it?&lt;/li&gt;
&lt;li&gt;Or once it's published, you don't touch it again at all? Like, religiously. Add comments instead maybe?&lt;/li&gt;
&lt;li&gt;Or add new stuff with a new "Edited" section.&lt;/li&gt;
&lt;li&gt;Or store your ideas till you have enough of them to make another independent article?&lt;/li&gt;
&lt;li&gt;&lt;em&gt;"You're crazy Gaurang. Nobody thinks about their articles so much. Here, have a cookie 🍪"&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;PS. I took more than half a day to post this question&lt;/p&gt;

</description>
      <category>discuss</category>
    </item>
    <item>
      <title>Do you really understand interfaces?</title>
      <dc:creator>Gaurang</dc:creator>
      <pubDate>Sun, 15 Mar 2020 14:52:38 +0000</pubDate>
      <link>https://dev.to/gaurang847/do-you-really-understand-interfaces-1g7l</link>
      <guid>https://dev.to/gaurang847/do-you-really-understand-interfaces-1g7l</guid>
      <description>&lt;h1&gt;
  
  
  Table of Contents:
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;The lead-up&lt;/li&gt;
&lt;li&gt;Deadly Diamond of Death&lt;/li&gt;
&lt;li&gt;How interfaces solve the diamond problem?&lt;/li&gt;
&lt;li&gt;The idea behind interfaces&lt;/li&gt;
&lt;li&gt;The practical use of interfaces while writing software&lt;/li&gt;
&lt;li&gt;To conclude..&lt;/li&gt;
&lt;li&gt;Official Oracle Documentation&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  The lead-up &lt;a&gt;
&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;When I was learning Java in college, I did not understand the use for interfaces.&lt;br&gt;
You can't define functions inside an interface. You can only declare them. You have to have a class implement the interface to define it's functions and then use them.&lt;br&gt;
That's crazy! Pretty worthless piece of crap I would think. I can just write my classes and their functions normally. Why would I declare them in one place and define in another.&lt;br&gt;
I don't even need to declare them. Just write my functions normally in a class. It's totally redundant.&lt;/p&gt;

&lt;p&gt;Confused and curious, I asked my teacher why I would ever require to implement an interface.&lt;br&gt;
Almost immediately, she said that interfaces let you achieve multiple inheritance, while normal classes don't - the textbook answer on the difference between classes and interfaces.&lt;br&gt;
And I was like, so? Even if my class can implement multiple interfaces at once, none of them would contain function definitions.&lt;br&gt;
They would all be pretty useless. I can just drop them all and write my class without implementing them. Be it single or multiple, what advantage do I get by using interfaces?&lt;br&gt;
My teacher did not have an answer to that.&lt;/p&gt;

&lt;p&gt;With the lack of a proper answer, me and most (if not all) of my friends resigned ourselves to accept that interfaces are some kind of mysterious artifacts. That, given the right incantation, would help us manipulate the space-time continuum, rip through the fabric of Java ecosystem and use multiple inheritance - the fruit that is wrongly forbidden from us.&lt;/p&gt;

&lt;p&gt;Meanwhile, we gave the same answer during our engineering vivas that my teacher gave me. And wrote the same in our written exams.&lt;br&gt;
Something felt off though. And I could never digest this.&lt;br&gt;
It wasn't until later, when I dabbled with Android that I really learnt the purpose of interfaces - as a contract which &lt;em&gt;lets you to enforce that, the classes you work with, exhibit certain behavior&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;I still come across people whose primary understanding of interfaces is something that allows multiple inheritance in Java.&lt;br&gt;
And that is so ridiculous - saying that interfaces support multiple inheritance while classes don't, when I ask you what's the difference between the two.&lt;br&gt;
It's like saying that the difference between a dog and a horse is that a dog barks while a horse neighs.&lt;br&gt;
While that is indeed true, you must understand that dogs and horses are fundamentally different animals!&lt;br&gt;
If your understanding of a horse is that of a dog that neighs. Then boy! You're going to have a wild time in a farm!&lt;/p&gt;
&lt;h1&gt;
  
  
  Deadly Diamond of Death &lt;a&gt;
&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;First off, lets understand why multiple inheritance is not allowed in Java.&lt;/p&gt;

&lt;p&gt;Consider there's a class 'Animals'. All animals makeNoise().&lt;br&gt;
We derive two child classes from this class - 'Horses' and 'Dogs'. Both of them override the makeNoise() behavior.&lt;br&gt;
Horses makeNoise() by neighing. Dogs makeNoise() by barking.&lt;br&gt;
Now suppose, for whatever godforsaken reason, a mad scientist decides to mix up the genes of Horses and Dogs together, and make a new species of Animals - Horgs.&lt;br&gt;
When the Horgs makeNoise(), will they bark? Or will they neigh?&lt;br&gt;
This is the infamous diamond problem (aka Deadly Diamond of Death) in object-oriented programming.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3tb30o8gf52llo9v2fx1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3tb30o8gf52llo9v2fx1.png" alt="Graphical representation of the given Animal problem" width="800" height="460"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Java doesn't support multiple inheritance with classes simply because &lt;em&gt;inheriting multiple classes together is problematic&lt;/em&gt;.&lt;br&gt;
If a class inherits multiple other classes, and these classes contain functions with the same signature, there arises an ambiguity - when I call the said function with an instance of the inheriting (derived) class, how should it behave?&lt;br&gt;
Out of the many function definitions coming from multiple inherited classes, which should be considered?&lt;br&gt;
And how do I control which definition is used when?&lt;/p&gt;

&lt;p&gt;C++ tries to handle the Diamond problem using &lt;a href="https://en.wikipedia.org/wiki/Virtual_inheritance" rel="noopener noreferrer"&gt;virtual inheritance and scope resolution&lt;/a&gt;.&lt;br&gt;
I'm not really a C++ person. And I don't understand how virtual inheritance works.&lt;br&gt;
But, from what I understand, it requires prior knowledge that a certain function may get involved in multiple inheritance. And it requires to be specially handled.&lt;br&gt;
Now that feels weird to me. I did not find many articles on the topic. So, maybe not many people use it. But, please do recommend me any good material on the topic if you have any.&lt;/p&gt;
&lt;h1&gt;
  
  
  How interfaces solve the diamond problem? &lt;a&gt;
&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;They don't.&lt;br&gt;
Like I said, they're not a mechanism to allow multiple inheritance. It's just that since interfaces don't have method definitions, they don't &lt;strong&gt;&lt;em&gt;have&lt;/em&gt;&lt;/strong&gt; the diamond problem.&lt;br&gt;
That is why you're allowed to implement multiple interfaces. That's all.&lt;br&gt;
But the notion that, the use of interfaces is to enable multiple inheritance, is wrong and far from the truth.&lt;/p&gt;
&lt;h1&gt;
  
  
  The idea behind interfaces &lt;a&gt;
&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;Consider you're a king. You have a beautiful daughter who has reached the age for marriage.&lt;br&gt;
A lot of princes' from the neighboring nations are eager to marry your daughter. But, you have some basic criteria in mind that they should fulfill before they could be considered to marry your daughter.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The Prince should be able to fight. 'cause that's a necessary skill to have for a prince&lt;/li&gt;
&lt;li&gt;The Prince should like reading books. 'cause books contain knowledge. And you'd want your son-in-law to be well-educated and wise.&lt;/li&gt;
&lt;li&gt;The Prince should be able to speak &lt;a href="https://en.wikipedia.org/wiki/Klingon_language" rel="noopener noreferrer"&gt;Klingon&lt;/a&gt;. 'cause that's the sign of a superior man.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You write these requirements down on a big banner and place it above the castle gate.&lt;br&gt;
If a Prince comes and satisfies all these conditions, the gatekeeper awards him a 'Badass Prince' badge and sends him in.&lt;br&gt;
If, however, the prince doesn't meet these requirements, the gatekeeper of the castle won't even allow him inside.&lt;/p&gt;

&lt;p&gt;Now, this is exactly the idea behind interfaces.&lt;br&gt;
The banner with the requirements on it is an interface for a 'Badass Prince'. And by enforcing those requirements on the prospective Prince, you're essentially requiring them to implement the 'Badass Prince' interface.&lt;br&gt;
Thus stating, that if a Prince &lt;a href="https://stackoverflow.com/questions/2218937/has-a-is-a-terminology-in-object-oriented-language" rel="noopener noreferrer"&gt;is not a&lt;/a&gt; 'Badass Prince' (as defined by your interface), they're not eligible to marry your daughter.&lt;/p&gt;
&lt;h1&gt;
  
  
  The practical use of interfaces while writing software &lt;a&gt;
&lt;/a&gt;
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;If you know a class implements an interface, then you know that class contains concrete implementations of the methods declared in that interface, and you are guaranteed to be able to invoke these methods safely.&lt;/em&gt;&lt;br&gt;
— &lt;a href="https://stackoverflow.com/a/17163473/5968979" rel="noopener noreferrer"&gt;StackOverflow answer&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Often, different teams work together to develop a product. They may work on different features or segments that are integrated together to form the final product.&lt;br&gt;
In such a case, it is obvious that there will be certain inter-dependencies within the teams.&lt;/p&gt;

&lt;p&gt;Say for example, we're building an e-commerce website. One team is developing the 'Add-to-cart' feature (the process before checkout). And another team is working on the online-payment system (the process after checkout).&lt;br&gt;
We definitely can't make the online-payments-team wait and idle till the cart-team has finished and tested their feature.&lt;/p&gt;

&lt;p&gt;But then, if both the teams start working on their features in parallel, how do we ensure that we'll be able to integrate the online-payments with the cart? In the end, both the features should come together to form the final product, right?&lt;br&gt;
For example, the online-payments team needs the cart to be cleared on successful payment. But, since the cart is not ready yet, how would the payments-team do that? What function should they call? What parameters should they pass to it? What if the cart team doesn't implement such a function at all? And instead, you have to write logic to remove every product from the cart individually?&lt;/p&gt;

&lt;p&gt;To mitigate such problems, both the teams must brainstorm about the technical requirements of their own features and realize the inter-dependencies. Once that is done, both the teams can design appropriate interfaces.&lt;br&gt;
For example, the teams decide that on checkout, the cart-system will provide a MyCart object to the payments-system. This MyCart class must implement the Cart interface that has the following methods:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. void addProduct(Product product)
2. Product removeProduct(integer productId)
3. List&amp;lt;Product&amp;gt; getProductsFromCart()
4. boolean emptyCart()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If object provided by the cart-system does not come from a class that has implemented the Cart interface, then the payments-system will throw an Exception.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(!(&lt;/span&gt;&lt;span class="n"&gt;myCart&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nc"&gt;Cart&lt;/span&gt;&lt;span class="o"&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="nf"&gt;Exception&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Expected an instance of Cart"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Due to the principle of &lt;a href="http://www.edibleapple.com/2011/10/29/steve-jobs-explains-object-oriented-programming/" rel="noopener noreferrer"&gt;abstraction&lt;/a&gt; in object oriented programming, the payments-team does not need to know how the emptyCart method works. They just need to know:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;that it exists&lt;/li&gt;
&lt;li&gt;that it is intended to do what they want (clear all products in the cart)&lt;/li&gt;
&lt;li&gt;how to call it (what parameters to pass and what to expect in return).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Out of these, interfaces take care of points 1 and 3. Point 2 is left to the human intellect.&lt;/p&gt;

&lt;p&gt;Basically, by asking the cart-team to implement the Cart interface, the payments-team says,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"Hey cart-team,&lt;/em&gt;&lt;br&gt;
&lt;em&gt;We are not concerned about the logic and implementation on your side.&lt;/em&gt;&lt;br&gt;
&lt;em&gt;But, we require certain functionality to be present in the object that you pass to us. And we require it in a very specific manner (with so and so name, so and so parameters, so and so value returned).&lt;/em&gt;&lt;br&gt;
&lt;em&gt;Please implement these exactly as specified so that we can just plug your cart-system into our payment-system. And we can have a seamless integration."&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This was just a simple example of when an interface is used. There might be more. But, I hope it helps you to understand what an important role the interfaces play in software development.&lt;br&gt;
If you can think of more examples, please let me know in the comments.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;In computing, an interface is a shared boundary across which two or more separate components of a computer system exchange information.&lt;/em&gt;&lt;br&gt;
&lt;em&gt;...&lt;/em&gt;&lt;br&gt;
&lt;em&gt;An interface is thus a type definition; anywhere an object can be exchanged (for example, in a function or method call) the type of the object to be exchanged can be defined in terms of one of its implemented interfaces or base-classes rather than specifying the specific class.&lt;/em&gt;&lt;br&gt;
— &lt;a href="https://en.wikipedia.org/wiki/Interface_%28computing%29#In_object-oriented_languages" rel="noopener noreferrer"&gt;Wikipedia&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Also, a lot of people relate interfaces with legal contracts. This is because interfaces are based on mutual agreement between two parties; on how one party could use the services of another.&lt;br&gt;
You meet the requirements in the contract. And you shall enjoy our uninterrupted services.&lt;br&gt;
You fall short on the terms of the contract, we shall no longer serve you.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;A contract is like a promise between people. It is an understanding, a deal between two or more people or organizations to do certain things.&lt;/em&gt;&lt;br&gt;
— &lt;a href="https://www.artslaw.com.au/legal/raw-law/what-is-a-contract/" rel="noopener noreferrer"&gt;Arts + Law article&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  To conclude.. &lt;a&gt;
&lt;/a&gt;
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;Multiple inheritance is problematic.&lt;/li&gt;
&lt;li&gt;Java novices often wrongly associate interfaces with multiple inheritance. That's not what they are for.&lt;/li&gt;
&lt;li&gt;An interface is actually a pretty powerful construct that lets you define the requirements of your software component. It also lets you enforce that other software components, that interact with yours, meet those requirements.&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Official Oracle documentation &lt;a&gt;
&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;Oracle has provided some really awesome documentation on interfaces. So, please check out the following links and look around.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.oracle.com/javase/tutorial/java/concepts/interface.html" rel="noopener noreferrer"&gt;What is an Interface&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html" rel="noopener noreferrer"&gt;Interface Tutorial&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>java</category>
      <category>oop</category>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>The odyssey of asynchronous JavaScript</title>
      <dc:creator>Gaurang</dc:creator>
      <pubDate>Wed, 30 Oct 2019 16:34:08 +0000</pubDate>
      <link>https://dev.to/gaurang847/the-odyssey-of-asynchronous-javascript-jlf</link>
      <guid>https://dev.to/gaurang847/the-odyssey-of-asynchronous-javascript-jlf</guid>
      <description>&lt;h1&gt;
  
  
  Table of Contents
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;The lead-up&lt;/li&gt;
&lt;li&gt;
Events, event-handlers and callbacks

&lt;ul&gt;
&lt;li&gt;Issue with callbacks&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Promises&lt;/li&gt;

&lt;li&gt;

Co-routines

&lt;ul&gt;
&lt;li&gt;Generators&lt;/li&gt;
&lt;li&gt;Co-routines&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Async/await&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  The lead-up &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;When I was learning JavaScript (about 1-2 years back), my mentor had me go through it step-by-step. So, first I spent some time to get comfortable with callbacks. Then I jumped onto Promises. And then, after months, I started using Async-await.&lt;/p&gt;

&lt;p&gt;Due to this reason, I was exposed to a number of flow-control methods and practises that evolved around JavaScript; which I would've missed otherwise - simply because of the fact that I wasn't part of that generation.&lt;/p&gt;

&lt;p&gt;Just like how our grandparents complain about how easy our generation has it due to the existence of internet, mobiles, electronic devices, etc. I strongly believe that in the next 2-3 years, we'll complain about how easy the JS-newcomers have it since they do not have to deal with callback-hell and all the other struggles of the &lt;em&gt;"pre-Promise" era&lt;/em&gt;. To them, it'll probably just be a textbook paragraph on the history of JavaScript that no one really cares about; except for the compulsory 1 mark question that is asked from it.&lt;/p&gt;

&lt;p&gt;When I was in college, I had no idea what &lt;em&gt;'asynchronous'&lt;/em&gt; meant. Coming from the world of C++, PHP, and Java, the word 'asynchronous' was completely alien to me. I had vague understanding of multi-threading in Java and I dreaded it. I've come a long way from there! 😌&lt;/p&gt;

&lt;p&gt;My intention of writing this article is simple.&lt;br&gt;
It is my humble attempt to immortalize the evolution of writing in JavaScript before it's too late and forgotten; in a way that even non-JS people can appreciate it. Even if they don't completely understand the specifics, as they're not familiar with JavaScript constructs, I'm trying to keep it so that they can at least get the general idea.&lt;br&gt;
Yet, if something doesn't make sense, or you want to talk more about it, feel free to reach out.&lt;/p&gt;
&lt;h2&gt;
  
  
  Events, event-handlers and callbacks. &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;This is from the pre-historic times.&lt;br&gt;
If you have enough experience, you must've definitely come across event-driven systems - Visual Basic, &lt;code&gt;OnClickListener()&lt;/code&gt; in Android, &lt;code&gt;onchange&lt;/code&gt; behavior in HTML, etc.&lt;br&gt;
Since node is primarily an event-based runtime environment, all it had initially was &lt;em&gt;events&lt;/em&gt; and &lt;em&gt;event-handlers&lt;/em&gt;.&lt;br&gt;
Event-handlers are just functions that are triggered once a certain event is fired/emitted. Just like the &lt;code&gt;onChange&lt;/code&gt; behaviour in HTML.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Simply put, it means something that occurs after an unknown amount of time, so don't expect immediate results.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;For example, "Mom, can I have five dollars?"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Putting my hand out for money is me expecting her to immediately respond by giving me money (synchronous).&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Realistically, she will look at me for a moment or two, and then decide to respond when she wants to (asynchronous).&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;— &lt;a href="https://stackoverflow.com/a/4559139/5968979" rel="noopener noreferrer"&gt;Kai (StackOverflow)&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Due to the asynchronous nature of JS, the system wouldn't wait while, say, you get some data from a database (It was really difficult to wrap my head around and get used to this initially).&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;However, &lt;em&gt;events&lt;/em&gt; enable you to put your work on hold when Node.js realises that it is an &lt;em&gt;async task&lt;/em&gt; you're performing; and then lets you resume your work when the task has completed and data is available.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In JavaScript, functions can be passed as arguments to other functions and functions can return functions. Such functions are called &lt;a href="https://hackernoon.com/effective-functional-javascript-first-class-and-higher-order-functions-713fde8df50a" rel="noopener noreferrer"&gt;&lt;em&gt;higher-order functions&lt;/em&gt;&lt;/a&gt; - similar to how a person who manages other people under him is considered to be at a higher level or position.&lt;br&gt;
Thus, a pattern emerged where a function will be passed as the last parameter to an asynchronous function; called a &lt;em&gt;callback function&lt;/em&gt;.&lt;br&gt;
Under the hood, this function would become the &lt;em&gt;event-handler&lt;/em&gt; for the concerned event.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h4&gt;
  
  
  Issue with callbacks. &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;There are hardly any practical applications that may not involve async operations.&lt;br&gt;
The advantage of using Node.js is that time-consuming async operations don't affect the performance of your server. The server won't hold off (or &lt;em&gt;starve&lt;/em&gt;) one request till another one is completely processed and its response is sent.&lt;br&gt;
As soon as Node.js realizes that an async operation is to be performed, it'll delegate a worker process to handle the operation and immediately start processing the next request. This gives a terrific boost to the speed of the system.&lt;br&gt;
If your server is getting a lot of requests, and each request requires some async operation (say, database queries), this turns out to be significantly efficient.&lt;/p&gt;

&lt;p&gt;However, this efficiency came at a great cost. Writing industry-grade applications with just events, event-handlers and callbacks is not easy.&lt;br&gt;
&lt;a href="http://callbackhell.com" rel="noopener noreferrer"&gt;Callback-hell&lt;/a&gt; is the biggest problem with callbacks that leads to decreased code-extensibility, reusability and manageability.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
Coming from the object-oriented background of Java, I found it very difficult to get used to writing code involving callbacks - how you have to split the code into a separate function, the callback function.&lt;br&gt;
The struggle was real during that time.

&lt;p&gt;Frustrated by writing asynchronous code with callbacks, developers started finding creative ways to write better, cleaner code.&lt;br&gt;
For example, we used to use &lt;a href="https://caolan.github.io/async/v3/" rel="noopener noreferrer"&gt;async.io&lt;/a&gt; at my workplace. It has utility methods like &lt;a href="https://caolan.github.io/async/v3/docs.html#series" rel="noopener noreferrer"&gt;&lt;code&gt;async.series()&lt;/code&gt;&lt;/a&gt;, &lt;a href="https://caolan.github.io/async/v3/docs.html#parallel" rel="noopener noreferrer"&gt;&lt;code&gt;async.parallel()&lt;/code&gt;&lt;/a&gt;, &lt;a href="https://caolan.github.io/async/v3/docs.html#waterfall" rel="noopener noreferrer"&gt;&lt;code&gt;async.waterfall()&lt;/code&gt;&lt;/a&gt;, etc.&lt;br&gt;
&lt;code&gt;async.waterfall()&lt;/code&gt; is the most interesting one according to me. It lets you chain async functions together so that one function's output is the next function's input - kind of like the human centipede but with functions. 😅&lt;/p&gt;
&lt;h2&gt;
  
  
  Promises &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Promises were introduced in &lt;a href="https://www.ecma-international.org/ecma-262/6.0/#sec-promise-objects" rel="noopener noreferrer"&gt;ES6 (2015)&lt;/a&gt;. Until then, people only had callbacks.&lt;br&gt;
Promises were the next step from callbacks. A major step that brought a revolution in the way we worked with Node.js. Consider it the industrial revolution of JavaScript.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value.&lt;/em&gt;&lt;br&gt;
 — &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" rel="noopener noreferrer"&gt;MDN web docs&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A promise is really just a wrapper around callbacks. An ingenious wrapper where we make a shift from using functions for storing the next code to using an object.&lt;br&gt;
The next function to call (the callback), instead of passing it into a function, we attach it to an object - the promise object.&lt;br&gt;
This object is then responsible to pass the callback function as an event handler to the concerned event.&lt;/p&gt;

&lt;p&gt;You can instantiate a promise object from any callback based function. Thus, you can always go from a function-based approach to an object-based one.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The significance of this is that your code turns from nested blocks of callbacks to a linear chain of &lt;code&gt;.then&lt;/code&gt;-ables.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
It is lot more easy to make modifications to your code when it is written in linear sequential manner (the very reason we love synchronous code) than when it is written in nested blocks.&lt;br&gt;
Your code instantly becomes readable, predictable and 200x more manageable.

&lt;p&gt;Read this article for more information on Promises:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag__link"&gt;
  &lt;a href="https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-promise-27fc71e77261" class="ltag__link__link" rel="noopener noreferrer"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fv2%2Fresize%3Afill%3A64%3A64%2F1%2AVZfJFJj5oVmZ5WzlrgSmRg.jpeg" alt="Eric Elliott"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-promise-27fc71e77261" class="ltag__link__link" rel="noopener noreferrer"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Master the JavaScript Interview: What is a Promise? | by Eric Elliott | JavaScript Scene | Medium&lt;/h2&gt;
      &lt;h3&gt;Eric Elliott ・ &lt;time&gt;Aug 24, 2021&lt;/time&gt; ・ 
      &lt;div class="ltag__link__servicename"&gt;
        &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.dev.to%2Fassets%2Fmedium-f709f79cf29704f9f4c2a83f950b2964e95007a3e311b77f686915c71574fef2.svg" alt="Medium Logo"&gt;
        Medium
      &lt;/div&gt;
    &lt;/h3&gt;
&lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;
&lt;br&gt;
If the Promise object sounded like magic, and you are interested in understanding its internal working, you might be interested in &lt;a href="https://www.mattgreer.org/articles/promises-in-wicked-detail/" rel="noopener noreferrer"&gt;this article&lt;/a&gt;.
&lt;h2&gt;
  
  
  Co-routines &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;
&lt;h4&gt;
  
  
  Generators &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;Generators were introduced in &lt;a href="https://www.ecma-international.org/ecma-262/6.0/#sec-generator-objects" rel="noopener noreferrer"&gt;ES6 (2015)&lt;/a&gt; along with promises. But, I believe not many people know about them or use them often.&lt;br&gt;
They are functions that return &lt;em&gt;generator objects&lt;/em&gt;.&lt;br&gt;
A generator object is an &lt;em&gt;iterator&lt;/em&gt;.&lt;br&gt;
An iterator is anything that implements &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols" rel="noopener noreferrer"&gt;&lt;em&gt;the iterator protocol&lt;/em&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The iterator protocol says that an object can be called an &lt;em&gt;iterator&lt;/em&gt; if it has the &lt;code&gt;next()&lt;/code&gt; method that is supposed to do a very specific job; get the next value of iteration/sequence. If you're familiar with &lt;a href="https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html" rel="noopener noreferrer"&gt;Scanner&lt;/a&gt; in Java, it's an Iterator (Although it breaks &lt;a href="https://stackoverflow.com/questions/31153006/why-does-scanner-implement-iteratorstring" rel="noopener noreferrer"&gt;Java design priniciples&lt;/a&gt;)&lt;br&gt;
&lt;/p&gt;
&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
//Simplest example of a custom iterator
function myIterator(){
    let a = 0;
    return {next: function(){return a++}}
}
let it = myIterator();
console.log(it.next());
console.log(it.next());
console.log(it.next());

  &lt;/code&gt;
&lt;/div&gt;
&lt;br&gt;
So, a generator object is basically an object that has this &lt;code&gt;next()&lt;/code&gt; method&lt;br&gt;
And generator functions are just functions that return generator objects. If you've ever used &lt;a href="https://www.geeksforgeeks.org/range-vs-xrange-python/" rel="noopener noreferrer"&gt;&lt;code&gt;xrange()&lt;/code&gt;&lt;/a&gt; in Python 2.x, that's literally a generator.&lt;br&gt;
A very good example for generators will be a Fibonacci generator.&lt;br&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
Read the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators" rel="noopener noreferrer"&gt;Mozilla docs&lt;/a&gt; for more information on generators and iterators.&lt;br&gt;
Also, this in-detail post on generators on Medium:&lt;br&gt;
&lt;div class="ltag__link"&gt;
  &lt;a href="https://medium.com/dailyjs/a-simple-guide-to-understanding-javascript-es6-generators-d1c350551950" class="ltag__link__link" rel="noopener noreferrer"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fv2%2Fresize%3Afill%3A64%3A64%2F1%2Adk4GVL5DORk8OyceL7EzkA.png" alt="Rajesh Babu"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://medium.com/dailyjs/a-simple-guide-to-understanding-javascript-es6-generators-d1c350551950" class="ltag__link__link" rel="noopener noreferrer"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;A Simple Guide to Understanding Javascript (ES6) Generators | by Rajesh Babu | DailyJS | Medium&lt;/h2&gt;
      &lt;h3&gt;Rajesh Babu ・ &lt;time&gt;Mar 29, 2023&lt;/time&gt; ・ 
      &lt;div class="ltag__link__servicename"&gt;
        &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.dev.to%2Fassets%2Fmedium-f709f79cf29704f9f4c2a83f950b2964e95007a3e311b77f686915c71574fef2.svg" alt="Medium Logo"&gt;
        Medium
      &lt;/div&gt;
    &lt;/h3&gt;
&lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;h4&gt;
  
  
  Co-routines &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;Now that we know what generators are, we make &lt;em&gt;coroutines&lt;/em&gt; simply by adding promises to the mix.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
Please note that the code has started looking very similar to its synchronous equivalent. It just needs some supplementary part.&lt;br&gt;
To take care of that, people came up with a few coroutine libraries such as &lt;a href="https://github.com/tj/co" rel="noopener noreferrer"&gt;CO&lt;/a&gt;.

&lt;p&gt;This part might have been pretty difficult to wrap your head around. It is pretty convoluted. But you might want to read this article if you're interested:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag__link"&gt;
  &lt;a href="https://medium.com/front-end-weekly/modern-javascript-and-asynchronous-programming-generators-yield-vs-async-await-550275cbe433" class="ltag__link__link" rel="noopener noreferrer"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fv2%2Fresize%3Afill%3A64%3A64%2F1%2Akuv1P0cZWtoP_F00Wp_jfg.jpeg" alt="Sergio Valverde"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://medium.com/front-end-weekly/modern-javascript-and-asynchronous-programming-generators-yield-vs-async-await-550275cbe433" class="ltag__link__link" rel="noopener noreferrer"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Modern Javascript and Asynchronous Programming: Generators/Yield vs. Async/Await | by Sergio Valverde | Frontend Weekly | Medium&lt;/h2&gt;
      &lt;h3&gt;Sergio Valverde ・ &lt;time&gt;Nov 9, 2018&lt;/time&gt; ・ 
      &lt;div class="ltag__link__servicename"&gt;
        &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.dev.to%2Fassets%2Fmedium-f709f79cf29704f9f4c2a83f950b2964e95007a3e311b77f686915c71574fef2.svg" alt="Medium Logo"&gt;
        Medium
      &lt;/div&gt;
    &lt;/h3&gt;
&lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;h2&gt;
  
  
  Async/await &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Soon, in &lt;a href="https://www.ecma-international.org/ecma-262/8.0/#sec-async-function-definitions" rel="noopener noreferrer"&gt;ES8 (2017)&lt;/a&gt;, &lt;a href="https://javascript.info/async-await" rel="noopener noreferrer"&gt;async-await&lt;/a&gt; was announced and that made writing coroutines redundant.&lt;br&gt;
Co-routines died out before they could become a thing. Many people today probably don’t even know about them.&lt;/p&gt;

&lt;p&gt;Async-await is just a wrapper around Promises. And again, a promise is just a wrapper around callbacks. So, in reality, promises and async-await are all just glamour. Under the skin, it's still callbacks everywhere!&lt;br&gt;
And yet, JS code now looks so clean, intuitive and manageable, that it's orgasmic!&lt;br&gt;
6 years back, nobody would've imagined we could write such clean code in JavaScript.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
This code looks exactly similar to the synchronous equivalent. And I’m awe-struck when I think about how much we hated callbacks, and how much we love structure that it led us from callbacks to async-await.&lt;br&gt;
I'm mesmerized by the transitions that happened around Node.js in such short span of time and I needed to talk about it.

&lt;p&gt;Now, the code looks really simple. Write your code using functions and when you're going to perform an async task, just use the &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;await&lt;/code&gt; keywords. Anybody can easily write asynchronous code in JavaScript now.&lt;br&gt;
But some times, things don't work as expected. Things that look simple and straight-forward often give unexpected results. And without enough understanding of the problem and the inherent system, one can go nuts in the process of debugging such errors.&lt;br&gt;
Happened with me once.&lt;br&gt;
&lt;iframe class="tweet-embed" id="tweet-1120390303440044033-173" src="https://platform.twitter.com/embed/Tweet.html?id=1120390303440044033"&gt;
&lt;/iframe&gt;

  // Detect dark theme
  var iframe = document.getElementById('tweet-1120390303440044033-173');
  if (document.body.className.includes('dark-theme')) {
    iframe.src = "https://platform.twitter.com/embed/Tweet.html?id=1120390303440044033&amp;amp;theme=dark"
  }



&lt;iframe class="tweet-embed" id="tweet-1120390582239682560-458" src="https://platform.twitter.com/embed/Tweet.html?id=1120390582239682560"&gt;
&lt;/iframe&gt;

  // Detect dark theme
  var iframe = document.getElementById('tweet-1120390582239682560-458');
  if (document.body.className.includes('dark-theme')) {
    iframe.src = "https://platform.twitter.com/embed/Tweet.html?id=1120390582239682560&amp;amp;theme=dark"
  }



&lt;br&gt;
My mentor probably understood that well. And that is why, he set me up on this journey to find and feel the true essence of Node.js.&lt;/p&gt;

&lt;p&gt;JS-veterans, if you find any inconsistencies in this piece, or would like to add more. Or simply want to talk, feel free to comment or DM me.&lt;br&gt;
JS-newbies and JS-virgins, I hope I've sparked an interest for the JS community in your minds. Feel free to reach out in case of any doubts.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>codenewbie</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
