<?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: Javier Gonzalez</title>
    <description>The latest articles on DEV Community by Javier Gonzalez (@javier123454321).</description>
    <link>https://dev.to/javier123454321</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%2F271297%2Fa8d33659-8169-4f74-874e-9f52cf6431fc.jpeg</url>
      <title>DEV Community: Javier Gonzalez</title>
      <link>https://dev.to/javier123454321</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/javier123454321"/>
    <language>en</language>
    <item>
      <title>ECDSA in Bitcoin</title>
      <dc:creator>Javier Gonzalez</dc:creator>
      <pubDate>Tue, 19 Apr 2022 18:22:17 +0000</pubDate>
      <link>https://dev.to/javier123454321/ecdsa-in-bitcoin-4130</link>
      <guid>https://dev.to/javier123454321/ecdsa-in-bitcoin-4130</guid>
      <description>&lt;p&gt;Here is this article in short video form:&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=tl6RGKKvHto"&gt;https://www.youtube.com/watch?v=tl6RGKKvHto&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since I never took computer science courses, I have to catch up with some of the fundamentals, and I find them fascinating. One such fundamental of computer science is cryptography, and today I want to go through how it is applied to generate wallets in Bitcoin from scratch.&lt;/p&gt;

&lt;p&gt;The math used to generate a wallet from scratch is called Elliptic Curve Cryptography. It used in Bitcoin and is crucial to maintaining the integrity of the protocol. The amazing part is that it is so secure, that you don't have to  even be connected to the internet because this process is just based on math.&lt;/p&gt;

&lt;p&gt;Let’s get into how it works.&lt;/p&gt;

&lt;p&gt;Everything is based on this curve called the Secp256k1 derived by the function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To use it, we generate any random number up to 2^256 and multiply it by a constant point G in the curve&lt;/p&gt;

&lt;p&gt;The point G is constant for every bitcoin address and here it is as x and y coordinates:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7RG_HHau--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9q0y37h6s9wjehe3s4zd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7RG_HHau--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9q0y37h6s9wjehe3s4zd.png" alt="Two points that make up G" width="880" height="313"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The math that is more interesting and we are more concerned about is how to generate a public key from ANY random number. And that math is a simple function here:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;K&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;  &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;G&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;where &lt;code&gt;k&lt;/code&gt; is the private key, &lt;code&gt;G&lt;/code&gt; is a constant point called the generator point, and &lt;code&gt;K&lt;/code&gt; is the resulting public key. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;k&lt;/code&gt; can be anything, the important part is that it is random. Once you have a random value for &lt;code&gt;k&lt;/code&gt; you just multiply that by G which is the process of extending a tangent until it intersects with a different point in the line and reflecting that point across the X axis.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KkuUo-v4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xvvi75nz4z6hya60uwh7.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KkuUo-v4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xvvi75nz4z6hya60uwh7.gif" alt="Image showing the above description" width="445" height="388"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The result is called your public key and can be shared with ANYONE because it is extremely improbable that someone can derive the private key backwards from it.&lt;/p&gt;

&lt;p&gt;You still have to run it through some hashing functions to generate an address, but this is pretty much it. This math has been around since the 80s and is considered secure enough to hide government secrets. By signing a transaction with that private key, you are basically proving that you are the only person with access to the private key used to generate that public key.&lt;/p&gt;

</description>
      <category>bitcoin</category>
      <category>crypto</category>
      <category>cryptography</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Solidity Gas Optimizations pt.4 - Efficient Strings</title>
      <dc:creator>Javier Gonzalez</dc:creator>
      <pubDate>Sun, 17 Apr 2022 16:35:09 +0000</pubDate>
      <link>https://dev.to/javier123454321/solidity-gas-optimizations-pt4-efficient-strings-46db</link>
      <guid>https://dev.to/javier123454321/solidity-gas-optimizations-pt4-efficient-strings-46db</guid>
      <description>&lt;p&gt;In the previous lesson, we saw a bit about how the solidty compiler and the EVM stores data in 32 byte 'buckets' or 'slots'. This applies to every operation that deals with storage. This can come in handy when dealing with strings, specially when using &lt;code&gt;revert()&lt;/code&gt; or &lt;code&gt;require()&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a String?
