<?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>Forem: Sivantha Paranavithana</title>
    <description>The latest articles on Forem by Sivantha Paranavithana (@sivantha96).</description>
    <link>https://forem.com/sivantha96</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%2F378832%2F18bc587f-6a79-4efd-ab7b-892a5b7b314e.jpg</url>
      <title>Forem: Sivantha Paranavithana</title>
      <link>https://forem.com/sivantha96</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/sivantha96"/>
    <language>en</language>
    <item>
      <title>The Art of Git Commits: Writing Messages That Your Future Self Will Thank You For</title>
      <dc:creator>Sivantha Paranavithana</dc:creator>
      <pubDate>Sun, 16 Feb 2025 14:47:54 +0000</pubDate>
      <link>https://forem.com/sivantha96/the-art-of-git-commits-writing-messages-that-your-future-self-will-thank-you-for-5bp7</link>
      <guid>https://forem.com/sivantha96/the-art-of-git-commits-writing-messages-that-your-future-self-will-thank-you-for-5bp7</guid>
      <description>&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;p&gt;Learn how to write meaningful Git commits that enhance collaboration, make code reviews easier, and help maintain a clean project history. We'll cover commit message structure, conventional commits, and tools to enforce commit standards.&lt;/p&gt;

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

&lt;p&gt;If you're reading this, you probably know that Git is the most widely used version control system. While most developers use Git daily, not everyone makes the most of its commit functionality. Let's dive into how we can level up our commit game.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Git Commit?
&lt;/h2&gt;

&lt;p&gt;Think of a Git commit as a snapshot of your project at a specific point in time. It's like saving your progress in a video game - you want to save after completing important missions, not randomly in the middle of one.&lt;/p&gt;

&lt;p&gt;Each commit represents:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A checkpoint in your project's timeline&lt;/li&gt;
&lt;li&gt;A record of what changed and why&lt;/li&gt;
&lt;li&gt;A reference point you can return to if needed&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Anatomy of a Good Commit
&lt;/h2&gt;

&lt;p&gt;Have you ever looked at your commit history and wondered, "What was I thinking?" You're not alone. A good commit message should tell a story about what changed and why.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bad Commit Messages:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fix stuff
updated code
final commit
final final commit
PLEASE WORK
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Good Commit Messages:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fix(auth): resolve JWT token expiration issue
feat(dashboard): add real-time data updates
docs: update API authentication guidelines
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Quick Tips for Better Commit Messages
&lt;/h2&gt;

&lt;p&gt;Here are some immediate improvements you can make to your commit messages:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Avoid Unnecessary Capitalization&lt;/strong&gt;&lt;br&gt;
Instead of "Added New Feature", use "add new feature"&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Double Check Spelling&lt;/strong&gt;&lt;br&gt;
Typos in commit messages make it harder to search history and look unprofessional&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Skip Punctuation in Summary Lines&lt;/strong&gt;&lt;br&gt;
Don't end your commit message summaries with periods or other punctuation&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These simple guidelines make your repository more visually appealing and easier to search through.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use the Imperative Mood
&lt;/h2&gt;

&lt;p&gt;A powerful trick for writing better commit messages is using the imperative verb form. Think of your commit message as completing this sentence:&lt;/p&gt;

&lt;p&gt;"If applied, this commit will..."&lt;/p&gt;

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

&lt;p&gt;❌ "fixed login bug"&lt;br&gt;
✅ "fix login bug"&lt;/p&gt;

&lt;p&gt;❌ "updated user documentation"&lt;br&gt;
✅ "update user documentation"&lt;/p&gt;

&lt;p&gt;Real-world example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;add new Kief-the-kraken Keanu-Kief to keif gallery

modify menu.yml and keif-gallery.md related to ticket #12345
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Commit Frequently
&lt;/h2&gt;

&lt;p&gt;One of the best ways to improve your commit messages is simply to commit more often. Here's why:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Smaller changes are easier to describe&lt;/li&gt;
&lt;li&gt;Each commit has a clear, single purpose&lt;/li&gt;
&lt;li&gt;You're less likely to lose work&lt;/li&gt;
&lt;li&gt;Code reviews become more manageable&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Imagine trying to write a commit message after a full day of uncommunicated changes - it's nearly impossible to be concise and clear. Instead, commit each meaningful change as you make it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Git Commit Workflow
&lt;/h2&gt;

&lt;p&gt;The journey to a perfect commit involves three main steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Make Changes&lt;/strong&gt;: Write your code and test it thoroughly&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Stage Changes&lt;/strong&gt;: Use &lt;code&gt;git add&lt;/code&gt; to select which changes to include&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Commit Changes&lt;/strong&gt;: Create your snapshot with a meaningful message&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Conventional Commits: A Standard That Makes Sense
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.conventionalcommits.org/en/v1.0.0/" rel="noopener noreferrer"&gt;Conventional Commits&lt;/a&gt; provide a structured format that makes commit histories readable and automated tools possible. This specification has become the de facto standard for many open-source projects and development teams.&lt;/p&gt;

&lt;p&gt;One of the most powerful aspects of Conventional Commits is its ability to automate various DevOps processes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Automated Changelog Generation&lt;/strong&gt;: Tools can parse commit messages to automatically create and update changelogs&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Version Management&lt;/strong&gt;: Following &lt;a href="https://semver.org/" rel="noopener noreferrer"&gt;Semantic Versioning (SemVer)&lt;/a&gt;, commit types directly correlate to version bumps:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;fix:&lt;/code&gt; commits trigger PATCH version increments (1.0.0 -&amp;gt; 1.0.1)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;feat:&lt;/code&gt; commits trigger MINOR version increments (1.0.0 -&amp;gt; 1.1.0)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;BREAKING CHANGE:&lt;/code&gt; commits trigger MAJOR version increments (1.0.0 -&amp;gt; 2.0.0)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Release Management&lt;/strong&gt;: Automated release notes and version tagging based on commit history&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;CI/CD Integration&lt;/strong&gt;: Automated deployment workflows based on commit types and scopes Here's the basic structure:
&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;type&amp;gt;[optional scope]: &amp;lt;description&amp;gt;

[optional body]

[optional footer(s)]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Common Types:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;feat&lt;/code&gt;: New features&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fix&lt;/code&gt;: Bug fixes&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docs&lt;/code&gt;: Documentation changes&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;style&lt;/code&gt;: Formatting changes&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;refactor&lt;/code&gt;: Code restructuring&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;test&lt;/code&gt;: Adding or modifying tests&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;chore&lt;/code&gt;: Maintenance tasks&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Real-world Example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;feat(payment): implement PayPal integration

Add PayPal as a payment option for checkout process.
This change includes:
- PayPal SDK integration
- Payment verification flow
- Success/failure handling

BREAKING CHANGE: Updates payment processor interface
Closes #123
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Git Hooks: Your Commit Quality Guardian
&lt;/h2&gt;

&lt;p&gt;Git hooks are scripts that run automatically before or after Git commands. They're perfect for enforcing commit message standards.&lt;br&gt;
We can run Commitlint as a commit hook to ensure the quality of our commit messages.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting Up Commitlint
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Install commitlint:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-g&lt;/span&gt; @commitlint/cli @commitlint/config-conventional
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Add configuration:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"module.exports = {extends: ['@commitlint/config-conventional']}"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; commitlint.config.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Install husky for Git hooks:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;husky &lt;span class="nt"&gt;--save-dev&lt;/span&gt;
npx husky &lt;span class="nb"&gt;install
&lt;/span&gt;npx husky add .husky/commit-msg &lt;span class="s1"&gt;'npx --no -- commitlint --edit $1'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can refer to &lt;a href="https://commitlint.js.org/" rel="noopener noreferrer"&gt;Commitlint Documentation&lt;/a&gt; for more details.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tools to Make Your Life Easier
&lt;/h2&gt;

&lt;p&gt;If you're using VS Code, the "Conventional Commits" extension by vivaxy is a game-changer. It provides:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Interactive commit message building&lt;/li&gt;
&lt;li&gt;Type and scope suggestions&lt;/li&gt;
&lt;li&gt;Conventional Commits specification compliance&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://marketplace.visualstudio.com/items?itemName=vivaxy.vscode-conventional-commits" rel="noopener noreferrer"&gt;Install the extension here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Long-Term Benefits
&lt;/h2&gt;

&lt;p&gt;Investing time in writing quality commit messages pays off in numerous ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Easier debugging when issues arise&lt;/li&gt;
&lt;li&gt;Smoother code reviews&lt;/li&gt;
&lt;li&gt;Better project documentation&lt;/li&gt;
&lt;li&gt;Clearer project history&lt;/li&gt;
&lt;li&gt;More efficient team collaboration&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Remember: The time you spend writing good commit messages is an investment in your project's future maintainability.&lt;/p&gt;

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

&lt;p&gt;Writing good commit messages is an art that pays dividends in the long run. It makes collaboration smoother, code reviews more efficient, and debugging less painful. Start implementing these practices today, and your future self (and teammates) will thank you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Discussion
&lt;/h2&gt;

&lt;p&gt;What's your approach to writing commit messages? Share your best practices and tools in the comments below!&lt;/p&gt;




&lt;p&gt;&lt;em&gt;If you found this article helpful, consider following me for more development tips and best practices.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>git</category>
      <category>webdev</category>
      <category>programming</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Git Branching - A Production-Grade Branching Strategy for Enterprise Teams</title>
      <dc:creator>Sivantha Paranavithana</dc:creator>
      <pubDate>Thu, 13 Feb 2025 04:26:14 +0000</pubDate>
      <link>https://forem.com/sivantha96/git-branching-a-production-grade-branching-strategy-for-enterprise-teams-hao</link>
      <guid>https://forem.com/sivantha96/git-branching-a-production-grade-branching-strategy-for-enterprise-teams-hao</guid>
      <description>&lt;p&gt;Enterprise teams need robust, battle-tested branching strategies to handle complex release cycles while maintaining code stability. In this guide, I'll share a production-grade modification of the Gitflow branching model that has proven successful in enterprise environments.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Another Gitflow Guide? 🤔
&lt;/h2&gt;

&lt;p&gt;While the &lt;a href="https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow" rel="noopener noreferrer"&gt;classic Gitflow model &lt;/a&gt; is excellent, enterprise teams often need more control over their release process, especially when dealing with multiple environments and strict QA requirements. This modified approach introduces dedicated QA and staging branches, making it perfect for teams that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Require thorough testing before production&lt;/li&gt;
&lt;li&gt;Manage multiple release environments&lt;/li&gt;
&lt;li&gt;Need strict control over what goes into production&lt;/li&gt;
&lt;li&gt;Handle frequent hotfixes alongside regular feature development&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Branch Structure 🌿
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Core Branches
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Main Branch (&lt;code&gt;main&lt;/code&gt;)
&lt;/h4&gt;

&lt;p&gt;The source of truth for your production code. Think of it as your production environment's mirror.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ Contains only production-ready code&lt;/li&gt;
&lt;li&gt;🚫 Never commit directly here&lt;/li&gt;
&lt;li&gt;🏷️ All production releases are tagged&lt;/li&gt;
&lt;li&gt;🔧 Source for hotfix branches&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Development Branch (&lt;code&gt;develop&lt;/code&gt;)
&lt;/h4&gt;

