<?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: David Sanwald</title>
    <description>The latest articles on DEV Community by David Sanwald (@davidsanwald).</description>
    <link>https://dev.to/davidsanwald</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%2F189746%2Fc42af576-5aa9-4e2c-8eee-ff73b60be767.jpeg</url>
      <title>DEV Community: David Sanwald</title>
      <link>https://dev.to/davidsanwald</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/davidsanwald"/>
    <language>en</language>
    <item>
      <title>How to Design Better Types in Typescript by Following One Simple Principle</title>
      <dc:creator>David Sanwald</dc:creator>
      <pubDate>Fri, 03 Jul 2020 18:26:16 +0000</pubDate>
      <link>https://dev.to/davidsanwald/how-to-design-better-types-in-typescript-by-following-one-simple-principle-1eia</link>
      <guid>https://dev.to/davidsanwald/how-to-design-better-types-in-typescript-by-following-one-simple-principle-1eia</guid>
      <description>&lt;p&gt;All maintainable, long-lived React codebases that are a joy to work with, even after years, share one thing:&lt;br&gt;
They consist of components that are built around data, that has the right structure.&lt;br&gt;
One of my favorite text about React of all time explains this perfectly:&lt;br&gt;
&lt;a href="https://jxnblk.com/blog/defining-component-apis-in-react/"&gt;Defining Component APIs in React&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But even the official React docs emphasize the importance of choosing the right structure for your application data and building your components around that data:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Since you’re often displaying a JSON data model to a user, &amp;gt;you’ll find that if your model was built correctly, your UI &amp;gt;(and therefore your component structure) will map nicely.&lt;br&gt;
&lt;a href="https://reactjs.org/docs/thinking-in-react.html#step-1-break-the-ui-into-a-component-hierarchy"&gt;Thinking in React&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Fortunately, there are simple principles that make modeling your application data very easy.&lt;/p&gt;

&lt;p&gt;This article starts with the most important one:&lt;br&gt;
The space our models cover should only include cases that are valid in our domain&lt;/p&gt;
&lt;h2&gt;
  
  
  A Simple Example: Building Cars
&lt;/h2&gt;

&lt;p&gt;While the following example might not be very realistic for the average Typescript codebase, it's types are examples of two basic structures that are part of every codebase.&lt;/p&gt;
&lt;h3&gt;
  
  
  First Try Modeling Car Configurations
&lt;/h3&gt;

&lt;p&gt;To build cars we might come up with the following types:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;PowerSource&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;gas tank&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;battery&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Engine&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;electric motor&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;petrol engine&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;diesel engine&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Fuel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;petrol&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;diesel&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;electrons&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Car&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;engine&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Engine&lt;/span&gt;
  &lt;span class="na"&gt;fuel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Fuel&lt;/span&gt;
  &lt;span class="na"&gt;powerSource&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;PowerSource&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's look at the Car type. There are three kinds of engines, three kinds of fuel and two different types of power sources.&lt;br&gt;
Taking the &lt;em&gt;product&lt;/em&gt;&lt;br&gt;
&lt;strong&gt;2 x 3 x 3&lt;/strong&gt;&lt;br&gt;
we get &lt;strong&gt;18&lt;/strong&gt; the number of all possible car configurations. At first, everything looks all nice and dandy. We are happy that Typescript prevents us from assigning random strings to our car parts, and we successfully prevent typos.&lt;/p&gt;

&lt;p&gt;The following example shows a valid car.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;buggyCar&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Car&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;engine&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;petrol engine&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;fuel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;diesel&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;powerSource&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;gas tank&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;but filling the tank and starting the engine leads to a nasty surprise:&lt;br&gt;
Powering the petrol engine with diesel would be its certain death. Yet the combination is a valid type.&lt;br&gt;
How could we design our types to prevent failures like this right away?&lt;/p&gt;
&lt;h3&gt;
  
  
  Designing Better Types for Our Car
&lt;/h3&gt;

