<?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: Codebreather</title>
    <description>The latest articles on DEV Community by Codebreather (@oyelowo).</description>
    <link>https://dev.to/oyelowo</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%2F160074%2F1a3c6074-d9fb-404e-a19f-5334e6179a28.jpeg</url>
      <title>DEV Community: Codebreather</title>
      <link>https://dev.to/oyelowo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/oyelowo"/>
    <language>en</language>
    <item>
      <title>Coding Best Practices</title>
      <dc:creator>Codebreather</dc:creator>
      <pubDate>Mon, 23 Mar 2020 09:10:29 +0000</pubDate>
      <link>https://dev.to/oyelowo/coding-best-practices-2a65</link>
      <guid>https://dev.to/oyelowo/coding-best-practices-2a65</guid>
      <description>&lt;p&gt;Code guidelines&lt;/p&gt;

&lt;p&gt;Prefer small, simpler functions and components over longer ones. Bigger functions and components can always be decomposed into smaller parts.&lt;/p&gt;

&lt;p&gt;Why? &lt;/p&gt;

&lt;p&gt;Bigger Functions/components are harder to follow/read.  They can also introduce complexities into your type declarations and testing.&lt;/p&gt;

&lt;p&gt;Ideally, Functions should hardly be longer than 20 lines. When it starts to get longer than that, start thinking of how you could break down the complexity and abstract more specific logics out into other functions, in line with Single Responsibility of the SOLID principle.&lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;keeping things more focused can help keep others reading your code engaged.&lt;/p&gt;

&lt;p&gt;DRY up your code, however, be mindful of premature abstractions/generalizations. It’s okay to repeat somethings. It can help to save some headaches down the line if you did not make a wide generalization earlier on.  Use your discretion to strike a balance.&lt;/p&gt;

&lt;p&gt;It is okay not to know every underlying implementation details of each sub-function just the same way we utilize helpers from third party libraries and not worry about their implementation details. We mostly care about what arguments the function takes and what it returns. Typescript and automated testing of your helper function can help to increase confidence. However, when creating abstraction, consider if people would ever have to understand the nitty-gritty of your abstraction to use it well.  Otherwise, consider simplifying it &lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;A&lt;/p&gt;

&lt;p&gt;React Components should hardly be longer than 100 - 200 lines. Rather than having several logics in a component, abstract the logic into normal helper functions or hooks if the logic relies on other hooks.&lt;/p&gt;

&lt;p&gt;Prefer at most 5-10 custom properties for your react component. Having more custom properties might be a sign that you need to break down the component into smaller parts and group them into a logical shared folder&lt;/p&gt;

&lt;p&gt;Avoid passing props beyond one level. Prop drilling can often make debugging harder and also make it difficult for others to follow your code. When you need to pass props beyond one level, prefer using Context API with hooks. This combined with typescript greatly simplifies things&lt;/p&gt;

&lt;p&gt;Be mindful of premature optimizations with React’s memoizing functionalities as React is quite fast. Memoizing introduces complexities into your codebase, hence, you want to be sure that you are reaping the benefits&lt;/p&gt;

&lt;p&gt;Use very specific descriptive names for variables, functions, components, and types, even when it seems longer e.g getEntireStudentsSummerData over getData. &lt;/p&gt;

&lt;p&gt;Why? &lt;/p&gt;

&lt;p&gt;It reduces the mental work for others and yourself to understand your code. It documents your code better and prevents the need for comments oftentimes. Too much commenting of code can also be a bad practice because the code and comment could get out of sync if the implementation changes at some point but the developer forgets to change the comment. &lt;/p&gt;

&lt;p&gt;Prefer pure functions whenever possible. &lt;/p&gt;

&lt;p&gt;Why? &lt;/p&gt;

&lt;p&gt;They are easier to test and lead to fewer bugs.&lt;/p&gt;

&lt;p&gt;Prefer arrow functions whenever possible. &lt;/p&gt;

&lt;p&gt;Why? &lt;/p&gt;

&lt;p&gt;They are easier to type and more predictable(no this complication).&lt;/p&gt;

&lt;p&gt;Type your react component as FunctionComponent(FC)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface ModalProps{
  id: string
}
const Modal:FC&amp;lt;ModalProps&amp;gt; = () =&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
    Some text here
    &amp;lt;/div&amp;gt;
  )
}

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



&lt;p&gt;Avoid big reducers as they are just like every other function. Reducers can always be split into sub-reducers.The React ecosystem has moved from HOC/Props towards more functional hooks, hence, we should try to move towards that as they are simpler, easier to type and debug&lt;/p&gt;

&lt;p&gt;Avoid reinventing the wheel. &lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;It saves time and energy&lt;/p&gt;

&lt;p&gt;However, avoid overusing of third-party libraries. If it will take just a few lines to implement functionality, it might be better to just implement that yourself rather than downloading an entire library?&lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;It reduces the work of upgrading dependencies.&lt;/p&gt;

&lt;p&gt;It helps with bundle-sizes, especially when the alternative library does not support tree-shaking.&lt;/p&gt;

&lt;p&gt;Not having to deal with breaking changes introduced by the third-party library.&lt;/p&gt;

&lt;p&gt;Be mindful of using third-party libraries with smaller community/support, except if it’s critical and time-saving.&lt;/p&gt;