&lt;/h2&gt;

&lt;p&gt;In the Solidity Documentation we can see that:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Variables of type bytes and string are special arrays. The bytes type is similar to bytes1[], but it is packed tightly in calldata and memory. string is equal to bytes but does not allow length or index access.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Basically, strings are arrays of UTF-8 characters, and arrays are basically a fixed length sequence of storage slots located next to each other. Therefore, like everything else we need to think in terms of storage buckets&lt;/p&gt;

&lt;h2&gt;
  
  
  How to use this to our advantage
&lt;/h2&gt;

&lt;p&gt;This means that when we compile our contracts, we would prefer to use a single slot for the strings we use.&lt;/p&gt;

&lt;p&gt;Each character in a string is a UTF-8 Encoded byte, meaning that your strings can be up to 32 characters in length if you do not use specially encoded characters like emojis to be contained in a single storage slot.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;A Note About UTF-8&lt;/strong&gt;&lt;br&gt;
UTF encoding is a standard for how computers represent each character in a language. All the standard English characters take up one byte, but things get a bit more difficult when we start with characters like 💩 which takes up 4 bytes (64 bits). &lt;br&gt;
Here's a great resource to look up how many bytes a string takes up under UTF-8 Encoding. &lt;a href="https://mothereff.in/byte-counter"&gt;https://mothereff.in/byte-counter&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So basically, when we can use this everywhere for example in &lt;code&gt;revert&lt;/code&gt; statements, we can think of how to make these messages fit in multiples of 32 bytes. &lt;/p&gt;

&lt;p&gt;So instead of having this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// 40 bytes or two slots&lt;/span&gt;
&lt;span class="nx"&gt;revert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;User has insufficient funds for transfer&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;p&gt;Consider having this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//32 bytes or one slot&lt;/span&gt;
&lt;span class="nx"&gt;revert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Insufficient funds for transfer.&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;p&gt;Again, sometimes being verbose is better for everyone to know what is happening, so consider the tradeoffs. This works best when an extra character can add a considerable expense of a function call.&lt;/p&gt;

&lt;p&gt;Some further reading on UTF-8 characters: &lt;a href="https://blog.hubspot.com/website/what-is-utf-8"&gt;https://blog.hubspot.com/website/what-is-utf-8&lt;/a&gt;&lt;/p&gt;

</description>
      <category>solidity</category>
      <category>smartcontracts</category>
      <category>ethereum</category>
      <category>assembly</category>
    </item>
    <item>
      <title>Solidity Gas Optimizations pt. 3 - Packing Structs</title>
      <dc:creator>Javier Gonzalez</dc:creator>
      <pubDate>Sun, 03 Oct 2021 16:35:26 +0000</pubDate>
      <link>https://dev.to/javier123454321/solidity-gas-optimizations-pt-3-packing-structs-23f4</link>
      <guid>https://dev.to/javier123454321/solidity-gas-optimizations-pt-3-packing-structs-23f4</guid>
      <description>&lt;p&gt;So now that we've looked at some of the basics of assembly reads and writes memory, we can begin to look at how that memory is actually stored on each node. Everytime we make a variable in our solidity code, the evm stores it in a storage slot of 32 bytes (256 bits). That means that every time we have a uint (which is read as a uint256) we have packed a storage slot fully.&lt;/p&gt;

&lt;p&gt;So lets look at some code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="nx"&gt;uint128&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="nx"&gt;uint256&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="nx"&gt;uint128&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What the evm does is try to fit everything into storage slots sequentially, but since variable &lt;code&gt;b&lt;/code&gt; takes up an entire slot, it cannot fit in in the first one and needs to allocate a total of 3 32byte slots. If you instead order them so that the two smaller slots are next to each other, you can save one such storage operation. A more efficient code would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="nx"&gt;uint128&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="nx"&gt;uint128&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="nx"&gt;uint256&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That would allow for the EVM to only need to allocate two storage slots and 'pack' your variables. &lt;/p&gt;

&lt;h2&gt;
  
  
  Variable types
&lt;/h2&gt;

