<?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 Raza on Medium]]></title>
        <description><![CDATA[Stories by Raza on Medium]]></description>
        <link>https://medium.com/@razamoh?source=rss-f63005bddf75------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by Raza on Medium</title>
            <link>https://medium.com/@razamoh?source=rss-f63005bddf75------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 09:49:07 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@razamoh/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[JS Code Refactoring : How to refactor conditional Statements ,Use object literals instead.]]></title>
            <link>https://medium.com/@razamoh/js-code-refactoring-how-to-refactor-conditional-statements-use-object-literals-instead-669c1933f089?source=rss-f63005bddf75------2</link>
            <guid isPermaLink="false">https://medium.com/p/669c1933f089</guid>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[refactor]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[refactoring]]></category>
            <dc:creator><![CDATA[Raza]]></dc:creator>
            <pubDate>Sun, 10 Apr 2022 11:44:09 GMT</pubDate>
            <atom:updated>2022-04-10T12:15:01.536Z</atom:updated>
            <content:encoded><![CDATA[<h3>Basic JS Code Refactoring : How to refactor conditional Statements using object literals ?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/976/1*QHaNj2zsH3xd5bB_iHr2iA.png" /></figure><p>Across our codebase, we are all aware with the use of conditional expressions such as if,else, else if, and switch statements.<br>To avoid the if if spaghetti problem and illegible ifs and elses, we frequently employ a switch statement, which is accepted by the majority of reviewers.</p><p>as an example Check out the code below, which calls three different payment methods based on the conditions we provide.<br>If the payment method does not exist, an error is thrown.</p><pre>const makePayment = (paymentType) =&gt; {<br>    if (&#39;card&#39; === paymentType) {<br>        this.payByCreditCard()<br>    } else if (&#39;cash&#39; == paymentType) {<br>        this.payByCashOnDelivery()<br>    } else if (&#39;voucher&#39; === paymentType) {<br>        this.payByVoucher()<br>    } else {<br>        throw new Error(&#39;Invalid payment type.&#39;)<br>    }<br>}</pre><pre>const payByCreditCard = () =&gt; {<br>    //Some process goes here <br>}</pre><pre>const payByCashOnDelivery = () =&gt; {<br>    //Some process goes here <br>}</pre><pre>const payByVoucher = () =&gt; {<br>    //Some process goes here <br>}</pre><p>Usually we suggest to use switch case statements to make the code more readable. such as</p><pre>const makePayment = (paymentType) =&gt; {<br>    switch (paymentType) {<br>        case &#39;card&#39;:<br>            this.payByCreditCard();<br>            break;<br>        case &#39;cash&#39;:<br>            this.payByCashOnDelivery()<br>            break;<br>        case &#39;voucher&#39;:<br>            this, payByVoucher()<br>            break;<br>         default:<br>            throw new Error(&#39;Invalid payment type.&#39;)<br>            break;<br>    }<br>}</pre><p>This is nice but lets make it more readable and elegant using object literals ;)</p><p>Simply take type of payments and related functions / Methods and move it to an object and access it by payment type .</p><pre>const payments = {<br>   &#39;card&#39;: this.payByCreditCard(),<br>   &#39;cash&#39;: this.payByCashOnDelivery(),<br>   &#39;voucher&#39;: this.payByVoucher()<br>}</pre><pre>const processPayment = payments[paymentType];<br>//Throw an error if payment type does not exists.<br>if(!processPayment) throw new Error(&#39;Invalid payment type.&#39;)<br>//Invoke variable as function/ method.<br>processPayment();</pre><p>Does not it neat and readable ? . <br>Type your comments and let me know if you have any questions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=669c1933f089" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Create own custom helper functions / Classes in Laravel.]]></title>
            <link>https://medium.com/@razamoh/create-own-custom-helper-functions-classes-in-laravel-e8d2f50ff38?source=rss-f63005bddf75------2</link>
            <guid isPermaLink="false">https://medium.com/p/e8d2f50ff38</guid>
            <dc:creator><![CDATA[Raza]]></dc:creator>
            <pubDate>Sat, 06 Jun 2020 09:28:46 GMT</pubDate>
            <atom:updated>2022-04-10T10:33:19.922Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xzP69oyed3TQStckNP2yQA.jpeg" /><figcaption>A sample laravel repository code</figcaption></figure><p>We all need to build certain helper functions as programmers to eliminate repetitive code and speed up our development. Helper functions are usually generalised functions that are used to prevent repeating codes.</p><p>Laravel has a huge number of built-in helper functions. (Collect() and dd() are my favorites.)<br><a href="https://laravel.com/docs/7.x/helpers">https://laravel.com/docs/7.x/helpers</a> Laravel Helper functions</p><p>Sometimes we need to create our own custom helper functions to meet our own requirements.</p><p>Let’s pretend we require a few methods to get the current user’s business information, logged-in user information, and find a user by User Id.</p><p>Let’s see what we can do.</p><p>There are three options.</p><p>1. <strong>Autoloading</strong>: In a PHP file, create helper functions and load them using Autoload Composer.</p><p>2.Application of<strong> global namespaced functions</strong>.</p><p>3.Autoloading the helper class using <strong>Service Providers</strong></p><h3><strong>1.Self-loading (Simple and easiest method.)</strong></h3><p>First and foremost, you must build a helper file.</p><p><strong>Step 1</strong>: Create a helper.php file in the laravel app folder.</p><p>The helpers.php files often are usually found in the following places:</p><blockquote>app/Http/helpers.php <br>app/Helpers/helpers.php <br>app/Http/helpers.php</blockquote><p>For this lesson, we’ll start with app/Helpers/helpers.php.</p><p><strong>Step 2</strong>: Add your own function to app/Helpers/helpers.php.</p><p>In our case, we needed the three functions listed below.</p><pre>&lt;?php<br>use App\User;<br>use App\Company;<br>use Auth;</pre><pre>if (! function_exists(&#39;getUser&#39;)) {<br> function getUser(int $id): ?object<br> { <br>       return User::find($id);<br> }<br>}</pre><pre>if (! function_exists(&#39;getCurrentUser&#39;)) {<br> function getCurrentUser() : ?object<br> { <br>        return Auth::user();<br>  <br> }<br>}</pre><pre>if (! function_exists(&#39;getUserCompany&#39;)) {<br> function getUserCompany() : ?object<br> { <br>        $companyId = Auth::user()-&gt;comp_id;<br>        return Company::find($companyId);    <br> }<br>}</pre><p>So, let’s use composer autoload to load this file.</p><p><strong>Step 3. </strong>Open the composer.json file in your laravel root directory and scroll down to the autoload section. In the file section, put the path of the helpers.php file.</p><pre>&quot;autoload&quot;: {<br>        &quot;classmap&quot;: [<br>            &quot;database&quot;<br>        ],<br>        &quot;psr-4&quot;: {<br>            &quot;App\\&quot;: &quot;app/&quot;<br>        },<br>       <strong> &quot;files&quot;: [&quot;app/Helpers/helpers.php&quot;]</strong><br>    },<br>    &quot;autoload-dev&quot;: {<br>        &quot;classmap&quot;: [<br>            &quot;tests/TestCase.php&quot;<br>        ]<br>    },</pre><p>Don’t forget to save the files.<br><strong>Step 4. </strong>Now you must execute dump-autoload, which you may accomplish by typing the following command.</p><pre>composer dump-autoload</pre><p>That’s it. <br>Your helper functions can now be used everywhere in your code..</p><pre>$company = dd(getUserCompany());</pre><pre>$user = dd(getCurrentUser());</pre><pre>$customUser = dd(getUser(10));</pre><h3>2.Using Global namespace functions.</h3><p>Create a helper PHP file in the same location just like with the Autoloading approach.</p><p>Let’s use app/Helpers/helpers.php for this example.</p><p><strong>Step 1.</strong> Open <strong>app/Helpers/helpers.php</strong></p><p>Add the following code.</p><pre>&lt;?php</pre><pre>namespace App\Helpers; // Your helpers namespace <br>use App\User;<br>use App\Company;<br>use Auth;</pre><pre>class UserHelper<br>{<br>    public static function getUser(int $userid): ?object<br>    {<br>        return User::find($id);<br>    }</pre><pre>    public static function getCurrentUser(): ?object<br>    {<br>         return Auth::user();<br>    }</pre><pre>    public static function getUserCompany(): ?object<br>    {<br>        $companyId = Auth::user()-&gt;comp_id;<br>        return Company::find($companyId);<br>    }<br>}</pre><p><strong>Step 2:</strong> Create an alias for the helper file in <strong>config/app.php</strong></p><p>Go to your laravel config/app.php scroll down to aliases section.</p><p>Add following line to aliases <br>‘UserHelper’ =&gt; App\Helpers\Helper::class, // Add this line in the end of Aliases array</p><pre>&lt;?php</pre><pre>&#39;aliases&#39; =&gt; [<br>        &#39;App&#39; =&gt; Illuminate\Support\Facades\App::class,<br>        &#39;Arr&#39; =&gt; Illuminate\Support\Arr::class,<br>        &#39;Artisan&#39; =&gt; Illuminate\Support\Facades\Artisan::class,<br>        ...<br>        &#39;UserHelper&#39; =&gt; App\Helpers\UserHelper::class, //&lt;&lt;this line    <br>        ...</pre><p>You do not need to run composer dump-autoload.<br>Now, <strong>psr-4 autoload</strong> will do the job for you.</p><p>Now you can use Helper functions anywhere in the app.</p><pre>&lt;?php</pre><pre>namespace App\Http\Repositories;</pre><pre>use UserHelper;</pre><pre>class SponsorRepository extends BaseRepository<br>{<br>    private $company;</pre><pre>    public function __construct()<br>    {<br>       $this-&gt;company = UserHelper::getUserCompany();<br>    }</pre><p>Or use it in the Controller or wherever you want</p><pre>&lt;?php</pre><pre>namespace App\Http\Controllers;</pre><pre>use UserHelper;</pre><pre>class SponsorController extends Controller<br>{<br>    public function getUserCompany() <br>    {<br>        return  UserHelper::getUserCompany();<br>    }</pre><h3>3.Using Service providers to Autoload the helper class.</h3><p>(If you have many helper files This will be the easiest way to load the helper classes)</p><p>Laravel Service providers are used to autoload classes, lets use this method load our helper class.</p><p><strong>Step 1: </strong>Follow step 1 and step 2 from Using Global namespace functions.</p><p><strong>Step 2:</strong> Create a Service provider</p><pre>php artisan make:provider UserHelpServiceProvider</pre><p><strong>Step 3:Open</strong> app/Provider/UserHelpServiceProvider.php and edit the register function and load helper class(es).</p><pre>   protected $customHelpers = [<br>       &#39;StringHelper&#39;,<br>       &#39;UserHelper&#39;<br>   ];</pre><pre>   public function register()<br>   {<br>        foreach ($this-&gt;customHelpers as $helper) {<br>            require_once(app_path().&#39;/Helpers/&#39;.$helper.&#39;.php&#39;);<br>        }<br>   }</pre><p><strong>Step 4:</strong> Create an alias for the helper file in <strong>config/app.php</strong></p><p>Go to your laravel config/app.php scroll down to providers array and Add the following lines.</p><pre>App\Providers\HelperServiceProvider::class,</pre><p>Your providers will look like this now.</p><pre>&#39;providers&#39; =&gt; [</pre><pre>        /*<br>        * Laravel Framework Service Providers...<br>        */<br>        Illuminate\Auth\AuthServiceProvider::class,<br>        Illuminate\Broadcasting\BroadcastServiceProvider::class,<br>        Illuminate\Bus\BusServiceProvider::class,<br>        Illuminate\Cache\CacheServiceProvider::class,<br><br>        App\Providers\EventServiceProvider::class,<br>        App\Providers\RouteServiceProvider::class,<br>        ...<br>        App\Providers\HelperServiceProvider::class,<br>        ...</pre><pre>]</pre><p><strong>Step 5: </strong>Create an alias (Refer to Using Global namespace functions. Step 2)</p><p>That&#39;s it :)</p><p>Let me know if you know any other methods to load Custom helpers.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e8d2f50ff38" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>