<?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>Forem: Rommel Suarez</title>
    <description>The latest articles on Forem by Rommel Suarez (@hackmel).</description>
    <link>https://forem.com/hackmel</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%2F382891%2F988c3b8a-0ead-4eb9-a43b-abbec1331f11.jpeg</url>
      <title>Forem: Rommel Suarez</title>
      <link>https://forem.com/hackmel</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/hackmel"/>
    <language>en</language>
    <item>
      <title>What I learned while building a Snake Game with React and Recoil</title>
      <dc:creator>Rommel Suarez</dc:creator>
      <pubDate>Thu, 01 Oct 2020 03:23:35 +0000</pubDate>
      <link>https://forem.com/hackmel/what-i-learned-while-building-a-snake-game-with-react-and-recoil-13cd</link>
      <guid>https://forem.com/hackmel/what-i-learned-while-building-a-snake-game-with-react-and-recoil-13cd</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3487nww4xesn8cjrj1tt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3487nww4xesn8cjrj1tt.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Recoil is a new state management library developed by Facebook used for React applications. It has a very interesting concept that promises simplicity, but with powerful capabilities. Unlike redux, it has a simple framework without messy boiler plates. I decided to take a look and see how it works. However, instead of making a simple app, I thought of building a game with it. I ended up building a Snake Game to fully test my understanding of how the library works. I know some will say I don’t need to have a sophisticated state manager to build this game and I certainly agree. However, I also believe that the most effective way to learn a technology is to apply it in an unusual way or on a more complicated application. Writing a game in React is unusual and complicated, but is possible. It’s a perfect way to learn Recoil.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fh8bu60gmhw0k4fi3avi1.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fh8bu60gmhw0k4fi3avi1.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Atom
&lt;/h1&gt;

&lt;p&gt;Unlike redux and react context, Recoil has the concept of multiple units of states called &lt;strong&gt;Atom&lt;/strong&gt;, where components can subscribe to. Components will render only when the atom where they subscribe for changes. This will avoid unnecessary rendering when state changes. An atom can be defined by using the &lt;em&gt;atom()&lt;/em&gt; function. An atom should have a unique key and a default value for its state. On my game I have created 3 separate atoms that represent its own data:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The &lt;strong&gt;SnakeTailState&lt;/strong&gt; holds the information of all the snake's tail location, by default it has 3 tails. The &lt;strong&gt;FoodState&lt;/strong&gt; holds the location where the food will appear in the screen. And lastly, the &lt;strong&gt;KeyPressState&lt;/strong&gt; will hold the keyboard entries that will tell the direction of the snake. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fbngn2td0qmemwophs4zy.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fbngn2td0qmemwophs4zy.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  React hooks
&lt;/h1&gt;

&lt;p&gt;Recoil is designed for React Developers who love hooks. Yes, if you love developing functional components and use hooks a lot then you will enjoy the benefits of recoil. Recoil has some ready made hooks in order to access and update atoms.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;useRecoilState(stateKey) returns a tuple where the first element is the value of state and the second element is a setter function that will update the value of the given state when called. &lt;/li&gt;
&lt;li&gt;useSetRecoilState(stateKey) returns a setter function for updating the value of writeable Recoil state.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These sample hooks are just among the other hooks that you can use to access and modify your atoms. On my code I used &lt;strong&gt;useRecoilState&lt;/strong&gt; to access the &lt;strong&gt;SnakeTailState&lt;/strong&gt; and pass it to my snake component that display it on the screen. While &lt;strong&gt;useSetRecoilState&lt;/strong&gt; is used to update the &lt;strong&gt;KeyPressState&lt;/strong&gt; everytime a user pressed the keyboard. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fuh194dsov2y74jexyuun.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fuh194dsov2y74jexyuun.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Selector
&lt;/h1&gt;

&lt;p&gt;Selectors are functions or derived state in Recoil. Recoil can have a &lt;strong&gt;get&lt;/strong&gt; and a &lt;strong&gt;set&lt;/strong&gt; function. Get functions can return calculated values from an atom or other selectors. A get function does not change the values of the state. However, a set function, also called a writeable selector can be used to change or update a state.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;As you can see on my selector, I built the following logic that corresponds to my states. These selector can communicate with other atoms and other selectors to build a new set of states. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Calculate how to create new tails whenever the snake has eaten the food. &lt;/li&gt;
&lt;li&gt;Decide where the food will randomly appear on the screen. &lt;/li&gt;
&lt;li&gt;Check the snake's next direction based on the key pressed.&lt;/li&gt;
&lt;li&gt;Check if the food was eaten&lt;/li&gt;
&lt;li&gt;Check if the snake hit the walls&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I don't have to write those logic inside the presentation layer which made my code very clean. All I have to do is use Recoils helper hooks to access the selectors from the presentation layer, the same way I access an atom.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h1&gt;
  
  
  Findings and opinion
&lt;/h1&gt;

&lt;p&gt;For me Recoil is a better choice on state management. One reason is that it promotes one of the SOLID principle, &lt;strong&gt;Single responsibility principle&lt;/strong&gt;. By designing your state to have  different units of state that represents one thing you avoid making a convoluted state.&lt;/p&gt;

&lt;h1&gt;
  
  
  Why does a single global state is bad?
&lt;/h1&gt;

&lt;p&gt;If our app is simple, probably we can get away with it. But, as our app becomes larger and more complicated, having a single global state that holds everything will be problematic. &lt;/p&gt;

