<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Muhammad Nazirul Amin bin Khairul Anwar</title>
    <description>The latest articles on DEV Community by Muhammad Nazirul Amin bin Khairul Anwar (@nazirul_amin).</description>
    <link>https://dev.to/nazirul_amin</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%2F1054231%2F6751f77c-85d4-4747-bf0a-cd9406e984c8.jpg</url>
      <title>DEV Community: Muhammad Nazirul Amin bin Khairul Anwar</title>
      <link>https://dev.to/nazirul_amin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nazirul_amin"/>
    <language>en</language>
    <item>
      <title>Code Refactoring: Avoid Prop Drilling in Vue with Provide/Inject</title>
      <dc:creator>Muhammad Nazirul Amin bin Khairul Anwar</dc:creator>
      <pubDate>Tue, 16 Jul 2024 08:40:40 +0000</pubDate>
      <link>https://dev.to/nazirul_amin/code-refactoring-avoid-prop-drilling-in-vue-with-provideinject-19le</link>
      <guid>https://dev.to/nazirul_amin/code-refactoring-avoid-prop-drilling-in-vue-with-provideinject-19le</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Prop drilling is a common concept in frontend development, especially in component based frameworks like Vue.js. It refers to the practice of passing data from a parent component down to its nested child components through props. While this is a straightforward way to share data, it can lead to certain challenges, especially in large and deeply nested component trees.&lt;/p&gt;

&lt;h2&gt;
  
  
  Challenges
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Boilerplate Code:&lt;/strong&gt; Prop drilling often leads to redundant code as props need to be passed through multiple layers of components.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tight Coupling:&lt;/strong&gt; Components become tightly coupled to the data they receive via props, reducing reusability and making maintenance more challenging.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalability Issues:&lt;/strong&gt; As component trees grow larger and deeper, managing prop passing becomes increasingly complex.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Debugging Complexity:&lt;/strong&gt; Tracking data flow through multiple levels of components can complicate debugging efforts, especially when props are mutated or modified along the way.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Scenario
&lt;/h2&gt;

&lt;p&gt;Imagine an application where we have a ProductList component that displays a list of products. Each product has a ProductDetail component that needs to display detailed information, and within ProductDetail, there is a ProductReview component showing customer reviews. Let's see how to pass data down from the ProductList to the ProductReview component.&lt;/p&gt;

&lt;h4&gt;
  
  
  ProductList.vue
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;template&amp;gt;
  &amp;lt;ul&amp;gt;
    &amp;lt;li v-for="product in products" :key="product.id"&amp;gt;
      &amp;lt;ProductDetail :product="product" /&amp;gt;
    &amp;lt;/li&amp;gt;
  &amp;lt;/ul&amp;gt;
&amp;lt;/template&amp;gt;

&amp;lt;script setup&amp;gt;
import { ref } from 'vue';
import ProductDetail from './ProductDetail.vue';

const products = ref([
  { id: 1, name: 'Product A', reviews: ['Great product!', 'Loved it!'] },
  { id: 2, name: 'Product B', reviews: ['Not bad.', 'Could be better.'] },
]);
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  ProductDetail.vue
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;template&amp;gt;
  &amp;lt;div&amp;gt;
    &amp;lt;h2&amp;gt;{{ product.name }}&amp;lt;/h2&amp;gt;
    &amp;lt;ProductReview :reviews="product.reviews" /&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/template&amp;gt;

&amp;lt;script setup&amp;gt;
import { defineProps } from 'vue';
import ProductReview from './ProductReview.vue';

const props = defineProps({
  product: {
    type: Object,
    required: true,
  },
});
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  ProductReview.vue
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;template&amp;gt;
  &amp;lt;ul&amp;gt;
    &amp;lt;li v-for="review in reviews" :key="review"&amp;gt;{{ review }}&amp;lt;/li&amp;gt;
  &amp;lt;/ul&amp;gt;
&amp;lt;/template&amp;gt;

&amp;lt;script setup&amp;gt;
import { defineProps } from 'vue';

const props = defineProps({
  reviews: {
    type: Array,
    required: true,
  },
});
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Solutions
&lt;/h2&gt;

&lt;p&gt;To avoid prop drilling we can make use of state management library like &lt;a href="https://vuex.vuejs.org/" rel="noopener noreferrer"&gt;Vuex&lt;/a&gt; and &lt;a href="https://pinia.vuejs.org/" rel="noopener noreferrer"&gt;Pinia&lt;/a&gt;, but for this article I will demonstrate of using Vue provide and inject. Let's refactor our components.&lt;/p&gt;

