<?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: Bogdan Jiang</title>
    <description>The latest articles on DEV Community by Bogdan Jiang (@bogdan0810).</description>
    <link>https://dev.to/bogdan0810</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%2F339122%2Fcdeada10-975c-453f-a81b-3423f6349360.jpg</url>
      <title>DEV Community: Bogdan Jiang</title>
      <link>https://dev.to/bogdan0810</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bogdan0810"/>
    <language>en</language>
    <item>
      <title>TypeScript Best Practices</title>
      <dc:creator>Bogdan Jiang</dc:creator>
      <pubDate>Tue, 13 Oct 2020 23:31:16 +0000</pubDate>
      <link>https://dev.to/bogdan0810/typescript-best-practices-565d</link>
      <guid>https://dev.to/bogdan0810/typescript-best-practices-565d</guid>
      <description>&lt;p&gt;To make code easy to read and maintain, you should follow some best practices.&lt;/p&gt;

&lt;p&gt;In this article, I am going to talk about some best practices you should follow to make everyone’s lives easier.&lt;/p&gt;

&lt;h1&gt;
  
  
  Group Function Overloads Together
&lt;/h1&gt;

&lt;p&gt;You can overload functions.&lt;/p&gt;

&lt;p&gt;This means you can have multiple function signatures for a function, which TypeScript can check for.&lt;/p&gt;

&lt;p&gt;To make your lives easier, you should group them together so that it’s easier to read:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function foo(a: string, b: string): string;

function foo(a: number, b: number): number;