&lt;p&gt;We start by analyzing the domain, and right away, we see that there are only three configurations that would result in functional cars.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ElectricCar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;engine&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;electric motor&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="na"&gt;fuel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;electrons&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="na"&gt;powerSource&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;battery&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;DieselCar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;engine&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;diesel motor&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="na"&gt;fuel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;diesel&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="na"&gt;powerSource&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;gas tank&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;PetrolCar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;engine&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;petrol motor&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="na"&gt;fuel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;petrol&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="na"&gt;powerSource&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;gas tank&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;Now we can model the car type as one union of those interfaces:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Car&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;PetrolCar&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;ElectricCar&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;DieselCar&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The new type only includes our three functional cars because we get the number of cases by building the &lt;em&gt;sum&lt;/em&gt; &lt;strong&gt;1+1+1=3&lt;/strong&gt; instead of the &lt;em&gt;product&lt;/em&gt; &lt;strong&gt;2x3x3=18&lt;/strong&gt; of our previous types.&lt;br&gt;
If we used the old types, we would need to use a combination of testing and documentation to prevent dysfunctional car configurations.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why bother?
&lt;/h3&gt;

&lt;p&gt;Typescript is helpful. Even the first types would have prevented bugs by catching small mistakes like typos. But typing our code can also communicate intent or knowledge to other developers. Maybe it could bring us closer to communities of other languages like Elm, Clojure or Haskell. We could benefit a lot.&lt;/p&gt;

&lt;h3&gt;
  
  
  What's next?
&lt;/h3&gt;

&lt;p&gt;The following links are a good start for digging deeper:&lt;/p&gt;

&lt;p&gt;-&lt;a href="https://lispcast.com/what-do-product-and-sum-types-have-to-do-with-data-modeling/"&gt;WHAT DO PRODUCT AND SUM TYPES HAVE TO DO WITH DATA MODELING?&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;-&lt;a href="https://youtu.be/IcgmSRJHu_8"&gt;"Making Impossible States Impossible" by Richard Feldman&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What do you think?
&lt;/h3&gt;

&lt;p&gt;Tell me if Typescript changed the way you think about code? When we remove the types, does your Typescript code still look different from your JavaScript code?&lt;br&gt;
Do you think Typescript brings us closer to learn from other communities?&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>functional</category>
      <category>javascript</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Touch Typing- The Most Important Skill For Developers Nobody Talks About</title>
      <dc:creator>David Sanwald</dc:creator>
      <pubDate>Thu, 02 Jul 2020 16:02:01 +0000</pubDate>
      <link>https://dev.to/davidsanwald/touch-typing-the-most-important-skill-for-developers-nobody-talks-about-3352</link>
      <guid>https://dev.to/davidsanwald/touch-typing-the-most-important-skill-for-developers-nobody-talks-about-3352</guid>
      <description>&lt;p&gt;As a developer, the code I commit during a typical workday varies greatly, but about 8000 characters of code might be an okay estimate.&lt;br&gt;
Before I learned how to touch-type, my typing speed was about 30 WPM.&lt;br&gt;
That means I could type all of that in about 50 minutes. My current typing-speed is about 70 WPM, so I would only save 30 minutes per day.&lt;/p&gt;

&lt;p&gt;As a developer, I'm not limited by the typing speed and thinking about code, reading code, looking for the right place to change that single line of code to fix a bug matter way more than my raw output of code.&lt;/p&gt;

&lt;p&gt;All in all, it seems like touch-typing is only a minor optimization, so why was touch-typing the single most important skill I picked up as a developer?&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Touch-Typing Matters
&lt;/h2&gt;

&lt;p&gt;Many of the things that matter most to me might also be personal and subjective. But I still believe the following things are true not only for myself.&lt;/p&gt;

&lt;h3&gt;
  
  
  Syncing Code and Thoughts
&lt;/h3&gt;

&lt;p&gt;The flow of thoughts is not consistent. Often we think in bursts. Further, our mind does not emit ideas in a linear self-consistent manner. Often we only can associate and combine ideas that did not directly succeed each other.&lt;br&gt;
Touch-typing, to me, means less buff between thinking about code and writing code. I'm able to quickly type ideas I had to keep in my head before. Not being able to follow my thought process in the editor forced me to do more steps in my head.&lt;br&gt;
Typing made the way I code more iterative. It's no problem at all writing code that will not be in the final commit. I often worried about writing clean code, which limited me and slowed me down. Now I'm able to write code without worrying about how it looks because immediately refactoring a line of code after typing it is so easy.&lt;/p&gt;

&lt;h3&gt;
  
  
  It's also about navigating and editing code
&lt;/h3&gt;