&lt;h4&gt;
  
  
  ProductList.vue
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;template&amp;gt;
  &amp;lt;ul&amp;gt;
    &amp;lt;li v-for="product in products" :key="product.id"&amp;gt;
      &amp;lt;ProductDetail :product="product" /&amp;gt;
    &amp;lt;/li&amp;gt;
  &amp;lt;/ul&amp;gt;
&amp;lt;/template&amp;gt;

&amp;lt;script setup&amp;gt;
import { ref, provide } from 'vue';
import ProductDetail from './ProductDetail.vue';

const products = ref([
  { id: 1, name: 'Product A', reviews: ['Great product!', 'Loved it!'] },
  { id: 2, name: 'Product B', reviews: ['Not bad.', 'Could be better.'] },
]);

provide('products', products);
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  ProductDetail.vue
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;template&amp;gt;
  &amp;lt;div&amp;gt;
    &amp;lt;h2&amp;gt;{{ product.name }}&amp;lt;/h2&amp;gt;
    &amp;lt;ProductReview :product-id="product.id" /&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/template&amp;gt;

&amp;lt;script setup&amp;gt;
import { defineProps } from 'vue';
import ProductReview from './ProductReview.vue';

const props = defineProps({
  product: {
    type: Object,
    required: true,
  },
});
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  ProductReview.vue
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;template&amp;gt;
  &amp;lt;ul&amp;gt;
    &amp;lt;li v-for="review in productReviews" :key="review"&amp;gt;{{ review }}&amp;lt;/li&amp;gt;
  &amp;lt;/ul&amp;gt;
&amp;lt;/template&amp;gt;

&amp;lt;script setup&amp;gt;
import { defineProps, inject, computed } from 'vue';

const props = defineProps({
  'product-id': {
    type: Number,
    required: true,
  },
});

const products = inject('products');
const productReviews = computed(() =&amp;gt; {
  const product = products.value.find(p =&amp;gt; p.id === props['product-id']);
  return product ? product.reviews : [];
});
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By using provide/inject we eliminate the needs to pass down our props hence we can keep our component hierarchy clean and maintainable, improving the overall quality and scalability of your application.&lt;/p&gt;

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

&lt;p&gt;Prop drilling in Vue can lead to redundant code, tight coupling, scalability issues, and challenging debugging. Using Vue's provide/inject can solve these problems. This approach bypasses intermediate components, resulting in smoother data flow and cleaner code, making our code more maintainable and scalable.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Code Refactoring: Avoid Nested If Statements with Early Returns</title>
      <dc:creator>Muhammad Nazirul Amin bin Khairul Anwar</dc:creator>
      <pubDate>Thu, 11 Jul 2024 06:57:54 +0000</pubDate>
      <link>https://dev.to/nazirul_amin/code-refactoring-avoid-nested-if-statements-with-early-returns-52ml</link>
      <guid>https://dev.to/nazirul_amin/code-refactoring-avoid-nested-if-statements-with-early-returns-52ml</guid>
      <description>&lt;h2&gt;
  
  
  The Problem with Nested If Statements
&lt;/h2&gt;

&lt;p&gt;Nested if statements occur when multiple conditional checks are placed within each other. While nested if statements are sometimes necessary, excessive nesting can lead to "arrow code," which is difficult to read and understand. Here's an example of nested if statements:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function processOrder($order)
{
    if ($order-&amp;gt;isValid()) {
        if ($order-&amp;gt;isPaid()) {
            if ($order-&amp;gt;isShipped()) {
                // Process the order
                return 'Order processed';
            } else {
                return 'Order not shipped';
            }
        } else {
            return 'Order not paid';
        }
    } else {
        return 'Invalid order';
    }
}

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  The Concept of Early Returns
&lt;/h2&gt;

&lt;p&gt;The early return technique involves checking for conditions that should cause the function to exit early. By handling these conditions first, you can reduce the nesting level of your code and make the main logic more visible. Here's how the previous example looks with early returns:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function processOrder($order)
{
    if (!$order-&amp;gt;isValid()) {
        return 'Invalid order';
    }

    if (!$order-&amp;gt;isPaid()) {
        return 'Order not paid';
    }

    if (!$order-&amp;gt;isShipped()) {
        return 'Order not shipped';
    }

    // Process the order
    return 'Order processed';
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Using early returns simplifies how code is structured and avoid complex arrow code. This approach makes code easier to read, maintain, and in overall better in quality. By refactoring nested if statements with early returns, we will create cleaner and easier to understand code, which boosts productivity and reduces errors.&lt;/p&gt;

</description>
      <category>refactoring</category>
      <category>earlyreturns</category>
      <category>nestedstatements</category>
      <category>softwaredevelopment</category>
    </item>
  </channel>
</rss>
