<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Moha Asghari on Medium]]></title>
        <description><![CDATA[Stories by Moha Asghari on Medium]]></description>
        <link>https://medium.com/@asghari.moha?source=rss-31f9a285ef8a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*wiTO7qW7StkVidSVAD4wwQ.jpeg</url>
            <title>Stories by Moha Asghari on Medium</title>
            <link>https://medium.com/@asghari.moha?source=rss-31f9a285ef8a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 14:51:39 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@asghari.moha/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Refactoring Challenge 1 (Enhanced Code Management in PHP)]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@asghari.moha/refactoring-challenge-1-enhanced-code-management-in-php-0a5e2b2ce227?source=rss-31f9a285ef8a------2"><img src="https://cdn-images-1.medium.com/max/600/1*lhoGcv3J4R7SGMcySP79RQ.png" width="600"></a></p><p class="medium-feed-snippet">In my previous post, We explored refactoring techniques to improve code quality and readability. I recommend you to review that post from&#x2026;</p><p class="medium-feed-link"><a href="https://medium.com/@asghari.moha/refactoring-challenge-1-enhanced-code-management-in-php-0a5e2b2ce227?source=rss-31f9a285ef8a------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@asghari.moha/refactoring-challenge-1-enhanced-code-management-in-php-0a5e2b2ce227?source=rss-31f9a285ef8a------2</link>
            <guid isPermaLink="false">https://medium.com/p/0a5e2b2ce227</guid>
            <category><![CDATA[clean-code]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[refactoring]]></category>
            <category><![CDATA[php-developers]]></category>
            <category><![CDATA[dto]]></category>
            <dc:creator><![CDATA[Moha Asghari]]></dc:creator>
            <pubDate>Wed, 06 Mar 2024 10:22:57 GMT</pubDate>
            <atom:updated>2024-03-06T10:22:57.083Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[“this” in JavaScript: Challenges to Level Up Your Skills]]></title>
            <link>https://medium.com/@asghari.moha/this-in-javascript-challenges-to-level-up-your-skills-ac9285ab78b1?source=rss-31f9a285ef8a------2</link>
            <guid isPermaLink="false">https://medium.com/p/ac9285ab78b1</guid>
            <category><![CDATA[this]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[challeng]]></category>
            <dc:creator><![CDATA[Moha Asghari]]></dc:creator>
            <pubDate>Fri, 13 Oct 2023 06:32:53 GMT</pubDate>
            <atom:updated>2023-10-13T06:32:53.014Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EYedIOG9kwNgqlcrQAePvA.png" /></figure><p><strong>Introduction:</strong><br>Understanding the concept of “this” in JavaScript is crucial for any developer striving to become proficient in the language. It can be a source of confusion due to its dynamic nature and its behavior in different contexts. In this blog post, we will explore some tricky challenges that will put your knowledge of “this” to the test. Get ready to dive deep into the world of JavaScript and master the intricacies of “this” like a pro.</p><p><strong>Challenge 1: The Binding Puzzle</strong><br>In this challenge, you will encounter a function that uses “this” in an unexpected way. Your task is to identify what “this” refers to within the function. Remember, the value of “this” is determined by the execution context. Can you decipher the puzzle and figure out the correct result?</p><pre>function puzzle() { <br>  console.log(this); <br>} <br> <br>puzzle(); // What does this output? </pre><blockquote>Result: <br>In this case, the “puzzle()” function is called without any specific context. Therefore, the value of this inside the function will be the global object (e.g., window in a browser environment or global in Node.js). The output will depend on the environment in which the code is executed.</blockquote><p><strong>Challenge 2: The Lost Reference</strong><br>In this challenge, you will be presented with a scenario where “this” loses its reference unexpectedly. Your mission is to identify the reason behind the lost reference and find a solution to restore it. This challenge will test your ability to identify common pitfalls that can cause “this” to behave unexpectedly.</p><pre>const obj = { <br>  name: &#39;John&#39;, <br>  sayName() { <br>    console.log(this.name); <br>  } <br>}; <br> <br>const sayName = obj.sayName; <br>sayName(); // What does this output? </pre><blockquote>Result: <br>In this challenge, the sayName() function is assigned to a variable sayName and then invoked as a standalone function. Since the function is called without a specific context, this loses its reference to the obj object. As a result, this.name will be undefined, and the output will be undefined.</blockquote><p><strong>Challenge 3: The Arrow Function Twist</strong><br>Arrow functions have a unique behavior when it comes to “this”. In this challenge, you will be working with arrow functions and their implications on the value of “this”. Can you predict the output of an arrow function when it is used within an object method? This challenge will require you to understand the lexical scoping of arrow functions and how they differ from regular functions.</p><pre>const obj = { <br>  name: &#39;John&#39;, <br>  sayName: () =&gt; { <br>    console.log(this.name); <br>  } <br>}; <br> <br>obj.sayName(); // What does this output? </pre><blockquote>Result: <br>In this challenge, the sayName method is defined using an arrow function. Arrow functions do not have their own this binding and instead inherit the this value from the enclosing scope, which is the global scope in this case. Therefore, this.name will be undefined, and the output will be undefined.</blockquote><p><strong>Challenge 4: The Event Handler Enigma</strong><br>Handling events in JavaScript can sometimes introduce complexities related to “this”. In this challenge, you will be tasked with creating an event handler that correctly maintains the reference to the intended object. This challenge will test your understanding of event handling and how “this” interacts with event listeners.</p><pre>const button = document.querySelector(&#39;button&#39;); <br> <br>button.addEventListener(&#39;click&#39;, function() { <br>  console.log(this); <br>}); </pre><blockquote>Result:<br>In this challenge, the event handler function is invoked when the button is clicked. Inside the event handler function, this refers to the element that triggered the event, which is the button element in this case. The output will be the button element itself.</blockquote><p><strong>Challenge 5: The Prototype Predicament</strong><br>Prototypes are a fundamental part of JavaScript’s object-oriented nature. In this challenge, you will be working with prototypes and their relationship with “this”. Your goal is to correctly bind “this” within a prototype method and ensure its proper functionality. This challenge will assess your knowledge of prototypes and their impact on “this” binding.</p><pre>function Person(name) { <br>  this.name = name; <br>} <br> <br>Person.prototype.sayName = function() { <br>  console.log(this.name); <br>}; <br> <br>const john = new Person(&#39;John&#39;); <br>john.sayName(); // What does this output? </pre><blockquote>Result: <br>In this challenge, a Person constructor function is defined, which sets the name property on the newly created object using this. The sayName method is added to the prototype of the Person function, allowing all instances of Person to access it. <br>When john.sayName() is called, this refers to the john object. Therefore, the output will be ‘John’, as it accesses the name property of the john object.</blockquote><p><strong>Conclusion:</strong><br>By tackling these tricky challenges, you will gain a deeper understanding of “this” in JavaScript and sharpen your skills as a developer. Remember, mastering “this” is crucial for writing clean, effective, and maintainable JavaScript code. With practice and persistence, you will become adept at handling “this” in a variety of scenarios. So, gear up and embrace the challenges that lie ahead. Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ac9285ab78b1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Refactoring Challenge 1: For PHP Developers]]></title>
            <link>https://medium.com/@asghari.moha/refactoring-challenge-1-making-the-code-better-7af11f853a7c?source=rss-31f9a285ef8a------2</link>
            <guid isPermaLink="false">https://medium.com/p/7af11f853a7c</guid>
            <category><![CDATA[refactoring]]></category>
            <category><![CDATA[challenge]]></category>
            <category><![CDATA[clean-code]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[php]]></category>
            <dc:creator><![CDATA[Moha Asghari]]></dc:creator>
            <pubDate>Mon, 09 Oct 2023 09:10:27 GMT</pubDate>
            <atom:updated>2024-03-06T10:29:06.571Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pnNCoait-kVoeh7o-iANQA.png" /></figure><p>I decided to design some code challenges to play with code and improve our coding skills. If you are ready, let’s start with the first challenge.</p><p>In this refactoring challenge, we will improve the code by implementing several changes step by step. The original code is as follows:</p><pre>class Order {<br>    <br>    private $items;<br><br>    public function __construct() {<br>        $this-&gt;items = [];<br>    }<br><br>    public function calculateTotal() {<br>        $total = 0;<br>        foreach ($this-&gt;items as $item) {<br>            $total += $item-&gt;price;<br>        }<br>        $this-&gt;applyDiscount($total);<br>        return $total;<br>    }<br><br>    private function applyDiscount(&amp;$total) {<br>        $total *= 0.9;<br>    }<br>}</pre><p>Think about the code and then let’s start.</p><ol><li><strong>Remove pass-by-reference for testability</strong></li></ol><p>To improve testability, we can remove the pass-by-reference in the applyDiscount method. Here&#39;s the updated code:</p><pre>class Order {<br>    <br>    private $items;<br><br>    public function __construct() {<br>        $this-&gt;items = [];<br>    }<br><br>    public function calculateTotal() {<br>        $total = 0;<br>        foreach ($this-&gt;items as $item) {<br>            $total += $item-&gt;price;<br>        }<br>        return $this-&gt;applyDiscount($total);<br>    }<br><br>    private function applyDiscount($total) {<br>        return $total * 0.9;<br>    }<br>}</pre><p><strong>2. Single Responsibility</strong></p><p>Next, we’ll refactor the code to adhere to the Single Responsibility Principle. Here’s the updated code:</p><pre>class Order {<br>    <br>    private $items;<br><br>    public function __construct() {<br>        $this-&gt;items = [];<br>    }<br><br>    public function calculateTotal() {<br>        $subtotal = $this-&gt;calculateSubtotal();<br>        return $this-&gt;applyDiscount($subtotal);<br>    }<br><br>    private function calculateSubtotal() {<br>        $subtotal = 0;<br>        foreach ($this-&gt;items as $item) {<br>            $subtotal += $item-&gt;price;<br>        }<br>        return $subtotal;<br>    }<br><br>    private function applyDiscount($total) {<br>        return $total * 0.9;<br>    }<br>}</pre><p><strong>3. Constructor property promotion</strong></p><p>We can utilize constructor property promotion to simplify the code. Here’s the updated code:</p><pre>class Order {<br>    public function __construct(private $items=[]) {}<br><br>    public function calculateTotal() {<br>        $subtotal = $this-&gt;calculateSubtotal();<br>        return $this-&gt;applyDiscount($subtotal);<br>    }<br><br>    private function calculateSubtotal() {<br>        $subtotal = 0;<br>        foreach ($this-&gt;items as $item) {<br>            $subtotal += $item-&gt;price;<br>        }<br>        return $subtotal;<br>    }<br><br>    private function applyDiscount($total) {<br>        return $total * 0.9;<br>    }<br>}</pre><p><strong>4. Type Declaration</strong></p><p>Lastly, we’ll add type declarations to improve code readability and maintainability. Here’s the final refactored code:</p><pre>class Order {<br><br>    public function __construct(<br>       private readonly array $items=[]<br>    ) {}<br><br>    public function calculateTotal(): float<br>    {<br>        $subtotal = $this-&gt;calculateSubtotal();<br>        return $this-&gt;applyDiscount($subtotal);<br>    }<br><br>    private function calculateSubtotal(): int<br>    {<br>        $subtotal = 0;<br>        foreach ($this-&gt;items as $item) {<br>            $subtotal += $item-&gt;price;<br>        }<br>        return $subtotal;<br>    }<br><br>    private function applyDiscount($total): float<br>    {<br>        return $total * 0.9;<br>    }<br>}</pre><p><strong>5. Final Changes</strong></p><p>After writing the post, I received some good comments from you. Actually I forgot to take a constant for number in the code. Yes, it is better to change it:</p><pre>class Order {<br><br>    private const DISCOUNT_RATE = 0.9;<br><br>    public function __construct(<br>       private readonly array $items=[]<br>    ) {}<br><br>    public function calculateTotal(): float<br>    {<br>        $subtotal = $this-&gt;calculateSubtotal();<br>        return $this-&gt;applyDiscount($subtotal);<br>    }<br><br>    private function calculateSubtotal(): int<br>    {<br>        $subtotal = 0;<br>        foreach ($this-&gt;items as $item) {<br>            $subtotal += $item-&gt;price;<br>        }<br>        return $subtotal;<br>    }<br><br>    private function applyDiscount($total): float<br>    {<br>        return $total * self::DISCOUNT_RATE;<br>    }<br>}</pre><p>If you have any more idea please put it into comments.</p><p>If you think there’s nothing left to improve, think again. Read the following post to elevate your code to the next level.</p><ul><li><a href="https://medium.com/@asghari.moha/refactoring-challenge-1-enhanced-code-management-in-php-0a5e2b2ce227">Refactoring Challenge 1 (Enhanced Code Management in PHP)</a></li><li><a href="https://www.linkedin.com/posts/sm-asghari_refactoring-code-challenge-activity-7117045778669260800-2e2Z?utm_source=share&amp;utm_medium=member_desktop">Mohammad Asghari ✪ on LinkedIn: Refactoring code challenge</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7af11f853a7c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>