&lt;h1&gt;
  
  
  Imagine our state as a database
&lt;/h1&gt;

&lt;p&gt;When we first design our database, do we build one table to represent our data? Unless we have a very good reason, a database should always be  &lt;strong&gt;normalized&lt;/strong&gt;. Each tables on our database should represent one particular data, example: Employee, Department, Salary etc. And I believe that states should also be designed the same way. Each state should only represent a particular set of data.&lt;br&gt;
On a database, if we want to combine rows between tables, we can define a view. On Recoil, we can do the same by using selectors. &lt;/p&gt;

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

&lt;p&gt;Building a game with React is fun though not recommended, but it helped me understand Recoil much better. I will probably continue this by writing another version of my snake game app using Redux and share my experience.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>html</category>
    </item>
    <item>
      <title>WebAssembly and the evolution of web front end development</title>
      <dc:creator>Rommel Suarez</dc:creator>
      <pubDate>Tue, 19 May 2020 08:37:40 +0000</pubDate>
      <link>https://forem.com/hackmel/webassembly-and-the-evolution-of-web-development-2c6e</link>
      <guid>https://forem.com/hackmel/webassembly-and-the-evolution-of-web-development-2c6e</guid>
      <description>&lt;p&gt;Javascript has played a very important role in shaping web development over the years. It has evolved so much that it is now used to write software solutions other than browser applications. It has also been adapted to write server side applications and machine learning. Indeed, javascript has become very popular and powerful nowadays. However, as javascript matures, web front end development is still on its early stage. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffu4nunmwbgbnggb7ujds.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffu4nunmwbgbnggb7ujds.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Innovation starvation&lt;/strong&gt;&lt;br&gt;
The web browser has been around for about 30 years. However, better user experience on web apps only began just recently. It only started when smart phones arrived and dominated the market. Suddenly HTML5 was introduced and Single Page Applications or (SPA) conquered the web browser. Also, during this time, ECMAScript finally unveiled ES6 that gave modern features to javascript implementations. JavaScript libraries and frameworks such as ReactJS, Angular, Vue and more have sprouted everywhere. But why did it take a while for us to make these innovations? Why do we need to wait for smart phones and tablets to realize these needs?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fezrqtx00ytu6bdpzri9g.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fezrqtx00ytu6bdpzri9g.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Someone has tried to make innovations, but failed&lt;/strong&gt;&lt;br&gt;
Innovations like this has been done years before to help developers create modern and responsive web app without javascript. Notably, Sun Microsystems built and design Java to run Applets in the browser to make a better user experience. Who would forget Microsoft Silverlight and Adobe Flash that were built for creating rich internet applications. But ultimately there is one thing in common about them, they all failed because they are not naturally part of the browser as compared to javascript. You need to install a runtime environment in order to run these type of applications in the browser. For some reason, some browsers stopped supporting it, especially on mobile phones.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F74oloy77tg5ff4xheuqb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F74oloy77tg5ff4xheuqb.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why javascript alone is not sufficient?&lt;/strong&gt;&lt;br&gt;
When working with machine learning, API development and game development you could choose the tools and programming language you want. However, in the web browser there is only javascript. Javascript is good, but it's not the best. Sadly, it's the only one we have. There are a lot of pitfalls and weirdness on javascript. The good and the bad of javascript are widely discussed on the book "Javascript the good parts" written by Douglas Crockford. Even though ES6 tried to solve those issues, not all browsers supported it yet. So developers relied on transpilers. Transpilers read codes written in ES6 and spit out javascript guaranteed to work anywhere even on some old browsers. You can also use other programming languages to help you write better codes and again use transpilers to convert them back to javascript. However, the browser doesn't know these languages, they are just a facade. Try to debug your code from the browser and you'll be surprised. It would be great if one day we can have the freedom to build apps in the browsers where languages like C#, Java, C++ and your favorite programming languages can natively run and debugged in the browser. Just compile it into machine code and the browser can execute it faster without any third party run time environments. You don't have to use transpilers and other tools such as webpack, grunt and gulp to run your codes on the browser which I think are just hacks and workaround to make javascript work. And still, you have the option to use javascript. That means development will be easier and simpler. You can use one language to build your app. You can write better and efficient codes because most of the features that javascript is missing are already built in other languages.   &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffcj6iaqpo6hx0ecl5b1w.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffcj6iaqpo6hx0ecl5b1w.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;WebAssembly to the rescue&lt;/strong&gt;&lt;br&gt;
On 2015 WebAssembly was first announced to the public. It has proved the possibility of running an application written in a language different from javascript. And yes, everyone was astonished. It's like a dream come true. It doesn't need a third party run time environment. A game written completely on Unity3D was compiled to target WebAssembly with a binary format. It can execute on the browsers with a performance close to native code. This has opened up a lot of possibilities for app development on the browser. Developers that have no background in web development can now write apps that run on browsers. They don't even need to be an expert on CSS,HTML or javascript. WebAssembly is very promising and most browsers are supporting it now. It's still very young and there's more work coming along to make it better. One of the major excitement is Blazor WebAssembly. It's a framework for building Single Page Apps using C# created by Microsoft. It's an open-source framework where you can use and build  .Net Core components of your app and run it in a browser. Before, you can only use javascript to build this kind of applications. I'm pretty sure, with this innovation a few more technologies and frameworks that uses a different programming language will come out. It's a pretty exciting future ahead of us isn't it?  &lt;/p&gt;

</description>
      <category>webassembly</category>
      <category>dotnet</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