&lt;p&gt;Your integration hub where features come together.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;🔄 Latest development changes live here&lt;/li&gt;
&lt;li&gt;👥 First stop for all new features&lt;/li&gt;
&lt;li&gt;🧪 Integration testing happens here&lt;/li&gt;
&lt;li&gt;🚫 Not for direct feature branching&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  QA Branch (&lt;code&gt;release/qa&lt;/code&gt;)
&lt;/h4&gt;

&lt;p&gt;Your dedicated testing environment.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;🧪 QA team's workspace&lt;/li&gt;
&lt;li&gt;⬆️ Updated from &lt;code&gt;develop&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;✅ Verification checkpoint&lt;/li&gt;
&lt;li&gt;🚫 No feature branching allowed&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Staging Branch (&lt;code&gt;release/staging&lt;/code&gt;)
&lt;/h4&gt;

&lt;p&gt;The final stop before production.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;🎭 Pre-production environment&lt;/li&gt;
&lt;li&gt;✨ Contains release-ready features&lt;/li&gt;
&lt;li&gt;🔍 Final verification point&lt;/li&gt;
&lt;li&gt;🎯 Merges to &lt;code&gt;main&lt;/code&gt; for releases&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Working Branches
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Feature Branches (&lt;code&gt;feature/*&lt;/code&gt;)
&lt;/h4&gt;

&lt;p&gt;Where new features come to life.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Creating a feature branch&lt;/span&gt;
git checkout main
git checkout &lt;span class="nt"&gt;-b&lt;/span&gt; feature/awesome-new-feature
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Hotfix Branches (&lt;code&gt;hotfix/*&lt;/code&gt;)
&lt;/h4&gt;

&lt;p&gt;For those urgent production fixes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Creating a hotfix branch&lt;/span&gt;
git checkout main
git checkout &lt;span class="nt"&gt;-b&lt;/span&gt; hotfix/critical-bug-fix
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Bugfix Branches (&lt;code&gt;bugfix/*&lt;/code&gt;)
&lt;/h4&gt;

&lt;p&gt;For fixing issues in unreleased features.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Creating a bugfix branch&lt;/span&gt;
git checkout feature/parent-feature
git checkout &lt;span class="nt"&gt;-b&lt;/span&gt; bugfix/feature-bug-fix
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Real-World Scenarios 🎯
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Scenario 1: Developing a New Feature
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Create your feature branch:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git checkout main
git checkout &lt;span class="nt"&gt;-b&lt;/span&gt; feature/user-authentication
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Follow the PR flow:

&lt;ul&gt;
&lt;li&gt;First PR: Your branch → &lt;code&gt;develop&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;After merge: &lt;code&gt;develop&lt;/code&gt; → &lt;code&gt;release/qa&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;After QA: Your branch → &lt;code&gt;release/staging&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Release time: &lt;code&gt;release/staging&lt;/code&gt; → &lt;code&gt;main&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Scenario 2: Fixing Production Issues
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Create a hotfix branch:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git checkout main
git checkout &lt;span class="nt"&gt;-b&lt;/span&gt; hotfix/login-error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Follow the same PR flow as features&lt;/li&gt;
&lt;li&gt;Gets priority in the release cycle&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Golden Rules for Success 🌟
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Never:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;❌ Branch from &lt;code&gt;develop&lt;/code&gt; or &lt;code&gt;release/qa&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;❌ Pull from &lt;code&gt;develop&lt;/code&gt; into working branches&lt;/li&gt;
&lt;li&gt;❌ Mix unrelated changes in PRs&lt;/li&gt;
&lt;li&gt;❌ Commit directly to protected branches&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Always:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;✅ Branch from &lt;code&gt;main&lt;/code&gt; for new work&lt;/li&gt;
&lt;li&gt;✅ Keep changes focused and atomic&lt;/li&gt;
&lt;li&gt;✅ Follow the proper environment flow&lt;/li&gt;
&lt;li&gt;✅ Maintain a clean commit history&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Pro Tips for Team Success 💡
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Branch Hygiene&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Delete merged branches promptly&lt;/li&gt;
&lt;li&gt;Use clear, descriptive branch names&lt;/li&gt;
&lt;li&gt;Keep branches focused on single features&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Commit Practices&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Write meaningful commit messages&lt;/li&gt;
&lt;li&gt;Commit regularly but thoughtfully&lt;/li&gt;
&lt;li&gt;Squash commits before merging&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Release Management&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use semantic versioning&lt;/li&gt;
&lt;li&gt;Tag all production releases&lt;/li&gt;
&lt;li&gt;Maintain a changelog&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Tools to Make Your Life Easier 🛠️
&lt;/h2&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Git Aliases&lt;/strong&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Add to your .gitconfig&lt;/span&gt;
&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;alias&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
    feature &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"!f() { git checkout main &amp;amp;&amp;amp; git checkout -b feature/&lt;/span&gt;&lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="s2"&gt;; }; f"&lt;/span&gt;
    hotfix &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"!f() { git checkout main &amp;amp;&amp;amp; git checkout -b hotfix/&lt;/span&gt;&lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="s2"&gt;; }; f"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;Branch Protection Rules&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Require PR reviews&lt;/li&gt;
&lt;li&gt;Enforce status checks&lt;/li&gt;
&lt;li&gt;Lock protected branches&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Although this modified Gitflow approach might initially seem complex, it provides a robust foundation for enterprise development. It's battle-tested and scales well with team size and project complexity.&lt;/p&gt;

&lt;p&gt;Remember: The goal isn't to follow these rules blindly but to understand and adapt them to your team's needs while maintaining code quality and release stability.&lt;/p&gt;

&lt;h2&gt;
  
  
  Share Your Thoughts! 💭
&lt;/h2&gt;

&lt;p&gt;How does your team handle branching strategies? Have you modified Gitflow for your needs? Share your experiences in the comments below!&lt;/p&gt;

</description>
      <category>git</category>
      <category>github</category>
      <category>devops</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Node.js E2E Testing with Cypress: A Comprehensive Setup Guide</title>
      <dc:creator>Sivantha Paranavithana</dc:creator>
      <pubDate>Wed, 12 Feb 2025 07:35:54 +0000</pubDate>
      <link>https://forem.com/sivantha96/nodejs-e2e-testing-with-cypress-a-comprehensive-setup-guide-1287</link>
      <guid>https://forem.com/sivantha96/nodejs-e2e-testing-with-cypress-a-comprehensive-setup-guide-1287</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;End-to-end testing is crucial for ensuring your Node.js applications work flawlessly from the user's perspective. In this comprehensive guide, we'll walk through setting up Cypress for e2e testing in a Node.js Express project, complete with code coverage, Docker-based test databases, and best practices for organizing your test suites.&lt;/p&gt;

&lt;h2&gt;
  
  
  What You'll Learn 🎯
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Setting up Cypress with Node.js and Express&lt;/li&gt;
&lt;li&gt;Configuring test databases using Docker&lt;/li&gt;
&lt;li&gt;Implementing code coverage with NYC&lt;/li&gt;
&lt;li&gt;Organizing test suites effectively&lt;/li&gt;
&lt;li&gt;Writing maintainable test cases&lt;/li&gt;
&lt;li&gt;TypeScript integration&lt;/li&gt;
&lt;li&gt;Best practices for e2e testing&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;Before we dive in, make sure you have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Node.js installed (v14 or later)&lt;/li&gt;
&lt;li&gt;Basic understanding of Express.js&lt;/li&gt;
&lt;li&gt;Docker installed (for test databases)&lt;/li&gt;
&lt;li&gt;Your favorite code editor (VSCode recommended)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Setting Up Your Testing Environment
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Installing Essential Dependencies
&lt;/h3&gt;

&lt;p&gt;First, let's install the necessary packages:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;yarn add &lt;span class="nt"&gt;-D&lt;/span&gt; cypress
yarn add &lt;span class="nt"&gt;-D&lt;/span&gt; nyc
yarn add &lt;span class="nt"&gt;-D&lt;/span&gt; ts-node
yarn add &lt;span class="nt"&gt;-D&lt;/span&gt; @cypress/code-coverage
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Docker-Based Test Database Setup
&lt;/h3&gt;

&lt;p&gt;For consistent testing environments, we'll use Docker. Here's how to set up both MySQL and MongoDB:&lt;/p&gt;

&lt;h4&gt;
  
  
  MySQL Configuration:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# docker-compose.yml&lt;/span&gt;
&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;test-db&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;mysql:8.0&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;MYSQL_ROOT_PASSWORD&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${DB_PASSWORD}&lt;/span&gt;
      &lt;span class="na"&gt;MYSQL_DATABASE&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${DB_NAME}&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;${DB_PORT}:3306&lt;/span&gt;
    &lt;span class="na"&gt;command&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;--default-authentication-plugin=mysql_native_password&lt;/span&gt;
    &lt;span class="na"&gt;healthcheck&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;test&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;CMD'&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;mysqladmin'&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;ping'&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;-h'&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;localhost'&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
      &lt;span class="na"&gt;timeout&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;20s&lt;/span&gt;
      &lt;span class="na"&gt;retries&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  MongoDB Configuration:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# docker-compose.yml&lt;/span&gt;
&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;mongodb&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;mongo:latest&lt;/span&gt;
    &lt;span class="na"&gt;command&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;mongod --replSet rs0 --bind_ip_all&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;MONGO_INITDB_DATABASE&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${DB_NAME}&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;${DB_PORT}:27017&lt;/span&gt;
    &lt;span class="na"&gt;healthcheck&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;test&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;mongosh --eval "try { rs.status() } catch (err) { rs.initiate() }"&lt;/span&gt;
      &lt;span class="na"&gt;interval&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;10s&lt;/span&gt;
      &lt;span class="na"&gt;timeout&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;10s&lt;/span&gt;
      &lt;span class="na"&gt;retries&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Environment Configuration
&lt;/h3&gt;

&lt;p&gt;Create a &lt;code&gt;.env.test&lt;/code&gt; file for your test environment:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PORT=3000
ENV="test"
PACKAGE_NAME="my-api"
DB_USERNAME="root"
DB_PASSWORD="root"
DB_HOST="127.0.0.1"
DB_PORT=3300
DB_NAME="test-db"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Configuring Code Coverage with NYC
&lt;/h2&gt;

&lt;p&gt;NYC (Istanbul) provides detailed code coverage metrics. Here's how to set it up:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;.nycrc&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"all"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"include"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"src/**/*.ts"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"exclude"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"**/*.spec.ts"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"**/*.test.ts"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"**/*.cy.ts"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"coverage/**"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"extension"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;".ts"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"reporter"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"json"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"lcov"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"html"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"report-dir"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"coverage/cypress"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"temp-dir"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;".nyc_output"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Cypress Configuration
&lt;/h3&gt;

&lt;p&gt;Create a &lt;code&gt;cypress.config.ts&lt;/code&gt; file to configure Cypress with code coverage:&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;defineConfig&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cypress&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;path&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;environment&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./src/config/env.config&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;defineConfig&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;e2e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setupNodeEvents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@cypress/code-coverage/task&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;config&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;config&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;baseUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`http://localhost:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;environment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;/api`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;packageName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;environment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;packageName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;packageVersion&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;environment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;packageVersion&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;environment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;apiKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;environment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;apiKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;dbURI&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;environment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dbURI&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;codeCoverage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`http://localhost:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;environment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;/__coverage__`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;expectBackendCoverageOnly&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;defaultCommandTimeout&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;screenshotOnRunFailure&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;video&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Organizing Your Cypress Tests
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Project Structure
&lt;/h3&gt;

&lt;p&gt;Here's a clean, maintainable structure for your Cypress tests:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cypress/
├── constants/          # Test data and constants
├── e2e/               # Test files
├── plugins/           # Cypress plugins
├── support/           # Commands and types
└── tsconfig.json      # TypeScript config
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Writing Effective Test Suites
&lt;/h3&gt;

&lt;p&gt;Let's look at a real-world example of a well-structured test suite:&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="nf"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;POST /v1/users&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;apiKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dbURI&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Cypress&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;env&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="nf"&gt;beforeEach&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;cy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;task&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cleanupUsers&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;dbURI&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;should create a user with required fields&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;cy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;request&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/v1/users&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;x-api-key&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;apiKey&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John Doe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;john@example.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;to&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;201&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;to&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;have&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;property&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="c1"&gt;// More test cases...&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Custom Commands
&lt;/h3&gt;

&lt;p&gt;Create reusable test operations:&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;// commands/users.commands.ts&lt;/span&gt;
&lt;span class="nx"&gt;Cypress&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Commands&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;createUser&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;cy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;request&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/v1/users&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;x-api-key&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Cypress&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;env&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;apiKey&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;userData&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;failOnStatusCode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Best Practices for E2E Testing
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Clean State for Each Test&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;beforeEach&lt;/code&gt; hooks to reset the database&lt;/li&gt;
&lt;li&gt;Avoid test interdependence&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Organized Test Structure&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Group related tests in describe blocks&lt;/li&gt;
&lt;li&gt;Use meaningful test descriptions&lt;/li&gt;
&lt;li&gt;Follow the AAA pattern (Arrange, Act, Assert)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Error Handling&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Test both success and failure scenarios&lt;/li&gt;
&lt;li&gt;Validate error messages and status codes&lt;/li&gt;
&lt;li&gt;Check edge cases&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Performance Considerations&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use appropriate timeouts&lt;/li&gt;
&lt;li&gt;Minimize database operations&lt;/li&gt;
&lt;li&gt;Implement parallel test execution when possible&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Common Pitfalls and Solutions
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Database Connection Issues&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Always check your connection strings&lt;/li&gt;
&lt;li&gt;Implement proper error handling&lt;/li&gt;
&lt;li&gt;Use connection pooling&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Flaky Tests&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add proper wait conditions&lt;/li&gt;
&lt;li&gt;Don't rely on arbitrary timeouts&lt;/li&gt;
&lt;li&gt;Use Cypress's built-in retry mechanism&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  TypeScript Integration
&lt;/h2&gt;

&lt;p&gt;Ensure your &lt;code&gt;tsconfig.json&lt;/code&gt; includes these essential compiler options:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"compilerOptions"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"target"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"es5"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"lib"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"es5"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"dom"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"types"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"cypress"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"node"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"esModuleInterop"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"sourceMap"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"include"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"**/*.ts"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Setting up end-to-end testing with Cypress in your Node.js applications might seem daunting at first, but with proper organization and best practices, it becomes a powerful tool in your testing arsenal. Remember to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Keep your test environment consistent&lt;/li&gt;
&lt;li&gt;Maintain clean and organized test code&lt;/li&gt;
&lt;li&gt;Follow best practices for reliable tests&lt;/li&gt;
&lt;li&gt;Regular monitoring of code coverage&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.cypress.io/" rel="noopener noreferrer"&gt;Official Cypress Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/goldbergyoni/nodebestpractices#6-testing-and-overall-quality-practices" rel="noopener noreferrer"&gt;Node.js Testing Best Practices&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://istanbul.js.org/" rel="noopener noreferrer"&gt;Istanbul (NYC) Documentation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What's Next?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Implementing CI/CD with Cypress&lt;/li&gt;
&lt;li&gt;Advanced test patterns&lt;/li&gt;
&lt;li&gt;Visual regression testing&lt;/li&gt;
&lt;li&gt;API mocking strategies&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Happy testing! 🚀&lt;/p&gt;

</description>
      <category>node</category>
      <category>cypress</category>
      <category>testing</category>
      <category>webdev</category>
    </item>
    <item>
      <title>API Endpoint Design: CRUD vs Action-Specific Endpoints - A Developer's Guide</title>
      <dc:creator>Sivantha Paranavithana</dc:creator>
      <pubDate>Tue, 11 Feb 2025 13:57:59 +0000</pubDate>
      <link>https://forem.com/sivantha96/api-endpoint-design-crud-vs-action-specific-endpoints-a-developers-guide-1eig</link>
      <guid>https://forem.com/sivantha96/api-endpoint-design-crud-vs-action-specific-endpoints-a-developers-guide-1eig</guid>
      <description>&lt;h2&gt;
  
  
  API Endpoint Design: The Great Debate 🤔
&lt;/h2&gt;

&lt;p&gt;Hey there, fellow developers! 👋 Today, we're diving deep into one of the most crucial decisions you'll make when designing APIs: choosing between CRUD endpoints and action-specific endpoints. Let's break this down in a way that will help you make better architectural decisions for your next project.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Two Contenders in the Ring 🥊
&lt;/h2&gt;

&lt;p&gt;Before we jump into the nitty-gritty, let's understand what we're comparing:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. The CRUD Champion
&lt;/h3&gt;

&lt;p&gt;These are your traditional REST-style endpoints that follow the Create, Read, Update, Delete pattern. Think of them as your Swiss Army knife for resource manipulation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;GET    /api/resources          # List all the things!
POST   /api/resources          # Create new stuff
GET    /api/resources/{id}     # Get that specific thing
PUT    /api/resources/{id}     # Update all the things
PATCH  /api/resources/{id}     # Update just some things
DELETE /api/resources/{id}     # Goodbye, thing!
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. The Action-Specific Challenger
&lt;/h3&gt;

&lt;p&gt;These endpoints are like specialized tools, each designed for a specific job. They're more explicit about what they do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;POST /api/resources/{id}/activate    # Power up! ⚡
POST /api/resources/{id}/deactivate  # Power down! 💤
POST /api/resources/{id}/archive     # Into the vault! 📦
POST /api/resources/{id}/publish     # Show time! 🎉
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The CRUD Approach: Pros and Cons 📊
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What's Awesome About CRUD
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Follows REST conventions like a well-behaved API should&lt;/li&gt;
&lt;li&gt;Keeps your API surface clean and minimal&lt;/li&gt;
&lt;li&gt;Your code stays DRY (Don't Repeat Yourself)&lt;/li&gt;
&lt;li&gt;Documentation practically writes itself&lt;/li&gt;
&lt;li&gt;Flexibility to handle various update scenarios&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The Not-So-Great Parts
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Business operations can feel a bit... hidden&lt;/li&gt;
&lt;li&gt;Validation logic can get messy&lt;/li&gt;
&lt;li&gt;Authorization rules might feel shoehorned&lt;/li&gt;
&lt;li&gt;Sometimes exposes more than you want&lt;/li&gt;
&lt;li&gt;Business intent can get lost in translation&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Action-Specific Endpoints: The Good and The Bad 🎯
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The Good Stuff
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Crystal clear about what's happening&lt;/li&gt;
&lt;li&gt;Lock down those permissions like Fort Knox&lt;/li&gt;
&lt;li&gt;Validation rules that make sense&lt;/li&gt;
&lt;li&gt;Audit logs that tell the whole story&lt;/li&gt;
&lt;li&gt;Optimize all the things!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The Challenges
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;More endpoints = More maintenance&lt;/li&gt;
&lt;li&gt;Endpoint multiplication is real&lt;/li&gt;
&lt;li&gt;REST purists might raise an eyebrow&lt;/li&gt;
&lt;li&gt;Code reuse needs extra attention&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Real-World Example: Show Me The Money! 💰
&lt;/h2&gt;

&lt;p&gt;Let's look at a payment processing system because, hey, money talks!&lt;/p&gt;

&lt;h3&gt;
  
  
  The CRUD Way
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;PATCH /api/payments/{id}
Content-Type: application/json
{
    "status": "pending"
}
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The Action-Specific Way
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;POST /api/payments/{id}/mark-as-pending
Content-Type: application/json
{
    "reason": "awaiting_verification",
    "note": "Additional documentation required"
}
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Making The Choice: Your Decision Framework 🤔
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Go CRUD When:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You're just moving data around&lt;/li&gt;
&lt;li&gt;Business logic is straightforward&lt;/li&gt;
&lt;li&gt;Standard auth patterns work fine&lt;/li&gt;
&lt;li&gt;Side effects are minimal&lt;/li&gt;
&lt;li&gt;Resource states are simple&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Go Action-Specific When:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You're implementing business processes&lt;/li&gt;
&lt;li&gt;Complex rules are in play&lt;/li&gt;
&lt;li&gt;You need granular access control&lt;/li&gt;
&lt;li&gt;Side effects matter&lt;/li&gt;
&lt;li&gt;Audit trails are crucial&lt;/li&gt;
&lt;li&gt;State transitions need tracking&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Hybrid Approach: Best of Both Worlds? 🌟
&lt;/h2&gt;

&lt;p&gt;Here's a pro tip: You don't have to choose just one! Many successful APIs use CRUD for simple operations and action-specific endpoints for complex business processes. It's like having both a Swiss Army knife and a specialized toolbox.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts 💭
&lt;/h2&gt;

&lt;p&gt;Remember, there's no one-size-fits-all solution in API design. The key is understanding your use case and choosing the approach that brings the most value to your team and your users.&lt;/p&gt;

&lt;p&gt;The best API is the one that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Makes sense to your team&lt;/li&gt;
&lt;li&gt;Is easy to maintain&lt;/li&gt;
&lt;li&gt;Serves your business needs&lt;/li&gt;
&lt;li&gt;Keeps your users happy&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What's your take on this? Have you used both approaches? Share your experiences in the comments below! 👇&lt;/p&gt;




&lt;p&gt;&lt;em&gt;P.S. If you found this helpful, don't forget to like and share! Follow me for more API design insights and development tips!&lt;/em&gt; 🚀&lt;/p&gt;

</description>
      <category>api</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Organizing styles better with BEM + ITCSS + Sass</title>
      <dc:creator>Sivantha Paranavithana</dc:creator>
      <pubDate>Sun, 26 Sep 2021 19:16:36 +0000</pubDate>
      <link>https://forem.com/sivantha96/organizing-styles-better-with-bem-itcss-sass-43d0</link>
      <guid>https://forem.com/sivantha96/organizing-styles-better-with-bem-itcss-sass-43d0</guid>
      <description>&lt;p&gt;Writing CSS is easy. &lt;/p&gt;

&lt;p&gt;But maintaining them is not!&lt;/p&gt;

&lt;p&gt;On smaller projects, how you organize your code isn’t usually a big concern. However, when it comes to larger, complex projects, how you write and organize code can affect at least in three ways.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;How long it takes to write code.&lt;/li&gt;
&lt;li&gt;How much code you have to write.&lt;/li&gt;
&lt;li&gt;How much loading your browser have to do.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Organizing code becomes really important when you are working with a team. The code you write should be transparent. In other words, it should be clear and obvious to understand. And also they should be consistent. Having consistent code throughout the project reduces the amount of mental overhead needed when writing new code, debugging or refactoring.&lt;/p&gt;

&lt;p&gt;Another importance of organizing code is to make the code self documenting as much as possible. In that way we don't have to lose time in writing or reading lengthy, supplementary documentation or give boring KTs to other developers.&lt;/p&gt;

&lt;p&gt;There are plenty of methodologies to reduce the CSS footprint, organize collaboration and maintain large CSS codebases. Among them the mix of BEM, ITCSS and the power of Sass is the most favorite of mine.&lt;/p&gt;

&lt;p&gt;Let me convince you why it should be your favorite as well.&lt;/p&gt;

&lt;p&gt;Let's start with ITCSS.&lt;/p&gt;

&lt;h2&gt;
  
  
  ITCSS
&lt;/h2&gt;

&lt;p&gt;ITCSS is a CSS architecture to make your CSS more scalable and maintainable. ITCSS stands for "Inverted Triangle CSS". What that mean is that dividing the CSS codebase to several sections (called layers), which take the form of an inverted triangle.&lt;/p&gt;

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

&lt;p&gt;ITCSS works with other methodologies such as &lt;a href="http://smacss.com" rel="noopener noreferrer"&gt;SMACSS&lt;/a&gt;, &lt;a href="http://oocss.org" rel="noopener noreferrer"&gt;OOCSS&lt;/a&gt; and even &lt;a href="http://getbem.com" rel="noopener noreferrer"&gt;BEM&lt;/a&gt;. Also, we can take the advantage of the power of a preprocessor such a &lt;a href="https://sass-lang.com" rel="noopener noreferrer"&gt;Sass&lt;/a&gt; to make it more flexible and powerful.&lt;/p&gt;

&lt;p&gt;The layers in ITCSS are ordered in a way that it takes the full advantage of some of the most fundamental concepts of CSS, &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/Cascade_and_inheritance" rel="noopener noreferrer"&gt;cascade, inheritance and specificity&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It start out with the most generic, low-level, catch-all, unremarkable styles, and eventually progress to more explicit and specific rules as we move through the project. &lt;/p&gt;

&lt;p&gt;In other words, selectors in the beginning of the triangle affect a lot of the DOM and the reach gets narrower as we go down the triangle.&lt;/p&gt;

&lt;p&gt;So it's all about organization. We can use a folder structure as below to organize our styles according to the ITCSS architecture.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// folder structure

theme
│
└─── settings
│    ├── _settings.colors.scss
│    ├──  _settings.fonts.scss
│    ├──  ...
│    └─── _index.scss
│
└─── tools
│    ├──  _tools.gradients.scss
│    ├──  _tools.font-sizing.scss
│    ├──  ...
│    └─── _index.scss
│
└─── generics
│    ├──  _generics.page.scss
│    ├──  _generics.normalize.scss
│    ├──  ...
│    └─── _index.scss
│
└─── elements
│    ├──  ...
│    └─── _index.scss
│
└─── objects
│    ├──  ...
│    └─── _index.scss
│
└─── components
│    ├──  ...
│    └─── _index.scss
│
└─── trumps
│    ├──  ...
│    └─── _index.scss
│
└─── _index.scss
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that here we are using a Sass concept called &lt;a href="https://sass-lang.com/guide#topic-4" rel="noopener noreferrer"&gt;partials&lt;/a&gt; for stylesheets inside layers. A partial is a small chunk of styles which we can later import into another sass file. We name partials with a preceding underscore. &lt;/p&gt;

&lt;p&gt;Inside each layer we create an &lt;code&gt;_index.scss&lt;/code&gt; file. Inside that file, we use the &lt;code&gt;@forward&lt;/code&gt; rule in sass to combine everything inside that layer and forward it as a module.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="c1"&gt;// theme/settings/_index.scss&lt;/span&gt;

&lt;span class="k"&gt;@forward&lt;/span&gt; &lt;span class="s1"&gt;'./settings.colors'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;@forward&lt;/span&gt; &lt;span class="s1"&gt;'./settings.fonts'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we import all the modules into the &lt;code&gt;_index.scss&lt;/code&gt; file using the &lt;code&gt;@use&lt;/code&gt; rule as below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="c1"&gt;// theme/_index.scss&lt;/span&gt;

&lt;span class="k"&gt;@use&lt;/span&gt; &lt;span class="s1"&gt;'./settings'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;@use&lt;/span&gt; &lt;span class="s1"&gt;'./tools'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;@use&lt;/span&gt; &lt;span class="s1"&gt;'./generics'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;@use&lt;/span&gt; &lt;span class="s1"&gt;'./elements'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;@use&lt;/span&gt; &lt;span class="s1"&gt;'./objects'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;@use&lt;/span&gt; &lt;span class="s1"&gt;'./components'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;@use&lt;/span&gt; &lt;span class="s1"&gt;'./trumps'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These &lt;code&gt;_index.scss&lt;/code&gt; files are optional and we use this pattern only if we want a single global style file. Otherwise it is recommended to use each layer as modules using &lt;code&gt;@use&lt;/code&gt; rule or use individual partials by themselves.&lt;/p&gt;

&lt;p&gt;You can learn more about sass "at-rules" in &lt;a href="https://sass-lang.com/documentation/at-rules" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Notice that in each sass at-rule (&lt;code&gt;@forward&lt;/code&gt; or &lt;code&gt;@use&lt;/code&gt;), underscore and the '.scss' extension is omitted&lt;/p&gt;

&lt;p&gt;Here we have used &lt;code&gt;@use&lt;/code&gt; rule instead of &lt;code&gt;@import&lt;/code&gt; since Sass recommend the use of &lt;code&gt;@use&lt;/code&gt; over &lt;code&gt;@import&lt;/code&gt; for many reasons. You can read more about that in &lt;a href="https://sass-lang.com/documentation/at-rules/import" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's take a look at each layer of ITCSS.&lt;/p&gt;

&lt;h3&gt;
  
  
  Settings
&lt;/h3&gt;

&lt;p&gt;This layer holds any global settings for your project. We should include settings which can be accessed from anywhere in this layer. For examples, font size, color palettes and configuration available for the entire project should be included in this layer. &lt;/p&gt;

&lt;p&gt;In here we can use &lt;a href="https://sass-lang.com/documentation/variables" rel="noopener noreferrer"&gt;Sass variables&lt;/a&gt; to define colors, fonts used in the project as settings, then we can use that variable around the project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="c1"&gt;// theme/settings/_settings.color.scss&lt;/span&gt;

&lt;span class="nv"&gt;$primary&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mh"&gt;#ea691e&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$secondary&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mh"&gt;#ff9988&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$primary-light&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mh"&gt;#fbcfc7&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="c1"&gt;// theme/components/_components.button.scss&lt;/span&gt;

&lt;span class="k"&gt;@use&lt;/span&gt; &lt;span class="s1"&gt;'../settings/settings.colors'&lt;/span&gt; &lt;span class="nt"&gt;as&lt;/span&gt; &lt;span class="nt"&gt;colors&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nc"&gt;.example-button&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;colors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;$primary&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;When using variables, mixing, etc. inside another partial, it is recommended to use the &lt;code&gt;@use&lt;/code&gt; rule with an &lt;code&gt;alias&lt;/code&gt; instead of using &lt;code&gt;@import&lt;/code&gt; to import everything inside the partial. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Because, if we import the same stylesheet more than once, it will be evaluated again each time. If that stylesheet just defines functions and mixins, this usually isn’t a big deal, but if it contains style rules they’ll be compiled to CSS more than once.&lt;/p&gt;

&lt;p&gt;With this approach, if you realize you need to change something, for an example the theme color, you only need change it in one place.&lt;/p&gt;

&lt;h3&gt;
  
  
  Tools
&lt;/h3&gt;

&lt;p&gt;This layer houses the globally available tooling, namely &lt;a href="https://sass-lang.com/documentation/at-rules/mixin" rel="noopener noreferrer"&gt;mixins&lt;/a&gt; and &lt;a href="https://sass-lang.com/documentation/at-rules/function" rel="noopener noreferrer"&gt;functions&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Note that in here as well, we only keep globally available stuff in here. Any mixin or function that does not need accessing globally should belong in the partial to which it relates.&lt;/p&gt;

&lt;p&gt;Tools layer comes after the Settings layer because, these mixins and functions may need global settings as parameters.&lt;/p&gt;

&lt;p&gt;Examples for tooling that goes inside this layer are gradient mixins, font-sizing mixins, etc.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="c1"&gt;// theme/tools/_tools.gradients.scss&lt;/span&gt;

&lt;span class="k"&gt;@mixin&lt;/span&gt; &lt;span class="nf"&gt;linear-gradient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$direction&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$colors&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;-webkit-linear-gradient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$direction&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$colors&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;-moz-linear-gradient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$direction&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$colors&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;-o-linear-gradient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$direction&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$colors&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;linear-gradient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$direction&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$colors&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Generics
&lt;/h3&gt;

&lt;p&gt;The Generic layer is the layer that houses very high-level, far reaching styles. It contains things like global box-sizing rules, normalize styles, CSS resets, etc.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="c1"&gt;// theme/generics/_generics.input.scss&lt;/span&gt;

&lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"search"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="nd"&gt;::-webkit-search-decoration&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
&lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"search"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="nd"&gt;::-webkit-search-cancel-button&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;-webkit-appearance&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Elements
&lt;/h3&gt;

&lt;p&gt;This layer includes the styles which are applied to bare HTML elements. We basically use HTML tag selectors in this layer. This is still a very low-specificity layer, but affects slightly less of the DOM, and is slightly more opinionated, hence its location in the Triangle.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="c1"&gt;// theme/elements/_elements.headings.scss&lt;/span&gt;
&lt;span class="k"&gt;@use&lt;/span&gt; &lt;span class="s1"&gt;'../settings/settings.fonts'&lt;/span&gt; &lt;span class="nt"&gt;as&lt;/span&gt; &lt;span class="nt"&gt;fonts&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nt"&gt;h2&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;25rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;font-family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;fonts&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;$primary&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Typically, this layer is the last layer we'd find bare HTML element selectors. After this layer we'll basically be using classes to define everything else.&lt;/p&gt;

&lt;h3&gt;
  
  
  Objects
&lt;/h3&gt;

&lt;p&gt;This is the first layer in which we find class selectors. As the name imply, we write the styles aiming objects which defines undecorated design patterns. Such objects can range from something simple as a &lt;code&gt;.container&lt;/code&gt; of layout systems to complex object with many children. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If we are going to combine our partials to a single file, it's better to prefix everything inside our partials some keyword to avoid conflicts between class names. &lt;br&gt;
(We have used &lt;code&gt;xmpl&lt;/code&gt; as our prefix from here on)&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="c1"&gt;// theme/objects/_objects.container.scss&lt;/span&gt;
&lt;span class="k"&gt;@use&lt;/span&gt; &lt;span class="s1"&gt;'../settings/settings.breakpoints'&lt;/span&gt; &lt;span class="nt"&gt;as&lt;/span&gt; &lt;span class="nt"&gt;breakpoints&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nc"&gt;.xmpl-container&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100%&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;padding-right&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;15px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;padding-left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;15px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;margin-right&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;margin-left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;@media&lt;/span&gt; &lt;span class="n"&gt;screen&lt;/span&gt; &lt;span class="nf"&gt;and&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max-width&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;breakpoints&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;$medium&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;max-width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;720px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;@media&lt;/span&gt; &lt;span class="n"&gt;screen&lt;/span&gt; &lt;span class="nf"&gt;and&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max-width&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;breakpoints&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;$small&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;max-width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;540px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note that here we have used &lt;a href="https://sass-lang.com/guide#topic-3" rel="noopener noreferrer"&gt;nesting&lt;/a&gt; to add the &lt;code&gt;@media&lt;/code&gt; queries to the class.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This layer affects less of the DOM than the last layer, yet making modifications to the classes inside this layer could potentially have effects in a lot of other unrelated places.&lt;/p&gt;

&lt;h3&gt;
  
  
  Components
&lt;/h3&gt;

&lt;p&gt;This I where we begin to style the recognizable parts of our UI. So, this is where the majority of our work takes place. In here also we are using classes for defining styles. Therefore the specificity has not increased yet. We shouldn't find any selectors with a lower specificity than one class in this layer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="c1"&gt;// theme/components/_components.button.scss&lt;/span&gt;

&lt;span class="nc"&gt;.xmpl-button&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;inline-block&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;font-weight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;400&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;text-align&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;white-space&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;nowrap&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;vertical-align&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;middle&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1px&lt;/span&gt; &lt;span class="nb"&gt;solid&lt;/span&gt; &lt;span class="nb"&gt;transparent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;.375rem&lt;/span&gt; &lt;span class="mi"&gt;.75rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;line-height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="mi"&gt;.5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;.25rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;transition&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;all&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="mi"&gt;.5s&lt;/span&gt; &lt;span class="n"&gt;ease&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Trumps
&lt;/h3&gt;

&lt;p&gt;This is the highest specificity layer. It includes the most explicit types of rule, with the most narrow focus. &lt;/p&gt;

&lt;p&gt;Typically this layer beats (trumps) all other layers, and has the power to override anything at all that has gone before it. This layer contains utility and helper classes, hacks and overrides. &lt;/p&gt;

&lt;p&gt;A lot of the declarations in this layer will carry &lt;code&gt;!important&lt;/code&gt; tag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sass"&gt;&lt;code&gt;&lt;span class="c1"&gt;// theme/trumps/_trumps.utilities.scss&lt;/span&gt;

&lt;span class="nc"&gt;.text-center&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;text-align&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;centre&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;important&lt;/span&gt;&lt;span class="err"&gt;;&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, with ITCSS we are breaking our CSS codebase into groups based around specificity, reach and explicitness. This format allows us to write our CSS in an order that only ever adds to and inherits from what came previously.&lt;/p&gt;

&lt;p&gt;So, now you might be wondering (or not), &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;what if I want to change the h2 tag in a specific page? &lt;/li&gt;
&lt;li&gt;Where do I put that style? &lt;/li&gt;
&lt;li&gt;Should it go inside the elements layer in the headings partial, or should I define those kind of style outside the ITCSS folder structure? &lt;/li&gt;
&lt;li&gt;Maybe inside another separate stylesheet or go with in-line styles?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Well, personally I don't like inline styles. And defining those styles outside the ITCSS folder structure in a different stylesheet is not right as well.&lt;/p&gt;

&lt;p&gt;So, if we need to change the styles of the h2 tag in a specific page (let's call it &lt;code&gt;my-page&lt;/code&gt;), what we have to do is create a &lt;code&gt;_components.my-page.scss&lt;/code&gt; inside the components layer and create a specific class for that h2 element and bind that h2 element to that class instead of binding to the html element directly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- my-page.html --&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;h2&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"xmpl-my-page__title"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Hello world&lt;span class="nt"&gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="c1"&gt;// theme/components/_components.my-page.scss&lt;/span&gt;

&lt;span class="nc"&gt;.xmpl-my-page__title&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mh"&gt;#ffffff&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In my experience, with this approach we end-up creating so many component partials inside the component layer as well. Therefore, I prefer to maintain another layer right below the components layer called &lt;code&gt;features&lt;/code&gt; or &lt;code&gt;pages&lt;/code&gt; to keep these kind of styles separate. But it's not in the ITCSS architecture. It's just my preference 🤓.&lt;/p&gt;

&lt;p&gt;So, that's pretty much everything (As I know) about the ITCSS architecture. Right now we have organized all of our stylesheets (files) in a scalable and maintainable manner.&lt;/p&gt;

&lt;p&gt;Now let's move into how we can organize the code we write inside all the partials we have created to house them. &lt;/p&gt;

&lt;p&gt;You might have noticed in the above example that I have used a strange way to name my class (&lt;code&gt;xmpl-my-page__title&lt;/code&gt;). Well, that's the BEM convention. Let's dig deeper on that topic.&lt;/p&gt;

&lt;h2&gt;
  
  
  BEM
&lt;/h2&gt;

&lt;p&gt;BEM stands for "Block Element Modifier". It is a naming convention that makes our code more  consistent, transparent, scalable and maintainable. The BEM approach ensures that everyone in the team works with a single codebase and speaks the same language. &lt;/p&gt;

&lt;p&gt;Now, what are Blocks, Elements and Modifiers?&lt;/p&gt;

&lt;h3&gt;
  
  
  Blocks
&lt;/h3&gt;

&lt;p&gt;Blocks are the standalone entities which are meaningful on its own. For examples, we can identify a header, a list, a menu, a checkbox, an input, etc. as an entity.&lt;/p&gt;

&lt;h3&gt;
  
  
  Elements
&lt;/h3&gt;

&lt;p&gt;An element is a part of a block that has no standalone meaning and is semantically tied to its block. For an example, we can identify a single item of a menu as an element, or the caption of a checkbox, or an item of a list, etc.&lt;/p&gt;

&lt;p&gt;We use &lt;code&gt;__element&lt;/code&gt; suffix to denote elements in BEM approach.&lt;/p&gt;

&lt;h3&gt;
  
  
  Modifiers
&lt;/h3&gt;

&lt;p&gt;A modifier is a flag on a block or an element. We can use them to identify a change appearance or behavior of a particular block or an element. For an example, we can identify 'disabled' as the modifier in a disabled menu item, or the 'color yellow' of a container, etc.&lt;/p&gt;

&lt;p&gt;We use &lt;code&gt;--modifier&lt;/code&gt; suffix to denote elements in BEM approach.&lt;/p&gt;

&lt;p&gt;So, how can BEM be any help?&lt;/p&gt;

&lt;p&gt;Either we follow ITCSS or not, we have to write CSS class names sooner or later. But when naming those classes, we need to have a specific way of doing that. That's where BEM comes into play. &lt;/p&gt;

&lt;p&gt;We can use the BEM's Blocks, Elements and Modifier pattern to name each class we create.&lt;/p&gt;

&lt;p&gt;But why is it REALLY helpful? &lt;/p&gt;

&lt;p&gt;The power of BEM comes with a preprocessor such as Sass. &lt;/p&gt;

&lt;p&gt;Let's see why with an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;button&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/assets/icon.svg"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;span&amp;gt;&lt;/span&gt;Submit&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's say we need to style the above button which has an icon inside (A sharable component). Also we need to have three background colors for the button to support default, success and danger scenarios as below.&lt;/p&gt;

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

&lt;p&gt;Let's try to follow the previously learned ITCSS style as well.&lt;/p&gt;

&lt;p&gt;In this case, first we need to create a partial to hold this component. Let's name it &lt;code&gt;_components.button.scss&lt;/code&gt;. Then we need to identify Blocks, Elements and Modifiers. So, in this case the Block would be the &lt;code&gt;&amp;lt;button&amp;gt;&lt;/code&gt; itself. We can identify the &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt; &amp;amp; &lt;code&gt;&amp;lt;span&amp;gt;&lt;/code&gt; as elements. And we can identify &lt;code&gt;default&lt;/code&gt;, &lt;code&gt;success&lt;/code&gt; and &lt;code&gt;danger&lt;/code&gt; as modifiers.&lt;/p&gt;

&lt;p&gt;Now we can get into naming classes.&lt;/p&gt;

&lt;p&gt;We can name the Block as &lt;code&gt;btn&lt;/code&gt; since it is a button. From there we start to name elements as below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"btn"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"btn__icon"&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/assets/icon.svg"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"btn__text"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Submit&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What we have right now is this.&lt;/p&gt;

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

&lt;p&gt;With that we can define the styles as below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="c1"&gt;// theme/components/_components.button.scss&lt;/span&gt;

&lt;span class="nc"&gt;.btn&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="mi"&gt;.5rem&lt;/span&gt; &lt;span class="m"&gt;1rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;10px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;justify-content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;align-items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mh"&gt;#cdcccc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nt"&gt;__icon&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;margin-right&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="mi"&gt;.5rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nt"&gt;__text&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;15px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mh"&gt;#000000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the beauty of BEM + Sass. We can nest the classes using the &lt;code&gt;&amp;amp;&lt;/code&gt; operator of Sass. What it does is, basically denoting the parent objects name. So that &lt;code&gt;&amp;amp;__icon&lt;/code&gt; equals to &lt;code&gt;.btn__icon&lt;/code&gt; and &lt;code&gt;&amp;amp;__text&lt;/code&gt; equals to &lt;code&gt;.btn__text&lt;/code&gt;. Since here we have the BEM's Blocks and Elements notations as well, nesting like this doesn't make our code unreadable.&lt;/p&gt;

&lt;p&gt;After applying those styles, now we have this.&lt;/p&gt;

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

&lt;p&gt;Since we need three buttons to show three states, we can create three &lt;code&gt;btn&lt;/code&gt; blocks with the modifiers to denote each state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"btn"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"btn__icon"&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/assets/icon.svg"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"btn__text"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Submit&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"btn btn--success"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"btn__icon"&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/assets/icon.svg"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"btn__text"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Submit&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"btn btn--danger"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"btn__icon"&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/assets/icon.svg"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"btn__text"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Submit&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have this,&lt;/p&gt;

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

&lt;p&gt;Notice that we haven't used the &lt;code&gt;--default&lt;/code&gt; modifier since, it is not needed as we already have the default state without any modifiers.&lt;/p&gt;

&lt;p&gt;Now we can take the advantage of the BEM's modifiers in the stylesheet to create the other two states.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="c1"&gt;// theme/components/_components.button.scss&lt;/span&gt;

&lt;span class="nc"&gt;.btn&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="mi"&gt;.5rem&lt;/span&gt; &lt;span class="m"&gt;1rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;10px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;justify-content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;align-items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mh"&gt;#cdcccc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nt"&gt;__icon&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;margin-right&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="mi"&gt;.5rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nt"&gt;__text&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;15px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mh"&gt;#000000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nt"&gt;--success&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mh"&gt;#709558&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nt"&gt;--danger&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mh"&gt;#ca5b5b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have nested the &lt;code&gt;--success&lt;/code&gt; and &lt;code&gt;--danger&lt;/code&gt; modifiers inside the &lt;code&gt;.btn&lt;/code&gt; class, so that we can get the resulting classes as &lt;code&gt;.btn--success&lt;/code&gt; and &lt;code&gt;.btn--danger&lt;/code&gt; as we have used in our HTML. &lt;/p&gt;

&lt;p&gt;Also notice that we haven't changed the btn class name in our HTML. Instead, we have added the btn--success class as another new class to the class list. Doing so, the styles defined under the &lt;code&gt;.btn&lt;/code&gt; class also get applied to the button and only the &lt;code&gt;background-color&lt;/code&gt; property gets replaced when the modifier class get applied.&lt;/p&gt;

&lt;p&gt;Now we have this,&lt;/p&gt;

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

&lt;p&gt;There are few more changes have to be done. We have to invert the color of the icon in success and danger states. And we also have to change the color of the text to white in that states as well.&lt;/p&gt;

&lt;p&gt;For this we can introduce more modifiers to our HTML like below and use them to style them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"btn"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"btn__icon"&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/assets/icon.svg"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"btn__text"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Submit&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"btn btn--success"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"btn__icon btn__icon--inverted"&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/assets/icon.svg"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"btn__text btn__text--white"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Submit&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"btn btn--danger"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"btn__icon btn__icon--inverted"&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/assets/icon.svg"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"btn__text btn__text--white"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Submit&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="c1"&gt;// theme/components/_components.button.scss&lt;/span&gt;

&lt;span class="nc"&gt;.btn&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="mi"&gt;.5rem&lt;/span&gt; &lt;span class="m"&gt;1rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;10px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;justify-content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;align-items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mh"&gt;#cdcccc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nt"&gt;__icon&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;margin-right&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="mi"&gt;.5rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nt"&gt;--inverted&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;invert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nt"&gt;__text&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;15px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mh"&gt;#000000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nt"&gt;--white&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mh"&gt;#ffffff&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nt"&gt;--success&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mh"&gt;#709558&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nt"&gt;--danger&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mh"&gt;#ca5b5b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With that, now we have the final result.&lt;/p&gt;

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

&lt;p&gt;Additionally, we also can prefix the &lt;code&gt;.btn&lt;/code&gt; class at the root with some keyword (eg: &lt;code&gt;.xmpl-btn&lt;/code&gt;) so that we won't get any conflicts in class names later on.&lt;/p&gt;

&lt;p&gt;That's how we can take the advantage of BEM to write more organized CSS. &lt;/p&gt;

&lt;p&gt;There are 3 major benefits we get with BEM approach.&lt;/p&gt;

&lt;h3&gt;
  
  
  Modularity
&lt;/h3&gt;

&lt;p&gt;Block styles are never dependent on other elements on a page, so you will never experience problems from cascading.&lt;/p&gt;

&lt;p&gt;You can also export the blocks from your old finished projects to new ones with ease.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reusability
&lt;/h3&gt;

&lt;p&gt;Composing independent blocks in different ways, and reusing them intelligently, reduces the amount of CSS code that you will have to maintain.&lt;/p&gt;

&lt;p&gt;Also, with the power of a preprocessor such as Sass, you can further reduce the amount of code you have to write using nesting.&lt;/p&gt;

&lt;h3&gt;
  
  
  Structure
&lt;/h3&gt;

&lt;p&gt;BEM methodology gives your CSS code a solid and consistent structure that remains simple and easy to understand.&lt;/p&gt;

&lt;p&gt;That's it folks. It was quite a long article. But I hope this will be helpful guide for you.&lt;/p&gt;

&lt;p&gt;See you in the next article. ✌️&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>css</category>
      <category>html</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Render props in React</title>
      <dc:creator>Sivantha Paranavithana</dc:creator>
      <pubDate>Sat, 25 Sep 2021 21:52:46 +0000</pubDate>
      <link>https://forem.com/sivantha96/render-props-d06</link>
      <guid>https://forem.com/sivantha96/render-props-d06</guid>
      <description>&lt;p&gt;In the previous articles, we discussed what is stateful logic sharing, why do we need it and one of the popular solution for that, HOCs.&lt;/p&gt;

&lt;p&gt;In this articles let's take a look at another popular solution for the stateful logic sharing.&lt;/p&gt;

&lt;p&gt;For the explanation I will use the same example that I have used in the previous articles.&lt;/p&gt;

&lt;p&gt;In that examples we had three separate components before applying HOCs, with some duplicated stateful logic inside them. I will mention them here as well for the ease of reference.&lt;/p&gt;


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



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



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


&lt;p&gt;If we take a closer look at these components, we can see that the only thing that are different from each other is the render method (And the name of the component obviously 😅).&lt;/p&gt;

&lt;p&gt;So what if we just created a single component and pass what to render to that component as a prop? &lt;/p&gt;

&lt;p&gt;That's exactly what we are going to do.&lt;/p&gt;

&lt;p&gt;We can create a component called &lt;code&gt;Counter&lt;/code&gt; and put the sharable stateful logic inside of it. And instead of rendering specific content we are going to render the content dynamically. &lt;/p&gt;

&lt;p&gt;For that, we can use the render prop.&lt;/p&gt;

&lt;p&gt;The render prop is, is an ordinary prop like any other whose value is a function. This function is responsible of rendering the dynamic content. And we can pass whatever information we need to share to that function as arguments.&lt;/p&gt;

&lt;p&gt;Let's see that in code.&lt;/p&gt;

&lt;p&gt;In below Counter component you can see that we just execute the render prop inside the render method. Also, we are passing the count and the increment handler as arguments to the &lt;code&gt;this.props.render()&lt;/code&gt; function.&lt;/p&gt;


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


&lt;p&gt;Then we can create a Button counter using the Counter component as below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newButtonCounter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Counter&lt;/span&gt; &lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ButtonCounter&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;increment&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt;} /&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we extract out the function inside the render prop...&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="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ButtonCounter&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;increment&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here you can see that the argument which were passed to the function inside the render prop, are passed again to the &lt;code&gt;ButtonCounter&lt;/code&gt; as props. Then we can use them inside the ButtonCounter as below.&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ButtonCounter&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;increment&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Clicked&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;times&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;ButtonCounter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In this way we don't have to write the stateful logic inside the ButtonCounter. &lt;/p&gt;

&lt;p&gt;Likewise, we can create InputCounter and HoverCounter or any other component we want by this technique and not duplicate the stateful logic.&lt;/p&gt;

</description>
      <category>react</category>
    </item>
    <item>
      <title>HOCs in React</title>
      <dc:creator>Sivantha Paranavithana</dc:creator>
      <pubDate>Tue, 21 Sep 2021 13:43:49 +0000</pubDate>
      <link>https://forem.com/sivantha96/hocs-in-react-caj</link>
      <guid>https://forem.com/sivantha96/hocs-in-react-caj</guid>
      <description>&lt;p&gt;In the previous post we discussed what is stateful logic sharing and why do we need it. &lt;/p&gt;

&lt;p&gt;In this article let's discuss about HOCs which is one of the popular solution for the stateful logic sharing.&lt;/p&gt;

&lt;p&gt;According to the React documentation, a higher-order component is a function that takes a component and returns a new component. &lt;/p&gt;

&lt;p&gt;In other words, a higher order component is a function that transform a component into another enhanced component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;EnhancedComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;higherOrderComponent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;WrappedComponent&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;HOCs are used in common React libraries such as Redux’s &lt;code&gt;connect&lt;/code&gt; and Relay’s &lt;code&gt;createFragmentContainer&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now let's see how HOCs can help us in avoiding duplication and stateful logic sharing.&lt;/p&gt;

&lt;p&gt;In the previous article we saw that in each counter component, there were sharable stateful logic. We can move that into a HOC and wrap our counters from that HOC. &lt;/p&gt;

&lt;p&gt;Let's see that in code.&lt;/p&gt;

&lt;p&gt;First, we create a function called &lt;code&gt;withCounter&lt;/code&gt; which takes a component as an argument. This function will return a new React component. And we can move all the sharable stateful logic inside of that new component. &lt;/p&gt;


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



&lt;p&gt;Then we can pass the count state and the increment handler as props to the wrapped component. &lt;/p&gt;

&lt;p&gt;Notice that we are also passing any additional props that might get passed into the new component we are creating, to the original component through &lt;code&gt;{...this.props}&lt;/code&gt; notation. In that way we can still pass props to the original component even after wrapping it by the HOC.&lt;/p&gt;

&lt;p&gt;Since we are now passing the count state and the increment handler as props, we have to reflect that change in each component as below, where we get the count and the increment handler from props.&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ButtonCounter&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;increment&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Clicked&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;times&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;ButtonCounter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;With that, we can create the components with the counter functionality without duplicating the code using the HOC as below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ButtonWithCounter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;withCounter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ButtonCounter&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;HoverWithCounter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;withCounter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;HoverCounter&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;InputWithCounter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;withCounter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;InputCounter&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With that, we don't need to lift the state up to share the state and the logic, therefore we can reuse this stateful logic in anywhere in the react component tree.&lt;/p&gt;

&lt;p&gt;Not all HOCs look the same. Sometimes they accept only the component we want to wrap as the argument. Eg: &lt;code&gt;withRouter&lt;/code&gt; HOC in React Router.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;NavbarWithRouter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;withRouter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Navbar&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some HOCs accept additional arguments. Usually these arguments are used inside the HOCs for configurations of the HOC. eg: &lt;code&gt;createContainer&lt;/code&gt; in Relay.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;CommentWithRelay&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createContainer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Comment&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some HOCs look like below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ConnectedMyComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mapStateToProps&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;mapDispatchToProps&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="nx"&gt;MyComponent&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This pattern is common in React Redux. &lt;code&gt;connect&lt;/code&gt; is just a function that returns an HOC. Then that HOC is used to wrap the &lt;code&gt;MyComponent&lt;/code&gt;. We can see it clearly when we break it down as below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;enhance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mapStateToProps&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;mapDispatchToProps&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ConnectedMyComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;enhance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;MyComponent&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we want to talk more about HOCs, there are few things to keep in mind when creating HOCs.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We should avoid mutating the original component inside the HOC at all costs. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The reason for this is, when you mutate the original component's prototype inside the HOC, it affects every instance of the original component. This makes the original component unreliable to use anywhere else. &lt;/p&gt;

&lt;p&gt;For an example, let's say we created an HOC which mutate the prototype of a component, and wrapped a component named MyComponent with that HOC. Then even when we use MyComponent without wrapping it with the HOC, it will still contain the mutation that the HOC did. Furthermore, if we apply another HOC on top of that, it might replace the first HOCs logic accidentally as well. Therefore, making HOCs as pure functions is crucial.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Don't use HOCs inside the render method.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each time render method returns a component, React compares the previous component subtree recursively with the new subtree to identify any changes and decide either to update the component subtree or to unmount the current subtree completely and render the new subtree as a new. &lt;/p&gt;

&lt;p&gt;And when using HOCs to wrap components, each of these components receive separate states. If we were apply HOC to a component inside a render method, each time the component get unmounted, the state of that component and all of children to be lost.&lt;/p&gt;

&lt;p&gt;Instead of applying inside the render method, apply HOCs outside the component definition so that the resulting component is created only once. Then, its identity will be consistent across renders.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Static methods must be copied over.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Static methods inside a component won't be available in the new component which wrapped the original component with an HOC. Therefore we must copy all the static methods to the new component before returning it. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Refs aren't passed through&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Although we pass &lt;code&gt;ref&lt;/code&gt;s as props to components, it's really not a prop. It is handled specially by React just like the &lt;code&gt;key&lt;/code&gt; prop. Therefore refs won't be passed to the original component from the wrapped component through &lt;code&gt;{...this.props}&lt;/code&gt; notation. Therefore, instead of using refs we should use forwardRefs and that is discussion for another time😉.&lt;/p&gt;

&lt;p&gt;Now that we talked about HOCs, let's talk about render props which we can use as another solution for the stateful logic sharing in the next article.&lt;/p&gt;

</description>
      <category>react</category>
      <category>reactnative</category>
    </item>
    <item>
      <title>What is stateful logic sharing and why do we need it?</title>
      <dc:creator>Sivantha Paranavithana</dc:creator>
      <pubDate>Tue, 21 Sep 2021 12:59:11 +0000</pubDate>
      <link>https://forem.com/sivantha96/what-is-stateful-logic-sharing-and-why-do-we-need-it-3fdg</link>
      <guid>https://forem.com/sivantha96/what-is-stateful-logic-sharing-and-why-do-we-need-it-3fdg</guid>
      <description>&lt;p&gt;Stateful logic is any code that uses the state. The stateful logic sharing is sharing stateful logic between multiple react components.&lt;/p&gt;

&lt;p&gt;I believe the best way to understand this is with an example.&lt;/p&gt;

&lt;p&gt;Let's say you want to create a component which has a button that increments a counter on click. We can implement that using a class component as below.&lt;/p&gt;


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


&lt;p&gt;This will result a view like this, the counter increments with each button click.&lt;/p&gt;

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

&lt;p&gt;Now, let's say we want another component which increments a counter on hover over a div. We can implement that using a class component as below.&lt;/p&gt;


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


&lt;p&gt;This will result a view like this. The counter increments when the mouse is hovered over the div.&lt;/p&gt;

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

&lt;p&gt;Now, let's say we want another component which increments a counter on each key press. We can implement that using a class component as below.&lt;/p&gt;


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


&lt;p&gt;This will result a view like this. The counter increments on each keypress.&lt;/p&gt;

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

&lt;p&gt;Now, let's backup for a minute. We can see that, in each of these scenarios we duplicated some logic. Specifically this part,&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="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;increment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;prevState&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;prevState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;}));&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This part uses the component's local state, therefore this is a stateful logic. And in each component it stayed basically the same, since the logic is the same.&lt;/p&gt;

&lt;p&gt;Although in this example we only had to duplicate a simple few lines of code, this situation can occur in more complex scenarios as well. &lt;/p&gt;

&lt;p&gt;So, how can we avoid this duplication?&lt;/p&gt;

&lt;p&gt;The immediate thought (At least in my mind😉) is to "lift the state up". &lt;/p&gt;

&lt;p&gt;We can declare the state and the increment handler in the parent component and pass down the state and the handler to children as props.&lt;/p&gt;

&lt;p&gt;But this can be done only when we can create the same parent for the children. If the components are scattered throughout the react component tree, lifting the state up is not gonna be the current solution,&lt;/p&gt;

&lt;p&gt;In that case, we need another solution to share this stateful logic between each component to avoid duplication.&lt;/p&gt;

&lt;p&gt;That's where HOCs (Higher Order Components) and render props comes into the rescue. We will discuss about HOCs and render props in the upcoming articles.&lt;/p&gt;

</description>
      <category>react</category>
      <category>hoc</category>
      <category>hooks</category>
      <category>render</category>
    </item>
    <item>
      <title>GitHub Student Developer Pack [FREE]</title>
      <dc:creator>Sivantha Paranavithana</dc:creator>
      <pubDate>Sat, 18 Jul 2020 12:40:30 +0000</pubDate>
      <link>https://forem.com/sivantha96/github-student-developer-pack-2020-15kg</link>
      <guid>https://forem.com/sivantha96/github-student-developer-pack-2020-15kg</guid>
      <description>&lt;h1&gt;
  
  
  Are you a student?
&lt;/h1&gt;

&lt;p&gt;No matter what you are studying, this post is for you.&lt;/p&gt;

&lt;p&gt;Everyone knows that hands-on experience is very important. But the cost of most tools can be overwhelming. That's why GitHub started this program seven years ago to provide the best professional-grade developer tools and training for students for &lt;strong&gt;free&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It offers a lot of tools that are very useful for developers as well as designers, analysts, etc.&lt;/p&gt;

&lt;p&gt;But first of all, you need to verify that you are eligible for the offer. &lt;/p&gt;

&lt;p&gt;To be eligible for the GitHub Student Developer Pack, you must:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Be currently enrolled in a degree or diploma-granting course of studies such as a high school, secondary school, college, university, homeschool, or similar educational institution.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Have a verifiable school-issued email address or upload documents (school ID, class schedule, transcript, affiliation or enrollment verification letter) that prove your current student status.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Have a GitHub user account. (Obviously!)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Be at least 13 years old.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you are eligible for the offer you can &lt;a href="https://education.github.com/pack" rel="noopener noreferrer"&gt;get the pack.&lt;/a&gt; It might take few days to verify your information. Once your information is verified you will get access to tons of good dev tools and courses for free. &lt;/p&gt;

&lt;p&gt;So, let's take a brief look at what they offer this year,&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%2F4n262yhi0y5hzni94irp.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%2F4n262yhi0y5hzni94irp.png" alt="Alt Text" width="800" height="120"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;free&lt;/strong&gt; 3-month individual subscription to learn about data science from the world’s top data scientists.&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%2Fmfqknfvns5qsjdq77i8f.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%2Fmfqknfvns5qsjdq77i8f.png" alt="Namecheap" width="800" height="120"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Domain registration, hosting, and management with 1 year &lt;strong&gt;free&lt;/strong&gt; domain name registration on the .me TLD and SSL certification.&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%2Fpc7x8q0qymc659z1a3hx.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%2Fpc7x8q0qymc659z1a3hx.png" alt="Alt Text" width="800" height="120"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Domain names, web hosting, and websites with 1 year &lt;strong&gt;free&lt;/strong&gt; domain name and Advanced Security (SSL, privacy protection, and more)&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%2Fp5lmjk8boe3axi2mbrq8.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%2Fp5lmjk8boe3axi2mbrq8.png" alt="Alt Text" width="800" height="120"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Free AWS Educate Starter Account worth $100 with access to the AWS cloud, free training, and collaboration resources.&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%2Fqa6a1oz1xnpe3gl59r8p.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%2Fqa6a1oz1xnpe3gl59r8p.png" alt="Alt Text" width="800" height="120"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Free access to 25+ Microsoft Azure cloud services plus $100 in Azure credit and learning resources.&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%2Fpg8usm7f6ijpzbls44md.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%2Fpg8usm7f6ijpzbls44md.png" alt="Alt Text" width="800" height="120"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Real-time development platform used to create half of the world’s games with &lt;strong&gt;free&lt;/strong&gt; Unity Student Plan while you are a student.&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%2Folkynwvgg4vakgena3er.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%2Folkynwvgg4vakgena3er.png" alt="Alt Text" width="800" height="120"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A free subscription for professional desktop IDEs such as IntelliJ IDEA, PyCharm, and more.&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%2Fcq3c22qc47oky52q2s0x.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%2Fcq3c22qc47oky52q2s0x.png" alt="Alt Text" width="800" height="120"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A flexible, easy-to-use platform to deploy, run, and manage your apps with one free Hobby Dyno for up to two years.&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%2Ftgjfmj5zp3ckglcdgc4m.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%2Ftgjfmj5zp3ckglcdgc4m.png" alt="Alt Text" width="800" height="120"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;1 year &lt;strong&gt;free&lt;/strong&gt; pro tier access to a professional graphics and design platform. &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%2Fs5yc84g8te6ndozw8m6v.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%2Fs5yc84g8te6ndozw8m6v.png" alt="Alt Text" width="800" height="120"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;$50 worth of platform credits for cloud hosting on DigitalOcean.&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%2Fri7i2z6mz99njbk6i876.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%2Fri7i2z6mz99njbk6i876.png" alt="Alt Text" width="800" height="120"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Six &lt;strong&gt;free&lt;/strong&gt; months of 60+ courses covering in-demand topics like Web Development, Python, Java, and Machine Learning on educative.&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%2Fpwjdwswd0tpy8dwytq7s.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%2Fpwjdwswd0tpy8dwytq7s.png" alt="Alt Text" width="800" height="120"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A free license for Bootstrap Studio which is a powerful desktop app for creating responsive websites using the Bootstrap framework.&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%2F4i33jmpe264fheake43h.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%2F4i33jmpe264fheake43h.png" alt="Alt Text" width="800" height="120"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Free&lt;/strong&gt; GitHub Pro while you are a student.&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%2F2fpo9rgsxe1c1sxnrhnb.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%2F2fpo9rgsxe1c1sxnrhnb.png" alt="Alt Text" width="800" height="120"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One standard .TECH domain &lt;strong&gt;free&lt;/strong&gt; for 1 year and 2 free email accounts with 100 MB free storage.&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%2Fexqusnkd36zm9og5knum.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%2Fexqusnkd36zm9og5knum.png" alt="Alt Text" width="800" height="120"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Free&lt;/strong&gt; 6-months access to all courses and workshops on Frontend Masters which provides in-depth JavaScript, Node.js &amp;amp; front-end engineering courses.&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%2Fdv3wdjqmqnhc3ubogbdj.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%2Fdv3wdjqmqnhc3ubogbdj.png" alt="Alt Text" width="800" height="120"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;MongoDB is a general-purpose, document-based, distributed database. With the GitHub pack, you get $200 in MongoDB Atlas Credits, plus access to MongoDB Compass and MongoDB University including &lt;strong&gt;free certification&lt;/strong&gt;.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fh3qukhbbxshzz70cz6gi.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%2Fh3qukhbbxshzz70cz6gi.png" alt="Alt Text" width="800" height="120"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Free&lt;/strong&gt; access to Pro versions of GitKraken Git GUI, GitKraken Boards, and GitKraken Timelines while you're a student.&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%2Folqjwobcy2t8wrm58zgz.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%2Folqjwobcy2t8wrm58zgz.png" alt="Alt Text" width="800" height="120"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;free&lt;/strong&gt; 30-day One Month subscription to learn HTML, CSS, JavaScript, and Python in just 30 days!&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%2Fiic1wmrv29opfifmve51.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%2Fiic1wmrv29opfifmve51.png" alt="Alt Text" width="800" height="120"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Free&lt;/strong&gt; Arduino Create Maker plan for 6 months and discounts on selected hardware.&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%2Fee1fbboabuq6fwm8dzc0.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%2Fee1fbboabuq6fwm8dzc0.png" alt="Alt Text" width="800" height="120"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;GitHub Campus Experts are students who build technical communities on campus, with training and support from GitHub. You can apply to become part of the program while you’re a student.&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%2F3yii598b27gvrqdgy6k9.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%2F3yii598b27gvrqdgy6k9.png" alt="Alt Text" width="800" height="120"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With next.tech you can learn tech skills and build software directly from your browser with real, online computing environments. With the GitHub pack, you get &lt;strong&gt;free&lt;/strong&gt; access to all interactive courses and cloud computing environments for 12 months. &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%2Fuxj4vvlfsoqv79sdk3tz.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%2Fuxj4vvlfsoqv79sdk3tz.png" alt="Alt Text" width="800" height="120"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A Web Hosting platform to host assignment &amp;amp; project work. You get the Designer package for &lt;strong&gt;free&lt;/strong&gt; for 1 year &amp;amp; discounted upgrade offers with the GitHub pack.&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%2Fuqatjzxmmy9t6zr01jnp.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%2Fuqatjzxmmy9t6zr01jnp.png" alt="Alt Text" width="800" height="120"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;An SSH client that works on desktop and mobile. Termius securely syncs data across all your devices. You get &lt;strong&gt;free&lt;/strong&gt; access to the Premium plan while you're a student.&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%2Fnr0n3qjfu3vgm4tn38c6.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%2Fnr0n3qjfu3vgm4tn38c6.png" alt="Alt Text" width="800" height="120"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Iconscout is a design resources marketplace to get high-quality icons, illustrations, and stock images.&lt;br&gt;
With the GitHub pack, you can download 60 premium icons per month for 1 year.&lt;/p&gt;




&lt;h3&gt;
  
  
  And the list goes on
&lt;/h3&gt;

&lt;p&gt;There over 80+ tools from categories like monitoring tools, analytic tools, IDEs, server management tools, APIs, optimization tools, cloud-services, translation management tools, security tools, and many others which I failed to mention here. &lt;/p&gt;

&lt;p&gt;I'll be sure to update the list as much as I can.&lt;/p&gt;

&lt;p&gt;Get the GitHub Student Developer Pack. Thank me later 😉&lt;/p&gt;

</description>
      <category>github</category>
      <category>git</category>
    </item>
    <item>
      <title>Java OOP Cheatsheet</title>
      <dc:creator>Sivantha Paranavithana</dc:creator>
      <pubDate>Thu, 16 Jul 2020 06:58:50 +0000</pubDate>
      <link>https://forem.com/sivantha96/java-oop-cheatsheet-3ph1</link>
      <guid>https://forem.com/sivantha96/java-oop-cheatsheet-3ph1</guid>
      <description>&lt;p&gt;Liquid syntax error: 'raw' tag was never closed&lt;/p&gt;
</description>
      <category>java</category>
      <category>jvm</category>
    </item>
    <item>
      <title>Java Under the Hood</title>
      <dc:creator>Sivantha Paranavithana</dc:creator>
      <pubDate>Sun, 12 Jul 2020 16:17:17 +0000</pubDate>
      <link>https://forem.com/sivantha96/java-under-the-hood-3071</link>
      <guid>https://forem.com/sivantha96/java-under-the-hood-3071</guid>
      <description>&lt;p&gt;This post offers a brief introduction to how Java executes a code written in Java Language under the hood.&lt;/p&gt;

&lt;p&gt;Here is the list that I am going to explore,&lt;/p&gt;

&lt;p&gt;1. Java Compiler&lt;br&gt;
2. JVM&lt;/p&gt;

&lt;h1&gt;
  
  
  Java Compiler
&lt;/h1&gt;

&lt;h4&gt;
  
  
  Is Java a &lt;a href="https://en.wikipedia.org/wiki/Compiled_language" rel="noopener noreferrer"&gt;compiled-language&lt;/a&gt; or an &lt;a href="https://en.wikipedia.org/wiki/Interpreted_language" rel="noopener noreferrer"&gt;interpreted-language&lt;/a&gt;?
&lt;/h4&gt;

&lt;p&gt;Kinda like both! The reason lies within the compilation process of Java. &lt;br&gt;
In many other languages, their compilers convert the source code into machine-specific code and then the machine will execute the instructions resides in that machine code.&lt;/p&gt;

&lt;p&gt;But in Java, the &lt;strong&gt;Java Compiler&lt;/strong&gt; does &lt;strong&gt;not&lt;/strong&gt; convert Java source code into machine code (i.e. Binary) directly. Instead, it converts the source code into an intermediary code called &lt;strong&gt;bytecode&lt;/strong&gt;. Then the Java Virtual Machine (JVM) will execute that bytecode by interpreting it to the machine code. But JVM uses a Just In Time (JIT) compiler to compile some of the code into native code (machine code). Therefore, Java is both compiled and interpreted language.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;javac&lt;/code&gt; is a component of the Java Development Kit (JDK) which specifies the Java compiler. &lt;/p&gt;

&lt;p&gt;The Java compiler transforms the source code located in &lt;code&gt;.java&lt;/code&gt; files into &lt;code&gt;.class&lt;/code&gt; files which are essentially the bytecodes of those Java Codes.&lt;/p&gt;

&lt;p&gt;Not only just Java, basically any language can implement its compiler that parses the source code into valid bytecode, and then it can be executed using the JVM.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you have multiple classes in a single &lt;code&gt;.java&lt;/code&gt; file then, it will generate a &lt;code&gt;.class&lt;/code&gt; file for each class.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Java Virtual Machine
&lt;/h1&gt;

&lt;p&gt;After &lt;code&gt;javac&lt;/code&gt; compiles the source code to bytecode, JVM executes it. This is called the program run phase. &lt;/p&gt;

&lt;p&gt;The JVM is divided into three main subsystems.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Classloading Subsystem&lt;/li&gt;
&lt;li&gt;Runtime Data Areas&lt;/li&gt;
&lt;li&gt;Execution Engine&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Other than that it consists of &lt;strong&gt;Native Method Libraries&lt;/strong&gt; which are platform-specific executable code (written in c/c++) contained in libraries or DLLs and a &lt;strong&gt;Java Native Interface (JNI)&lt;/strong&gt; which the interface that Execution Engine use to interact with the Native Method Libraries.&lt;/p&gt;

&lt;h2&gt;
  
  
  Classloading Subsystem
&lt;/h2&gt;

&lt;p&gt;Classloading Subsystem is used for &lt;strong&gt;loading, linking&lt;/strong&gt; and the &lt;strong&gt;initialization&lt;/strong&gt; of the &lt;code&gt;.class&lt;/code&gt; files generated by the &lt;code&gt;javac&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Loading
&lt;/h3&gt;

&lt;p&gt;Java classes aren't loaded into memory all at once. They get loaded when they are required by an application (dynamic loading). Classes are loaded with the help of three class loaders. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Bootstrap Classloader&lt;/strong&gt; - This loader is responsible for loading the core classes such as &lt;code&gt;java.lang.Object&lt;/code&gt;, &lt;code&gt;java.lang.Class&lt;/code&gt; and &lt;code&gt;java.lang.Classloader&lt;/code&gt; from bootstrap classpath which is &lt;code&gt;rt.jar&lt;/code&gt;. This Classloader is the parent of all the Classloaders.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Extention Loader&lt;/strong&gt; - This loader continues the loading process by loading the classes that are an extension of the standard core Java classes. These classes are available to all applications running on the platform (i.e. JRE).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Application Loader&lt;/strong&gt; - The loading ends by loading the initial user-defined class which resides in the application level classpath, which mentioned in the &lt;em&gt;Environment Variable&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Above classloaders will follow &lt;em&gt;Delegation Hierarchy Algorithm&lt;/em&gt; while loading class&lt;/p&gt;

&lt;h4&gt;
  
  
  What is Delegation Hierarchy Algorithm?
&lt;/h4&gt;

&lt;p&gt;When a Classloader is requested to load a class, the Classloader will delegate the request to the parent Classloader.&lt;/p&gt;

&lt;p&gt;For example, if the JVM is requested to load a class, the Application Classloader will delegate it to the Extension Classloader. Then the Extension Classloader will delegate it to the Bootstrap Classloader. If the Bootstrap Classloader is unsuccessful in loading the class, then the Extension Classloader will try to load it. Only if the Extension Classloader fails to find the class, then the Application Classloader will try to load the class.&lt;/p&gt;

&lt;p&gt;If the class is not found even after the Application Classloader tries to load it, then an error will be thrown.&lt;/p&gt;

&lt;h3&gt;
  
  
  Linking
&lt;/h3&gt;

&lt;p&gt;Linking a class involves following operations,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Verification&lt;/strong&gt; - Ensure the bytecode is structurally correct.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Preparation&lt;/strong&gt; - Memory will be allocated for static variables and the default values will be assigned to them.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Resolution&lt;/strong&gt; - Symbolic memory references will be replaced with the actual values.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Initialization
&lt;/h3&gt;

&lt;p&gt;This is the final phase of the Classloading subsystem. Here, all static variables will be assigned with their original values and then the static block will get executed. As a result, the &lt;code&gt;main()&lt;/code&gt; method will get executed, therefore the other classes as well. It will cause the loading, linking, and initialization of those classes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Runtime Data Area
&lt;/h2&gt;

&lt;p&gt;The JVM creates multiple runtime data areas. Some of them are created and destroyed with the JVM and some get created when a new thread is created and destroyed when the respective thread ends.&lt;/p&gt;

&lt;p&gt;There are five major data areas in the JVM.&lt;/p&gt;

&lt;h3&gt;
  
  
  Method Area
&lt;/h3&gt;

&lt;p&gt;The simplest type of memory to manage. This is a shared resource. There is only one Method Area per JVM. It can consist of anything that can be completely determined at compile time such as static variables, constants(perhaps), code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Heap Area
&lt;/h3&gt;

&lt;p&gt;The least organized and most dynamic data area. This is a resource that is shared with all threads. The Heap is used to dynamically allocate and deallocate memory for class instances (objects) and arrays. Special operations such as &lt;code&gt;new&lt;/code&gt; are needed to allocate heap storage. The memory assigned for objects never explicitly deallocated and this space is reclaimed by the &lt;em&gt;garbage collector&lt;/em&gt;(discussed later). The memory assigned for the Heap is not contiguous. Deallocation may leave "holes" in the heap (a.k.a fragmentation).&lt;/p&gt;

&lt;h3&gt;
  
  
  Stack Area
&lt;/h3&gt;

&lt;p&gt;For every thread, a separate runtime stack will be created. Therefore data stored in the stack are thread-safe, unlike in Method Area and Heap Area. For every method call, one entry will be made in the stack called a &lt;em&gt;Stack Frame&lt;/em&gt;. A Stack Frame is divided into three subentities.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Local Variable Array - stores local variables and their corresponding values.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Operand Stack - If any intermediate operation is required to perform, then this will act as a runtime workspace to operate.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Frame Data - All symbols corresponding to the method are stored here. The &lt;code&gt;catch&lt;/code&gt; block information is also stored here.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  PC Registers
&lt;/h3&gt;

&lt;p&gt;Each thread will have separate PC Register to hold the address of the machine instruction which is currently executing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Native Method Stacks
&lt;/h3&gt;

&lt;p&gt;For each thread, a Native Method Stack will be created to hold the native method information provided by the Native Method Libraries.&lt;/p&gt;

&lt;h2&gt;
  
  
  Execution Engine
&lt;/h2&gt;

&lt;p&gt;After the bytecode load into memory and the Runtime Data Areas are allocated, then the execution of the bytecode will be done by the Execution Engine. Execution Engine consists of three subsystems.&lt;/p&gt;

&lt;h3&gt;
  
  
  Interpreter
&lt;/h3&gt;

&lt;p&gt;The interpreter interprets the bytecode faster but executes slowly. If one method is called multiple times, every time the interpreter will interpret it.&lt;/p&gt;

&lt;h3&gt;
  
  
  JIT Compiler
&lt;/h3&gt;

&lt;p&gt;The Just In Time (JIT) compiler will identify the hotspots of the code which are the code that gets repeated and get interpreted repeatedly, and compile those code into native code (machine-specific code) which improves the performance. The JIT compiler consists of the following components.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Intermediate Code Generator - Produces intermediate code for optimization.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Code Optimize - optimize the intermediate code generated above. Such as elimination of common sub-expressions, translation from stack operations to register operations, reduction of memory accesses by register allocation, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Target Code Generator - Generate Machine Code (Native Code)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Profiler - Finds hotspots in the bytecode.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Garbage Collector
&lt;/h3&gt;

&lt;p&gt;Collects and removes unreferenced objects(inaccessible objects / orphans). Garbage Collection can also be triggered manually by calling &lt;code&gt;System.gc()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Thanks for reading. &lt;/p&gt;

&lt;p&gt;See you in the next post!&lt;/p&gt;

</description>
      <category>java</category>
      <category>jvm</category>
      <category>bytecode</category>
    </item>
  </channel>
</rss>