&lt;p&gt;Now let's look at the size of some common data types in Solidity:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;uint256&lt;/code&gt; is 32 bytes&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;uint128&lt;/code&gt; is 16 bytes&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;uint64&lt;/code&gt; is 8 bytes&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;address&lt;/code&gt; (and &lt;code&gt;address payable&lt;/code&gt;) is 20 bytes&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;bool&lt;/code&gt; is 1 byte&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;string&lt;/code&gt; is &lt;strong&gt;usually&lt;/strong&gt; one byte per character&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can further break down a &lt;code&gt;uint&lt;/code&gt; into different sizes, &lt;code&gt;uint8&lt;/code&gt;, &lt;code&gt;uint16&lt;/code&gt;, &lt;code&gt;uint32&lt;/code&gt;... just keep in mind that your integer will overflow if you are using solidity version &amp;lt; 0.8.0 or your function will fail if you are using version &amp;gt; 0.8.0. &lt;/p&gt;

&lt;p&gt;The largest number is calculated as &lt;code&gt;2^(number of bits) - 1&lt;/code&gt;, meaning &lt;code&gt;uint8&lt;/code&gt; goes up to &lt;code&gt;((2^8) - 1) = 255&lt;/code&gt; before your function fails. &lt;/p&gt;

&lt;p&gt;I suggest keeping these in mind when you are designing structs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Disclaimers
&lt;/h2&gt;

&lt;p&gt;A few small caveats, this only works when you are using &lt;code&gt;Structs&lt;/code&gt; (custom data objects) and/or declaring variables in your contract storage. Function arguments use memory which does not work in this way. &lt;/p&gt;

&lt;p&gt; Also, if you will not be packing your structs this way, it actually can be more expensive to use a datatype which is smaller than the 32byte limit! So if you have something like&lt;br&gt;
&lt;br&gt;
 &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;    &lt;span class="nx"&gt;uint8&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;uint256&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will be cheaper to make both variables &lt;code&gt;uint256&lt;/code&gt;! Counter intuitive, I know! &lt;/p&gt;

&lt;p&gt;The reason for this is that the EVM reads 32 bytes at a time and will have to do some operations to make the data it is reading go down to 8 bits (1 byte) which is counter productive. &lt;strong&gt;Lastly, this only really applies when you are doing multiple read and writes to the same struct or storage slot in the same operation&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When you are writing code, you don't always want to optimize for efficiency, but for developer productivity and readability. Sometimes, it might be OK to do slightly less efficient code if it makes the project easier to maintain. &lt;/p&gt;

&lt;p&gt;We will leave this here, stay tuned for part 4!&lt;/p&gt;

</description>
      <category>blockchain</category>
      <category>solidity</category>
      <category>ethereum</category>
    </item>
    <item>
      <title>Solidity Gas Optimizations pt.2 - Constants</title>
      <dc:creator>Javier Gonzalez</dc:creator>
      <pubDate>Wed, 29 Sep 2021 06:21:22 +0000</pubDate>
      <link>https://dev.to/javier123454321/solidity-gas-optimizations-pt-2-constants-570d</link>
      <guid>https://dev.to/javier123454321/solidity-gas-optimizations-pt-2-constants-570d</guid>
      <description>&lt;h2&gt;
  
  
  Variables in the blockchain
&lt;/h2&gt;

&lt;p&gt;Storing variables in memory, on a normal computer, is easy and cheap. However, blockchains are distributed systems, in which every node - or computer - has to store the data locally. That makes storing data expensive, this behavior is discouraged except where necessary.&lt;/p&gt;

&lt;h2&gt;
  
  
  SSTORE and SLOAD
&lt;/h2&gt;

&lt;p&gt;These are two of the more expensive OPCOs in the EVM for the reason outlined above. &lt;a href="https://dev.to/javier123454321/solidity-gas-optimization-pt1-4271"&gt;In my previous post&lt;/a&gt; I talk a bit about the gas cost of loading from storage using &lt;code&gt;SLOAD&lt;/code&gt;, and why it is better to use &lt;code&gt;MLOAD&lt;/code&gt; instead when possible.&lt;/p&gt;