function foo(a: any, b: any): any {
  console.log(a, b);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  Ordering Class Members
&lt;/h1&gt;

&lt;p&gt;You may consider ordering class members to make the members easier to read.&lt;/p&gt;

&lt;p&gt;You can order them by access modifiers, alphabetical order, etc.&lt;/p&gt;

&lt;p&gt;It’s good to stick with one.&lt;/p&gt;

&lt;p&gt;For example, instead of writing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Employee {
  private id: string;
  private empCode: number;
  private empName: string;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;you write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Employee {
  private empCode: number;
  private empName: string;
  private id: string;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;to sort them by alphabetical order.&lt;/p&gt;

&lt;h1&gt;
  
  
  Don’t Use module Keyword for Namespaces
&lt;/h1&gt;

&lt;p&gt;If you declare namespaces, then you should use the &lt;code&gt;namespace&lt;/code&gt; keyword.&lt;/p&gt;

&lt;p&gt;Instead of writing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module Math {
  function add(a: number, b: number): number {
    return a + b;
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;namespace Math {
  function add(a: number, b: number): number {
    return a + b;
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This way, you won’t confuse what you have with ES modules.&lt;/p&gt;

&lt;h1&gt;
  
  
  Visibility Declarations for Class Members
&lt;/h1&gt;

&lt;p&gt;To take advantage of the access control capabilities of TypeScript, you can add the visibility declarations or class members.&lt;/p&gt;

&lt;p&gt;For example, you write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Employee {
  private getSalary(): number {
    return 90000;
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You added the &lt;code&gt;private&lt;/code&gt; access modifier so that &lt;code&gt;getSalary&lt;/code&gt; can only be called by other methods in the class.&lt;/p&gt;

&lt;p&gt;There’s also the &lt;code&gt;public&lt;/code&gt; modifier to make the member available to outside code.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;protected&lt;/code&gt; makes the member available to subclasses and the current class.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;public&lt;/code&gt; is the default.&lt;/p&gt;

&lt;p&gt;You can also do the same for instance variables:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Employee {
  private empCode: number;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  Eliminate the Use of any Types
&lt;/h1&gt;

&lt;p&gt;You can eliminate the use of &lt;code&gt;any&lt;/code&gt; types in your code to take advantage of TypeScript’s type-checking capabilities.&lt;/p&gt;

&lt;p&gt;If you need something more flexible than static types, there are many ways to define them.&lt;/p&gt;

&lt;p&gt;You can use literal types to restrict values to only some literals.&lt;/p&gt;

&lt;p&gt;There’re union types to let you check for members for multiple types.&lt;/p&gt;

&lt;p&gt;The intersection type makes sure that the variable has members that are in both types.&lt;/p&gt;

&lt;p&gt;Index signatures let you check for dynamic properties.&lt;/p&gt;

&lt;p&gt;There’re many ways to avoid &lt;code&gt;any&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example, instead of writing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let bar: any;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;you write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let foo: string;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  No Empty Interface
&lt;/h1&gt;

&lt;p&gt;You shouldn’t have empty interfaces in your code since they’re useless.&lt;/p&gt;

&lt;p&gt;So instead of writing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface I {}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface I {
  bar: number;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  No for-in Loops
&lt;/h1&gt;

&lt;p&gt;for-in loops are legacy JavaScript syntax which has better modern alternatives.&lt;/p&gt;

&lt;p&gt;It’s bad since you need to use the &lt;code&gt;hasOwnProperty&lt;/code&gt; to check for non-inherited properties with it.&lt;/p&gt;

&lt;p&gt;Better alternatives include &lt;code&gt;Object.keys&lt;/code&gt; to get the non-inherited keys of an object.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Object.values&lt;/code&gt; to get the values and &lt;code&gt;Object.entries&lt;/code&gt; to get all entries.&lt;/p&gt;

&lt;p&gt;So instead of writing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (const key in obj) {
  if (obj.hasOwnProperty(key)) {
    console.log(obj[key]);
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;you write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (const key in Oject.keys(obj)) {
  console.log(obj[key]);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  No Import Side Effects
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;import&lt;/code&gt; should be used for import module members and using them.&lt;/p&gt;

&lt;p&gt;If they perform side effects, then it’s not good because it’s hard to statically analyze the code.&lt;/p&gt;

&lt;p&gt;So instead of writing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import 'foo';
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;you write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { bar } from 'foo';
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Some exceptions may be importing CSS with Webpack as modules.&lt;br&gt;
So you may still write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import 'styles.css';
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  No Explicit Type Declarations for Variable or Parameters with Literal Values
&lt;/h1&gt;

&lt;p&gt;It’s redundant to have type declarations for variables or parameters that are assigned with numbers strings or boolean.&lt;/p&gt;

&lt;p&gt;TypeScript can check these without an explicit type.&lt;/p&gt;

&lt;p&gt;Therefore, instead of writing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const foo: number = 10;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const foo = 10;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Grouping things together make them easier to read.&lt;/p&gt;

&lt;p&gt;Visibility modifiers are a useful TypeScript feature.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;any&lt;/code&gt; types can be replaced with many things.&lt;/p&gt;

&lt;p&gt;Use &lt;code&gt;namespace&lt;/code&gt; to declare namespace code.&lt;/p&gt;

&lt;p&gt;That’s it for today!&lt;/p&gt;

&lt;p&gt;Thanks for your reading and stay tuned!&lt;/p&gt;

</description>
      <category>typescript</category>
    </item>
    <item>
      <title>Javascript Pro Tips</title>
      <dc:creator>Bogdan Jiang</dc:creator>
      <pubDate>Sun, 11 Oct 2020 17:08:58 +0000</pubDate>
      <link>https://dev.to/bogdan0810/javascript-pro-tips-326l</link>
      <guid>https://dev.to/bogdan0810/javascript-pro-tips-326l</guid>
      <description>&lt;p&gt;In this article, I am going to talk about Javascript Pro Tips. Please learn how to write solid modern Javascript and avoid bad code from the olden days.&lt;/p&gt;

&lt;h1&gt;
  
  
  Debugging
&lt;/h1&gt;

&lt;p&gt;Actually it’s about console logging stuff.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How do you use console log?&lt;/strong&gt; There are good ways and bad ways to console log. Imagine you have three different objects. Each one assigned to its own variable like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const foo = { name: 'tom', age: 30, nervous: false } 
const bar = { name: 'dick', age: 40, nervous: false }
const baz = { name: 'harry', age:50, nervous: false }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The obvious way to log these is just one after the other.&lt;br&gt;
For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(foo);
console.log(bar);
console.log(baz);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But the main problem is we don’t know the name of the variable when this gets logged but there’s a trick we can use here called computed property names where we add the variables to an object.&lt;/p&gt;

&lt;p&gt;So you can do like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log({ foo, bar, baz });
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This does not only reduce the code footprint but it also tells us exactly which variable define this data. One line of code, one console log and all the information we need.&lt;/p&gt;

&lt;p&gt;But maybe this data is extra important so we want to make it stand out with some custom CSS styling. You can substitute data and also CSS styles by using % sign. &lt;/p&gt;

&lt;p&gt;So we’ll add %c and then have the second argument be our actual CSS style.&lt;br&gt;
For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log('%c My Friends', 'color: orange;')
console.log({ foo, bar, baz });
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;One thing you might have noticed is that the objects all share common properties so maybe we should display those as a table.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.table([foo, bar, baz]);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is really useful when you have an array of objects. You just do console table with the array.&lt;br&gt;
If you are benchmarking performance you can actually keep track of time in the console.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.time('looper');
let i = 0;
while (i &amp;lt; 1000000) { i ++ }
console.timeEnd('looper')
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Keeping track of time is great but what if you need to know where a console.log originated from.&lt;/p&gt;

&lt;p&gt;Let’s imagine that we have a really important function that deletes items from our database and we want to make sure that we don’t accidentally call this method twice. &lt;/p&gt;

&lt;p&gt;In this case you can add console trace to your function and it will give you a stack trace for where it was called and what defined it if we run this code, we’ll get a console log that tells us the function was defined on which number of line.&lt;br&gt;
For example, we can write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const deleteMe = () =&amp;gt; console.trace('bye bye database')
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  Destructuring
&lt;/h1&gt;

&lt;p&gt;Here, I am going to show you a few different ways you can make your code as concise and efficient as possible. &lt;/p&gt;

&lt;p&gt;Let’s imagine we have an object with some animal data and we need a function that will tell us how to feed the animal.&lt;br&gt;
For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function feed(animal) {
    return `Feed ${animal.name} ${animal.meal} kilos of ${animal.diet}`
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This doesn’t look too bad but you’ll notice that we’re repeating the word animal over and over again. There’s a technique called object destructuring that we can use to eliminate most of the repetition here.&lt;/p&gt;

&lt;p&gt;If we have a function that takes an object but we only need to use a handful of its properties. We can destrcture those in the argument itself. We just wrap it in brackets and then pass in the names of the object properties that we want to use.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function feed({ name, meal, diet }) {
    return `Feed ${name} ${meal} kilos of ${diet}`
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So now we can format the same string but we never actually have to use the word animal directly. this might seem like a modest gain on this simple function but when you have a big object with a lot of properties this can make a huge difference.&lt;/p&gt;

&lt;p&gt;If you don’t like this bracket syntax in the object argument, there is actually another way we can do this which is just as good.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function feed(animal) {
    const { name, meal, diet } = animal;
    return `Feed ${name} ${meal} kilos of ${diet}`
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now you can use properties like variables throughout the function and this tends to be the better way to go if you have multiple objects 2d structure and a single function.&lt;/p&gt;

&lt;h1&gt;
  
  
  Template Literals
&lt;/h1&gt;

&lt;p&gt;In this part, I am going to talk about template literals which we’ve already been using in the code. But actually there’s more to talk about here.&lt;/p&gt;

&lt;p&gt;For example if we have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const horse = {
   name: 'Topher',
   size: 'large',
   skills: ['jousting', 'racing'],
   age: 7
}
let bio = horse.name + ' is a ' + horse.size + ' horse skilled in ' + horse.skills.join(' &amp;amp; ')
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You would see a lot of string concatenation that looks like above where you have a variable plus string and you have to manage the spaces in between plus an expression plus a whole bunch of other stuff. &lt;br&gt;
This type of code is incredibly annoying to deal with but template literals in modern Javascript solve this problem completely.&lt;br&gt;
Instead of concatenating values together, we can actually interpolate them directly into the string. &lt;/p&gt;

&lt;p&gt;You can do this by defining your string with backticks and then use dollar sign brackets and then whatever variable or expression you want inside up there. Like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const horse = {
   name: 'Topher',
   size: 'large',
   skills: ['jousting', 'racing'],
   age: 7
}
bio = `${name} is a ${size} skilled in ${skills.join(' &amp;amp; ')}`
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will look a lot more readable and a lot easier to maintain.&lt;br&gt;
But you can actually take things a step further and build strings in a purely functional way. So we’ll write a function here called horse age that takes in array of strings as the first argument and then it can take whatever other arguments that wants after that. &lt;br&gt;
Imagine we have this function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function horseAge(str, age) {
   const ageStr = age &amp;gt; 5 ? 'old' : 'young';
   return `${str[0]}${ageStr} at ${age} years`;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But the interesting thing here is that instead of passing a regular argument to this function we can actually just attach it to a template literal and it will parse the arguments in it.&lt;br&gt;
For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const bio2 = horseAge`This horse is ${horse.age}`;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then it will handle all the other arguments in the order in which they appear inside of the dollar sign brackets. In other words you can take a single argument and use it to compose multiple values in the return string. &lt;br&gt;
This can be a very powerful concept for templating.&lt;/p&gt;
&lt;h1&gt;
  
  
  Spread Syntax
&lt;/h1&gt;

&lt;p&gt;Let’s imagine we have one object for a Pokemon and the other one for the stats that define its various attributes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const pikachu = { name: 'Pikachu' }
const stats = { hp: 40, attack: 60, defense: 45 }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let’s say we want to assign the properties from the stats object to the Pikach object. One way to do that is to just redefine them one by one on the original Pikachu object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pikachu['hp] = stats.hp
pikachu['attack'] = stats.attack
pikachu['defense'] = stats.defense
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;For one, this is just really ugly and verbose but we’re also mutating the original object when we most likely want to create a new immutable object because let’s say that our Pokemon levels up over time, we want to represent each level up as its own object.&lt;br&gt;
We could use Object.assign() here and take the original object and merge it in with the stats and this will merge them together from left to right.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const lv10 = Object.assign(pikachu, stats)
const lvl1 = Object.assign(pikachu, { hp: 45 })
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This isn’t too bad but there’s a more concise way to do this with the spread syntax. By creating a new object and placing our existing objects in it with three dots in front of them. &lt;br&gt;
For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const lvl0 = { ...pikachu, ...stats }
const lvl1 = { ...pikachu, hp: 45 }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will compose a new object from left to right. So the property is farthest to the right will have the priority. Again this is mostly just syntactic sugar and it just makes your code more readable and easier to maintain.&lt;br&gt;
And it’s also possible to use the spread syntax on arrays. Let’s imagine we have an array of strings and we need to push additional items to this array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let pokemon = ['Arbok', 'Raichu', 'Sandshrew']
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The old school way to do this would be to just push new items to the array one by one. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pokemon.push('Bulbasaur')
pokemon.push('Metapod')
pokemon.push('Weedle')
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But in today’s world, we can reduce these three lines of code to just one by defining an array with the new items and in the spread syntax on the original array. Like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pokemon = [...pokemon, 'Bulbasaur', 'Metapod', 'Weedle']
pokemon = ['Bulbasaur', ...pokemon, 'Metapod', 'Weedle']
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  Loops
&lt;/h1&gt;

&lt;p&gt;Let’s imagine that we have an array of numbers here that represent the other totals.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const orders = [500, 30, 99, 15, 223]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now we need to compute some values based on this array such as the order total. Maybe we need to add some tax to each one and filter out the high value orders to be reviewed by a manager. One option is to just use a classic for loop like you’ll find in pretty much every programming language. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const total = 0;
const withTax = [];
const highValue = [];
for(i = 0; i &amp;lt; orders.length; i ++) {
  // Reduce
  total += orders[i];
  // Map
  withTax.push(orders[i] * 1.1);
  // Filter
  if (orders[i] &amp;gt; 100) {
    highValue.push(orders[i])
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This code is very ugly and it’s mutating values might make our code a little more unpredictable. Luckily we can reduce this down to just three lines of code by using modern Javascript array methods. Like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Reduce
const total = orders.reduce((acc, cur) =&amp;gt; acc + cur)
// Map
const withTax = orders.map(v =&amp;gt; v * 1.1)
// Filter
const highValue = orders.filter(v =&amp;gt; v &amp;gt; 100)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  async / await
&lt;/h1&gt;

&lt;p&gt;Let’s create a method called random that returns a promise that resolves to a random number asynchronously.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const random = () =&amp;gt; {
  return Promise.resolve(Math.random())
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now we want to retrieve three different asynchronous numbers one after the other and then add them all together at the end. That might seem like a silly example but that’s actually how things work a lot of times in the real world when you have to retrieve one item from the database get some data retrieving another item from an API and so on.&lt;br&gt;
With promises you wait for an asynchronous value to resolve and then you handle it with a callback function inside of then. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const sumRandomAsyncNums = () =&amp;gt; {
  let first;
  let second;
  let third;
return random()
       .then(v =&amp;gt; {
           first = v;
           return random();
       })
       .then(v =&amp;gt; {
           second = v;
           return random();
       })
       .then(v =&amp;gt; {
           third = v;
           return first + second + third;
       })
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can rewrite our promise chain. The only difference is adding async in front of the function which will force it to return a promise. Like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const sumRandomAsyncNums = async () =&amp;gt; {

    const first = await random();
    const second = await random();
    const third = await random();
    return first + second + third;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The real benefit here is that we can use a await in front of our promises and have them resolve to an actual variable value. So instead of using those then callbacks we can just say&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const first = await random();&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;and do the same thing for the second and third number. Now it’s much easier to read and understand this code because we can just go line by line and see that we’re waiting one number awaiting another number and so on.&lt;/p&gt;

&lt;p&gt;This async/await concept is one of the most awesome things to ever happen to Javascript!&lt;/p&gt;

&lt;p&gt;That’s it for today.&lt;/p&gt;

&lt;p&gt;Thanks for you reading!&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>React Developer Tooling</title>
      <dc:creator>Bogdan Jiang</dc:creator>
      <pubDate>Tue, 06 Oct 2020 21:34:47 +0000</pubDate>
      <link>https://dev.to/bogdan0810/react-developer-tooling-4noo</link>
      <guid>https://dev.to/bogdan0810/react-developer-tooling-4noo</guid>
      <description>&lt;p&gt;Today I am going to talk about developer tooling to improve these 3 areas.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Write code quickly&lt;/li&gt;
&lt;li&gt;Debug code effectively&lt;/li&gt;
&lt;li&gt;Update code easily&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  1. Fast Refresh (write code quickly)
&lt;/h2&gt;

&lt;p&gt;Fast refresh makes your React app reloads slick and painless on code changes and Re-renders only the required React components if a code change is made instead of reloading the entire app.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is it the same as hot reloading?
&lt;/h3&gt;

&lt;p&gt;The answer is NO. The hot reloading has some limitations.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No function component support (React hook)&lt;/li&gt;
&lt;li&gt;No error recovery&lt;/li&gt;
&lt;li&gt;Often did not reload after changes&lt;/li&gt;
&lt;li&gt;Required brittle code transforms&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How is Fast Refresh different?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;First class feature for React core team&lt;/li&gt;
&lt;li&gt;Built for all renderers&lt;/li&gt;
&lt;li&gt;Supports function components and Hooks&lt;/li&gt;
&lt;li&gt;Recovers after errors&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How does Fast Refresh work?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Decides when to ‘update’ or ‘remount’&lt;/li&gt;
&lt;li&gt;Computes a component ‘signature’&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What are limitations of Fast Refresh?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Resets class component state&lt;/li&gt;
&lt;li&gt;Mixed React and non-React exports&lt;/li&gt;
&lt;li&gt;Memoization&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  When can you use Fast Refresh?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;React Native v61+&lt;/li&gt;
&lt;li&gt;React DOM (soon)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. Developer Tools (debug code effectively)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Why rewrite React DevTools?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Better performance&lt;/li&gt;
&lt;li&gt;Support new APIs&lt;/li&gt;
&lt;li&gt;Add new UX features&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What can you do with React DevTools?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Navigate Unfamiliar Projects&lt;/li&gt;
&lt;li&gt;Iterate and Test&lt;/li&gt;
&lt;li&gt;Profile and Measure Performance&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. Codemods (update code easily)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is a codemod?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Tool for refactoring large codebase&lt;/li&gt;
&lt;li&gt;Like a really smart field find-and-replace&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How do codemods work?
&lt;/h3&gt;

&lt;p&gt;It starts by reading Javascript source code from the file. The source code is parsed and converted into an abstract syntax tree or AST for short.&lt;/p&gt;

&lt;p&gt;An AST is a tree representation of the structure of a program.&lt;/p&gt;

&lt;p&gt;Each node in the tree corresponds to an element in the source code.&lt;/p&gt;

&lt;p&gt;The codemod itself is a small Javascript program that modifies this tree. And it can do more complicated edits than find and replace code.&lt;/p&gt;

&lt;h3&gt;
  
  
  What are codemods good for?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Renaming things&lt;/li&gt;
&lt;li&gt;Reordering things&lt;/li&gt;
&lt;li&gt;Replacing things(under certain conditions)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What are codemods not good for?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Complex behavioral changes&lt;/li&gt;
&lt;li&gt;Fixing bugs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Check React codemods here!&lt;/p&gt;

&lt;p&gt;And you can also check the Demo!&lt;/p&gt;

&lt;p&gt;That’s it for today.&lt;/p&gt;

&lt;p&gt;Stay tuned! Happy coding!&lt;/p&gt;

</description>
      <category>react</category>
    </item>
  </channel>
</rss>
