<?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: Ashish Surana</title>
    <description>The latest articles on DEV Community by Ashish Surana (@ashishsurana).</description>
    <link>https://dev.to/ashishsurana</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%2F217666%2F9c7d46ab-76b5-4eb1-b6a6-f6312d6caf75.jpeg</url>
      <title>DEV Community: Ashish Surana</title>
      <link>https://dev.to/ashishsurana</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ashishsurana"/>
    <language>en</language>
    <item>
      <title>Refactoring Redux - A clever way</title>
      <dc:creator>Ashish Surana</dc:creator>
      <pubDate>Wed, 17 Jun 2020 21:23:59 +0000</pubDate>
      <link>https://dev.to/ashishsurana/refactoring-redux-a-clever-way-4lia</link>
      <guid>https://dev.to/ashishsurana/refactoring-redux-a-clever-way-4lia</guid>
      <description>&lt;p&gt;Redux is a &lt;strong&gt;A predictable state container for JavaScript apps.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Also, Redux is a nice example of simple functional programming paradigm where we use pure functions for data mutation. Or we can say, by using functional programming we can be more predictable and eventually less errors (This is what makes me happy).&lt;/p&gt;

&lt;p&gt;Redux became popular exponentially along with the react.js. However, It can be used with any application and it is independant of Any front end technology.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Clutter starts from here&lt;/strong&gt;&lt;br&gt;
As our Product managers enrich application with new features, our store experiences the maximum impact due to this. Length of Reducers grows linearly along with the source code. The no. of cases inside the switch blocks keep on increasing and in many cases reducer file length crosses 700, 900 lines.&lt;br&gt;
Our objects dives in deep level of nesting, Updation of nested objects become a pain, And now Our Clean reducer became a big ball of mud. So after Looking for solutions, I end up finding few tips for redux refactoring&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Using functions&lt;/strong&gt; to return new state inside switch cases&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flat State shape&lt;/strong&gt; inside a reducer&lt;/li&gt;
&lt;li&gt;using &lt;strong&gt;combineReducer&lt;/strong&gt; to club multiple reducers&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's discuss if they can really help us in refactoring&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://redux.js.org/recipes/structuring-reducers/refactoring-reducer-example"&gt;Using functions&lt;/a&gt;:&lt;/strong&gt; This can be a great way of clean code. We can import pure functions and use them in our reducers, but still our noumber of switch cases are intact. Biggest problem with this approach is it can affect readability because now our state and state mutation lies in separate files.&lt;br&gt;
so what's next?????&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://redux.js.org/recipes/structuring-reducers/normalizing-state-shape"&gt;Flat State shape&lt;/a&gt;:&lt;/strong&gt; This must be kept in mind while designing and creating reducer states. Less level of nesting will lead to a cleaner store. But what if our reducer is already messed up with deep level of objects and actions to mutuate those objects??? Changing structure is almost next to impossible because it will affect all connected components.&lt;br&gt;
How about combining Reducers???&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://redux.js.org/recipes/structuring-reducers/using-combinereducers"&gt;Combine Reducers&lt;/a&gt;:&lt;/strong&gt; This is a great way to club multiple reducers under a single key name, but this is also not a feasible approach for existing messed up reducers, because the challenge with this is it contains &lt;strong&gt;all reducers under a key Reducers&lt;/strong&gt;, and if we want to add combineReducer function, then we have to make every state object a reducer&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;rootReducer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;combineReducers&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="nx"&gt;firstNamedReducer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;nested&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;combineReducers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;reducerA&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reducerB&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reducerC&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="nx"&gt;secondNamedReducer&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;store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;createStore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rootReducer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the above example, state will look like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;firstNamedReducer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{},&lt;/span&gt;
&lt;span class="nx"&gt;nestedReducer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;reducerA&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{},&lt;/span&gt;
  &lt;span class="nx"&gt;reducerB&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{},&lt;/span&gt;
  &lt;span class="nx"&gt;reducerC&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{},&lt;/span&gt;