&lt;p&gt;However, there are times that you need to instantiate a variable on creation or deployment and do not expect that variable to change. In these cases, you can use a &lt;code&gt;constant&lt;/code&gt; or &lt;code&gt;immutable&lt;/code&gt; modifier which will let the solidity compiler know about the future of that variable. Let's take an example simplified contract:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;contract&lt;/span&gt; &lt;span class="nx"&gt;Token&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;uint8&lt;/span&gt; &lt;span class="nx"&gt;VERSION&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;uint256&lt;/span&gt; &lt;span class="nx"&gt;decimals&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;uint256&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;decimals&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;val&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;The &lt;code&gt;decimals&lt;/code&gt; variable in the contract is only there for display purposes in the frontend, ERC20 tokens don't actually have a concept of 'decimals'. This means that it does not use it, it is only for users of the contract to know how to format outputs. This also means that the variable should not change. To indicate that it should not change, we have two options, &lt;code&gt;constant&lt;/code&gt; or &lt;code&gt;immutable&lt;/code&gt;. According to the docs:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The compiler does not reserve a storage slot for these variables, and every occurrence is replaced by the respective value.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Constant variables are replaced at compile time by their values, while immutable variables are replaced at deployment time. Either way, we avoid the annoying fees required with doing an &lt;code&gt;SLOAD&lt;/code&gt; operation.&lt;/p&gt;

&lt;p&gt;One simple fix looks like :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;contract&lt;/span&gt; &lt;span class="nx"&gt;Token&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;uint8&lt;/span&gt; &lt;span class="nx"&gt;constant&lt;/span&gt; &lt;span class="nx"&gt;VERSION&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;uint256&lt;/span&gt; &lt;span class="nx"&gt;immutable&lt;/span&gt; &lt;span class="nx"&gt;decimals&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;uint256&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;decimals&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;val&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;One small caveat to note is that constant variables cannot make reference to the state of the blockchain. You cannot do something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;uint256&lt;/span&gt; &lt;span class="nx"&gt;constant&lt;/span&gt; &lt;span class="nx"&gt;VERSION&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;block&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or things to do with context generally, like &lt;code&gt;block.timestamp&lt;/code&gt;, &lt;code&gt;address(this).balance&lt;/code&gt; or block.number &lt;code&gt;msg.value&lt;/code&gt; or &lt;code&gt;gasleft()&lt;/code&gt; nor call external contracts.&lt;/p&gt;

&lt;p&gt;Immutable variables however, are fine to do so.&lt;/p&gt;

&lt;p&gt;We'll leave it here, and stay tuned for part 3!&lt;/p&gt;

</description>
      <category>blockchain</category>
    </item>
    <item>
      <title>Solidity Gas Optimizations pt.1 - Memory vs Storage</title>
      <dc:creator>Javier Gonzalez</dc:creator>
      <pubDate>Thu, 09 Sep 2021 10:05:32 +0000</pubDate>
      <link>https://dev.to/javier123454321/solidity-gas-optimization-pt1-4271</link>
      <guid>https://dev.to/javier123454321/solidity-gas-optimization-pt1-4271</guid>
      <description>&lt;h2&gt;
  
  
  Building on a blockchain
&lt;/h2&gt;

&lt;p&gt;If you are familiar with a language like JavaScript, you tend to never think about how your variable is stored, except to deal with the scope of the variable. When you are making programs to run on a distributed system like a blockchain, you have to think about things a bit differently.&lt;/p&gt;

&lt;p&gt;Solidity works as a compiled language where each operation gets converted to a lower level opco which the EVM can understand and interpret. Then, every operation that you write on your program gets executed on every computer in the network, which is why every operation costs 'gas' to prevent spamming and more importantly, infinite loops. In solidity, getting to know the machine readable operations and their associated cost literally saves you money.&lt;/p&gt;