</description>
      <category>react</category>
      <category>typescript</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>HOW TO WRITE MAINTAINABLE CODE</title>
      <dc:creator>Codebreather</dc:creator>
      <pubDate>Tue, 17 Mar 2020 06:00:26 +0000</pubDate>
      <link>https://dev.to/oyelowo/how-to-write-maintainable-code-2952</link>
      <guid>https://dev.to/oyelowo/how-to-write-maintainable-code-2952</guid>
      <description>&lt;p&gt;Frontend guidelines&lt;/p&gt;

&lt;p&gt;Prefer small, simpler functions and components over longer ones. Bigger functions and components can always be decomposed into smaller parts.&lt;br&gt;
Why? Bigger Functions/components are harder to follow/read.  They can also introduce complexities into your type declarations and testing.&lt;/p&gt;

&lt;p&gt;Ideally, Functions should hardly be longer than 20 lines (humans have short attention spans) and keeping things more focused can help keep others reading your code engaged. When it’s getting longer than that, start thinking of how you could break down the complexity and abstract more specific logics out into other functions, inline with Single Responsibility of the SOLID principle.  &lt;/p&gt;

&lt;p&gt;It is okay not to know every underlying implementation details of each sub-function just the same way we utilize helpers from third party libraries and not worry about their implementation details.  Testing increases the confidence of a codebase and smaller functions enable easier testing. &lt;/p&gt;

&lt;p&gt;React Components should hardly be longer than 100 - 200 lines. Rather than having several logics in a component, abstract the logic into normal helper functions or hooks if the logic relies on other hooks. &lt;/p&gt;

&lt;p&gt;Prefer at most 5-10 custom properties for your react component. Having more custom properties might be a sign that you need to break down the component into smaller parts and group them into a logical shared folder&lt;/p&gt;

&lt;p&gt;Avoid passing props beyond one level. Prop drilling can often make debugging harder and also make it difficult for others to follow your code. When you need to pass props beyond one level, prefer using Context API with hooks. This combined with typescript greatly simplifies things&lt;/p&gt;

&lt;p&gt;Be mindful of premature optimizations with React’s memoizing functionalities as React is quite fast. Memoizing introduces complexities into your codebase, hence, you want to be sure that you are reaping the benefits&lt;/p&gt;

&lt;p&gt;Use more specific descriptive names for variables, functions, components and types e.g getEntireStudentsSummerData  over getData&lt;/p&gt;

&lt;p&gt;Prefer pure functions whenever possible&lt;/p&gt;

&lt;p&gt;Avoid big reducers as they are just like every other function. Reducers can always be split into subReducers.&lt;br&gt;
The React ecosystem has moved from HOC/Props towards more functional hooks, hence, we should try to move towards that as they are simpler, easier to type and debug&lt;/p&gt;

</description>
      <category>react</category>
      <category>redux</category>
      <category>functions</category>
      <category>hooks</category>
    </item>
    <item>
      <title>How to reduce your redux boilerplate by up to 80% - reduxios</title>
      <dc:creator>Codebreather</dc:creator>
      <pubDate>Sun, 12 Jan 2020 17:48:21 +0000</pubDate>
      <link>https://dev.to/oyelowo/how-to-reduce-your-redux-boilerplate-by-80-reduxios-4o58</link>
      <guid>https://dev.to/oyelowo/how-to-reduce-your-redux-boilerplate-by-80-reduxios-4o58</guid>
      <description>&lt;h1&gt;
  
  
  &lt;code&gt;reduxios&lt;/code&gt;- Reduce your redux boilerplate by 80% in 4 simple steps
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;This library provides utility functions specifically for handling reducers and actions related to data fetching which helps to decrease redux data fetching code by about 80%&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install reduxios
# or
yarn add reduxios
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Example Usage in 4 simple steps
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Generate the helper with the basename for action types&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;reduxios&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;reduxios&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;booksStoreFetcher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;reduxios&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Book&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;FETCH_BOOKS&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt; &lt;strong&gt;Create the Reducer, which will handle various Fetch states.
It takes the initial data as an argument&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;booksReducer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;booksStoreFetcher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createReducer&lt;/span&gt;&lt;span class="p"&gt;([]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt; &lt;strong&gt;Makes the action hook for Fetching your data or calling the API.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;axios&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;axios&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;useFetchBooks&lt;/span&gt; &lt;span class="o"&gt;=&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;booksStoreFetcher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useResource&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;axiosInstance&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;axios&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// This can also be an axios instance created&lt;/span&gt;
    &lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;get&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/books&lt;/span&gt;&lt;span class="dl"&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;ul&gt;
&lt;li&gt;
&lt;strong&gt;Use the action hook and state in your component. No need to dispatch the action.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;BooksList&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;FC&lt;/span&gt; &lt;span class="o"&gt;=&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fetchBooks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useFetchBooks&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fetchState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;axiosErrorResponse&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useSelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;RootState&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;books&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;useEffect&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="nx"&gt;fetchBooks&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="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;My Book List&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;ul&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;book&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;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Book&lt;/span&gt; &lt;span class="na"&gt;key=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;book=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;book&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;ul&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;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;That's it! No need to manually write out action creators, type declarations, reducers and data fetching attempt/success/failure/reset handling. You get everything out of the box!&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Want More Detailed Explanation?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/Oyelowo/lib-steroids/tree/master/packages/reduxios-only"&gt;Check the Github Repository here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>redux</category>
      <category>react</category>
      <category>axios</category>
      <category>restapi</category>
    </item>
  </channel>
</rss>