&lt;span class="nx"&gt;secondNamedReducer&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;&lt;strong&gt;Challenges&lt;/strong&gt; In real applications we have deep nested messed up structure like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;todoActivity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;documents&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{...},&lt;/span&gt;
  &lt;span class="nx"&gt;newActivity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{...},&lt;/span&gt;
  &lt;span class="nx"&gt;selectedTab&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;TODO&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;selectedActivities&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="p"&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This cannot be clubbed under "todoActivity" key as a combinereducers in cleaner way. The todoActivity is having more than 15 keys already, many of them are deeply nested. So, I think, I can create separate reducer for "documents" and rest of the keys can stay inside this object (reducer) only.&lt;br&gt;
But how???&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;newCleanerReducer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;action&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="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;todoActivityReducer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="na"&gt;documents&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;documentReducer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;documents&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&gt;action&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;As you can see, my all clutter related to documentReducer can be moved to a separate files with it's own types, action and reducer files. I can also adhere to Seperation of Concern here&lt;/p&gt;

</description>
      <category>redux</category>
      <category>react</category>
      <category>javascript</category>
      <category>codequality</category>
    </item>
    <item>
      <title>10 Concerns &amp; How we can make the whole React codebase cleaner </title>
      <dc:creator>Ashish Surana</dc:creator>
      <pubDate>Sun, 20 Oct 2019 19:13:22 +0000</pubDate>
      <link>https://dev.to/ashishsurana/10-concerns-how-we-can-make-the-whole-react-codebase-cleaner-3h9l</link>
      <guid>https://dev.to/ashishsurana/10-concerns-how-we-can-make-the-whole-react-codebase-cleaner-3h9l</guid>
      <description>&lt;p&gt;Creating a project from scratch is easy but shipping features after some time become very difficult and that comes with a cost of delays, bugs and eventually a broken product.&lt;/p&gt;

&lt;p&gt;"It is not enough for code to work. So if you want to go fast, if you want to get done quickly, if you want your code to be easy to write, make it easy to read."  - &lt;strong&gt;Robert C. Martin&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---SK87YFL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/9dwnwszeiryfatl3dp2f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---SK87YFL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/9dwnwszeiryfatl3dp2f.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Wow, this looks like a great tip, but how to make code readable especially when it already became a big ball of mud and every change is getting disastrous day by day. While working on a React.Js applications, we went with these strategies and principles:&lt;/p&gt;