&lt;p&gt;So let's take an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;    &lt;span class="nx"&gt;uint256&lt;/span&gt; &lt;span class="nx"&gt;percentage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;splitAmountToOwnerAndSeller&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;uint256&lt;/span&gt; &lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nx"&gt;internal&lt;/span&gt;
        &lt;span class="nx"&gt;view&lt;/span&gt;
        &lt;span class="nf"&gt;returns &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;uint256&lt;/span&gt; &lt;span class="nx"&gt;amountForSender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;uint256&lt;/span&gt; &lt;span class="nx"&gt;amountForOwner&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;amountForSender&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;percentage&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;amountForOwner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;percentage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;100&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 functions calculates how much ether should go to the owner and to the seller of an item. The owner gets a percentage defined by the storage variable called &lt;code&gt;percentage&lt;/code&gt;. If we break it down into how this works, you actually need to read from the storage variable twice. In some languages that might not be a problem, however if you understand how data is stored on the blockchain, you would realize that reading the variable for &lt;code&gt;amount&lt;/code&gt; is an in-memory operation, while reading the variable &lt;code&gt;percentage&lt;/code&gt; is a storage operation. It is a different assembly code. &lt;/p&gt;

&lt;h2&gt;
  
  
  EVM Assembly
&lt;/h2&gt;

&lt;p&gt;Whenever you read the variable &lt;code&gt;percentage&lt;/code&gt; you are getting data from the blockchain database (A network of computers that each have to validate that piece of data), and this is done through an opco called &lt;code&gt;SLOAD&lt;/code&gt; which according to the Ethereum Yellow Paper costs 800 gas to execute:&lt;br&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%2Fuploads%2Farticles%2Fob0y9lpwhlb2i4r58ahc.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%2Fuploads%2Farticles%2Fob0y9lpwhlb2i4r58ahc.png" alt="image"&gt;&lt;/a&gt;&lt;br&gt;
Because you do this twice, you would end up spending 1600 gas on reading this variable. To combat that, you could always store the object in memory, and load it from there, which is much cheaper (around 3 gas). So what you could do is write from storage to memory once (&lt;code&gt;SLOAD&lt;/code&gt; + &lt;code&gt;MSTORE&lt;/code&gt;) = 803 gas, then read the memory variable twice (&lt;code&gt;MLOAD&lt;/code&gt; + &lt;code&gt;MLOAD&lt;/code&gt;) = 6 gas, for an almost 50% gas reduction for that transaction (1).&lt;br&gt;
So, the code would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;    &lt;span class="nx"&gt;uint256&lt;/span&gt; &lt;span class="nx"&gt;percentage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;splitAmountToOwnerAndSeller&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;uint256&lt;/span&gt; &lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;internal&lt;/span&gt; &lt;span class="nx"&gt;view&lt;/span&gt; &lt;span class="nf"&gt;returns &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;uint256&lt;/span&gt; &lt;span class="nx"&gt;amountForSender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;uint256&lt;/span&gt; &lt;span class="nx"&gt;amountForOwner&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;uint256&lt;/span&gt; &lt;span class="nx"&gt;ownerPercentage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;percentage&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;amountForSender&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;ownerPercentage&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;amountForOwner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;ownerPercentage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;100&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 is especially necessary when you are dealing with loops that read from state regularly, always cast a state variable to memory before entering a loop. &lt;/p&gt;

&lt;h3&gt;
  
  
  NOTES:
&lt;/h3&gt;

&lt;p&gt;(1) Thanks to user CPlusPlusDeveloper in reddit for pointing out that this is not entirely true, ever since &lt;a href="https://eips.ethereum.org/EIPS/eip-2929" rel="noopener noreferrer"&gt;EIP-2929&lt;/a&gt; the first &lt;code&gt;SLOAD&lt;/code&gt; operation costs 2100 gas, but once that memory is read, it is cached and considered considered warm, which has a cost of 100 gas to load again. It would still save gas to load and retrieve that variable in memory, especially if read more than twice. &lt;/p&gt;

</description>
      <category>blockchain</category>
    </item>
    <item>
      <title>Javascript vs HTML standard form behavior</title>
      <dc:creator>Javier Gonzalez</dc:creator>
      <pubDate>Mon, 08 Feb 2021 04:38:23 +0000</pubDate>
      <link>https://dev.to/javier123454321/javascript-vs-html-standard-form-behavior-2k86</link>
      <guid>https://dev.to/javier123454321/javascript-vs-html-standard-form-behavior-2k86</guid>
      <description>&lt;p&gt;Forms are useful. They allow us to pass data to a website. We see them everywhere, signup forms, contact forms, and in a million other ways. They are an html way to communicate with other html pages over HTTP. &lt;/p&gt;