&lt;p&gt;Before knowing how to type, I failed to get into using VIM (or its keybindings) countless times. I tried but never managed to build up muscle memory navigating and editing code the vim-way. Nobody told me that typing skills are almost a hard requirement for benefitting from VIM.&lt;br&gt;
After I was able to touch-type (even though I was still very slow), it only took me a week to pick up enough VIM to feel productive.&lt;br&gt;
Only a fraction of "writing code" is actually about writing code. It's easy to overlook how much working with code is about editing and navigating it.&lt;/p&gt;

&lt;h3&gt;
  
  
  It's not only code we type
&lt;/h3&gt;

&lt;p&gt;Often we are focussed about writing actual code, that there are many more things that depend on being typed so that we can contribute them to a product/team/discussion:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;code reviews&lt;/li&gt;
&lt;li&gt;slack messages&lt;/li&gt;
&lt;li&gt;documentation&lt;/li&gt;
&lt;li&gt;user stories&lt;/li&gt;
&lt;li&gt;stack overflow postings&lt;/li&gt;
&lt;li&gt;...&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Being Able To Type Without Looking is not the same as Touch Typing.
&lt;/h3&gt;

&lt;p&gt;Here I can only speak for myself:&lt;br&gt;
I could type without having to look at the keyboard. It's hard to explain but always using the same finger for the same keys when touch-typing still feels very different from before. The actual typing requires not nearly as much attention as before, and I can focus almost entirely on the code instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Nobody Talks About Touch-Typing
&lt;/h2&gt;

&lt;p&gt;Among developers, there are two main groups.&lt;br&gt;
Many developers learned how to touch-type at an early age at school. Therefore they take touch-typing as given and are not aware of how much it impacts their work.&lt;br&gt;
The group of developers that did not pick up touch-typing during childhood would need to invest a lot of time and energy, learning how to type before actually experiencing the benefits.&lt;br&gt;
Learning how to type takes time. After typing without a system for years, most people are still able to type quite fast. As developers, we depend on our efficiency. Learning how to type means becoming much slower at first, and this can be quite scary.&lt;br&gt;
Therefore there are not too many people that learn how to type as adults.&lt;br&gt;
For myself picking up touch-typing was more than worth it. Because I benefit so much from this decision every day, I want to share my experience, maybe even encouraging somebody picking it up, it's never too late.&lt;/p&gt;

&lt;h2&gt;
  
  
  My Touch-Typing Journey
&lt;/h2&gt;

&lt;p&gt;Because I built up some muscle-memory by typing without a system for all my life, I struggled to relearn my finger movements.&lt;br&gt;
For that reason, I chose the most radical way possible:&lt;/p&gt;

&lt;h3&gt;
  
  
  COLEMAK for the win
&lt;/h3&gt;

&lt;p&gt;I took all the vacation days I had accumulated and switched my keyboard layout from one day to the other from QWERTY to COLEMAK.&lt;br&gt;
To be honest, this was hell. Suddenly I couldn't type at all. Looking at the keyboard was not helpful anymore at all, because the letters on the keycaps did not match their actual letters.&lt;br&gt;
I struggled to type even short URLs into the address bar of the browser.&lt;/p&gt;

&lt;p&gt;To slowly relearn everything, I used the site &lt;a href="https://www.keybr.com/"&gt;https://www.keybr.com/&lt;/a&gt; by starting with the smallest keyset possible.&lt;br&gt;
To keep me from looking at my hands, I printed out a COLEMAK schema and put it on the wall behind my monitor.&lt;br&gt;
As I finally progressed to the full keyset on keybr, I switched to &lt;a href="https://www.keyhero.com/"&gt;https://www.keyhero.com/&lt;/a&gt; for my daily training.&lt;br&gt;
After two weeks, I was still slower than before but at least fast enough to start working again without being afraid of not being able to keep up.&lt;br&gt;
From there on, I went from 6 hours typing practice a day to only 2 hours after work. After another month, I stopped practicing everyday and only practiced occasionally.&lt;br&gt;
It took me about four months until typing felt like something natural to me. Today I'm still not too fast (about 70 WPM), but I think speed is only a minor factor for the positive impact that touch-typing had on my work.&lt;/p&gt;

&lt;h3&gt;
  
  
  Let me Hear Your Thoughts
&lt;/h3&gt;

&lt;p&gt;What are your thoughts on this? Do you agree or disagree? Why?&lt;br&gt;
I'm interested in all kinds of thoughts, opinions, and experiences. But I'd love to hear from people that picked up touch-typing at a later stage of their life and whether you had similar experiences.&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>productivity</category>
      <category>vim</category>
      <category>career</category>
    </item>
  </channel>
</rss>