&lt;p&gt;"Of course bad code can be cleaned up. But it's very expensive." - &lt;strong&gt;Robert C. Martin&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Linting and Prettification&lt;/strong&gt;&lt;br&gt;
The perfectly indented file is the first step of code readability. We added eslint rules and extended airbnb configuration with some relaxation like 'import/prefer-default-export' because it makes difficult to rename function at all places. We also tweaked &lt;code&gt;arrow-body-style&lt;/code&gt;, so that we can add breakpoints and debug the function if needed. And other minor changes like react/jsx-filename-extension&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Absolute Imports&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sI8HTTql--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/0zpqhfwmswjb8fg4c7lz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sI8HTTql--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/0zpqhfwmswjb8fg4c7lz.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
Importing functions, constants, components, is an integral part of building a JavaScript Application. But what in case of a deeply nested directory structure? We end up with writing &lt;code&gt;import {getUser} from &amp;amp;../../../store/users/action&lt;/code&gt;. To get rid of this, we configured our create-react-app setup by specifying &lt;code&gt;NODE\_PATH=src&lt;/code&gt; and we became free from dot/slash clutter and this was another little step.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Decoupling React components (Separation of Concerns)&lt;/strong&gt;&lt;br&gt;
Passing props to deep levels in React application becomes a bottleneck after 2-3 levels. We decoupled such props ladder and refactored components in a way so that they could be rendered with zero or minor dependency (with the help of redux). We also added Error Boundaries so that any failure doesn't propagate till the highest level.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Reusable components (Don't Repeat Yourself)&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--czhiwuBr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/i6j35ghltic1ujmhhb5z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--czhiwuBr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/i6j35ghltic1ujmhhb5z.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
Creating reusable components was a key thing in improving readability. We developed Modals, a theme for the app, Cards, Error Components, and other basic building blocks. And reused to maintain consistency and writing less code. We also focused on reusing code blocks at a granular level like filtering an array, transforming some API response, etc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Props Validation of components&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--v0GmrUIV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/xbt834g0kf6rrtt6j4uv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--v0GmrUIV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/xbt834g0kf6rrtt6j4uv.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
Adding props validation gave us a great advantage about the required, not required, string type, boolean type props. It gave us the flexibility to reuse easily and move components from one file to another for refactoring lengthy files.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Keeping business logic at one place (Keep It Simple Stupid)&lt;/strong&gt;&lt;br&gt;
If we talk about KISS, It's a little difficult to maintain in the frontend code base and it mostly contains major part of business logic. Hence we created Container components that assemble multiple pure components and renders the desired complex design implementation. By this approach, our helper components were as dumb as they could be. These pure components were free from any business logic and could be tested very easily at a later point in time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. Config at a Single Source&lt;/strong&gt;&lt;br&gt;
While engineering a product, we often get a requirement to drive the features based on different configurations. We kept it at the highest level and all the features were referenced by that singleton object. We also added the transformation to change the data modeling to ensure ease of access to the nested properties of a config data Object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8. Functional approach (Single Responsibility Principle)&lt;/strong&gt;&lt;br&gt;
Testing is the only way to maintain the readability of code through out the journey. But unit testing requires major shift in the way we think and write code. It becomes very easy if we have pure functions in our code and follow the principles of &lt;strong&gt;immutability&lt;/strong&gt; that prevents unwanted side effects. Functional approach is a value-oriented programming paradigm. We can use small pure functions that follow the &lt;strong&gt;S&lt;/strong&gt;ingle &lt;strong&gt;R&lt;/strong&gt;esponsibility &lt;strong&gt;P&lt;/strong&gt;rinciple (SRP) and do only one thing at a time. We can use multiple such pure functions to get the required result.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;9. Variable naming and writing comments&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pOjYaJdb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ygpcye2f95lzj7liv3lo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pOjYaJdb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ygpcye2f95lzj7liv3lo.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
Naming variable and function is the most underrated this I realized, We must spend enough time on deciding the names and there should not be any room for ambiguity. Comments are just an excuse for writing bad code. Writing them can never be a good practice as it becomes a liability to update comments on future code changes and consequently. However, In the case of complex business logic, it is justified to write comments. But our focus must be on writing clear, expressive and obvious code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;10. Enforcing rules so that code gets cleaner by every change&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rD_rJese--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/3rubu01edlqbvxhkq89g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rD_rJese--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/3rubu01edlqbvxhkq89g.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
After doing these major changes in codebase we put checks in our CI pipeline and integrated Jest using Enzyme to improve unit test coverage. We also integrated cypress for integration testing. We are having eslint, cypress, jest stages in our pipeline. Every change in codebase must ensure the success of these 3 stages.&lt;/p&gt;

&lt;p&gt;We are sure and confident in delivering features faster than ever without breaking anything. And yes, Now we don't say "It works" frequently. We will continue to make codebase cleaner than ever day by day. All the refactoring in our product is inspired by the book ' &lt;strong&gt;Clean Code&lt;/strong&gt; by &lt;strong&gt;Robert C. Martin&lt;/strong&gt;'.&lt;br&gt;
And here I conclude with this quote&lt;/p&gt;

&lt;p&gt;"Clean code always looks like it was written by someone who cares. - &lt;strong&gt;Robert C. Martin&lt;/strong&gt;"&lt;/p&gt;

&lt;p&gt;Hence, refactor your code and show care towards your product and show some ❤️. please!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>codequality</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