&lt;p&gt;You can see how your document tries to communicate with itself with this simple and ugly form.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;form&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;'submission'&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;'text'&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;'submit'&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If I type the word 'something' then press submit, I get this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--w17GTBwI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3x45nbn0te92az96k7hd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--w17GTBwI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3x45nbn0te92az96k7hd.png" alt="image" title="screenshot of a url with a query parameter at the end."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;My URL is updating on form submission! In technical terms, my form is submitting a GET request with the query parameter "submission" set to "something" to the server. This is great for dumb pages or server side rendered applications, but we frontend developers love to handle state on the browser. We could be having state that needs to be persistent, and refreshing the page is unacceptable. (think of a modal popup window with a form, you don't want to refresh the page and make the user re-open it).&lt;/p&gt;

&lt;p&gt;So why does the browser refresh happen? Basically, forms in the browser want to pass data to a page. When you submit a form, it redirects you to the page that you submitted the data to. If you don't supply an action attribute to the form, or a method it will submit it to your current page using a GET parameter like we showed above. &lt;/p&gt;

&lt;p&gt;so the HTML code above is doing the same as this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;action=&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt; &lt;span class="na"&gt;method=&lt;/span&gt;&lt;span class="s"&gt;"GET"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;'submission'&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;'text'&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;'submit'&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The form [action="/"] attribute is telling the browser that we want to post the data to the root URL of the website. The method is expecting an HTTP verb (POST, PUT, PATCH, DELETE, etc...) but GET is the default one. GET requests are appended to the url as shown in the image above. This is handy, but often you don't want that because you don't need that information in your browser history for everyone to see. &lt;/p&gt;

&lt;p&gt;When you submit a form, the browser navigates to the url that you provide to the [action] attribute. However, we said we wanted to avoid page reload all together. To do this, you need to intercept the standard form behavior with javascript. You can actually do this by using the preventDefault() method on the submit event itself.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;action=&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt; &lt;span class="na"&gt;method=&lt;/span&gt;&lt;span class="s"&gt;"GET"&lt;/span&gt; &lt;span class="na"&gt;onsubmit=&lt;/span&gt;&lt;span class="s"&gt;"event.preventDefault()"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;'submission'&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;'text'&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;'submit'&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you run test again, you no longer get a query string in the URL and your state persists!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lorhRLFI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/91uiyowfzfovdsjlw1xp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lorhRLFI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/91uiyowfzfovdsjlw1xp.png" alt="image" title="screenshot of a url without a queryparameter at the end"&gt;&lt;/a&gt;&lt;br&gt;
(This is a boring image but it shows the form element still holding a value because the page didn't refresh!!)&lt;/p&gt;

&lt;p&gt;You are preventing the browser from doing what it does whenever it sees a form element. &lt;/p&gt;

&lt;p&gt;Now let's handle it with javascript because we all know that you should handle everything with javascript (I'm just leaving this here to piss off backend developers). You can do that without writing it in the html by handling it in your js like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt; &lt;span class="nt"&gt;&amp;lt;form&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;'submission'&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;'text'&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;'submit'&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;form&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;submit&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;span class="nx"&gt;event&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;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ahh much nicer, we took away the native properties of the form element and told the browser, we'll take it from here. Now we can decide exactly how we want the form to behave.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why use forms at all?
&lt;/h2&gt;

&lt;p&gt;So the more adept programmer might say something along the lines of 'if I have to jump through hoops when I put a form element on the page, can't I just omit it altogether?'. And yes, they would be right that they could do the same as the example above only using an input element and a button. However, forms are the 'semantic' way of writing html, which is useful especially for screen readers. Screen readers are all programmed to react predictably to a form element, but if you don't group the functionality of an input with a submit, you'll have to keep track of all that yourself through adding labels and grouping the behavior together. This is possible, but it is not recommended because it introduces more surface area for mistakes appear.  &lt;/p&gt;

