<?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 Shuvojit Sarkar on Medium]]></title>
        <description><![CDATA[Stories by Shuvojit Sarkar on Medium]]></description>
        <link>https://medium.com/@shuvojitsarkar?source=rss-28b91177e737------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*zSJxOR_fyfpIqeLWVTWdwg.jpeg</url>
            <title>Stories by Shuvojit Sarkar on Medium</title>
            <link>https://medium.com/@shuvojitsarkar?source=rss-28b91177e737------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 04:01:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shuvojitsarkar/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[5 ways to write cleaner Angular Code: Part I]]></title>
            <link>https://codeburst.io/5-ways-to-write-cleaner-angular-code-part-i-8a5339a3e957?source=rss-28b91177e737------2</link>
            <guid isPermaLink="false">https://medium.com/p/8a5339a3e957</guid>
            <category><![CDATA[readable-code]]></category>
            <category><![CDATA[clean-code]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[enterprise-software]]></category>
            <dc:creator><![CDATA[Shuvojit Sarkar]]></dc:creator>
            <pubDate>Wed, 11 Mar 2020 03:22:32 GMT</pubDate>
            <atom:updated>2020-03-11T03:22:32.506Z</atom:updated>
            <content:encoded><![CDATA[<h3>Using Enums</h3><p>Let’s say you have a use case, where there are three modes in a component, there are three buttons that set the mode, and according to the active mode some divs get either hidden or shown.</p><p>Now the solution is pretty simple, we will have a state variable containing some data, and you do <strong>*ngIf </strong>on the div(s) and have a <strong>setState()</strong> function which sets a new state, which is triggered via buttons.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R1X2RDWnYr70_aCO05mHcg.jpeg" /></figure><p>Now this is how I see ~80% people doing it, this code obviously works, but the issue with this code is, if someone else from the team has to work on it, or god forbid you yourself have to edit this part few months later,</p><ol><li>You’re gonna have a hard time remembering what all of those mean, or how many types of states are there;</li><li>You will not get any autocomplete support from IDE(s) like vscode;</li><li>You will not get a warning from the IDE if you mistakenly put another value which is not in the set, which is a very easy way to introduce bugs into your component.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/688/1*nMeXXkvW5UfROEQuCEMAWg.png" /></figure><p>Now let’s look at this thing we call an enum.</p><p>In simple words, enums allow us to declare a set of named constants i.e. a collection of related values that can be numeric or string values.</p><p>Let’s look at how introducing enum to our codebase might help us -</p><ol><li><strong>Type safety:</strong> Enums provide compile-time type safety and prevent from comparing constants in different enums.</li><li><strong>Limits inputs:</strong> The linter won’t allow parsing a constant of a wrong enum to a method that expects an enum type. When using enum in switch statement, the enum constants are the only allowed labels for cases. IDEs like vscode even provide autocomplete feature for filling them.</li><li>Enums <strong>groups things</strong> in a set.</li><li>Enums are <strong>iterable</strong>.</li><li><strong>Maintainablity</strong>: Let’s say the value for a type needs to be changed, if you’re using raw data throughout the application you’ll have to change it everywhere, but with enums, as you’re resuing it, changing it at one place will work seamlessly.</li></ol><p>Now let’s look at our previous component with the enum class implemented.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IxtIXgYqG-sxOyQ_W_VgNg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/665/1*v1IA70N7ZAO4-j6NY1BxEQ.png" /></figure><p>There you have it, a much cleaner code, and the linter will understand your code and assist you better.</p><p>You can click <a href="https://stackblitz.com/edit/angular-qheqnd?file=src/app/app.component.ts">here</a> to get the example code on StackBlitz.</p><h3>Closing Thoughts</h3><p>I’m planning to post 4 more articles in this series, and some Angular Design patterns I use as well.</p><p>If you liked this post, please do hit the 👏 below to support.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8a5339a3e957" width="1" height="1" alt=""><hr><p><a href="https://codeburst.io/5-ways-to-write-cleaner-angular-code-part-i-8a5339a3e957">5 ways to write cleaner Angular Code: Part I</a> was originally published in <a href="https://codeburst.io/">codeburst</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to maintain core PHP projects?]]></title>
            <link>https://codeburst.io/how-to-maintain-core-php-projects-594721858cad?source=rss-28b91177e737------2</link>
            <guid isPermaLink="false">https://medium.com/p/594721858cad</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-maintenance]]></category>
            <category><![CDATA[php]]></category>
            <dc:creator><![CDATA[Shuvojit Sarkar]]></dc:creator>
            <pubDate>Thu, 07 Dec 2017 14:04:19 GMT</pubDate>
            <atom:updated>2017-12-07T14:04:19.627Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1c9UyiQ7ClMuR4Pk-BkVxA.jpeg" /></figure><p>It’s almost 2018 and most modern websites written in PHP are written in some sort of a MVC or MVT framework, the most famous ones being Laravel and CodeIgniter, Laravel being my personal favourite.</p><p>However, I know a lot of my peers just getting started with PHP and while they try to turn some idea into a PHP project, I see them struggle with maintaining the project. Now unlike a framework, maintaining a core PHP project is really really tough(if you haven’t read this article).</p><p>Over the years of writing core PHP and using frameworks like Laravel or CodeIgniter, I’ve developed few techniques or hacks if you may, that help me maintain a core PHP project in a much easier way which I’m gonna explain in the following article.</p><p>First would be the folder structure, I would recommend breaking your project into 4 folders.</p><pre>.<br>├── assets/<br>├── helpers/<br>├── views/<br>├── process/<br>├── .htaccess<br>└── index.php</pre><ul><li>assets : This folder will contain all the asset files, images, style-sheets and JavaScript files and if you wanna go even hard core, make separate directories for each type of files, which I personally do.</li><li>views: This folder will contain all the php files which will act as “views”, which basically means these are the files which are showing some form or some webpage.</li><li>process: This folder will contain all the processes, which basically means all the files than handle form submissions and other form of processes. Limit these files to a three step process, input &gt; process &gt; redirect.</li><li>helpers: This folder will contain specific functions which all the files would be using, the biggest example would be database connection. Have you ever written mysqli_connect() on top of each file in a big-ass project and then had to change the password in 20 different files? I have.</li></ul><p>Now let’s understand how we can further automate some of the above folders.</p><h3>Views</h3><p>One of the simplest hacks we can use in order to make our lives easier would be to use sub-views. That is, breaking some parts of code that is used in every page and write them into a separate file, and include them in each view file.</p><p>So, suppose we are making a blog, and the webpage has a header, a footer and sidebar and main body.</p><p>Clearly, there are three parts of the website that will be there on every page header, footer and sidebar. So what we can easily do is make separate files for each of the sections and include them using the <strong>include() </strong>function.</p><p>After recreating the folder structure, it would look kind-of like this.</p><pre>.<br>├── assets<br>├── helpers<br>├── process<br>├── views<br>│   └── sub-views<br>│       ├── footer.php<br>│       ├── header.php<br>│       └── sidebar.php<br>├── .htaccess<br>└── index.php</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0776f2a357d6b9a7f220a4ce212b732b/href">https://medium.com/media/0776f2a357d6b9a7f220a4ce212b732b/href</a></iframe><p>Now, here we’ll have some issues with this code if we have a file under the views directory. Because the relative reference to the sub-views directory will then won’t work. What we can do as a cheap hack would be to just change the include path from <strong>views/sub-views/footer.php</strong> to <strong>sub-views/footer.php</strong>.</p><p>But, we don’t wanna get more technical dept, so we’ll try to automate the whole process. Enter <strong>helpers</strong>.</p><h3>Helpers</h3><p>The helper directory will be mostly used to write helper functions to re-use code that we’re gonna need to write a lot in the project. First helper function we’ll write would be subview(String), which will take the name of the view file, and include the file using an absolute path. Let me show you how it’ll look.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/78efbdfbf6cf1b88323206fd3a33a514/href">https://medium.com/media/78efbdfbf6cf1b88323206fd3a33a514/href</a></iframe><p>Let’s understand what this function does, it takes the filename as input, let’s say <strong>header.php</strong>, generates an absolute path for that file, and includes it, so no matter where we call this function from we’ll always get the correct path.</p><p>We’re using <strong>__DIR__</strong> to get the current directory, which would be the helpers directory, now first thing we do is a <strong>../</strong> which changes the directory to the project root, then we navigate to <strong>views/sub-views/</strong> and then append the filename.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/737/1*NTJ444EeKrYk2jE-6UveKA.png" /><figcaption>Output from php interactive shell.</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fe60f90d2cba9be5ae8f4ea5cec24810/href">https://medium.com/media/fe60f90d2cba9be5ae8f4ea5cec24810/href</a></iframe><p>Looks cleaner than before, right? Notice the <strong>include_once </strong>on top of the page, we’re including the helper file so that we can use the functions that we’ve written in the file.</p><p>The most used helper file would be where we connect to the database, something every dynamic website needs. Let’s create a <strong>conn.php</strong> in helpers directory.</p><pre>.<br>├── assets<br>├── helpers<br>│   ├── conn.php<br>│   └── helper.php<br>├── process<br>├── views<br>│   └── sub-views<br>│       ├── footer.php<br>│       ├── header.php<br>│       └── sidebar.php<br>├── .htaccess<br>└── index.php</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/503e04c5217c96907075dad0d1afa82b/href">https://medium.com/media/503e04c5217c96907075dad0d1afa82b/href</a></iframe><p>Notice the query function, it not only will execute the query but will also do the error handling automatically each time.</p><p>One thing that I noticed is that the newbies totally ignore error handling, the most important part of writing core php is to handle all errors.</p><p>Always expect that something will go wrong, prepare for every error!</p><p>Now, if we want to include this file in every view/process file, we’ll have to write an include_once again on top of every file, and it would look something like this.</p><pre><strong>&lt;?php include_once </strong>&#39;helper/helper.php&#39;;<br><strong>&lt;?php include_once </strong>&#39;helper/conn.php&#39;;</pre><p>Let’s think long-term, suppose this is a big project, we’ll have many helper files, it’s never a good idea to write all functions in a same file, separate them according to their purpose, easier for the future you to find.</p><blockquote>While programming, always make your current self work more so that your future self can chill while adding new features or fixing bugs.</blockquote><p>So, in order to not write a thousand include lines on top of every file what we can do is, write an <strong>init.php</strong> in the helper directory, which will include all the helper files, and then include that <strong>init.php</strong> on top of all the files, that way you’ll get access to all the helper files, and if you add a new helper file in <strong>init.php</strong>, it’ll get added to all files.</p><pre>.<br>├── assets<br>├── helpers<br>│   ├── conn.php<br>│   ├── helper.php<br>│   └── init.php<br>├── process<br>├── views<br>│   └── sub-views<br>│       ├── footer.php<br>│       ├── header.php<br>│       └── sidebar.php<br>├── .htaccess<br>└── index.php</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/791bb302078f76a92439f343b0fb70a3/href">https://medium.com/media/791bb302078f76a92439f343b0fb70a3/href</a></iframe><p>While these are some of the very subtle ways to eventually decrease the amount of work you need to do to maintain a project, it is certainly not limited to just these methods, so next time you start working on a project, think long-term, think about your future-self and be creative!</p><h3>Closing Thoughts</h3><p>If this post gets enough traction, I will share more hacks and techniques which I use on a daily basis to make maintaining code a little bit easier.</p><p>If you liked this post, please do hit the 👏 below to support.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=594721858cad" width="1" height="1" alt=""><hr><p><a href="https://codeburst.io/how-to-maintain-core-php-projects-594721858cad">How to maintain core PHP projects?</a> was originally published in <a href="https://codeburst.io/">codeburst</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Art of Laziness — Form Validation]]></title>
            <link>https://medium.com/error-logs-from-last-night/art-of-laziness-form-validation-2bc130d44300?source=rss-28b91177e737------2</link>
            <guid isPermaLink="false">https://medium.com/p/2bc130d44300</guid>
            <category><![CDATA[jquery]]></category>
            <category><![CDATA[html5]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Shuvojit Sarkar]]></dc:creator>
            <pubDate>Tue, 05 Dec 2017 01:13:54 GMT</pubDate>
            <atom:updated>2017-12-05T17:55:14.449Z</atom:updated>
            <content:encoded><![CDATA[<h3>Art of Laziness — Form Validation</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jjfaJ_RNhDuWCtplK9UD_g.png" /></figure><p>One of the things that made me fall in love with programming is that you get rewarded if you’re lazy. It is good to be lazy and invent methods to do your task in shorter periods of time with least effort, provided you don’t write <strong>shitty</strong> algorithms in the process.</p><blockquote>“I choose a lazy person to do a hard job. Because a lazy person will find an easy way to do it.”<br>― Bill Gates</blockquote><p>Over the years, we’ve developed few methods to make our life a little bit easier, me and my co-editor <a href="https://medium.com/u/a6bcb42d3ab5">Debarshi Sen</a> will be sharing few of our techniques in the “<strong>Art of Laziness</strong>” series.</p><p>Few days ago, I’ve noticed some of my peers(who’re just getting started with web development) struggle with JavaScript form-validation while working with long-ass forms with a lot of fields. Their form looking something similar to <a href="https://i.stack.imgur.com/W4Zt0.jpg">this</a>.</p><p>Their solution to this was writing a long list of variables and checking them one by one resulting in a massive 200 line validation code. This inspired me to share our little hack which obviously is not patented, so don’t blame us later for stealing your idea.</p><p>So, what we do is, we assign each element with a unique class, and use <a href="http://html5doctor.com/html5-custom-data-attributes/">HTML5’s custom data attributes</a>. Let’s break it down.</p><p>The HTML looks something like this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/047ae94c396477e927ec568829b6d726/href">https://medium.com/media/047ae94c396477e927ec568829b6d726/href</a></iframe><p>The class we added to all the fields that we <strong>want to validate</strong> is “.form-validate”, also note the unusual attribute <strong>data-error-message</strong>. We’ll be using that to show a proper error message and not a generic “Please enter all fields properly” leaving the user confused as to what he did wrong.</p><p>Next step is the JavaScript, we’ll just use some jQuery to do it, because who doesn’t love jQuery?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4f83579f9371adfd7fec228f10eb1c0a/href">https://medium.com/media/4f83579f9371adfd7fec228f10eb1c0a/href</a></iframe><p>What we’re doing here is, we loop through each and every element which has the <strong>form-validate </strong>class when the user tries to submit the form. For each element, we check if the length of it’s trimmed value is equal to zero which is better than the traditional <strong>val()!=null &amp;&amp; val()!=’’</strong> because it also cancels out a string full of spaces.</p><p>If, any element fails the validation, it’s <strong>data-validation-error </strong>attribute is extracted via the <a href="http://api.jquery.com/attr/"><strong>$.attr()</strong></a> method, for demonstration purposes I’ve alerted the error message, but you can show it as a popup message or anyway you want.</p><p>The next line(#13) is where we use the reference to the submit event stored in <strong>e</strong> and invoke the <strong>preventDefault()</strong> method which will prevent the form from being submit, and the return false will break the <strong>$.each</strong> loop, if you do not exit the loop the user will get popups about all the fields that are not validated.</p><p>You can find a working example of this method in <a href="https://codepen.io/anon/pen/jaJXGL"><strong>CodePen</strong></a>.</p><p>There are few more things that you guys can try for experiments:</p><ul><li>Create classes like <strong>form-validate-email</strong>, or <strong>form-validate-phone</strong> and in the event function run loop through all fields which require an additional email check.</li><li>On error, change some CSS property of the input field to further visually express that an error has occurred.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2bc130d44300" width="1" height="1" alt=""><hr><p><a href="https://medium.com/error-logs-from-last-night/art-of-laziness-form-validation-2bc130d44300">Art of Laziness — Form Validation</a> was originally published in <a href="https://medium.com/error-logs-from-last-night">Error Logs from Last Night</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>