&lt;p&gt;So there you have it. Hopefully this helps if you are wondering why you are losing the state of the application every time you hit submit on your forms. Stay tuned for more tips and tricks! &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>html</category>
      <category>a11y</category>
    </item>
    <item>
      <title>I am also self taught</title>
      <dc:creator>Javier Gonzalez</dc:creator>
      <pubDate>Tue, 12 Jan 2021 21:55:48 +0000</pubDate>
      <link>https://dev.to/javier123454321/i-am-also-self-taught-1p5c</link>
      <guid>https://dev.to/javier123454321/i-am-also-self-taught-1p5c</guid>
      <description>&lt;p&gt;I taught myself how to code. But I taught myself everything I know. I am not trying to sound full of myself, I actually think that this is true for everyone.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is learning?
&lt;/h2&gt;

&lt;p&gt;I love the American Pragmatist Philosopher &lt;a href="https://en.wikipedia.org/wiki/John_Dewey"&gt;John Dewey&lt;/a&gt;. He was much smarter than I. He also thought very deeply about the question of what it is to "learn" something. He developed a theory that says something that might sound basic. To know something is to have an experience of something. He laid the theory that knowing something is being able to apply it in the context in which it will be used. And, the only way to know something is to have done it before, to feel the nuances based on your experiences, to gain the habit, the muscle memory, the traps and the ability to figure out what will probably work and what wont. This also means that knowledge cannot really be taught. &lt;/p&gt;

&lt;p&gt;The concept is very similar to what we hear all the time in programming communities. Do projects, build things, don't get stuck in tutorial purgatory, make projects apply what you learned. Dewey would agree with this.&lt;/p&gt;

&lt;p&gt;Saying that you can't be taught something sounds harsh. Of course there is value in teaching and learning from others. After all, there will always be people more experienced than you at what you are trying to do. Their experience is absolutely valuable, and what you get from them is knowledge about perils, it is knowledge about what will give you more leverage, about where to direct your attention to get a good basis, but the problem is when you begin to confuse hearing facts about something with learning about something. You cannot transfer knowledge into your head. You need to practice in the context as close as the real thing. Otherwise, you will only get good at answering quizzes, not at solving problems.&lt;/p&gt;

&lt;p&gt;So, we are all self taught, because it is only by doing the work that you learn. And doing the work, takes effort that you can simply not buy. A teacher can show you the way, but learning happens on your own. &lt;/p&gt;

&lt;p&gt;Do you agree?&lt;/p&gt;

</description>
      <category>programming</category>
      <category>productivity</category>
      <category>discuss</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Async JavaScript</title>
      <dc:creator>Javier Gonzalez</dc:creator>
      <pubDate>Mon, 11 Jan 2021 00:21:16 +0000</pubDate>
      <link>https://dev.to/javier123454321/async-javascript-1og2</link>
      <guid>https://dev.to/javier123454321/async-javascript-1og2</guid>
      <description>&lt;p&gt;JavaScript is a single threaded language.&lt;/p&gt;

&lt;p&gt;That means that JS only does one thing at a time. This is key to understanding how it works. &lt;/p&gt;

&lt;p&gt;JS has what's called an event loop. It is a one dimensional array of jobs to be done. When you execute a JS program it pops in an event to the queue, and executes it in First In First Out order. It does so sequentially and goes from the top of the program to the bottom. It will pop in whatever is at line 1 to the queue, then line 2, then line 3 and so on. It will execute those instructions in that order.&lt;/p&gt;

&lt;p&gt;So writing this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//a == 10 as expected.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will (and has to in order for the program to work) execute  from top to bottom. It will not declare the variable &lt;code&gt;b&lt;/code&gt; until variable &lt;code&gt;a&lt;/code&gt; is declared, and will not reassign variable &lt;code&gt;a&lt;/code&gt; until &lt;code&gt;b&lt;/code&gt; has been declared. This makes sense to us because we also think sequentially. &lt;/p&gt;

&lt;p&gt;This is nice, but sometimes we want JavaScript to do something while we wait for something else. For example, if you set a timeout with the setTimeout function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;

&lt;span class="nx"&gt;setTimeout&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;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//Setting a timeout of 0?&lt;/span&gt;

&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//here a == undefined??...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now look at that example and think about it. Why does setting a timeout of 0 not do the thing that we expect it to do? If we set a timeout with no wait, we expect it to happen right away, why doesn't it??&lt;/p&gt;

&lt;p&gt;There is a simple answer to this and it is in the event loop. Javascript does thing in the order of first in first out, remember? &lt;/p&gt;

&lt;p&gt;When it comes time for actually executing the calls in the order this happens:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    declare "a" equal to 0
    declare "b" equal to undefined

    set a timeout of 0

    declare "a" equal to the value of "b" (which is undefined)

    check if timeout is done

    now that the timeout is done, add the anonymous function that was passed inside setTimeout to the queue.
    assign to "b" the value of "a" + 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So if we look at the above, we see that setTimeout is not blocking the rest of the program to be added to the queue. Doing things asynchronously allow for larger timeouts to happen in the background without holding up the rest of the code from executing. If the timeout would have been longer, the output would have been no different. The timeout is waiting for the timer to be done to add the item to the bottom of the queue, but not before it adds the rest of the program to the queue. &lt;/p&gt;

&lt;p&gt;Understanding the interworkings of JS like these allows you to write more deliberate code, and makes it so the way that you make programs are more readable and take advantage of the way that the language was designed to your favor. Without it, programming JavaScript is more like the tail wagging the dog.&lt;/p&gt;

&lt;p&gt;Hope this helps you if you are having trouble understanding async code, stay tuned for more.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>computerscience</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>What is the Dom, the Virtual Dom, and the Shadow Dom</title>
      <dc:creator>Javier Gonzalez</dc:creator>
      <pubDate>Fri, 08 Jan 2021 21:37:52 +0000</pubDate>
      <link>https://dev.to/javier123454321/what-is-the-dom-the-virtual-dom-and-the-shadow-dom-3356</link>
      <guid>https://dev.to/javier123454321/what-is-the-dom-the-virtual-dom-and-the-shadow-dom-3356</guid>
      <description>&lt;p&gt;There is confusion out there between these three terms. The DOM is an absolutely integral portion of modern web development, and knowing how to differentiate the three and knowing when to use these is crucial for being an effective web developer.&lt;/p&gt;

&lt;h2&gt;
  
  
  The DOM
&lt;/h2&gt;

&lt;p&gt;So when you have markup on your page with HTML, your browser compiles all that into a Document Object Model which we abbreviate to the DOM. This is an application programming interface (API) that the browser gives you in order to be able to interact with the contents that are rendered on the page. &lt;/p&gt;

&lt;p&gt;This sounds more complicated than it is, because at its simplest, you can throw an html file into a server and what will come out are those contents rendered, just like you wrote them. You can think of the DOM as the way that the html is presented to you when you inspect your browser window and go to the elements tab.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Virtual Dom
&lt;/h2&gt;

&lt;p&gt;The virtual DOM is like the regular DOM, except that it is held in JavaScript memory. What that means is that the virtual dom has all the properties of the regular HTML elements that you usually see on the page, except that the browser has not rendered them. You can't actually see it, but you can interact with it through JavaScript. This is how frameworks like react, vue and angular make web pages with high degrees of interactivity. If you turn off JavaScript and load a react application, you might find a single DOM node and a blank page. &lt;/p&gt;

&lt;p&gt;This allows many different and cool things for web applications. Not having to worry about rerendering every time that something is changed, allows frameworks like react to update only the nodes which have changed when the data gets updated. These diffing algorithms are extremely powerful and make these frameworks very effective ( ...and sometimes fast!). &lt;/p&gt;

&lt;h1&gt;
  
  
  The Shadow Dom
&lt;/h1&gt;

&lt;p&gt;The Shadow DOM is something completely different than the virtual DOM. The shadow DOM is a browser specification for building completely isolated HTML elements that are not affected by the styles that are currently affecting the rest of the page. It is one of the specifications that are aiming at making modular HTML components which can be imported into and out of different documents. &lt;/p&gt;

&lt;p&gt;I'd say that this is not so popular in enterprise applications, but it allows for building HTML without the overhead of frameworks through the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/Web_Components#shadow_dom"&gt;Web Component Specification&lt;/a&gt;. It can be tricky if you encounter it and don't know how to use it, but very helpful if you use it deliberately.&lt;/p&gt;

&lt;p&gt;Hope that this helps you out if you are having trouble figuring out the differences.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>html</category>
    </item>
  </channel>
</rss>
