<?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: Oliver Jumpertz</title>
    <description>The latest articles on Forem by Oliver Jumpertz (@oliverjumpertz).</description>
    <link>https://forem.com/oliverjumpertz</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%2F220684%2F499f49d5-84b9-454a-b6c8-0e19f7579b20.jpg</url>
      <title>Forem: Oliver Jumpertz</title>
      <link>https://forem.com/oliverjumpertz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/oliverjumpertz"/>
    <language>en</language>
    <item>
      <title>The Ultimate Guide To GraphQL Error Handling</title>
      <dc:creator>Oliver Jumpertz</dc:creator>
      <pubDate>Wed, 08 Nov 2023 21:34:28 +0000</pubDate>
      <link>https://forem.com/oliverjumpertz/the-ultimate-guide-to-graphql-error-handling-1j2a</link>
      <guid>https://forem.com/oliverjumpertz/the-ultimate-guide-to-graphql-error-handling-1j2a</guid>
      <description>&lt;p&gt;Error handling in GraphQL is often not easy. It’s actually pretty hard. This is why many developers struggle to build a really usable GraphQL.&lt;/p&gt;

&lt;p&gt;If this sounds like you, you are not alone. If it doesn’t sound like you, you might still stumble upon these issues soon.&lt;/p&gt;

&lt;p&gt;A good API can be a game-changer, especially if the product you build does not only want to rely on a user interface you provide. Even if it’s only your own product that suffers from difficult error handling, any frontend engineer (and even yourself) will thank you if you make handling errors as simple as possible.&lt;/p&gt;

&lt;p&gt;There are gladly a few strategies that you can apply to improve the error handling of your GraphQL API, which will make it an overall more pleasure to use and work with.&lt;/p&gt;

&lt;p&gt;None of these are difficult to implement, they just require some careful thinking and planning before you can implement them.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Issue With GraphQL And Errors
&lt;/h2&gt;

&lt;p&gt;GraphQL is an amazing API technology, but it has its “flaws”.&lt;/p&gt;

&lt;p&gt;It’s great to enable your clients to simply retrieve only the data they really care for (while leaving out all the additional burden of the 321 properties they will never need in their lifetime). But if you use GraphQL over HTTP (yes, that is really a thing because GraphQL is protocol-agnostic and the &lt;a href="https://spec.graphql.org/June2018/"&gt;whole spec&lt;/a&gt; doesn’t mention a required protocol even once), you might (or might not. If so, I’m happy I can tell you about it!) have realized that any well-formed GraphQL response usually has to respond with an &lt;a href="https://github.com/graphql/graphql-over-http/blob/main/spec/GraphQLOverHTTP.md#status-codes"&gt;HTTP 200 status code&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Other status codes are reserved for very specific use cases, and this usually leaves you only with an HTTP 200, which does not help at all when encountering errors.&lt;/p&gt;

&lt;p&gt;This is why GraphQL introduces the &lt;a href="https://spec.graphql.org/June2018/#sec-Errors"&gt;optional “errors” array&lt;/a&gt; into any GraphQL response, which looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"errors"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"message"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"This somehow didn't work out well. Sorry."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"locations"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"line"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"column"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}],&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"path"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"thanks"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"for"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"reading"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"thisArticle"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"data"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"article"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"The Ultimate Guide To GraphQL Error Handling"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"author"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Oliver Jumpertz"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"..."&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Errors can always be appended to a GraphQL response, even if data is returned. Everything the presence of the errors array states is that something went wrong while processing the request.&lt;/p&gt;

&lt;p&gt;The above can be boiled down to the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If errors are absent, everything went fine&lt;/li&gt;
&lt;li&gt;If errors are present, something went wrong, and many clients will throw an error, even if data was returned&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Errors in GraphQL are specified, but not strictly. The three properties &lt;code&gt;message&lt;/code&gt;, &lt;code&gt;locations&lt;/code&gt;, and &lt;code&gt;path&lt;/code&gt; are conforming to the standard. Adding additional properties is &lt;strong&gt;not a violation&lt;/strong&gt; of the specification, but it is &lt;strong&gt;highly discouraged&lt;/strong&gt;. Many client libraries still expect only these three properties to be included within an error object. Adding anything else might give your users or colleagues a hard time using your API.&lt;/p&gt;

&lt;p&gt;To battle this inflexibility, the GraphQL specification introduces the &lt;code&gt;extensions&lt;/code&gt; object into any error object. Within these extensions, you can put anything you want, without giving clients a hard time interpreting your responses (technically, you only leave it up to them to discover and interpret them).&lt;/p&gt;

&lt;p&gt;The error example from before can be extended with extensions like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"errors"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"message"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"This somehow didn't work out well. Sorry."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"locations"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"line"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"column"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}],&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"path"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"thanks"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"for"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"reading"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"thisArticle"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"extensions"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"code"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"INTERNAL_ERROR"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"timestamp"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Su May 21 14:33:09 UTC 2023"&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"data"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"article"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"The Ultimate Guide To GraphQL Error Handling"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"author"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Oliver Jumpertz"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"..."&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;extensions&lt;/code&gt; object can contain anything. There are no boundaries to what you can put there. It allows for a lot of flexibility, but it also has one huge issue: &lt;strong&gt;You cannot type it reliably&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Unlike entities in your GraphQL schema that can be strictly typed by deriving a type system from the schema, error extensions are (conceptually) only like a &lt;code&gt;Map&amp;lt;String, Any&amp;gt;&lt;/code&gt;. There is nothing you can send within your response (except for some very nasty hacks like putting a whole JSON schema into each error’s extensions) to help clients generate type-safe code for your API’s (hopefully) standardized extensions.&lt;/p&gt;

&lt;p&gt;Error extensions also have a second, smaller issue: &lt;strong&gt;It’s more difficult to access them&lt;/strong&gt;. Unlike the usual GraphQL response that can easily be accessed from your favorite GraphQL client library, you need to put extra work into extracting the errors and their extensions to make them usable within a client’s logic.&lt;/p&gt;

&lt;p&gt;When errors are difficult to handle in GraphQL, what then? What is it that you can do to improve the situation and make your API as usable as possible? How can you prevent a night full of coffee or energy drinks with Netflix running on the side while trying to come up with a suitable way to use your API?&lt;/p&gt;

&lt;p&gt;You can first try to classify common errors and see whether you can apply different patterns to different kinds of errors because as you will see, not all errors need to be treated with all of someone’s energy.&lt;/p&gt;




&lt;h2&gt;
  
  
  Classifying Errors
&lt;/h2&gt;

&lt;p&gt;It makes a lot of sense to first think about which types of errors (on a very high level) your API can actually experience. But which classifications make sense?&lt;/p&gt;

&lt;p&gt;There are many ways to classify errors, from the lowest level to the highest one available, but in reality, there is one specific classification that makes the most sense: &lt;strong&gt;Classifying errors by their (un-)planned impact&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If you think about this for a moment, you will probably realize that it’s a good idea to think of errors as either wanted or unwanted states of your system.&lt;/p&gt;

&lt;p&gt;Some errors are always unexpected. They just happen although you might not be happy about it. An upstream API may be unavailable, your database might be down, or sometimes the math your API performs just doesn’t add up.&lt;/p&gt;

&lt;p&gt;Other errors are planned. Users aren’t logged in, there might be legal reasons that prevent you from showing content to users in certain regions (geo-blocking), or a user might simply need a subscription to view a specific page.&lt;/p&gt;

&lt;p&gt;Now, you might look for names to give to these errors (given that planned and unplanned somehow don’t please you well, and I feel you), but we can come up with two very fitting names:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Technical&lt;/strong&gt; errors and &lt;strong&gt;Business&lt;/strong&gt; errors.&lt;/p&gt;

&lt;p&gt;But before we go deeper into how to handle each class of errors, we should first take a look at the error classification and specify what technical errors and business errors really are.&lt;/p&gt;

&lt;h3&gt;
  
  
  Technical Errors
&lt;/h3&gt;

&lt;p&gt;Technical errors originate from technical issues. Upstream APIs that are down, a faulty database, or even your API running out of memory are all errors that can be boiled down to technical issues. The list can be extended infinitely but you probably get the idea by now.&lt;/p&gt;

&lt;p&gt;These kinds of errors are always unwanted. You can plan for them but never exactly tell when they will occur.&lt;/p&gt;

&lt;p&gt;Errors like these are a necessary evil and you can usually not eliminate them. They belong to the product you build like every line of code you chew out.&lt;/p&gt;

&lt;p&gt;Thankfully, they make up their own category and can all be handled just the same, which makes it easier to process and handle them in a way that also suits any client.&lt;/p&gt;

&lt;p&gt;Technically, there is not much a client can do when such an error occurs. It can retry the request and see whether it gets another response that time, or it can simply show a banner with an error message and say sorry to the user, asking them to retry the action again later.&lt;/p&gt;

&lt;p&gt;The important point about technical errors is that they can usually only be handled &lt;strong&gt;implicitly&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Business Errors
&lt;/h3&gt;

&lt;p&gt;Business errors originate from the need of the business to create error states. Although your business analysts or product managers and owners or even yourself might not call them errors, technically they still are.&lt;/p&gt;

&lt;p&gt;Errors like these are planned for. You expect and want them to happen in very specific circumstances. In these cases, there is simply no other way than to force the user to do something or deny them a specific action.&lt;/p&gt;

&lt;p&gt;When a user encounters such a state, there is usually a plan for what should happen next (there really should be one). If some content is behind a paywall, there is often a process to make it as easy as possible for the user to upgrade to a paid plan. This usually involves a whole flow within your software, which in the end, brings the user back to the actual resource they tried to access after they spent money on your product.&lt;/p&gt;

&lt;p&gt;At least theoretically, errors like these feel more like responses that lead to very specific actions, and this distinguishes them from technical errors. Unlike technical errors, business errors trigger specific actions. Or in other words: Business errors always need to be handled in a very &lt;strong&gt;explicit&lt;/strong&gt; way. You want to &lt;strong&gt;force&lt;/strong&gt; the client to do something based on that very specific response.&lt;/p&gt;




&lt;h2&gt;
  
  
  Handling Technical Errors
&lt;/h2&gt;

&lt;p&gt;Going back to our initial definition of technical errors as errors that simply happen out of your control and the fact that you can’t do too much about them, there is one very convenient way to state their existence: &lt;strong&gt;An error object within the response&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;As you can’t do very much about them, and given the fact that their occurrence usually hinders users of your API to proceed with whatever they wanted to do, an error object is the best way to make use of how most GraphQL clients handle errors by throwing some kind of exception.&lt;/p&gt;

&lt;p&gt;If you encounter a technical error within your API, you can usually just throw an exception yourself and let the framework pick it up, map it into an error object within the response, or simply add it yourself.&lt;/p&gt;

&lt;p&gt;Going back to the original, extended example, such a response can look as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"errors"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"message"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"This somehow didn't work out well. Sorry."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"locations"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"line"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"column"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}],&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"path"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"thanks"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"for"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"reading"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"thisArticle"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"extensions"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"code"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"INTERNAL_ERROR"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"timestamp"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Su May 21 14:33:09 UTC 2023"&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"data"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"article"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"The Ultimate Guide To GraphQL Error Handling"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"author"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Oliver Jumpertz"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"..."&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Most clients will pick this response up, realize that there is an error nested within the response, and then act accordingly. Some clients will throw an exception that you have to catch and process, others will give you hints in other ways (but they all usually point you in the right direction).&lt;/p&gt;

&lt;p&gt;The only thing you can then do is to at least extract the error message and display it in a popup, toast, banner, or wherever else you have planned to notify users of unsuccessful operations. This is the implicit error handling we talked about earlier (implicit because often, it is even enough to let a general exception handler do something about the error you don’t even care to handle yourself). There is just not much more you can do about it.&lt;/p&gt;




&lt;h2&gt;
  
  
  Handling Business Errors
&lt;/h2&gt;

&lt;p&gt;After you have tackled the easier part of handling errors in GraphQL, it’s time to jump into deeper water: &lt;strong&gt;Handling business errors&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let’s quickly look at a partial definition of business errors again:&lt;/p&gt;

&lt;p&gt;They are planned for. They are expected. If they occur, you usually want clients to do something very specific to solve the issue.&lt;/p&gt;

&lt;p&gt;In other words: You want to &lt;strong&gt;force&lt;/strong&gt; the client to do something and &lt;strong&gt;not just ignore&lt;/strong&gt; the error.&lt;/p&gt;

&lt;p&gt;If you are only a little like me, you might already think about how you can do this: Force clients to do something. And you, like me, will probably realize that GraphQL already has the best way to force clients to do what you want: &lt;strong&gt;The schema&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Whatever you put inside the schema becomes the reality of the API. Entities, properties, types, etc. All that is already being enforced. GraphQL schemas can even be picked up by code generators and transformed into strictly and dynamically typed code.&lt;/p&gt;

&lt;p&gt;Code and types are exactly what you need to solve this mystery. Especially types can force you to do so many things that you would usually (be honest. I’m also guilty!) just ignore and let be.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Unions&lt;/strong&gt; in many languages, for example, are a great way to state that a type is either this, that, or that, or that, or…wait a second…Unions! That’s the specific answer to the problem of making business errors explicit.&lt;/p&gt;

&lt;p&gt;A query with a potential business error means: Your query is either successful or unsuccessful. That’s a union with two possible types: Success and Error. And this is exactly how you can tackle the problem at hand. (Imagine borrowing the concept from Rust’s Result &lt;a href="https://oliverjumpertz.com/blog/rusts-enums-explained/"&gt;enum&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;If any of your queries or mutations can have potential business errors, you can introduce a union to model all potential outcomes of the call. As unions don’t have a hard limit on how many member types they can carry, they are flexible enough to serve the use case.&lt;/p&gt;

&lt;p&gt;Let’s look at a basic example of an API schema of an e-commerce system. For now, it only has one query that allows users to fetch a list of products:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight graphql"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Query&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;productsByCategory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;!):&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;]!&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Product&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;ID&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The query &lt;code&gt;productsByCategory&lt;/code&gt; allows clients to fetch all products by a specific category. This implies that there also might be no products at all for a given category.&lt;/p&gt;

&lt;p&gt;If no products are found, the list will simply be empty, which is enough for the client to handle this specific case. But what about some more exotic (and a little constructed) use cases? What if some of these products can not be sold worldwide? Trade and regional laws often differ, and sometimes, things that you can consume or buy in country A are simply banned in country B.&lt;/p&gt;

&lt;p&gt;There are two ways to deal with this constructed case:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You simply leave the products out of the list&lt;/li&gt;
&lt;li&gt;You tell people that these products are not available for legal reasons&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you go for the first case, you are instantly done and don’t need to spend more thought on this case. If you want to stay with me for a little longer and just assume for a second that the second case also isn’t far from reality, continue to read.&lt;/p&gt;

&lt;p&gt;Let’s assume that the second case is exactly what your business wants. You can still give customers a hint that these products exist, and perhaps there are some relatives somewhere else that could potentially order these items and send them over (come on, we’ve all done this already. There is so much US food banned in Europe that is actually quite tasty…). Why should your business throw away this opportunity?&lt;/p&gt;

&lt;p&gt;If you want to solve exactly this case, you can make use of a union with two member types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A Product&lt;/li&gt;
&lt;li&gt;A geo-restricted Product&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first one is the usual entity, while the second one can have other properties (but doesn’t necessarily need to). The second entity type however forces any client to handle this case right within its code, either through a simple &lt;code&gt;if-statement&lt;/code&gt;, an &lt;code&gt;instanceof&lt;/code&gt; check, or else.&lt;/p&gt;

&lt;p&gt;Applying this to the schema, you end up with a schema that looks something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight graphql"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Query&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;productsByCategory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;!):&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;ProductResult&lt;/span&gt;&lt;span class="p"&gt;]!&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="k"&gt;union&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ProductResult&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Product&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;GeoRestrictedProduct&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Product&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;ID&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;GeoRestrictedProduct&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;ID&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;reason&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;availableCountries&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;!]!&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of directly nesting a Product within the list, &lt;code&gt;productsByCategory&lt;/code&gt; now returns a list of &lt;code&gt;ProductResult&lt;/code&gt;‘s. The latter is a union that has two members: &lt;code&gt;Product&lt;/code&gt; and &lt;code&gt;GeoRestrictedProduct&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If a product is geo-restricted (and ignoring that some laws might prevent us from doing this…damn you legislature!), a client can explicitly handle this case and additionally render why a customer can’t order this product and in which other countries they are still available.&lt;/p&gt;

&lt;p&gt;You can generally apply this pattern to all other use cases that potentially come to your mind. But we can do one more example to give you a better idea of how broadly applicable this pattern is.&lt;/p&gt;

&lt;p&gt;Let’s look at Medium, the popular blogging platform. It allows you to post articles and additionally monetize them (if you sign up for it). This presents two potential cases to users:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A user is a subscriber and can pass the paywall&lt;/li&gt;
&lt;li&gt;A user is not a subscriber and can thus only read an excerpt, with a call to action to subscribe (let’s ignore the two/three articles per month free thing here but we all use incognito tabs nevertheless, okay?)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you want to model this in a GraphQL schema, you can come up with something close to the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight graphql"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Query&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;article&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;ID&lt;/span&gt;&lt;span class="p"&gt;!):&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ArticleResult&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="k"&gt;union&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ArticleResult&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Article&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;RestrictedArticle&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Article&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;ID&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;RestrictedArticle&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;ID&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;excerpt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When a user requests an article, the API can check whether they are logged in and subscribed to Medium. If so, the API returns an &lt;code&gt;Article&lt;/code&gt; and the user can go on reading. If not, a &lt;code&gt;RestrictedArticle&lt;/code&gt; is returned that only has an excerpt in it.&lt;/p&gt;

&lt;p&gt;Any client is forced to handle the case of a &lt;code&gt;RestrictedArticle&lt;/code&gt; without having to check the status of the user itself. Everything is left to the backend to decide. In case of Medium’s main page, the user is presented the usual view of an excerpt of an article with a call to action to upgrade if they want to continue reading.&lt;/p&gt;

&lt;p&gt;This is the power of this pattern. It allows you to force clients to do something explicitly. Even if someone else decides to implement their own client for your API, you can still dictate to them what to do in certain circumstances without having to directly communicate with them. It can all be done through the schema of your API.&lt;/p&gt;

&lt;p&gt;With this issue solved, there is only one border left to conquer. Something for companies that take GraphQL very seriously and spend a lot of money on flexibility, technical toys (and sometimes nightmares): Federated GraphQL.&lt;/p&gt;




&lt;h2&gt;
  
  
  Handling Business Errors In Federated GraphQL APIs
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.apollographql.com/docs/federation/"&gt;Apollo Federation&lt;/a&gt; is an addition to the GraphQL specification. Its idea is to allow multiple GraphQL backend services to be composed into a single API surface, without the need to implement an API monolith.&lt;/p&gt;

&lt;p&gt;To make this happen, Apollo Federation extends the GraphQL specification and adds new directives that are used by a central service, called Router (or older Gateway), to derive a query plan for incoming requests.&lt;/p&gt;

&lt;p&gt;In federated Graphs, entities can be shared and referenced among individual GraphQL services (called subgraphs). It’s easy for one subgraph to add a property pointing to an entity that is served by another subgraph because the Router takes care of fetching the data before a unified response is sent back to a client.&lt;/p&gt;

&lt;p&gt;This system, however, has limits. One of these limits is the fact that unions cannot be referenced and shared between subgraphs. This is simply for the reason that unions cannot be uniquely identified by a type and an id.&lt;/p&gt;

&lt;p&gt;You can still make this pattern work in such a federated graph by extending it and adding a layer of indirection, which does not look very beautiful (to be honest), but does its job well.&lt;/p&gt;

&lt;p&gt;Let’s first look at a very simple API for a blog. It serves articles and authors through one, unified API, and its schema looks as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight graphql"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Query&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;article&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;ID&lt;/span&gt;&lt;span class="p"&gt;!):&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Article&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Article&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fields&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;ID&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Author&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fields&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;ID&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;articles&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Article&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each Entity (&lt;code&gt;Article&lt;/code&gt; and &lt;code&gt;Author&lt;/code&gt;) is served by a different subgraph and defined in its schema. When both schemas come together, the result is what you see above (ignoring the fact that federated API schemas usually have a lot of boilerplate code, like directives and such, inside them).&lt;/p&gt;

&lt;p&gt;Let’s now go back to the paywall example from before (because we all want to earn money somehow, don’t we? I’m not judging you! Content creation is a serious business and takes more time than many of us would like to admit.).&lt;/p&gt;

&lt;p&gt;Let’s also assume we want the following to happen:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Authors should always be viewable publicly (Who puts authors behind paywalls anyway?!)&lt;/li&gt;
&lt;li&gt;Articles can either be behind the paywall or free&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An issue now arises from the fact that we (as the good engineers we are) have modeled a real graph where both sides of an entity relationship have a reference to the other side (&lt;code&gt;Article&lt;/code&gt; references &lt;code&gt;Author&lt;/code&gt;, &lt;code&gt;Author&lt;/code&gt; references multiple &lt;code&gt;Article&lt;/code&gt;s. You can navigate in both ways.).&lt;/p&gt;

&lt;p&gt;Applying our previous solution would result in &lt;code&gt;Article&lt;/code&gt; becoming a union called something like &lt;code&gt;ArticleResult&lt;/code&gt;. But unions can’t be referenced by other subgraphs. The &lt;code&gt;Author&lt;/code&gt; subgraph would lose its reference, which is nothing you should favor. So, what now?&lt;/p&gt;

&lt;p&gt;You can add a (relatively ugly but necessary) indirection into the schema to make the whole system work again. Even if a union cannot be referenced by other subgraphs, a wrapper entity that contains a property that points to a union can (this sounds more complex than it actually is). If the &lt;code&gt;Author&lt;/code&gt; subgraph references that entity, a client can then unpack the nested result union (with more code), but still be forced to handle all possible cases.&lt;/p&gt;

&lt;p&gt;Let’s look at the resulting schema, so you better understand where this is going:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight graphql"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Query&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;article&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;ID&lt;/span&gt;&lt;span class="p"&gt;!):&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Article&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ArticleWrapper&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fields&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="c"&gt;# same as the nested&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="c"&gt;# entity's id&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;ID&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;article&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ArticleResult&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="k"&gt;union&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ArticleResult&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Article&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;RestrictedArticle&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Article&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;ID&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;RestrictedArticle&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;ID&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;excerpt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Author&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fields&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;ID&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;articles&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;ArticleWrapper&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Article&lt;/code&gt; has lost its &lt;em&gt;&lt;a class="mentioned-user" href="https://dev.to/key"&gt;@key&lt;/a&gt;&lt;/em&gt; directive. This is because it no longer needs it. Only entities that can be referenced by other subgraphs need such a directive. Instead, &lt;code&gt;ArticleWrapper&lt;/code&gt; is now the entity that can be referenced by other subgraphs. Nested within it is the union you already know, and within that union, both possible types are present. (I told you it wouldn’t be beautiful). An &lt;code&gt;Author&lt;/code&gt; now references an &lt;code&gt;ArticleWrapper&lt;/code&gt; instead of an &lt;code&gt;Article&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This small workaround allows you to keep all the benefits of this pattern while enabling the use right within a federated schema. Clients are still forced to explicitly handle scenarios you want them to handle, and you only pay a small (depending on how you view it) price with one further indirection (which bloats code a little, but nothing a dedicated API lib cannot solve).&lt;/p&gt;




&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Writing good GraphQL APIs is no easy task and handling errors puts an additional burden on you. But there are ways to handle especially errors in a way that benefits both you as an API’s creator and your users (or colleagues) who implement clients for it.&lt;/p&gt;

&lt;p&gt;By first classifying errors into two categories, technical and business errors, you open up a way to handle each category as best as possible.&lt;/p&gt;

&lt;p&gt;Technical errors are errors that you usually don’t want to happen, but they happen nevertheless. You can’t circumvent them but still somehow need to deal with them. Their impact, however, is often larger because they hinder people from progressing further in your software. Still, you can often only handle them implicitly by catching and presenting some form of error message to your users.&lt;/p&gt;

&lt;p&gt;Business errors are errors you expect to happen because they belong to the design of the flow of your API or software. Geo-blocking, paywalls, or else are usually examples of these kinds of errors. You want to handle them explicitly because their occurrence usually leads to a different flow in your application. You also often want to force users of your API to react to them without too obvious ways to circumvent handling these cases.&lt;/p&gt;

&lt;p&gt;In case of technical errors, you can use the errors within the GraphQL response and give general directions on what happened and leave a message for users to read. General catch blocks can then pick up the errors forwarded by GraphQL clients and do something with them.&lt;/p&gt;

&lt;p&gt;In case of business errors, you can use techniques to design your schema in a way that forces anyone who uses your API to take care of these kinds of errors when they occur. You can achieve this by using unions that model results, which are types that can be either this or that or even more.&lt;/p&gt;

&lt;p&gt;In federated environments, you have to put in a little more work to make the pattern of presenting business errors feasible. This is achieved by adding an indirection and a further abstraction in the form of a wrapper that then contains the already-known union type that models the actual response. This ensures that entities can still be referenced by other subgraphs without further technical limitations.&lt;/p&gt;

</description>
      <category>graphql</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to Write Files in Rust</title>
      <dc:creator>Oliver Jumpertz</dc:creator>
      <pubDate>Fri, 19 May 2023 21:06:01 +0000</pubDate>
      <link>https://forem.com/oliverjumpertz/how-to-write-files-in-rust-m06</link>
      <guid>https://forem.com/oliverjumpertz/how-to-write-files-in-rust-m06</guid>
      <description>&lt;p&gt;All software has to write data to the file system at some point. This is also true for programs written in &lt;a href="https://oliverjumpertz.com/an-introduction-to-rust/"&gt;Rust&lt;/a&gt;. Log messages need to be persisted (as long as they are not just written to stdout), and data needs to be saved for later, or else. Naturally, writing data is the opposite of &lt;a href="https://oliverjumpertz.com/how-to-read-files-in-rust/"&gt;reading data&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;There are multiple ways to deal with writing data to files in Rust. Some of these ways are easier than others, but all have their justification. Sometimes you directly want to write text into a file, and other times you write some kind of binary format. No matter your use case, Rust probably has a way to deal with it.&lt;/p&gt;

&lt;p&gt;In this article, you will learn about common ways of how to write files in Rust.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/CE_7vZKMs2Y"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Just in case you enjoy YouTube videos more than an article, you can also watch the video above instead!&lt;/p&gt;




&lt;h2&gt;
  
  
  Writing All Data to a File at Once
&lt;/h2&gt;

&lt;p&gt;Regularly, you only want to persist data that you already have in memory. In this case, this method is one of the most straightforward ways to deal with it.&lt;/p&gt;

&lt;p&gt;It allows you to write all data at once, and it doesn’t matter whether your data is a &lt;a href="https://doc.rust-lang.org/std/string/struct.String.html"&gt;String&lt;/a&gt; or already in binary format. Additionally, a file is conveniently created for you if it does not exist yet.&lt;/p&gt;

&lt;p&gt;Its advantages include:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It is convenient to use&lt;/li&gt;
&lt;li&gt;It is merely one line of code&lt;/li&gt;
&lt;li&gt;fs::write takes care of everything for you&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As usual, this method also has a few drawbacks, which are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It always overwrites the file&lt;/li&gt;
&lt;li&gt;Its performance highly depends on the size of your data – the more data you write, the longer it takes&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If this is what you are looking for, then take a look at the following code as an example of how to write all data to a file at once:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;write_string_to_file&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nb"&gt;Box&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;dyn&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;error&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nn"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(())&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;write_data_to_file&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;u8&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nb"&gt;Box&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;dyn&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;error&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nn"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;Ok&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;h2&gt;
  
  
  Writing All Data to a File at Once With the fs::File API
&lt;/h2&gt;

&lt;p&gt;The method above is just a convenient wrapper around this method, which gives you a bit more control over how you deal with a file and its contents.&lt;/p&gt;

&lt;p&gt;The only difference here is that there is no convenient under-the-hood conversion of Strings into binary. This is something you need to do yourself, in this case.&lt;/p&gt;

&lt;p&gt;The advantage of this method is the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It is still convenient to use&lt;/li&gt;
&lt;li&gt;It only takes a few lines of code, depending on how you do it&lt;/li&gt;
&lt;li&gt;It’s flexible to extend and alter&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And its drawbacks include these:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It always overwrites the file – no appending&lt;/li&gt;
&lt;li&gt;Its performance highly depends on the size of your data – the more data you write, the longer it takes&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If that’s okay for you, you can take the following code as a starting point for your own implementation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt;&lt;span class="n"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;write_string_to_file&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nb"&gt;Box&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;dyn&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;error&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;File&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// You need to take care of the conversion yourself&lt;/span&gt;
    &lt;span class="c1"&gt;// and can either try to write all data at once&lt;/span&gt;
    &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="nf"&gt;.write_all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="nf"&gt;.as_bytes&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Or try to write as much as possible, but need&lt;/span&gt;
    &lt;span class="c1"&gt;// to take care of the remaining bytes yourself&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;remaining&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="nf"&gt;.write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="nf"&gt;.as_bytes&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;remaining&lt;/span&gt; &lt;span class="o"&gt;&amp;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;// You need to handle the remaining bytes&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(())&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;write_data_to_file&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;u8&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nb"&gt;Box&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;dyn&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;error&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;File&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// You can either try to write all data at once&lt;/span&gt;
    &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="nf"&gt;.write_all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Or try to write as much as possible, but need&lt;/span&gt;
    &lt;span class="c1"&gt;// to take care of the remaining bytes yourself&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;remaining&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="nf"&gt;.write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;remaining&lt;/span&gt; &lt;span class="o"&gt;&amp;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;// You need to handle the remaining bytes&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;Ok&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;h2&gt;
  
  
  Appending Data to a File
&lt;/h2&gt;

&lt;p&gt;You don’t always want to overwrite your files. Sometimes, you process things in a loop and don’t want to create a huge String or byte array before you dump your data. Such methods also help to keep the memory profile of your Rust binary low.&lt;/p&gt;

&lt;p&gt;If you want to append data to an existing file, there is a convenient method for it. It uses &lt;a href="https://doc.rust-lang.org/std/fs/struct.OpenOptions.html"&gt;fs::OpenOptions&lt;/a&gt; which allows for granular control over how the file is opened.&lt;/p&gt;

&lt;p&gt;The advantages of this method include the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You gain full control over how the file is handled&lt;/li&gt;
&lt;li&gt;It still only takes a few lines of code, depending on how you do it&lt;/li&gt;
&lt;li&gt;The performance of this method depends on you size the data you write&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As usual, this method has some drawbacks, as well:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Every call is a potential system call&lt;/li&gt;
&lt;li&gt;Every call writes to the file system, which can be costly if the data size is not optimal&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If this method seems like something you want to use, you can see an example below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt;&lt;span class="n"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;append_string_to_file&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nb"&gt;Box&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;dyn&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;error&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;OpenOptions&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// You need to take care of the conversion yourself&lt;/span&gt;
    &lt;span class="c1"&gt;// and can either try to write all data at once&lt;/span&gt;
    &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="nf"&gt;.write_all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="nf"&gt;.as_bytes&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Or try to write as much as possible, but need&lt;/span&gt;
    &lt;span class="c1"&gt;// to take care of the remaining bytes yourself&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;remaining&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="nf"&gt;.write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="nf"&gt;.as_bytes&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;remaining&lt;/span&gt; &lt;span class="o"&gt;&amp;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;// You need to handle the remaining bytes&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(())&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;append_data_to_file&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;u8&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nb"&gt;Box&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;dyn&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;error&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;OpenOptions&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// You can either try to write all data at once&lt;/span&gt;
    &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="nf"&gt;.write_all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Or try to write as much as possible, but need&lt;/span&gt;
    &lt;span class="c1"&gt;// to take care of the remaining bytes yourself&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;remaining&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="nf"&gt;.write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;remaining&lt;/span&gt; &lt;span class="o"&gt;&amp;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;// You need to handle the remaining bytes&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;Ok&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;h2&gt;
  
  
  Writing and Appending Data to a File with BufWriter
&lt;/h2&gt;

&lt;p&gt;All previously presented methods have one thing in common: They always try to write everything you give them at once, and they offer no flexibility in this regard.&lt;/p&gt;

&lt;p&gt;If you are looking for a way to write large chunks of data consistently without impacting the performance of your program, you can use a &lt;a href="https://doc.rust-lang.org/std/io/struct.BufWriter.html"&gt;BufWriter&lt;/a&gt;. Internally, a BufWriter buffers the data it receives and tries to optimize the way it writes it to the file system.&lt;/p&gt;

&lt;p&gt;This method requires not many new concepts and even uses the APIs you have already learned about above. Additionally, it also has a few advantages:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You have full control over how the file is handled&lt;/li&gt;
&lt;li&gt;It gives you a lot of flexibility&lt;/li&gt;
&lt;li&gt;A BufWriter optimizes access to the file system under the hood&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Gladly, there are only two drawbacks to this method:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It can be a little more difficult to use&lt;/li&gt;
&lt;li&gt;It takes a little more code to implement&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you want to use the most flexible way to write data to files, check the code below for an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt;&lt;span class="n"&gt;BufWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;append_string_to_file&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nb"&gt;Box&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;dyn&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;error&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;OpenOptions&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;BufWriter&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// You need to take care of the conversion yourself&lt;/span&gt;
    &lt;span class="c1"&gt;// and can either try to write all data at once&lt;/span&gt;
    &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="nf"&gt;.write_all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="nf"&gt;.as_bytes&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Or try to write as much as possible, but need&lt;/span&gt;
    &lt;span class="c1"&gt;// to take care of the remaining bytes yourself&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;remaining&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="nf"&gt;.write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="nf"&gt;.as_bytes&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;remaining&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="c1"&gt;// handle...&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// You definitely need to flush a BufWriter&lt;/span&gt;
    &lt;span class="c1"&gt;// as it cannot guarantee emptying its buffer&lt;/span&gt;
    &lt;span class="c1"&gt;// when it goes out of scope&lt;/span&gt;
    &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="nf"&gt;.flush&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(())&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;append_data_to_file&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;u8&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nb"&gt;Box&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;dyn&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;error&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;OpenOptions&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;BufWriter&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// You can either try to write all data at once&lt;/span&gt;
    &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="nf"&gt;.write_all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Or try to write as much as possible, but need&lt;/span&gt;
    &lt;span class="c1"&gt;// to take care of the remaining bytes yourself&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;remaining&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="nf"&gt;.write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;remaining&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="c1"&gt;// handle...&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// You definitely need to flush a BufWriter&lt;/span&gt;
    &lt;span class="c1"&gt;// as it cannot guarantee emptying its buffer&lt;/span&gt;
    &lt;span class="c1"&gt;// when it goes out of scope&lt;/span&gt;
    &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="nf"&gt;.flush&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nf"&gt;Ok&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;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;While developing software, you often have to write data to files. As usual, Rust is no different than other languages when it offers multiple ways to perform these kinds of operations. In this article, you learned about four different ways to write files in Rust.&lt;/p&gt;

&lt;p&gt;Like always in life, everything you do can have both advantages and disadvantages; thus, the concept is no different in this regard. You always have to spend a few thoughts and think about which way is the best for your specific use case.&lt;/p&gt;

&lt;p&gt;Writing all data at once is a great way to perform the action with as little code as possible. Additionally, you don’t have to worry too much about the format of the data. This convenience comes at a cost, though. The more data you need to write, the larger your memory grows, and there is no control over preventing an existing file from being overridden.&lt;/p&gt;

&lt;p&gt;Writing all data at once with the File API is another way to perform the action of dumping all your data onto the file system. Unlike its wrapper API, it offers you more flexibility in handling this process. You also lose the flexibility of a convenient API that does conversion if you deal with Strings. Additionally, the problem with large data blobs remains. A lot of data means a lot of memory.&lt;/p&gt;

&lt;p&gt;Appending data to a file with the OpenOptions API brings even more flexibility than all previous methods. Here, you don’t have to worry about the size of your data. You append data to a file as you process it and can keep a lower memory profile. This method needs some fine-tuning, though. Appending data often leads to system calls that are sometimes pretty costly, so there is still room for improvement.&lt;/p&gt;

&lt;p&gt;Finally, using a BufWriter gives you the most flexibility, but it’s also a relatively low-level approach to performing write operations. The BufWriter itself does at least buffer the data for you, but its effectiveness still depends on your usage. If you don’t take care while implementing your write operations with a BufWriter, you can also quickly sabotage all your efforts at improving the performance of your program.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to Read Files in Rust</title>
      <dc:creator>Oliver Jumpertz</dc:creator>
      <pubDate>Sun, 07 May 2023 18:01:44 +0000</pubDate>
      <link>https://forem.com/oliverjumpertz/how-to-read-files-in-rust-525d</link>
      <guid>https://forem.com/oliverjumpertz/how-to-read-files-in-rust-525d</guid>
      <description>&lt;p&gt;Reading files is one of the most common operations you can come across in software development. Loading configuration files, processing files, and more are often part of the use case of the software you build.&lt;/p&gt;

&lt;p&gt;Like in any other programming language, there are multiple ways how to read files in Rust. However, all have their advantages and disadvantages. This is why it is crucial also to understand which method to use in which case.&lt;/p&gt;

&lt;p&gt;In this article, you will learn about Rust’s most common ways to read files.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/CE_7vZKMs2Y"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Just in case you enjoy YouTube videos more than an article, you can also watch the video above instead!&lt;/p&gt;




&lt;h2&gt;
  
  
  Reading an entire File into a String
&lt;/h2&gt;

&lt;p&gt;Reading an entire file into a &lt;a href="https://doc.rust-lang.org/std/string/struct.String.html"&gt;String&lt;/a&gt; has its advantages. You don’t need to worry about anything other than handling the file and processing its content. This is an excellent choice for files that:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Contain String content&lt;/li&gt;
&lt;li&gt;Can be processed as a whole at once&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;On the other hand, this method also has its drawbacks:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Files too large might have severe performance impacts&lt;/li&gt;
&lt;li&gt;The larger the file, the larger the memory consumption of your program&lt;/li&gt;
&lt;li&gt;Files that do not contain String but binary content can’t be processed this way&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The following example shows how to read a whole file into a String:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;read_file_content_as_string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Box&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;dyn&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;error&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;string_content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;read_to_string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string_content&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;h2&gt;
  
  
  Reading an entire File into a Byte Vector
&lt;/h2&gt;

&lt;p&gt;Reading an entire file into a &lt;a href="https://doc.rust-lang.org/std/primitive.u8.html"&gt;byte vector&lt;/a&gt; is the way to go if you don’t necessarily have to deal with String content but some form of binary format you need to process. This method still works for String content, though. Instead of directly receiving a String from the method call, you have to instantiate it yourself. You don’t have to do that if you don’t deal with String content.&lt;/p&gt;

&lt;p&gt;This method is a great choice for files that:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Contain any form of content&lt;/li&gt;
&lt;li&gt;Can be processed as a whole file at once&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Still, some of the same drawbacks that also affect reading an entire file into a String apply. These are namely:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Files too large might have a severe performance impact&lt;/li&gt;
&lt;li&gt;The larger the file, the larger the memory consumption of your program&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The example below demonstrates how to read a whole file into a byte vector:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;read_file_as_bytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;u8&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Box&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;dyn&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;error&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;byte_content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;byte_content&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;If you still want to convert the byte vector into a String yourself, you can do it as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;read_file_as_bytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Box&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;dyn&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;error&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;byte_content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;string_content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;str&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from_utf8&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;byte_content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string_content&lt;/span&gt;&lt;span class="nf"&gt;.to_string&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;h2&gt;
  
  
  Reading a File Line by Line
&lt;/h2&gt;

&lt;p&gt;As stated above, reading an entire file at once can lead to problems if you deal with large files. In cases like this, it’s best to process these files with a line-by-line approach. This is, of course, something mostly applicable to files with String content.&lt;/p&gt;

&lt;p&gt;Rust gladly has a convenient struct in its standard library that also takes away some lower-level details, called &lt;a href="https://doc.rust-lang.org/std/io/struct.BufReader.html"&gt;BufReader&lt;/a&gt;. This method is a solid choice for files that:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Contain String content&lt;/li&gt;
&lt;li&gt;Are too large to be processed at once&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This approach, however, also has a few drawbacks that need to be mentioned, and these are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It only works for files with String content&lt;/li&gt;
&lt;li&gt;Implementations can quickly become more complex&lt;/li&gt;
&lt;li&gt;Depending on the formatting of the file, you might have to buffer lines yourself if not everything you want to process is placed on the same line&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The following example shows how to read a file line by line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;File&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt;&lt;span class="n"&gt;BufReader&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BufRead&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;read_file_line_by_line&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nb"&gt;Box&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;dyn&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;error&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;File&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;reader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;BufReader&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;reader&lt;/span&gt;&lt;span class="nf"&gt;.lines&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// line is a String&lt;/span&gt;
            &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;process_line&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="nf"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;handle_error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;Ok&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;h2&gt;
  
  
  Reading a File in Single Byte-Steps
&lt;/h2&gt;

&lt;p&gt;While the previous approach allowed you to read a file line-by-line, this one allows you to read individual bytes from the file you want to process with a BufReader. It is one of the most basic approaches as you put away nearly all guard rails the standard library gives you. It does, however, give you some of the most flexibility.&lt;/p&gt;

&lt;p&gt;Use this approach if you:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Need complete control over what happens with the content of a file&lt;/li&gt;
&lt;li&gt;Are perfectly fine to implement a lot of the content handling yourself&lt;/li&gt;
&lt;li&gt;Have to deal with large files that would make your memory consumption explode if read all at once&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Although already mentioned, it is still a good idea to talk about the drawbacks of this method, as well. Its drawbacks include:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You have to work with raw data. In this case, its even single raw bytes&lt;/li&gt;
&lt;li&gt;You probably still need a buffer to temporarily save single bytes until you can merge several of them into something more meaningful&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The following example demonstrates how to read a file in single byte-steps:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;File&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt;&lt;span class="n"&gt;BufReader&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Read&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;read_file_as_single_bytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nb"&gt;Box&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;dyn&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;error&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;File&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;reader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;BufReader&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;byte&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;reader&lt;/span&gt;&lt;span class="nf"&gt;.bytes&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;byte&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// byte is exactly one byte&lt;/span&gt;
            &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;process_byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="nf"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;handle_error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;Ok&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;h2&gt;
  
  
  Reading a File in Byte Chunks
&lt;/h2&gt;

&lt;p&gt;You can use BufReader to read chunks from a file if you want more flexibility. To be completely honest, BufReader also does optimizations under the hood and doesn’t read each byte individually when you use its .bytes() method. It reads them in chunks and then returns single bytes from the Iterator.&lt;/p&gt;

&lt;p&gt;That doesn’t help much when you want to process chunks yourself, though. You can, of course, buffer the bytes manually when using bytes(), or you simply follow this method.&lt;/p&gt;

&lt;p&gt;Reading the content of a file in byte chunks has advantages and disadvantages like any other method. Its advantages are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You gain complete control over how you deal with the contents of a file&lt;/li&gt;
&lt;li&gt;It gives you the most flexibility, as you can adjust the chunk size dynamically and react to specific circumstances&lt;/li&gt;
&lt;li&gt;You can use it if you have to deal with large files that would make your memory consumption explode if read all at once&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Of course, there are once again a few known drawbacks that apply to this method:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You have to work with raw data. All decoding and processing is up to you&lt;/li&gt;
&lt;li&gt;It might take a few attempts to optimize the buffer size for specific scenarios&lt;/li&gt;
&lt;li&gt;If you make the chunk size too small, you might actually hurt the overall performance of your program (too many system calls)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The example below shows how to read a file in byte chunks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;File&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt;&lt;span class="n"&gt;BufReader&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BufRead&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;BUFFER_SIZE&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;512&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;read_file_in_byte_chunks&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nb"&gt;Box&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;dyn&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;error&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;File&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;reader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;BufReader&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;with_capacity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BUFFER_SIZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;loop&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;reader&lt;/span&gt;&lt;span class="nf"&gt;.fill_buf&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;buffer_length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="nf"&gt;.len&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;// BufRead could not read any bytes.&lt;/span&gt;
        &lt;span class="c1"&gt;// The file must have completely been read.&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;buffer_length&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="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="nf"&gt;do_something_with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// All bytes consumed from the buffer&lt;/span&gt;
        &lt;span class="c1"&gt;// should not be read again.&lt;/span&gt;
        &lt;span class="n"&gt;reader&lt;/span&gt;&lt;span class="nf"&gt;.consume&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer_length&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;Ok&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;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Reading files is a common operation when developing software. Like any other programming language, Rust offers several ways to deal with it. This guide covered five common ways to read files (both as Strings and in raw binary format) in Rust.&lt;/p&gt;

&lt;p&gt;All methods presented have advantages and drawbacks, and you need to choose the one appropriate for your specific situation and use case.&lt;/p&gt;

&lt;p&gt;Reading an entire file into a String is a great choice if you have small files and deal with String content. On the other hand, the method is not the best if your files become larger or you don’t deal with String content at all.&lt;/p&gt;

&lt;p&gt;Reading an entire file into a byte vector is a good choice if you have small files and deal with arbitrary raw content. It is lacking if your files become larger and you have memory constraints, though.&lt;/p&gt;

&lt;p&gt;Reading files line by line is an excellent choice if you deal with String content and don’t want your memory to grow too much. The method falls short if you don’t deal with String content, and have files that spread the content you want at once over multiple lines, which requires you to buffer lines yourself.&lt;/p&gt;

&lt;p&gt;Reading files in single byte-steps is one of the most basic methods. It’s a great choice if you want flexibility and need a lot of control. On the other hand, you need to work with raw data and also have to probably buffer data yourself if you need to merge multiple bytes into something more meaningful.&lt;/p&gt;

&lt;p&gt;Lastly, reading a file in byte chunks is a little more flexible than reading each byte individually. It offers full control over the processing of the data and can also be adjusted dynamically. But once again, you need to work with raw data, and it might take some time to fine-tune the chunking.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>beginners</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Your Guide To Computer Science Fundamentals</title>
      <dc:creator>Oliver Jumpertz</dc:creator>
      <pubDate>Wed, 23 Nov 2022 10:29:07 +0000</pubDate>
      <link>https://forem.com/oliverjumpertz/your-guide-to-computer-science-fundamentals-545d</link>
      <guid>https://forem.com/oliverjumpertz/your-guide-to-computer-science-fundamentals-545d</guid>
      <description>&lt;p&gt;Computer science fundamentals are the foundation of any software developer's job. Starting with how a computer works, over logic, maths, algorithms, and data structures, and ending with networking and I/O. Any developer should at least have basic knowledge about these fundamentals. This is why &lt;strong&gt;Your Guide to Computer Science Fundamentals&lt;/strong&gt; exists.&lt;/p&gt;

&lt;p&gt;Many of the concepts presented in this guide are things you will just use on a day-to-day basis as a developer, others are used less frequently. The important part is still having some knowledge about them in the back of your head.&lt;/p&gt;

&lt;p&gt;This guide comes with a mix of practical advice and a collection of learning resources you can use to advance your knowledge in the respective fields. While this guide can probably be read within a few minutes, you will need way longer to fully understand all the topics covered.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Fundamentals are And why they are Important
&lt;/h2&gt;

&lt;p&gt;Fundamentals are concepts, and maybe even techniques, that you can apply in multiple areas and multiple programming languages. They are not tied to anything specific and are sometimes pretty theoretical.&lt;/p&gt;

&lt;p&gt;The importance of these fundamentals comes from the fact that they build the structure the rest of your programming and development knowledge is based on. Without knowing how a computer works, it becomes more difficult to understand what a programming language actually is and why the instructions you type in are the way they are.&lt;/p&gt;

&lt;p&gt;It can take a long time to learn all the fundamentals there are, and it can cost a lot of energy to stay motivated. In the end, however, every piece of fundamental knowledge you acquire speeds up the learning process for everything else that is based on it.&lt;/p&gt;

&lt;p&gt;Too many young or inexperienced software developers jump right into frameworks when they learn how to program. Later, they face problems that are not solved that easily if they don't know how the underlying technology or concept works. Just take networking as an example: It is very easy to perform remote requests these days, but if the underlying network has problems, you might be clueless as to why your requests are never successful. Knowing at least that a network exists and how it approximately works can help with the debugging process.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Most Important Fundamentals
&lt;/h2&gt;

&lt;p&gt;Here is a list of the most important ones:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How a computer works&lt;/li&gt;
&lt;li&gt;What a programming language actually is and does&lt;/li&gt;
&lt;li&gt;Logic&lt;/li&gt;
&lt;li&gt;Math&lt;/li&gt;
&lt;li&gt;Algorithms&lt;/li&gt;
&lt;li&gt;Data Structures&lt;/li&gt;
&lt;li&gt;Networking / I/O&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This list seems long and contains many very broad topics, but if you get broad knowledge in these topics, your chances are high that your programming and software design skills will profit from it a lot. And in return, you will learn new frameworks faster, your code will become better, safer, and more resilient (because you know what awaits you).&lt;/p&gt;

&lt;p&gt;This guide will go through these fundamentals a little more in-depth soon, so don't worry that you are left with only a list of topics you have to dive into yourself. Just know that these exist for now.&lt;/p&gt;




&lt;h2&gt;
  
  
  How to Learn the Fundamentals
&lt;/h2&gt;

&lt;p&gt;There are multiple strategies for learning something. One approach might be to just try to learn everything at once and then jump into more advanced topics. Another approach might be to learn as you go. Overall, there is no overall correct approach. There is only the approach that suits you best.&lt;/p&gt;

&lt;p&gt;No matter what way you take, there are a few key concepts that can help you along the way.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Use curiosity to your advantage
&lt;/h3&gt;

&lt;p&gt;No matter whether you try to learn everything up front or along the way, always give in to your curiosity.&lt;/p&gt;

&lt;p&gt;Dive a little deeper whenever you become curious because you don't know something yet. Make a small list and note down all the topics or questions you discover. This ensures you can take care of learning more about it at a later point.&lt;/p&gt;

&lt;p&gt;As long as you are curious, chances are high that you are willing to go even down a rabbit hole, and your brain will most probably be more welcome to consume the information you find. This can also have a positive effect on your motivation.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Don't overwhelm yourself
&lt;/h3&gt;

&lt;p&gt;It might often seem tempting to learn everything at once, but most people do best when they learn one topic at a time. This is where a list of topics and questions comes in handy. You always have something to return to later without fearing that you forget what topics you want to research.&lt;/p&gt;

&lt;p&gt;You should also make sure to take frequent breaks. There are &lt;a href="https://health.cornell.edu/about/news/study-breaks-stress-busters#:~:text=Research%20shows%20that%20taking%20purposeful,%E2%80%9D%20(see%20the%20research)."&gt;studies&lt;/a&gt; that show that frequent breaks only help you refresh your energy faster and thus enabling you to learn more in less time. The worst thing to happen is not remembering everything from a 2-hour hardcore study session on a complex topic.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Revisit topics regularly
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.encyclopedia.com/psychology/encyclopedias-almanacs-transcripts-and-maps/repetition-and-learning"&gt;Repetition is one major key&lt;/a&gt; to learning efficiently and effectively. Reading something once is not enough to build long-lasting connections in your brain. Revisiting topics regularly, especially if you don't come in contact with them often, helps you keep the crucial knowledge you worked hard for.&lt;/p&gt;

&lt;p&gt;If you work with a list, like already proposed, don't throw it away as soon as you are done. Try to keep it and think about persisting it (Notion, a Todo app, etc.) while additionally attaching your notes to each point. This gives you a great personal learning resource you can revisit again and again whenever you see the need arise.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Connect with peers or a mentor
&lt;/h3&gt;

&lt;p&gt;There are many more developers who still need to cover computer science fundamentals. If you have a chance, try connecting with them and learning together. You can exchange your learning materials, explain concepts, and motivate each other to continue this interesting journey. A short weekly call where you talk about your last week of learning and share important discoveries can already be enough.&lt;/p&gt;

&lt;p&gt;Another way is to find a mentor. That is usually a developer with more experience than you, who already knows their way around these fundamentals. Admittedly, finding a mentor is difficult these days, but if you can find one who is willing to help you, take the chance. A mentor can help you in many ways. They can guide you, provide you with explanations, share learning material with you, and much more.&lt;/p&gt;

&lt;p&gt;Now that you are prepared with crucial knowledge of what fundamentals are, why they are important, and how you should approach learning them, it is time to get into them.&lt;/p&gt;




&lt;h2&gt;
  
  
  How A Computer Works
&lt;/h2&gt;

&lt;p&gt;The most basic knowledge of a software developer is understanding how a computer works. The level you work at is already so far away from the current running through wires and circuits inside your computer that it is often hard to even imagine how this can work at all.&lt;/p&gt;

&lt;p&gt;This knowledge doesn't benefit you directly as modern operating systems and programming languages hide these details from you perfectly, though, but it is also foundational knowledge to understand how information travels through networks (and especially the internet as a network on a global scale).&lt;/p&gt;

&lt;p&gt;Next to knowing that bits and bytes exist, knowing how they are stored in your computer's memory (and even in your CPU's cache) or on your hard drive is crucial information for any developer. It helps you understand that certain ways of storing information are slower than others and can indeed affect the performance of your software.&lt;/p&gt;

&lt;p&gt;Finally, it's always incredible to be able to explain in detail how someone's smartwatch works at parties, isn't it? Because conceptually, a smartwatch and a smartphone are also just smaller computers, and most people will probably be quite happy (actually not) if you tell them everything they always (hint: they probably didn't) wanted to know about their devices.&lt;/p&gt;

&lt;h3&gt;
  
  
  What you should know
&lt;/h3&gt;

&lt;p&gt;There is a whole lot to know, and knowing everything probably won't benefit you (except if you are really into hardware engineering and design), but a few key concepts are a necessity for developers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How current and transistors actually bring your PC to life&lt;/li&gt;
&lt;li&gt;What a CPU is and how it works&lt;/li&gt;
&lt;li&gt;What memory is and how it works&lt;/li&gt;
&lt;li&gt;How an operating system makes a computer usable for ordinary people and what it provides to developers&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Where you can learn about computers
&lt;/h3&gt;

&lt;p&gt;Many resources on the internet can teach you a lot about computers and how they work. It would probably take ages to list them all here. The following list is thus only an excerpt and a starting point for you to dig deeper and learn more (if you wish to):&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;a href="https://www.javatpoint.com/computer-fundamentals-tutorial"&gt;Computer Fundamentals by Javatpoint&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;This is an excellent guide for beginners by Javatpoint, and it covers the most important points (and even a bit more). It's a great starting point to learn everything you can about computers.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;a href="https://homepage.cs.uri.edu/faculty/wolfe/book/Readings/Reading01.htm"&gt;Introduction to Computers by the University of Rhode Island&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;This is another great entry guide to computers by an actual university. The only drawback to it is that you can only navigate by iterating over the resources (Reading01.html -&amp;gt; Reading02.html -&amp;gt; Reading03.html -&amp;gt; etc.).&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;a href="https://www.learncomputerscienceonline.com/how-computer-works/"&gt;Learn Computer Science&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;This is an online course on how computers work. If you prefer a whole guide, including videos, this might be the right one for you.&lt;/p&gt;




&lt;h2&gt;
  
  
  What A Programming Language Actually Is And Does
&lt;/h2&gt;

&lt;p&gt;When you have a basic understanding of how a computer works, you need to understand that the high-level languages most of us work with do a lot to abstract away low-level details.&lt;/p&gt;

&lt;p&gt;There are many different programming languages nowadays, and every one of them has a special purpose (even the esoteric ones, although they often prove a specific point instead of being created for a specific purpose).&lt;/p&gt;

&lt;p&gt;A crucial thing to understand with programming languages is that all of them end up as machine code in one way or another. And that machine code is somehow mapped to a processor's instructions. Some code results in more machine code than others, and some results in less. At least understanding why this is the case and how it can affect the performance of your software is knowledge that can come in handy depending on where you end up working.&lt;/p&gt;

&lt;h3&gt;
  
  
  What you should know
&lt;/h3&gt;

&lt;p&gt;You should at least understand the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A programming language is a set consisting of

&lt;ul&gt;
&lt;li&gt;A specification&lt;/li&gt;
&lt;li&gt;Tools&lt;/li&gt;
&lt;li&gt;Sometimes a runtime&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Why different programming languages exist&lt;/li&gt;
&lt;li&gt;What purpose different programming languages have&lt;/li&gt;
&lt;li&gt;What manual memory management is&lt;/li&gt;
&lt;li&gt;How a garbage collector works&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Where you can learn more about programming languages
&lt;/h3&gt;

&lt;p&gt;You can search for days and find new articles and guides on this topic. The internet is just a vast place of information. To give you a general direction, though, here are some resources that you can start with:&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;a href="https://www.cs.mcgill.ca/~rwest/wikispeedia/wpcd/wp/p/Programming_language.htm"&gt;Programming language by McGill University&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;A small introduction to programming languages, what they are, and what they do.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;a href="https://www.javatpoint.com/programming-language"&gt;Programming Language by Javatpoint&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;A good introduction to programming languages at a higher level.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;a href="https://en.wikibooks.org/wiki/Memory_Management/Manual_Memory_Management"&gt;Memory Management - Wiki Books&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;This a good entry into memory management and why it is necessary.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;a href="https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/fundamentals"&gt;Fundamentals of garbage collection by Microsoft Learn&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;A guide on garbage collection that helps you understand what these garbage collectors actually do and how they manage memory for you.&lt;/p&gt;




&lt;h2&gt;
  
  
  Logic
&lt;/h2&gt;

&lt;p&gt;Logic is an integral part of programming. Although it is actually a subarea of mathematics, it has a crucial place in any developer's tool belt, which is why it is mentioned separately.&lt;/p&gt;

&lt;p&gt;Without logic, there would be no way to write effective software. Everything would run always or never, without any middle way. If this happens, then do that is just a fundamental construct. It is so important that every language has at least one construct (if, else if, else, and often also switch) to enable working with it.&lt;/p&gt;

&lt;p&gt;Once you have conditions, you are confronted with Boolean algebra. That is another subarea of math (and of logic, which makes up the algebra of operations. With boolean algebra, you can evaluate and simplify your statements and much more.&lt;/p&gt;

&lt;h3&gt;
  
  
  What you should know
&lt;/h3&gt;

&lt;p&gt;If you plan to stay in software development for longer, you should try to learn as much as possible about logic and boolean algebra. At least you should learn the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What mathematical logic is&lt;/li&gt;
&lt;li&gt;How mathematical logic works&lt;/li&gt;
&lt;li&gt;Set theory&lt;/li&gt;
&lt;li&gt;Boolean algebra&lt;/li&gt;
&lt;li&gt;Operators&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Where you can learn more about logic
&lt;/h3&gt;

&lt;p&gt;There are a few noteworthy resources that you should definitely take a look at:&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;a href="https://www.khanacademy.org/"&gt;Khan Academy&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;Khan Academy is a free learning platform that aims at teaching many subjects, including STEM. It has a well-curated section on math and logic, which is why it must be listed here.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;a href="https://www.logicmatters.net/tyl/"&gt;Logic Matters&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;Peter Smith has done a great job at writing a book about mathematical logic for beginners. It is well worth downloading and reading.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;a href="https://study.com/academy/topic/boolean-algebra-logic-gates.html"&gt;Boolean Algebra &amp;amp; Logic Gates by Study.com&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;A few videos are never bad in a study mix. If you are more the kind of a visual learner, these lessons might be great for you.&lt;/p&gt;




&lt;h2&gt;
  
  
  Math
&lt;/h2&gt;

&lt;p&gt;Computers are pretty good at math. This is why a lot of software deals with math problems (with difficulties ranging from easy to insane) every day. Every frontend, for example, does regularly need to calculate the position of certain UI elements, and a lot of business software takes care of accounting tasks and needs to calculate costs or else.&lt;/p&gt;

&lt;p&gt;Sooner or later, every software developer is confronted with a more or less simple math problem. Thus, having some maths skills doesn't hurt at all. It simplifies many tasks and can often also help with designing efficient algorithms.&lt;/p&gt;

&lt;p&gt;Even if you never stumble upon a math-related problem in all your career (and I highly doubt it), you will probably need to calculate an average or median at some point (especially when evaluating the performance of your software) or sum up a few numbers and find the standard deviation.&lt;/p&gt;

&lt;h3&gt;
  
  
  What you should know
&lt;/h3&gt;

&lt;p&gt;Math is an ultra broad topic with many subareas that themselves are probably deeper than the mariana trench. It is thus really difficult to state how much you should know. Generally speaking, you should try to learn as much as possible, but if math really is not for you, you should at least focus on a few key areas:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Basic algebra&lt;/li&gt;
&lt;li&gt;Calculus&lt;/li&gt;
&lt;li&gt;Linear algebra (especially useful in the frontend but also for UI work in general)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Where you can learn more about math
&lt;/h3&gt;

&lt;p&gt;As usual, the internet is full of resources, but if you only have the time and resources to visit just one resource, let it be the following one:&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;a href="https://www.khanacademy.org/"&gt;Khan Academy&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;You probably already know Khan Academy from the section on logic. It is really an incredible resource if you want to learn math, and it thus deserves a second spot in this guide.&lt;/p&gt;




&lt;h2&gt;
  
  
  Algorithms
&lt;/h2&gt;

&lt;p&gt;Algorithms are the bread and butter of any software developer. Each line of code you write contributes to an algorithm, even if it only prints a single statement to the console. Without algorithms, there would be no software at all.&lt;/p&gt;

&lt;p&gt;An algorithm is a way or a set of instructions the computer follows to get an expected result. If you think about this for a moment, you probably understand that everything you do is an algorithm. The computer always follows your instructions precisely to get the output you intended (or not - nasty bugs) to get.&lt;/p&gt;

&lt;p&gt;Additionally, practicing your algorithmic skills also trains your own brain to think more like a developer and how to approach certain problems with a computer, which often differs fundamentally from how you would approach them in the real world. The more you practice, the better you become at solving problems. The better you become at solving problems, the easier it becomes for you to create solutions.&lt;/p&gt;

&lt;h3&gt;
  
  
  What you should know
&lt;/h3&gt;

&lt;p&gt;You should touch a few basics at least once in your career because other software craftsmen spent years discovering these basic techniques. Next to that, you should, of course, also train your skills outside of these well-known fields.&lt;/p&gt;

&lt;p&gt;To give you a general idea, here is a (non-exhaustive) list:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Backtracking&lt;/li&gt;
&lt;li&gt;Greedy Algorithms&lt;/li&gt;
&lt;li&gt;Divide &amp;amp; Conquer&lt;/li&gt;
&lt;li&gt;Branch and Bound&lt;/li&gt;
&lt;li&gt;Dynamic Programming&lt;/li&gt;
&lt;li&gt;Randomized Algorithms&lt;/li&gt;
&lt;li&gt;Brute-force or exhaustive search&lt;/li&gt;
&lt;li&gt;Simple algorithms without any special technique

&lt;ul&gt;
&lt;li&gt;e.g., just a sequence of statements to solve a specific problem&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Where you can learn more about algorithms
&lt;/h3&gt;

&lt;p&gt;Algorithms usually come together with data structures, which is why you will probably cover a good chunk of the next section with this one. Nevertheless, algorithms themselves are not mutually tied to data structures, as you have already learned, and thus learning algorithms on their own definitely is a good idea. A few good resources to get started are the following:&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;a href="https://www.edx.org/learn/algorithms"&gt;Courses on algorithms on edX&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;edX is a learning platform dedicated to bringing university-level education to anyone. They have a large section on algorithms, and a few of those courses are well worth taking.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;a href="https://www.geeksforgeeks.org/fundamentals-of-algorithms/"&gt;Algorithms on geeks for geeks&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;Geeks for geeks is a big platform these days, with many resources on nearly everything related to software development. Their section on algorithms is a good entry point to get started.&lt;/p&gt;




&lt;h2&gt;
  
  
  Data Structures
&lt;/h2&gt;

&lt;p&gt;Data structures are often dreaded because many companies still or used to ask questions about them in technical interviews. Many of those interviews involve reimplementing common data structures on a whiteboard or paper. Although this practice is questionable, it has done a lot to make many developers fear this special area of software development.&lt;/p&gt;

&lt;p&gt;Generally, data structures are just a common way to collect and interlink data. Like a drawer where you probably store a few important documents, data structures store data that a developer wants to access sooner or later. Every data structure has advantages and drawbacks. Some take longer to insert data, some take longer to retrieve it again.&lt;/p&gt;

&lt;p&gt;Knowing the characteristics of data structures is important for a software developer because it influences the runtime of algorithms and software as a whole. A wrong data structure chosen for a specific task can make the difference between an instant response or having to wait a second.&lt;/p&gt;

&lt;h3&gt;
  
  
  What you should know
&lt;/h3&gt;

&lt;p&gt;A good software developer should know which classes of data structures exist and what types there are. You should also know the most important characteristics of those data structures and their runtime behavior. Being able to implement them by yourself is also a great goal because if you can do that, you probably know everything about them, but it is not an absolute must.&lt;/p&gt;

&lt;p&gt;Generally speaking, you should look at the following data structures that all of their own sub types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Arrays&lt;/li&gt;
&lt;li&gt;Lists&lt;/li&gt;
&lt;li&gt;Sets&lt;/li&gt;
&lt;li&gt;Queues&lt;/li&gt;
&lt;li&gt;Stacks&lt;/li&gt;
&lt;li&gt;Trees&lt;/li&gt;
&lt;li&gt;Graphs&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Where you can learn more about data structures
&lt;/h3&gt;

&lt;p&gt;You already know that algorithms and data structures are usually interlinked and taught together. This is why you will find the same type of resources here as for algorithms:&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;a href="https://www.edx.org/learn/data-structures"&gt;Data structures on edX&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;You probably already know edX from algorithms. No wonder they also have a section on data structures.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;a href="https://www.geeksforgeeks.org/data-structures/"&gt;Data structures on geeks for geeks&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;The same as above. Geeks for geeks has a good section on data structures, as well. If you liked their guides on algorithms, you will probably also like their guides on data structures.&lt;/p&gt;




&lt;h2&gt;
  
  
  Networking / I/O
&lt;/h2&gt;

&lt;p&gt;Depending on how deep you already dug while learning about how computers work, you might probably already have touched this topic. No matter whether you did or not, networking and I/O are crucial aspects of modern software.&lt;/p&gt;

&lt;p&gt;Chances are high that you view this article in a browser on one of your devices (a notebook, a desktop PC, a smartphone, a tablet, or whatever else). This means that the content of this guide was transferred to your device over the internet. Its contents were probably read from a database (that stores data on a hard drive and in memory), sent over your local network to your router, through many more routers and switches until it reached your own router, and through your local network until your device's browser was able to render the page.&lt;/p&gt;

&lt;p&gt;Most modern software uses some form of I/O these days. It either reads from or writes to files or accesses servers elsewhere in the world. With all this I/O, a whole new class of issues can arise, and it also leads to interesting design challenges for software. If you know how all this works at least approximately, you can save a lot of time trying to solve issues that you will face while developing software.&lt;/p&gt;

&lt;h3&gt;
  
  
  What you should know
&lt;/h3&gt;

&lt;p&gt;Networking and I/O are both pretty broad topics. You can basically learn everything from fundamental concepts down to how routers and switches route packets. As a software developer, however, you don't need to know everything in-depth. A few basics like the following are enough, but the deeper you dig the better:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How a network works&lt;/li&gt;
&lt;li&gt;How routing works&lt;/li&gt;
&lt;li&gt;How hard drives work&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Where you can learn more about networking and I/O
&lt;/h3&gt;

&lt;p&gt;As usual, the number of resources is overwhelming, but here are a few you can start with:&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;a href="https://www.ibm.com/cloud/learn/networking-a-complete-guide"&gt;The Fundamentals of Networking by IBM&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;This guide by IBM is not the largest, but it comes with a good level of detail and can be a starting point for you to search further.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;a href="https://cs.stanford.edu/people/nick/how-hard-drive-works/#:~:text=The%20hard%20drive%20contains%20a,the%20stored%200's%20and%201's"&gt;How a Hard Drive Works by Stanford&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;Hard drives store your data. Software regularly needs to access this data, and this resource comes with a great video that explains the fundamentals to you.&lt;/p&gt;




&lt;h2&gt;
  
  
  Wrap Up
&lt;/h2&gt;

&lt;p&gt;This is the end of this guide. If you prepared yourself well, you now hopefully have a list of topics you can use as a starting point for your learning journey. If not, quickly prepare one (you will thank me later).&lt;/p&gt;

&lt;p&gt;Slowly start to work through these topics and fill the gaps you discover accordingly. And don't worry if you take longer to understand something. Computer science is far from easy. Sometimes it takes several tries or a longer break to finally grasp a complex concept.&lt;/p&gt;

&lt;p&gt;Always keep in mind that you might not get the reason for learning some of these topics immediately. One day, you might be thankful for having something in the back of your head, though.&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>beginners</category>
    </item>
    <item>
      <title>The Ultimate Smart Contract Developer Roadmap</title>
      <dc:creator>Oliver Jumpertz</dc:creator>
      <pubDate>Mon, 07 Feb 2022 09:10:03 +0000</pubDate>
      <link>https://forem.com/oliverjumpertz/the-ultimate-smart-contract-developer-roadmap-hij</link>
      <guid>https://forem.com/oliverjumpertz/the-ultimate-smart-contract-developer-roadmap-hij</guid>
      <description>&lt;p&gt;2022 could be the year you get into smart contract development. The market has a high demand for developers knowing their way around Solidity for Ethereum, and this is why the following roadmap could potentially help you get your foot in the door. Furthermore, with average salaries of around $150k for remote positions, Solidity developers can earn pretty well, even working from home. And even better, by learning Solidity and this development stack, you become proficient on multiple blockchains at once. From Ethereum, to Polygon, Binance Smart Chain, Avalanche, and more, Solidity covers them all.&lt;/p&gt;

&lt;p&gt;This roadmap is a long-term investment. It's nothing you finish in one day and are then good to go. But by following the path presented here, you will learn all the basics, including advanced concepts, and become ready for a job after a while.&lt;/p&gt;

&lt;p&gt;Without further ado, let's get into it.&lt;/p&gt;

&lt;p&gt;(This article was originally posted &lt;a href="https://blog.oliverjumpertz.dev/the-ultimate-smart-contract-developer-roadmap" rel="noopener noreferrer"&gt;on my personal blog&lt;/a&gt;)&lt;/p&gt;




&lt;h2&gt;
  
  
  CS50: Introduction to Computer Science
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://pll.harvard.edu/course/cs50-introduction-computer-science" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644143925362%2Fw5ZM6KSjr.png" alt="CS50: Introduction to Computer Science"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Depending on where you currently stand in terms of your skills, it might be that you first need an introduction to CS overall.&lt;/p&gt;

&lt;p&gt;Harvard offers its CS50 for free, and it'll take you a while, but it's worth it. Fundamentals are crucial, especially as many of the advanced concepts you will learn later build on top of them.&lt;/p&gt;




&lt;h2&gt;
  
  
  freeCodeCamp
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.freecodecamp.org/" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644144000636%2FniFffPqmY.png" alt="freeCodeCamp"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;JavaScript is the basis of many tools in the Ethereum ecosystem. You should know JavaScript relatively well before you get into Solidity later.&lt;/p&gt;

&lt;p&gt;Do at least the JavaScript courses here, including quality assurance. This will also take you quite some time, but knowing JavaScript well also helps you learn Solidity later.&lt;/p&gt;




&lt;h2&gt;
  
  
  Public Key Cryptography
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=GSIDS_lvRv4" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644144062331%2FCj77tRtOP.png" alt="Public Key Cryptography"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At the heart of a blockchain lies public key cryptography. You should understand what it is, how it works, and why what you'll do later makes sense.&lt;/p&gt;

&lt;p&gt;This video is a great introduction. Go through it a few times if you need to. Don't forget to take a few notes.&lt;/p&gt;




&lt;h2&gt;
  
  
  Cryptography Digital signatures
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.tutorialspoint.com/cryptography/cryptography_digital_signatures.htm" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644144111190%2FTGa3whxpl.png" alt="Cryptography Digital signatures"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With cryptography, you can begin to sign messages and verify identities. Blockchains use it a lot. Without signing messages, there would be no secure transactions.&lt;/p&gt;

&lt;p&gt;This article will give you a better idea of how transactions are initiated, for example, and why that is secure.&lt;/p&gt;




&lt;h2&gt;
  
  
  What is Hashing on the Blockchain?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=IGSB9zoSx70" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644144171025%2FEDDIBDSEN.png" alt="What is Hashing on the Blockchain?"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hashing is a core concept that blockchains use. And as a smart contract developer, you will probably also use it a lot.&lt;/p&gt;

&lt;p&gt;This video is an excellent introduction to hashing and its use in blockchains. Like before, take notes. At some point, you will be thankful for having them ready.&lt;/p&gt;




&lt;h2&gt;
  
  
  Blockchain Definition: What You Need To Know
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.investopedia.com/terms/b/blockchain.asp" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644144236221%2FsbKHphmBi.png" alt="Blockchain Definition: What You Need To Know"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that you know what cryptography is actually about, it's time to learn more about blockchains in general. The blockchain will be your place of work, so you better know well what you'll work with.&lt;/p&gt;




&lt;h2&gt;
  
  
  How does Ethereum work, anyway?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://preethikasireddy.medium.com/how-does-ethereum-work-anyway-22d1df506369" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644144306405%2FAFiM6HRSm.png" alt="How does Ethereum work, anyway?"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Knowing Ethereum is pretty essential, primarily as you work toward being able to code with Ethereum-like blockchains in the future. When you're done with this article, you'll have a general understanding of Ethereum.&lt;/p&gt;

&lt;p&gt;This is the last theory material for now. When you are done with this, you'll be able to get into the code again. And this time, it's Solidity you will work with.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Hitchhiker's Guide to Smart Contracts in Ethereum
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://blog.openzeppelin.com/the-hitchhikers-guide-to-smart-contracts-in-ethereum-848f08001f05/" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644144358456%2F-kl-msmfD.png" alt="The Hitchhiker's Guide to Smart Contracts in Ethereum"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Time to finally dip your toes into the waters of Solidity. While already a little older, this guide is a great way to finally implement your first Smart Contract.&lt;/p&gt;

&lt;p&gt;Take your time to go through this guide. Solidity is probably new for you, and so it's way better to really digest the learning material until it sticks.&lt;/p&gt;




&lt;h2&gt;
  
  
  Crypto Zombies
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://cryptozombies.io/" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644144402834%2FlgNtfHz7q.png" alt="Crypto Zombies"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;CryptoZombies is a game, but one that teaches you Solidity. In this tutorial, you'll write more advanced logic and get comfortable with the language.&lt;/p&gt;

&lt;p&gt;You can also take your time with Crypto Zombies. Do a few lectures here and there, and don't try to rush through it. You'll learn many basic and advanced concepts throughout this course. &lt;/p&gt;




&lt;h2&gt;
  
  
  Time-locked Wallets: An Introduction to Ethereum Smart Contracts
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.toptal.com/ethereum-smart-contract/time-locked-wallet-truffle-tutorial" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644144481009%2F8vhF0Di6K.png" alt="Time-locked Wallets: An Introduction to Ethereum Smart Contracts"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Time for even more practice. This tutorial walks you through creating a full dApp, including your own ERC 20 token.&lt;/p&gt;

&lt;p&gt;Take this course as an opportunity to learn Solidity even more profoundly and apply what you previously learned. Practice is everything, so all chances for you to code in Solidity are valuable.&lt;/p&gt;




&lt;h2&gt;
  
  
  Ultimate Introduction to Ethereum Ðapp Development
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/playlist?list=PLV1JDFUtrXpFh85G-Ddyy2kLSafaB9biQ" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644144532691%2Fu-cnnnBgw.png" alt="Ultimate Introduction to Ethereum Ðapp Development"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This one is a great video series on dApp development. Concentrate on the Smart Contract videos, and you'll get even more valuable practice with Solidity.&lt;/p&gt;

&lt;p&gt;Once again, it's all about applying Solidity. You might begin to notice that you already know a few of the concepts, but don't let this hold you back. The more often you do something, the higher the chance that this knowledge doesn't vanish again.&lt;/p&gt;




&lt;h2&gt;
  
  
  Ethernaut
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://ethernaut.openzeppelin.com/" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644144586261%2FnFWcxQyQ8Q.png" alt="Ethernaut"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ethernaut is another game that teaches you Solidity. This one primarily focuses a little more on security, a topic that will be very important to you as a smart contract developer.&lt;/p&gt;

&lt;p&gt;Security is actually one of the biggest issues in smart contract development these days, and this is why you should pay very close attention to the matter and take many notes. The industry loves developers who know what they are doing, and they will highly value candidates with experience in smart contract security.&lt;/p&gt;




&lt;h2&gt;
  
  
  ethgas
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://ethgas.io/" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644144644415%2FmIAYttwSr.png" alt="ethgas"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Every line of code you write has an impact on your users. They pay gas to call functions of your smart contracts. Time to learn about the what, why, and how of gas.&lt;/p&gt;

&lt;p&gt;You are back at some theory now, but a pretty important one, to be honest. Understanding the concept of gas and why it is both good and bad helps you to value good code design higher, and it will hopefully trigger different thought processes in you when writing Solidity.&lt;/p&gt;




&lt;h2&gt;
  
  
  Blockchain Oracles, Explained
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://cointelegraph.com/explained/blockchain-oracles-explained" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644144699063%2FkGC2KLKIG.png" alt="https://cointelegraph.com/explained/blockchain-oracles-explained"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You won't be able to do everything on the blockchain. Time to learn about oracles, a way for Smart Contracts to communicate with the outside world.&lt;/p&gt;

&lt;p&gt;Oracles are an advanced concept but one that every Solidity developer should understand. Knowing about Oracles is a considerable advantage. Depending on what projects you work on later, you will have more or less contact with them. But as soon as you understand that you already need an Oracle when you want a random number, you will see that learning a lot about them helps you a lot.&lt;/p&gt;




&lt;h2&gt;
  
  
  Contract ABI Specification
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://docs.soliditylang.org/en/develop/abi-spec.html" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644144768422%2FWgaRFXT4V.png" alt="Contract ABI Specification"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Every smart contract has an ABI. When someone wants to use your contracts, they need to know their ABIs, and you should also know what you actually create there.&lt;/p&gt;

&lt;p&gt;After you have already built quite a few contracts by now, it's time to learn what you actually create. ABIs are at the core of blockchain client usage. Without an ABI, no clients would be able to call your contracts on-chain.&lt;/p&gt;




&lt;h2&gt;
  
  
  Keccak
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.slideshare.net/RajeevVerma14/keccakpptx" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644144817882%2FORTLDOwp7.png" alt="Keccak"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Keccak is the important hash algorithm for Solidity developers. You'll work with it a lot, so you better know what you are using there.&lt;/p&gt;




&lt;h2&gt;
  
  
  Hardhat
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://hardhat.org/" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644144862426%2FdU-l49lzJ.png" alt="Hardhat"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The time has come to choose the stack you will use from now on.&lt;/p&gt;

&lt;p&gt;My recommendation for your development environment: &lt;strong&gt;Hardhat&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Go through the documentation and learn more about this Ethereum development environment. You will probably understand by now why you had to learn JavaScript in the beginning. Hardhat is - like many other Solidity tools - implemented in JavaScript.&lt;/p&gt;




&lt;h2&gt;
  
  
  Waffle
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://getwaffle.io/" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644144900309%2FkauCYWqRU.png" alt="Waffle"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Tests are crucial, especially for Smart Contracts.&lt;/p&gt;

&lt;p&gt;My recommendation: &lt;strong&gt;Waffle&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Waffle is the engine that drives your tests. It enables you to write JavaScript and test your smart contracts as a client.&lt;/p&gt;




&lt;h2&gt;
  
  
  ethers.js
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://docs.ethers.io/v5/" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644144952068%2F2D_DbyC4C.png" alt="ethers.js"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next to Waffle, you will also need a client library to invoke your contracts.&lt;/p&gt;

&lt;p&gt;My recommendation: &lt;strong&gt;ethers.js&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;ethers is the library that slowly completes your stack. You will use ethers in your tests a lot.&lt;/p&gt;




&lt;h2&gt;
  
  
  OpenZeppelin
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://openzeppelin.com/contracts/" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644145001197%2FPoO0l_C6G.png" alt="OpenZeppelin"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Smart contract development is like traditional development, and you should not try to reinvent the wheel every time. OpenZeppelin provides many useful standards already implemented.&lt;/p&gt;

&lt;p&gt;See what the library has to offer and learn to use it. Many professional projects use OpenZeppelin a lot, so why shouldn't you later?&lt;/p&gt;




&lt;h2&gt;
  
  
  Ethereum Improvement Proposals
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://eips.ethereum.org/erc" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644145046034%2FcJ5cvyNba.png" alt="Ethereum Improvement Proposals&amp;lt;br&amp;gt;
"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ethereum has defined many standards, especially for tokens. Standardized A(B/P)Is are crucial to making the ecosystem work. Go through them all, and learn the most important ones, like ERC20, ERC721, etc.&lt;/p&gt;




&lt;h2&gt;
  
  
  Solidity Patterns
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/fravoll/solidity-patterns" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644145094411%2FTt0sA_jFg_.png" alt="Solidity Patterns"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that you can freely build things without a browser, it's time to build and learn important Solidity patterns along the way.&lt;/p&gt;

&lt;p&gt;Patterns can be applied in certain places and help you to achieve specific things in a recognizable and maintainable way. You will see many of them regularly when reading code, and you will hopefully also apply them a lot. This is the last thing you should learn really well before you branch out to build your own projects and use your own imagination.&lt;/p&gt;




&lt;h2&gt;
  
  
  What's Next?
&lt;/h2&gt;

&lt;p&gt;You should be a pretty competent Solidity developer by now. Your skills, however, are still pretty general.&lt;/p&gt;

&lt;p&gt;From here on, build a lot of stuff, experiment even more, and see what fields you might like. You can definitely specialize from here on.&lt;/p&gt;

&lt;p&gt;Look into:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;NFTs&lt;/li&gt;
&lt;li&gt;Currencies/fungible tokens&lt;/li&gt;
&lt;li&gt;DeFi&lt;/li&gt;
&lt;li&gt;DAOs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Learn more about all of them, and see whether you like a field specifically.&lt;/p&gt;

&lt;p&gt;Another topic you can now begin to research is decentralized computing. You won't always be able to do everything within your smart contracts (remember learning about Oracles?). Look into Chainlink as a beginning.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://chain.link/" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644145199556%2F5pu0vnVu8.png" alt="Chainlink"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After that, you can also try to become comfortable with Functions as a Service in JavaScript and such because sometimes, you will have to write code outside of the blockchain. The more you research, the more interesting platforms and applications you will find.&lt;/p&gt;




&lt;h2&gt;
  
  
  Before You Leave
&lt;/h2&gt;

&lt;p&gt;Do you like written content like this? If so, &lt;a href="https://www.getrevue.co/profile/iterationthree" rel="noopener noreferrer"&gt;you will probably love my newsletter&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.getrevue.co/profile/iterationthree" rel="noopener noreferrer"&gt;Iteration Three&lt;/a&gt; is the Web 3 newsletter I would love to get into my inbox each week.&lt;/p&gt;

&lt;p&gt;From the latest news in the industry, opportunities, analyses, and development tips, to showcases of interesting new projects, this newsletter tries to give you everything you might have missed or did not even know existed.&lt;/p&gt;

</description>
      <category>web3</category>
      <category>solidity</category>
      <category>roadmap</category>
    </item>
    <item>
      <title>Thirteen Incredible GitHub Repositories To Leverage Your Blockchain Learning</title>
      <dc:creator>Oliver Jumpertz</dc:creator>
      <pubDate>Sat, 01 Jan 2022 18:44:16 +0000</pubDate>
      <link>https://forem.com/oliverjumpertz/thirteen-incredible-github-repositories-to-leverage-your-blockchain-learning-113p</link>
      <guid>https://forem.com/oliverjumpertz/thirteen-incredible-github-repositories-to-leverage-your-blockchain-learning-113p</guid>
      <description>&lt;p&gt;The internet is a beautiful place of knowledge. You can find educational material nearly everywhere, and GitHub is no different. This article focuses on showcasing thirteen incredible GitHub repositories to you, all packed and filled with learning resources and opportunities to make you better at blockchain development. From Bitcoin to Ethereum, from Python to JavaScript or Solidity, there's something in here for everyone.&lt;/p&gt;

&lt;p&gt;(This article is based on a &lt;a href="https://twitter.com/oliverjumpertz/status/1476187542248730627?s=20" rel="noopener noreferrer"&gt;thread I released on Twitter&lt;/a&gt;.)&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Awesome Blockchain Resources
&lt;/h2&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1640807291079%2F7_X-adzC8.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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1640807291079%2F7_X-adzC8.png" alt="Awesome Blockchain Resources"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/yjjnls/awesome-blockchain" rel="noopener noreferrer"&gt;Visit the repository.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This repository contains an awesomely curated list of resources related to blockchain. It contains tutorials, guides, articles, papers, and books. Many of your questions are already answered here.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Awesome Blockchain Collection
&lt;/h2&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1640807219808%2FvdUIY0zIr.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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1640807219808%2FvdUIY0zIr.png" alt="Awesome Blockchain Collection"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/openblockchains/awesome-blockchains" rel="noopener noreferrer"&gt;Visit the repository.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This repository contains a tremendous amount of learning resources. They range from tutorials covering the implementation of your very own blockchain to articles and books. There is something in for everyone.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Awesome Blockchain Curated
&lt;/h2&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1640807123728%2FG-sb_wc-O.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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1640807123728%2FG-sb_wc-O.png" alt="Awesome Blockchain Curated"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/imbaniac/awesome-blockchain" rel="noopener noreferrer"&gt;Visit the repository.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This repository does not contain the classical learning materials as many of the other repositories listed here. Instead, it has many real-life examples of applications working on the blockchain, for you to analyze and get an idea about. Sometimes, the best way to learn something is to see how others do it.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Blockchain Learning Path
&lt;/h2&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1640807505568%2F4N6YbGLKV.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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1640807505568%2F4N6YbGLKV.png" alt="Blockchain Learning Path"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/protofire/blockchain-learning-path" rel="noopener noreferrer"&gt;Visit the repository.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This learning path covers everything, from theory to practice. It guides you from the basics to the advanced concepts and is a great path to follow if you want to cover Bitcoin and Ethereum.&lt;/p&gt;




&lt;h2&gt;
  
  
  5. Learn Blockchain In 2 Months
&lt;/h2&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1640807618518%2Fp-kvj4JJL.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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1640807618518%2Fp-kvj4JJL.png" alt="Learn Blockchain In 2 Months"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/llSourcell/Learn_Blockchain_in_2_months" rel="noopener noreferrer"&gt;Visit the repository.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Learning blockchain in 2 months is a challenging task, but not impossible. This repository does not seem like much, but it covers all the resources you need to complete that task in chronological order. Follow this path, and you will be pretty up-to-date after only two months.&lt;/p&gt;




&lt;h2&gt;
  
  
  6. Ultimate Solidity, Blockchain, and Smart Contract - Beginner to Expert Full Course | Python Edition
&lt;/h2&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1640807832838%2FkL1H9Tmi_.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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1640807832838%2FkL1H9Tmi_.png" alt="Ultimate Solidity, Blockchain, and Smart Contract - Beginner to Expert Full Course | Python Edition"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/smartcontractkit/full-blockchain-solidity-course-py" rel="noopener noreferrer"&gt;Visit the repository.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The repository to the great freeCodeCamp YouTube course on blockchain with Python. If you don't know that course yet, it covers over 16 hours of in-depth knowledge and everything you need to use Python for smart contract development. It contains a lot of code that helps you to understand what's going on for sure.&lt;/p&gt;




&lt;h2&gt;
  
  
  7. A simple blockchain implementation in Python
&lt;/h2&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1640808016806%2FYRWG-AKwk.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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1640808016806%2FYRWG-AKwk.png" alt="A simple blockchain implementation in Python"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/dvf/blockchain" rel="noopener noreferrer"&gt;Visit the repository.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Learn to build a blockchain in Python by analyzing this implementation. It is the accompanying repository to another book that contains all explanations you need to understand the whole code. This repository is especially interesting because it shows you that even Python, a language not usually used for blockchain clients, is still a very good choice and gets the job done.&lt;/p&gt;




&lt;h2&gt;
  
  
  8. Learn Bitcoin From The Command Line
&lt;/h2&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1640808356407%2F7nMTCIdIl.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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1640808356407%2F7nMTCIdIl.png" alt="Bildschirmfoto 2021-12-29 um 21.05.50.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/BlockchainCommons/Learning-Bitcoin-from-the-Command-Line" rel="noopener noreferrer"&gt;Visit the repository.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A complete guide to interacting with Bitcoin and learning more about it through its CLI. This one is not directly tied to implementing logic, learning about cryptographic primitives, or peer 2 peer networking. Instead, it focuses on you learning to interact with Bitcoin through its CLI client. Using the major blockchain out there also helps you to understand blockchains themselves.&lt;/p&gt;




&lt;h2&gt;
  
  
  9. Learn How To Build A Blockchain With Go
&lt;/h2&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1640808686041%2F-ZAtK95_F.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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1640808686041%2F-ZAtK95_F.png" alt="Learn How To Build A Blockchain With Go"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/nosequeldeebee/blockchain-tutorial" rel="noopener noreferrer"&gt;Visit the repository.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you ever dreamed of implementing your own blockchain in one of the most popular programming languages for blockchain development out there, this repository might be exactly what you look for. Go is the language most Ethereum clients and nodes and many other blockchains are implemented in. Why not use it to implement your very own?&lt;/p&gt;




&lt;h2&gt;
  
  
  10. The Bitcoin Book
&lt;/h2&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1640808836530%2FuAgABY9Ww.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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1640808836530%2FuAgABY9Ww.png" alt="The Bitcoin Book"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/bitcoinbook/bitcoinbook" rel="noopener noreferrer"&gt;Visit the repository.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Bitcoin is still &lt;strong&gt;the&lt;/strong&gt; blockchain. It might not be a smart blockchain like Ethereum or Solana, but it definitely is the father of many modern implementations. It is always worth it to learn about Bitcoin, especially as Layer-2 solutions begin to emerge and add capabilities to Bitcoin itself.&lt;/p&gt;




&lt;h2&gt;
  
  
  11. The DeFi Developer Roadmap
&lt;/h2&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1640810286881%2FKywKnIsMt.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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1640810286881%2FKywKnIsMt.png" alt="The DeFi Developer Roadmap"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/OffcierCia/DeFi-Developer-Road-Map" rel="noopener noreferrer"&gt;Visit the repository.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;DeFi (decentralized finance) was one of the first, and still is one of the most important applications of blockchains. This curated Ethereum developer handbook contains everything you need to become a DeFi developer on Ethereum, sidechains like Polygon, and derived chains like the Binance Smart Chain.&lt;/p&gt;




&lt;h2&gt;
  
  
  12. Build Your Own X
&lt;/h2&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1640810463726%2F34xevE9V6.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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1640810463726%2F34xevE9V6.png" alt="Build Your Own X"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/danistefanovic/build-your-own-x#build-your-own-blockchain--cryptocurrency" rel="noopener noreferrer"&gt;Visit the repository.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A general-purpose repository containing curated resources for multiple purposes, including Blockchain/Crypto. You get material for JavaScript, Python, Go, and some more languages. All learning materials are of high quality and good to follow.&lt;/p&gt;




&lt;h2&gt;
  
  
  13. Build A Blockchain With JavaScript
&lt;/h2&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1640810647406%2F3dSdrFL2a.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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1640810647406%2F3dSdrFL2a.png" alt="Build A Blockchain With JavaScript"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/nambrot/blockchain-in-js" rel="noopener noreferrer"&gt;Visit the repository.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;JavaScript is a great language, and although it is not one of the major languages used to implement blockchains, it still can. This repository shows you how you can implement one for yourself and even comes with a UI that helps you to explore what happens under the hood better.&lt;/p&gt;




&lt;h2&gt;
  
  
  Before You Leave
&lt;/h2&gt;

&lt;p&gt;Do you like content like this? Why don't you follow me on social media for a daily dose of education?&lt;/p&gt;

&lt;p&gt;Join me on &lt;a href="https://www.instagram.com/oliverjumpertz" rel="noopener noreferrer"&gt;Instagram&lt;/a&gt; and get a daily dose of educational content right in your feed, soon also including short educational videos.&lt;/p&gt;

</description>
      <category>blockchain</category>
      <category>web3</category>
    </item>
    <item>
      <title>10 Important Array Methods In JavaScript Explained</title>
      <dc:creator>Oliver Jumpertz</dc:creator>
      <pubDate>Tue, 21 Dec 2021 20:42:56 +0000</pubDate>
      <link>https://forem.com/oliverjumpertz/10-important-array-methods-in-javascript-explained-5ahn</link>
      <guid>https://forem.com/oliverjumpertz/10-important-array-methods-in-javascript-explained-5ahn</guid>
      <description>&lt;p&gt;I recently read a &lt;a href="https://markodenic.com/10-javascript-array-methods-to-simplify-your-code/"&gt;great article by Marko Denic&lt;/a&gt; about array methods. Inspired by this, I thought to myself that it might be a great opportunity to explain them thoroughly and show you what you can use them for, and how you could implement them yourselves.&lt;br&gt;
Without further ado, let's get into it.&lt;/p&gt;
&lt;h2&gt;
  
  
  1. filter()
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;filter&lt;/code&gt; is &lt;strong&gt;the&lt;/strong&gt; method whenever you want to, well, filter out values. Only want positive values? Only looking for objects that have a certain property? &lt;code&gt;filter&lt;/code&gt; is your way to go.&lt;/p&gt;

&lt;p&gt;The following is the signature of the &lt;code&gt;filter&lt;/code&gt; method:&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="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// element is the element within the array&lt;/span&gt;
  &lt;span class="c1"&gt;// index is the index of the element in the array&lt;/span&gt;
  &lt;span class="c1"&gt;// array is a reference to the array filter works on&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;thisOverride&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// thisOverride is a way to override the semantical this within the callback function.&lt;/span&gt;
&lt;span class="c1"&gt;// If you set it to another object, calling this.anyThing would access anyThing within that&lt;/span&gt;
&lt;span class="c1"&gt;// object, and not the actual array.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example Use Case
&lt;/h3&gt;

&lt;p&gt;Imagine that you have an online shop. And now you want to send a discount code to all customers that live in a certain area.&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;const&lt;/span&gt; &lt;span class="nx"&gt;getElibigleCustomers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;zipCode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;customer&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;customer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;zipCode&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;zipCode&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;getElibigleCustomers&lt;/code&gt; returns all customers that have an address stored with a zipCode, which is the same zipCode you look for. All other customers are filtered out of the array.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reimplementing filter
&lt;/h3&gt;

&lt;p&gt;If you want to understand &lt;code&gt;filter&lt;/code&gt; even better, let's try to reimplement it.&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;function&lt;/span&gt; &lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callbackFn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;

  &lt;span class="k"&gt;for&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;i&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="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callbackFn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;newArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;newArray&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Attention:&lt;/strong&gt; Bear in mind that you should never replace a prototype method of a built-in type yourself. But this is just to show you how a possible implementation might look.&lt;/p&gt;

&lt;p&gt;As you see, &lt;code&gt;filter&lt;/code&gt; is nothing else than a loop that executes a callback function for each element. All elements the callback function returns false for, are filtered out.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. forEach()
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;forEach&lt;/code&gt; is a functional way to loop over array elements and execute some logic for each element. The method itself doesn't return a new array.&lt;/p&gt;

&lt;p&gt;The following is the signature of the &lt;code&gt;forEach&lt;/code&gt; method:&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="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// element is the element within the array&lt;/span&gt;
  &lt;span class="c1"&gt;// index is the index of the element in the array&lt;/span&gt;
  &lt;span class="c1"&gt;// array is a reference to the array forEach works on&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;thisOverride&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// thisOverride is a way to override the semantical this within the callback function.&lt;/span&gt;
&lt;span class="c1"&gt;// If you set it to another object, calling this.anyThing would access anyThing within that&lt;/span&gt;
&lt;span class="c1"&gt;// object, and not the actual array.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example Use Case
&lt;/h3&gt;

&lt;p&gt;Let's stay with the example of the online shop. Now, you want to print all names of the customers you previously filtered out.&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="nx"&gt;getElibigleCustomers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;123456&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;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;customer&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;customer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forename&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;customer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;surname&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;When forEach executes, the console prints the full name of all customers that were previously filtered out.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reimplementing forEach
&lt;/h3&gt;

&lt;p&gt;Let's reimplement &lt;code&gt;forEach&lt;/code&gt; so you better understand how it works.&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;function&lt;/span&gt; &lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callbackFn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for&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;i&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="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&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;callbackFn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once again, bear in mind that you should never replace prototype methods of built-in types in a real app, except you really know what you are doing.&lt;/p&gt;

&lt;p&gt;You probably begin to notice a pattern by now. &lt;code&gt;forEach&lt;/code&gt; is, once again, nothing else than a loop. And within this loop, the callback function is called. The result isn't interesting and thus thrown away.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. some()
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;some&lt;/code&gt; is a special array method. It tests whether at least one element within the array tests positive for a specific condition. If so, &lt;code&gt;some&lt;/code&gt; returns true, otherwise, it returns false.&lt;/p&gt;

&lt;p&gt;The following is the signature of the &lt;code&gt;some&lt;/code&gt; method:&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="nx"&gt;some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// element is the element within the array&lt;/span&gt;
  &lt;span class="c1"&gt;// index is the index of the element in the array&lt;/span&gt;
  &lt;span class="c1"&gt;// array is a reference to the array some works on&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;thisOverride&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// thisOverride is a way to override the semantical this within the callback function.&lt;/span&gt;
&lt;span class="c1"&gt;// If you set it to another object, calling this.anyThing would access anyThing within that&lt;/span&gt;
&lt;span class="c1"&gt;// object, and not the actual array.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example Use Case
&lt;/h3&gt;

&lt;p&gt;Back to our online shop example. Imagine that you now want to test whether at least some of the customers you filtered out are underage. If so, you want to show them another deal, because everyone else gets a discount for alcoholic drinks. But you don't want to support underage children drinking, of course.&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;const&lt;/span&gt; &lt;span class="nx"&gt;eligibleCustomers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getElibigleCustomers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;123456&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;containsUnderAgedCustomers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;eligibleCustomers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;customer&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;customer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When &lt;code&gt;some&lt;/code&gt; executes, it checks each customer's age property. If at least one is below 18, it returns false.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reimplementing some
&lt;/h3&gt;

&lt;p&gt;Time to reimplement &lt;code&gt;some&lt;/code&gt;.&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;function&lt;/span&gt; &lt;span class="nx"&gt;some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callbackFn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for&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;i&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="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callbackFn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;some&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;some&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;some&lt;/code&gt; loops through all elements of the array until it finds an element where the callback function returns true for. In this case, the method returns early, because for &lt;em&gt;some&lt;/em&gt;  elements to satisfy a condition, one is already enough. Only when no element matches, false is returned.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. every()
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;every&lt;/code&gt; is the counterpart of &lt;code&gt;some&lt;/code&gt;. It tests whether all elements satisfy a condition. Only then, the method returns true. If only one element fails the test, false is returned.&lt;/p&gt;

&lt;p&gt;The following is the signature of the &lt;code&gt;every&lt;/code&gt; method:&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="nx"&gt;every&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// element is the element within the array&lt;/span&gt;
  &lt;span class="c1"&gt;// index is the index of the element in the array&lt;/span&gt;
  &lt;span class="c1"&gt;// array is a reference to the array every works on&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;thisOverride&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// thisOverride is a way to override the semantical this within the callback function.&lt;/span&gt;
&lt;span class="c1"&gt;// If you set it to another object, calling this.anyThing would access anyThing within that&lt;/span&gt;
&lt;span class="c1"&gt;// object, and not the actual array.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example Use Case
&lt;/h3&gt;

&lt;p&gt;Many customers who received your discount code have ordered by now. To save on shipment costs, you want to send it out all at once. But first, you need to check whether all those customers have valid address data saved.&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;const&lt;/span&gt; &lt;span class="nx"&gt;customersWhoOrdered&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getCustomersForOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;discount1234&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;allCustomersHaveValidShipmentData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;customersWhoOrdered&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;every&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;customer&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;hasValidShipmentData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;customer&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;When &lt;code&gt;every&lt;/code&gt; executes, it passes each customer to a function that checks whether that customer has valid shipment data stored. If only one customer has invalid data, the whole function returns false.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reimplementing every
&lt;/h3&gt;

&lt;p&gt;Time to reimplement &lt;code&gt;every&lt;/code&gt;.&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;function&lt;/span&gt; &lt;span class="nx"&gt;every&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callbackFn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for&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;i&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="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;callbackFn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;every&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;every&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;every&lt;/code&gt; loops through all elements of the array until it finds an element where the callback function returns false for. In this case, the method returns early, because not &lt;strong&gt;all&lt;/strong&gt; elements satisfy the condition. One is already enough. Only when no element matches, true is returned.&lt;/p&gt;

&lt;p&gt;You might even have noticed that &lt;code&gt;every&lt;/code&gt; is not very different from &lt;code&gt;some&lt;/code&gt;. At some specific points, true is replaced with false, and the check with the callback function is negated. These small changes are already enough to make the method do exactly what you want.&lt;/p&gt;




&lt;h2&gt;
  
  
  5. includes()
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;includes&lt;/code&gt; is a method that checks whether an array contains a specific element. It's good for a quick check, but it also has its drawbacks, which we talk about in a minute.&lt;/p&gt;

&lt;p&gt;The following is the signature of the &lt;code&gt;includes&lt;/code&gt; method:&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="nx"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;searchElement&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fromIndex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// searchElement is the element you look for&lt;/span&gt;
  &lt;span class="c1"&gt;// fromIndex is the index the search should start at&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example Use Case
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;includes&lt;/code&gt; is special. It actually tests for strict equality, which means: It either searches for a value that is strictly equal to the search element, or it looks for the exact reference of an object.&lt;/p&gt;

&lt;p&gt;Let's say you want to check whether the customers orders include a very specific order, like 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;const&lt;/span&gt; &lt;span class="nx"&gt;allOrders&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getAllOrders&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;containsSpecificOrder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;allOrders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;customer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John Doe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
  &lt;span class="na"&gt;zipCode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;54321&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;You would probably be surprised to find out that include returns false, even if the array contains an object with the exact same properties. This is because it looks for strict equality, and objects are only strictly equal if they are the same reference. JavaScript does not know an equals method.&lt;/p&gt;

&lt;p&gt;This reduces &lt;code&gt;includes&lt;/code&gt;' use cases to primitive values. If you, for example want to check whether an array of numbers contains a specific number like 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;const&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;includesFive&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is a rule of thumb: If you deal with an array of primitive values, use &lt;code&gt;includes&lt;/code&gt;. If you deal with objects, use &lt;code&gt;some&lt;/code&gt; because it allows you to pass in a callback with which you can test objects for equality.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reimplementing includes
&lt;/h3&gt;

&lt;p&gt;Time to reimplement &lt;code&gt;includes&lt;/code&gt;.&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;function&lt;/span&gt; &lt;span class="nx"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;searchElement&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fromIndex&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="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fromIndex&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;fromIndex&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;for&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;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fromIndex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;searchElement&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;includes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The guard statements at the beginning are there to make the method execution a little safer. A negative fromIndex makes no sense, as well as an index larger than the array's max index. The rest is just a loop testing each element for strict equality with the element searched for.&lt;/p&gt;




&lt;h2&gt;
  
  
  6. map()
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;map&lt;/code&gt; is one of the most important array methods out there. Whenever you want to transform all values within an array, &lt;code&gt;map&lt;/code&gt; is &lt;strong&gt;the&lt;/strong&gt; way to go.&lt;/p&gt;

&lt;p&gt;The following is the signature of the &lt;code&gt;map&lt;/code&gt; method:&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="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// element is the element within the array&lt;/span&gt;
  &lt;span class="c1"&gt;// index is the index of the element in the array&lt;/span&gt;
  &lt;span class="c1"&gt;// array is a reference to the array map works on&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;thisOverride&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// thisOverride is a way to override the semantical this within the callback function.&lt;/span&gt;
&lt;span class="c1"&gt;// If you set it to another object, calling this.anyThing would access anyThing within that&lt;/span&gt;
&lt;span class="c1"&gt;// object, and not the actual array.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example Use Case
&lt;/h3&gt;

&lt;p&gt;Let's jump back to the point where you had all eligible customers filtered. Now, you want to send their orders off and need to get all their addresses. This is a great use case for map:&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;const&lt;/span&gt; &lt;span class="nx"&gt;eligibleCustomers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getElibigleCustomers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;123456&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;addresses&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;eligibleCustomers&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;customer&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;customer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;map&lt;/code&gt; iterates over all customers and then extracts all addresses. These are put into a new array and returned from the function.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reimplementing map
&lt;/h3&gt;

&lt;p&gt;Time to reimplement &lt;code&gt;map&lt;/code&gt;.&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;function&lt;/span&gt; &lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callbackFn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
  &lt;span class="k"&gt;for&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;i&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="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mappedValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;callbackFn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;newArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mappedValue&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;newArray&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;map&lt;/code&gt; iterates over all elements of the array. For each element, it calls the callback function and expects a new value to be returned. This value is then pushed to a new array. That array is returned in full, resulting in an array of the same size as the original one, but with probably different elements in it.&lt;/p&gt;




&lt;h2&gt;
  
  
  7. reduce()
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;reduce&lt;/code&gt; is the most powerful array method existing. It can be used to reimplement all existing array methods, and it's the most flexible one. Talking about all the advantages it offers would definitely need an article on its own, but you will get a glimpse of it soon.&lt;/p&gt;

&lt;p&gt;The following is the signature of the &lt;code&gt;reduce&lt;/code&gt; method:&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="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;accumulator&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;currentValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;currentIndex&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// accumulator is the result of the last call, or the initialValue in the beginning&lt;/span&gt;
  &lt;span class="c1"&gt;// currentValue is the value currently processed&lt;/span&gt;
  &lt;span class="c1"&gt;// currentIndex is the index of the current value within the array&lt;/span&gt;
  &lt;span class="c1"&gt;// array is a reference to the array reduce works on&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;initialValue&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example Use Case
&lt;/h3&gt;

&lt;p&gt;Remember when you wanted to find out whether there were underage customers? Another way to deal with this problem is to group all your eligible customers into two groups. Those of legal age, and those who are underage.&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;const&lt;/span&gt; &lt;span class="nx"&gt;eligibleCustomers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getElibigleCustomers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;123456&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;customerGroups&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;eligibleCustomers&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;accumulator&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;customer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;customer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;accumulator&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="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;customer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;accumulator&lt;/span&gt;&lt;span class="p"&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;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;customer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;accumulator&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[[],&lt;/span&gt; &lt;span class="p"&gt;[]]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;reduce&lt;/code&gt; can be pretty difficult to understand but we can go over the above code together and see what it does. In this case, the accumulator is a 2-dimensional array. Index 0 contains all customers &amp;gt;= 18 years of age. Index 1 contains all customers who are underage. The first time reduce runs, it sets accumulator to the empty 2-dimensional array. Within the method, the code checks whether the age property of the customer is above 18. If so, it pushes the customer to the first array. If the customer is underaged, they get pushed to the second array. In the end, the 2-dimensional array with the grouped customers is returned.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reimplementing reduce
&lt;/h3&gt;

&lt;p&gt;Time to reimplement &lt;code&gt;reduce&lt;/code&gt;.&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;function&lt;/span&gt; &lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callbackFn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;initialValue&lt;/span&gt;&lt;span class="p"&gt;)&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;accumulator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;initialValue&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="k"&gt;this&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="k"&gt;for&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;i&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="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&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;accumulator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;callbackFn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;accumulator&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;accumulator&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;reduce&lt;/code&gt; iterates over all elements as all the other array methods do. In the beginning, the method needs to decide whether an initialValue was supplied. If not, the first element from the array is taken as such. After that, the accumulator is replaced with the result of calling the callback each time, and then returned in its final form in the end.&lt;/p&gt;




&lt;h2&gt;
  
  
  8. sort()
&lt;/h2&gt;

&lt;p&gt;The name of &lt;code&gt;sort&lt;/code&gt; already says it all. Whenever you want to sort an array, this is the method you need to call.&lt;/p&gt;

&lt;p&gt;The following is the signature of the &lt;code&gt;sort&lt;/code&gt; method:&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="nx"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstElement&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;secondElement&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// firstElement is the first element to compare&lt;/span&gt;
  &lt;span class="c1"&gt;// secondElement is the second element to compare&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example Use Case
&lt;/h3&gt;

&lt;p&gt;Whenever you need to sort something, you found a use case for &lt;code&gt;sort&lt;/code&gt;. Let's, for example, try to sort your customers by age. For such a case, &lt;code&gt;sort&lt;/code&gt; allows you to pass in a comparator function that you can use to tell &lt;code&gt;sort&lt;/code&gt; how to properly order them.&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;const&lt;/span&gt; &lt;span class="nx"&gt;customers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getCustomers&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;((&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;b&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;customer&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;customer&lt;/span&gt;&lt;span class="p"&gt;.&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;The callback function must return a number based on the order of the elements. It should return a value &amp;lt; 0 if a comes before b, 0 if both are equal, and a value &amp;gt; 0 if a comes after b.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reimplementing sort
&lt;/h3&gt;

&lt;p&gt;Reimplementing &lt;code&gt;sort&lt;/code&gt; is a little difficult because, under the hood, runtimes are allowed to implement any sorting algorithm they seem fitting. There are only a few requirements, like that the algorithm must be stable. Usually, runtimes implement at least QuickSort, though, and sometimes change the implementation based on the elements within the array.&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;function&lt;/span&gt; &lt;span class="nx"&gt;partition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;compareFunction&lt;/span&gt;&lt;span class="p"&gt;)&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;pivot&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&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;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;left&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;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;compareFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;pivot&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;compareFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;pivot&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;
            &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;quickSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;compareFunction&lt;/span&gt;&lt;span class="p"&gt;)&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;index&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;partition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;compareFunction&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;index&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="p"&gt;{&lt;/span&gt;
          &lt;span class="nx"&gt;quickSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&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;compareFunction&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nx"&gt;quickSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;compareFunction&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;compareFunction&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;quickSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&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;compareFunction&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sort&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;sort&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 only an exemplary implementation of sort, in this case, QuickSort. But it should give you a general idea.&lt;/p&gt;




&lt;h2&gt;
  
  
  9. find()
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;find&lt;/code&gt; is your search function. Whenever you look for something within an array, you can use &lt;code&gt;find&lt;/code&gt; to retrieve the first element from the array that satisfies your conditions.&lt;/p&gt;

&lt;p&gt;The following is the signature of the &lt;code&gt;find&lt;/code&gt; method:&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="nx"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// element is the current element&lt;/span&gt;
  &lt;span class="c1"&gt;// index is the current index&lt;/span&gt;
  &lt;span class="c1"&gt;// array is a reference to the array find works on&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;thisOverride&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// thisOverride is a way to override the semantical this within the callback function.&lt;/span&gt;
&lt;span class="c1"&gt;// If you set it to another object, calling this.anyThing would access anyThing within that&lt;/span&gt;
&lt;span class="c1"&gt;// object, and not the actual array.&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example Use Case
&lt;/h3&gt;

&lt;p&gt;Imagine you try to find a customer with a specific name within all your customers.&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;const&lt;/span&gt; &lt;span class="nx"&gt;customers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getCustomers&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;customerJohn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;customer&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;customer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forename&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&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;In this case &lt;code&gt;find&lt;/code&gt; returnS the first user within the array that has the forename John. The important part is that &lt;code&gt;find&lt;/code&gt; won't return all customers with that name.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reimplementing find
&lt;/h3&gt;

&lt;p&gt;Time to reimplement &lt;code&gt;find&lt;/code&gt;.&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;function&lt;/span&gt; &lt;span class="nx"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callbackFn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for&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;i&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="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callbackFn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;find&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;find&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;find&lt;/code&gt; iterates over all elements as all the other array methods do. For every element, it checks whether the callback function returns true. If it does, it returns the element at that position. If it doesn't return early, it returns undefined at the end.&lt;/p&gt;




&lt;h2&gt;
  
  
  10. findIndex()
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;findIndex&lt;/code&gt; is a method that you can use to get the index of an element within the array. Like &lt;code&gt;find&lt;/code&gt;, it stops at the first element that satisfies the condition. It will thus only ever return the index of the first element satisfying the test.&lt;/p&gt;

&lt;p&gt;The following is the signature of the &lt;code&gt;findIndex&lt;/code&gt; method:&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="nx"&gt;findIndex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// element is the current element&lt;/span&gt;
  &lt;span class="c1"&gt;// index is the current index&lt;/span&gt;
  &lt;span class="c1"&gt;// array is a reference to the array find works on&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;thisOverride&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// thisOverride is a way to override the semantical this within the callback function.&lt;/span&gt;
&lt;span class="c1"&gt;// If you set it to another object, calling this.anyThing would access anyThing within that&lt;/span&gt;
&lt;span class="c1"&gt;// object, and not the actual array.&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example Use Case
&lt;/h3&gt;

&lt;p&gt;Imagine you have all your customers sorted by age, and now you want to find the first customer with a forename of John.&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;const&lt;/span&gt; &lt;span class="nx"&gt;customers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getCustomers&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;customersSortedByAge&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;sortByAge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;indexOfJohn&lt;/span&gt; &lt;span class="nx"&gt;customersSortedByAge&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;findIndex&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;customer&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;customer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forename&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;customerJohn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;customersSortedByAge&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;indexOfJohn&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case &lt;code&gt;findIndex&lt;/code&gt; returns the index of the first user within the array that has the forename John. The important part is that &lt;code&gt;findIndex&lt;/code&gt; won't return all indices of customers with that name.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reimplementing findIndex
&lt;/h3&gt;

&lt;p&gt;Time to reimplement &lt;code&gt;findIndex&lt;/code&gt;.&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;function&lt;/span&gt; &lt;span class="nx"&gt;findIndex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callbackFn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for&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;i&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="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callbackFn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="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="p"&gt;}&lt;/span&gt;

&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;findIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;findIndex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;findIndex&lt;/code&gt; iterates over all elements as all the other array methods do. You should notice the similarity to &lt;code&gt;find&lt;/code&gt;. Instead of returning the element, only the index is returned when an element is found. Instead of undefined, if nothing is found, -1 is returned.&lt;/p&gt;




&lt;h2&gt;
  
  
  That's It
&lt;/h2&gt;

&lt;p&gt;You have now learned a little more about 10 of the more popular and important array methods in JavaScript. As you've learned, many of them are pretty similar, and they all have in common that they loop a lot. Other than that, they all have their similarities and differences, but this is what makes them so interesting to explore and learn.&lt;/p&gt;




&lt;h2&gt;
  
  
  Before You Leave
&lt;/h2&gt;

&lt;p&gt;Do you like content like this? Why don't you follow me on social media for a daily dose of education?&lt;/p&gt;

&lt;p&gt;Join me on &lt;a href="https://www.instagram.com/oliverjumpertz"&gt;Instagram&lt;/a&gt; and get a daily dose of educational content right in your feed, soon also including short educational videos.&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>Why talking about your current salary with recruiters and in interviews is a horrible idea</title>
      <dc:creator>Oliver Jumpertz</dc:creator>
      <pubDate>Mon, 23 Aug 2021 13:52:59 +0000</pubDate>
      <link>https://forem.com/oliverjumpertz/why-talking-about-your-current-salary-with-recruiters-and-in-interviews-is-a-horrible-idea-1aoi</link>
      <guid>https://forem.com/oliverjumpertz/why-talking-about-your-current-salary-with-recruiters-and-in-interviews-is-a-horrible-idea-1aoi</guid>
      <description>&lt;p&gt;Interviews are never easy. We often feel like beggars hoping to get a well-paid job. Although this is actually not the reality, especially for tech workers, interviews stress our nerves, make us sweat, and we usually don't feel well doing them. We are constantly graded and evaluated, classified, and then deemed worthy or not. Interviews are full of questions we have all already heard of. There are classics, clever questions, riddles, whiteboarding, and what else.&lt;/p&gt;

&lt;p&gt;One of those classics that's asked more and more, especially by recruiters, is the following one:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"What is your current salary?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is one of the most critical and morally questionable questions to ask a candidate, and here is how you deal with it.&lt;/p&gt;




&lt;h2&gt;
  
  
  Determining the value of work
&lt;/h2&gt;

&lt;p&gt;Before we talk about the question and how to handle it, we need to talk about how the value of work is determined. With this knowledge, you will understand the catch of this question and why answering honestly puts you at a disadvantage. To completely cover a topic as deep as how to value work, it would definitely need a whole article or even a book on its own. We will thus go with a pretty simplified version that gives you at least a basic idea of how it works.&lt;/p&gt;

&lt;p&gt;The value of work is individual.&lt;/p&gt;

&lt;p&gt;It is partly based on:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Supply&lt;/li&gt;
&lt;li&gt;Demand&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;in the global, but even more in the local markets.&lt;/p&gt;

&lt;p&gt;Chances are high that no two employees doing the same job, even at the same company, are paid the same. Companies earn different amounts of money each month and year. They have different operating costs, and they have different leadership. All businesses, however, have in common that they try to minimize costs while maximizing profits, at least up to a certain extend.&lt;/p&gt;

&lt;p&gt;When a company starts to hire, it creates a budget. This is basically what they are willing to pay a new employee for a certain job. This amount is based on experience, supply and demand in the local markets, and more factors. Beginning with a certain salary, there will be interest in a position. Going below and no one will want to work for this company, going too high, and the company gives away too much money they could have spent on other things. This means that a company tries to find a sweet spot to get enough candidates to choose a suitable one from. It only needs one suitable candidate to fill a position—multiple ones to fill multiple positions.&lt;/p&gt;

&lt;p&gt;Multiple hires probably won't earn the same. Although a company has a salary range in mind, they will still try to negotiate your pay based on the outcome of the interview, your previous experience, and what you have in mind. If you ask for less than what they had in mind, that's awesome for them.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why this question is a trick
&lt;/h2&gt;

&lt;p&gt;As you learned, a company that wants to interview you usually has a certain salary or better range in mind. Often, they don't state this range publicly because it would weaken their position for negotiations. And this is where this tricky question comes in. If you currently earn $100k a year, why should they suddenly pay you $150k for a similar job? Stating what you currently earn weakens your own position for all future negotiations. Often, recruiters try to sell you this question because they "need to see whether you fit in the company's salary expectations," but this is largely a stupid excuse. They could easily have asked for your salary expectations then. For a company, it's easy to say that a 50% raise is "too much" and that they "can't see anything that justifies this jump in pay." Suddenly, they have a point to focus on. It doesn't even matter anymore that you are currently underpaid for all the work you do. Even a 20% raise would already be a huge win for you, wouldn't it? No. It wouldn't.&lt;/p&gt;

&lt;p&gt;The value of work is subjective and depends on the individual company. The same work you do at company A can bring this company fewer or higher profits than company B. It's not all about you. It's also about their marketing and sales teams, the size of their customer base, their customer's will to pay a certain amount, etc. If company B can sell your work for more than company A, the first one can easily afford to pay you more, even for the same work. It's as easy as that. You deserve a slice of the cake. This is what you show up for every day.&lt;/p&gt;




&lt;h2&gt;
  
  
  Dealing with this question
&lt;/h2&gt;

&lt;p&gt;If an honest answer isn't a great option, what then? Circumvent it and state your salary expectations instead. No matter what a recruiter or an interviewer says, don't state your current salary and don't lie about it. Really, please don't lie about it. If you ever happen to tell someone what your previous salary was, and this information finds its way to your manager or HR, there is a slight chance that you won't have an easy time negotiating for a raise in the foreseeable future.&lt;/p&gt;

&lt;p&gt;You both (the company and you) want to find a base for further negotiations. If your expectations don't align with the company's, this specific job isn't for you. You might have to look for another job then, but that's still better than underselling yourself in the end. In my experience, everyone who ever honestly gave their current salary away had a way harder time negotiating for a fair salary afterward. This is why you should politely decline to answer this question and offer your salary expectation instead.&lt;/p&gt;

&lt;h3&gt;
  
  
  A good example of an answer
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;"I am sorry, but I think that my current salary is not of interest here. I understand that you need a number to figure out whether our expectations align, though. I can tell you that I currently look for positions that pay in the range of around $140k to $170k. I am sure that we can narrow this down further as soon as we have talked deeper about what your expectations for me are and when I could show you what I am capable of. I believe that you will see that I am well worth this money, and I can promise you not to disappoint during our coming interview rounds. Is this range something you can live with? Do both our expectations align here?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is a great example of an answer. It politely declines to talk about your current salary and instead states your own expectations. But it still shows that you respect the company's need to determine whether they are willing to pay enough. It provides a range and not only a single number. This shows them you are willing to negotiate, based on how well you both align. What they expect of you might be different from what your current employer expects. After this, the answer shows that you are self-confident enough to promise to deliver in the interviews. The next important point is that this answer shows confidence that you are worth the money you ask for. It awakens the assumption that you thought about your salary expectations well enough to build this confidence. And lastly, the answer gives an exit. If the company can't live with this amount of money, they are free to decline at this point.&lt;/p&gt;




&lt;h2&gt;
  
  
  One counter-question, just in case
&lt;/h2&gt;

&lt;p&gt;If you doubt that you can decline to answer this question, or if a recruiter or company should ever insist on you telling them your current salary, here is what I have done in the past and would recommend you to do.&lt;/p&gt;

&lt;p&gt;Ask them how much they charge one of their large customers on a custom plan. They will most likely decline to answer this question because this is company-internal information. Next, ask them whether they would give this information to another customer if they asked. I can already tell you they would never do that. Custom plans are always negotiated. Why should a company give any customer at least a hint that they might try to charge them more than other customers? Correct. They should and would never. Your current salary is the last customer, and now they want you to give them a hint they would never, themselves, give one of their own customers. This counter-question should make clear that you don't take the bait.&lt;/p&gt;




&lt;h2&gt;
  
  
  Before You Leave
&lt;/h2&gt;

&lt;p&gt;If you would love to read even more content like this, feel free to visit me on &lt;a href="https://twitter.com/intent/follow?screen_name=oliverjumpertz"&gt;Twitter&lt;/a&gt; or &lt;a href="https://www.linkedin.com/in/oliverjumpertz/"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I'd love to count you as my ever-growing group of awesome friends!&lt;/p&gt;

</description>
      <category>interview</category>
      <category>techinterview</category>
    </item>
    <item>
      <title>Exposing your greatest weakness in an interview</title>
      <dc:creator>Oliver Jumpertz</dc:creator>
      <pubDate>Thu, 05 Aug 2021 14:57:37 +0000</pubDate>
      <link>https://forem.com/oliverjumpertz/exposing-your-greatest-weakness-in-an-interview-40g1</link>
      <guid>https://forem.com/oliverjumpertz/exposing-your-greatest-weakness-in-an-interview-40g1</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;"What's your greatest weakness?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Have you ever been asked this question in an interview and were like, "Yea, uhm, well...I sometimes want to achieve too much?"&lt;/p&gt;

&lt;p&gt;This question is tricky, but here is how you can crush it.&lt;/p&gt;




&lt;h2&gt;
  
  
  Foreword
&lt;/h2&gt;

&lt;p&gt;I've interviewed quite a few times in the last few years, and I also had the honor to interview a few outstanding software engineers. I witnessed this question many times, although I never asked it myself. My job as a tech lead is to assess the technical quality of candidates and see how much they fit into my team. I like to use other techniques to do this, but I still understand why HR managers love to ask questions like this one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why this question is important
&lt;/h2&gt;

&lt;p&gt;Self-awareness is important for everyone.&lt;/p&gt;

&lt;p&gt;It's valuable to know what your&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Strengths&lt;/li&gt;
&lt;li&gt;And weaknesses&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;are and what you&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Like&lt;/li&gt;
&lt;li&gt;And dislike.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Knowing your own weaknesses is especially crucial.&lt;/p&gt;

&lt;p&gt;It gives you the ability to know:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Where you could improve&lt;/li&gt;
&lt;li&gt;Which tasks you might struggle with&lt;/li&gt;
&lt;li&gt;When it is better to ask for help&lt;/li&gt;
&lt;li&gt;Which situations you might not want to find yourself in&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Self-aware employees can ask for education, certification, and help to improve their weaknesses themselves. They usually also know what tasks they can ask for help with right from the beginning. Additionally, they know about social situations they might not like.&lt;/p&gt;

&lt;p&gt;Employees who are self-reflective enough to know their weaknesses are valuable for a company. Even an introvert can be a great team member, especially if they are committed to improving to get along in their team well.&lt;/p&gt;

&lt;p&gt;Lastly, honesty is shown through this answer. Answering with an honest weakness exposes the candidate to the interviewer. But the candidate shows that they can be honest even if something isn't in their own favor.&lt;/p&gt;




&lt;h2&gt;
  
  
  How to deal with this question
&lt;/h2&gt;

&lt;p&gt;If you aren't already self-reflecting regularly, prepare yourself to put in some work before your interview. Assess yourself and honestly ask yourself what your weaknesses are. Write them all down and sort them.&lt;/p&gt;

&lt;p&gt;Here are some essential rules for the weaknesses, their order, and a description:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Choose a weakness that wouldn't cost you the job&lt;/li&gt;
&lt;li&gt;Be honest, only real weaknesses. Don't just make something up&lt;/li&gt;
&lt;li&gt;Add an example of how you worked to improve this weakness&lt;/li&gt;
&lt;li&gt;Show that you can take the help of others to overcome a weakness&lt;/li&gt;
&lt;li&gt;Neither be arrogant nor underestimate or short-sell yourself&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Take your time, and don't rush this step. You can reuse this work in the future for other interviews. Follow the rules above closely. You want to crush this question and not only give a probably satisfying answer. This is why only saying something like, "I tend to become impatient after a while," is not enough.&lt;/p&gt;

&lt;p&gt;The first spot on the list should be taken by:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An honest answer&lt;/li&gt;
&lt;li&gt;One that still favors you and doesn't shed light too negative on you&lt;/li&gt;
&lt;li&gt;A weakness you can tell a story about&lt;/li&gt;
&lt;li&gt;What it affects&lt;/li&gt;
&lt;li&gt;How you overcame it&lt;/li&gt;
&lt;li&gt;What you did and still do to improve on it&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  An example of a good answer
&lt;/h3&gt;

&lt;p&gt;"I tend to focus too much on details. A few times in the past, I put more work into some features I developed than would have been needed to create a good solution. This lead to me not having enough time to help my teammates out. I had a few talks with my managers about this, and I realized that software doesn't always have to be perfect. It only needs to do what it is supposed to do.&lt;/p&gt;

&lt;p&gt;Since realizing this, I put more thought into my features and create code reviews earlier to ask for feedback. This allows me to discuss my approach with my teammates and them the chance to evaluate my approach and inform me when we reached a point where my code is good enough.&lt;/p&gt;

&lt;p&gt;I still struggle with my detail orientation from time to time. But I actively work on improving on this, while early code reviews give me a good opportunity to learn from the experience of my peers.&lt;/p&gt;

&lt;p&gt;I'd love to get some help with this, and if you, as an employer, offer training on this, I'd be happy to take it."&lt;/p&gt;

&lt;p&gt;This example answer checks all marks.&lt;/p&gt;

&lt;p&gt;If this is really something that happened to you, no interviewer can ask for more.&lt;/p&gt;

&lt;p&gt;You exposed yourself, and it's their turn to assess whether they believe you and are okay with it.&lt;/p&gt;




&lt;h2&gt;
  
  
  Some common weaknesses
&lt;/h2&gt;

&lt;p&gt;Here are a few examples of common weaknesses you might also suffer from:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You have a problem with patience&lt;/li&gt;
&lt;li&gt;You struggle organizing your work&lt;/li&gt;
&lt;li&gt;You don't like to delegate tasks&lt;/li&gt;
&lt;li&gt;You might be timid&lt;/li&gt;
&lt;li&gt;You don't like public speaking&lt;/li&gt;
&lt;li&gt;You perhaps struggle with data analysis&lt;/li&gt;
&lt;li&gt;You need to improve your software testing skills&lt;/li&gt;
&lt;li&gt;You like to rush things&lt;/li&gt;
&lt;li&gt;You can't deal well with stubborn people&lt;/li&gt;
&lt;li&gt;You don't like letting go of a project&lt;/li&gt;
&lt;li&gt;You have trouble saying no&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Decide for yourself whether you suffer from one of these examples. If so, pick the one that is really your greatest weakness.&lt;/p&gt;

&lt;p&gt;If yours is not included here, you will have to dig deeper and honestly ask yourself what the issue is that you suffer the most from. It may take some time, and perhaps you don't even find the correct one, but it will still be an honest one because you came up with it.&lt;/p&gt;




&lt;h2&gt;
  
  
  Before You Leave
&lt;/h2&gt;

&lt;p&gt;If you would love to read even more content like this, feel free to visit me on &lt;a href="https://twitter.com/intent/follow?screen_name=oliverjumpertz"&gt;Twitter&lt;/a&gt; or &lt;a href="https://www.linkedin.com/in/oliverjumpertz/"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I'd love to count you as my ever-growing group of awesome friends!&lt;/p&gt;

</description>
      <category>interview</category>
      <category>techinterview</category>
    </item>
    <item>
      <title>That one obvious interview question that can cost you some nerves</title>
      <dc:creator>Oliver Jumpertz</dc:creator>
      <pubDate>Tue, 03 Aug 2021 16:25:24 +0000</pubDate>
      <link>https://forem.com/oliverjumpertz/that-one-obvious-interview-question-that-can-cost-you-some-nerves-1jf9</link>
      <guid>https://forem.com/oliverjumpertz/that-one-obvious-interview-question-that-can-cost-you-some-nerves-1jf9</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;"Why do you want to work here?"&lt;/p&gt;

&lt;p&gt;-&amp;gt; Your interviewer&lt;/p&gt;

&lt;p&gt;"Well, I am a web developer, and you are looking for one. Additionally, I need the money."&lt;/p&gt;

&lt;p&gt;-&amp;gt; You&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Some interview questions are stupid. But giving answers like the one above doesn't help.&lt;/p&gt;

&lt;p&gt;Let's see how to tackle this one effectively.&lt;/p&gt;

&lt;h2&gt;
  
  
  Foreword
&lt;/h2&gt;

&lt;p&gt;I've interviewed quite a few times in the last few years, and I also had the honor to interview a few outstanding software engineers. I witnessed this question many times, although I never asked it myself.&lt;/p&gt;

&lt;p&gt;I don't particularly like this question, and I love companies that simply throw such questions into the bin. But, there are still companies out there asking questions like this one. And this is why you should be prepared for it and the reason I share this with you.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why this question is important
&lt;/h2&gt;

&lt;p&gt;Like any other general question that is asked relatively often, this one is also aimed at giving your potential new employer some key information about you. It aims at providing the following information:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is your motivation to work exactly at this company?&lt;/li&gt;
&lt;li&gt;Are you really motivated to do the job?&lt;/li&gt;
&lt;li&gt;Are you genuinely interested in contributing to the company?&lt;/li&gt;
&lt;li&gt;Will you be a good investment for them?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You need to put yourself in the shoes of an employer. Every hire is an investment for them.&lt;/p&gt;

&lt;p&gt;They provide &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Guidance&lt;/li&gt;
&lt;li&gt;Mentors&lt;/li&gt;
&lt;li&gt;Education&lt;/li&gt;
&lt;li&gt;And a budget so you can evolve.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And they want you to make money for them.&lt;/p&gt;

&lt;p&gt;This is how a business works. Only motivated employees do their best to contribute to the goals of a company. They are the ones that put in their thoughts and work into reaching company goals. Unmotivated employees will do their work, but they won't give a hundred percent. Unmotivated employees are also more likely to leave the company again after a short while. This turnover is costly for any company, and it is also costly for the employees who leave. They have to go through the whole interview process once again, as well.&lt;/p&gt;

&lt;p&gt;Not all companies are faceless and only business-focused entities. Some of them really have a huge interest in your well-being. If they only hire you because you need the job, they don't do you a favor, either. If getting up to go to work each day becomes so hard for you because you don't like the job, you might even end up with depression.&lt;/p&gt;

&lt;p&gt;You should be happy with what you do. If you happen to get to this point, you found a good job. This is why this question is often asked at the beginning of your interview, where it sets the stone for it, or at the end of your interview when you learned more about the role, to check your interest and enthusiasm for it.&lt;/p&gt;




&lt;h2&gt;
  
  
  How to deal with this question
&lt;/h2&gt;

&lt;p&gt;Good preparation is everything. Never come to an interview unprepared. It usually only backfires. It is in your best interest to get to know the company, what it stands for, and what its mission is as good as possible.&lt;/p&gt;

&lt;p&gt;You make a commitment by signing their offer. This usually means showing up there every day (in-office or remote) and doing your work. You will inevitably contribute to the company's success. You should thus already be aware of what you expect from the company, and be sure that the role they offer and what they do is really for you.&lt;/p&gt;

&lt;p&gt;Take notes and do your best to write down:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What the company does&lt;/li&gt;
&lt;li&gt;What it stands for&lt;/li&gt;
&lt;li&gt;What mission it has&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With this information at hand, you can take a look at your own expectations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What work makes you happy?&lt;/li&gt;
&lt;li&gt;What do you want to achieve?&lt;/li&gt;
&lt;li&gt;What do you want to contribute?&lt;/li&gt;
&lt;li&gt;What culture do you need to thrive?&lt;/li&gt;
&lt;li&gt;How do you want to help the company succeed?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you can answer each of these questions honestly, you are actually already set up pretty well.&lt;/p&gt;

&lt;p&gt;There is no need to make up the perfect answer somehow. Although it might seem tempting to make it up, so you get the job, it &lt;strong&gt;will&lt;/strong&gt; only hurt you. Craft a story to tell your interviewers why you want to work at this exact company. This is everything you really need to answer this question because there is no right or wrong.&lt;/p&gt;

&lt;h3&gt;
  
  
  An example
&lt;/h3&gt;

&lt;p&gt;"This is a great question because I also asked myself why I should join your company when I read about you and your job offer. I think that our values align pretty well. You are doing an awesome job of making the cloud easier for everyone. And this is exactly what I also try to do as a content creator and open source contributor in my free time. Your vision of making the cloud as easily usable as any web application is what I am dreaming of for quite some time now. This job is the perfect opportunity for me to contribute to this great goal and use my expertise in this field to help you succeed with this mission."&lt;/p&gt;

&lt;h3&gt;
  
  
  Another example
&lt;/h3&gt;

&lt;p&gt;"I think my current skillset perfectly aligns with what you search for. I always wanted to get into product development, and your company seems to be the perfect fit for me. I like how you managed to build a SaaS product so many people love to use. And as I am a user myself, I would love to contribute to its further development with my previous expertise. I have found no other company that aligns with my goals as perfectly as yours. This is why I had to apply for this position."&lt;/p&gt;

&lt;p&gt;There is really nothing special with the two example answers above. It's just important to be honest, to only state what really is true, and to really believe in what you say there.&lt;/p&gt;

&lt;p&gt;If you manage this, you've already delivered on this question.&lt;/p&gt;




&lt;h2&gt;
  
  
  Before You Leave
&lt;/h2&gt;

&lt;p&gt;If you would love to read even more content like this, feel free to visit me on &lt;a href="https://twitter.com/intent/follow?screen_name=oliverjumpertz"&gt;Twitter&lt;/a&gt; or &lt;a href="https://www.linkedin.com/in/oliverjumpertz/"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I'd love to count you as my ever-growing group of awesome friends!&lt;/p&gt;

</description>
      <category>interview</category>
      <category>techinterview</category>
    </item>
    <item>
      <title>How to react when asked to rate your skills in an interview</title>
      <dc:creator>Oliver Jumpertz</dc:creator>
      <pubDate>Sun, 01 Aug 2021 09:58:37 +0000</pubDate>
      <link>https://forem.com/oliverjumpertz/how-to-react-when-asked-to-rate-your-skills-in-an-interview-2697</link>
      <guid>https://forem.com/oliverjumpertz/how-to-react-when-asked-to-rate-your-skills-in-an-interview-2697</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;"How would you rate your JavaScript (or XY) skills on a scale of 1-10?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This one is another tricky question that can cost candidates in job interviews some nerves. Let's face it, most of us find it dumb. But you can turn this question into a massive win for you.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why is this question asked?
&lt;/h2&gt;

&lt;p&gt;You could go ahead and call it dumb now, but there is more to it than only the sheer will to bring candidates to sweat during interviews. But it serves a purpose. Such a question might not be the best question or even way to get this kind of information, but it exists, and from time to time, you will have to face it. And if you want this job, you need to deal with it.&lt;/p&gt;

&lt;p&gt;You somehow have to tackle it and deliver a satisfying answer. And the actual idea of this kind of question is to test your self-awareness.&lt;/p&gt;

&lt;p&gt;It tests how:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You perceive yourself&lt;/li&gt;
&lt;li&gt;Well you rate yourself&lt;/li&gt;
&lt;li&gt;Good you are at self-reflection&lt;/li&gt;
&lt;li&gt;Much thought you put into yourself and your answer&lt;/li&gt;
&lt;li&gt;You react to such a question&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Being a professional means applying a certain skill set regularly. A software developer usually develops a lot of software (surprise!).&lt;/p&gt;

&lt;p&gt;This includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Thinking about&lt;/li&gt;
&lt;li&gt;Designing&lt;/li&gt;
&lt;li&gt;Prototyping&lt;/li&gt;
&lt;li&gt;Testing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;solutions, for example.&lt;/p&gt;

&lt;p&gt;And it also includes coding.&lt;/p&gt;

&lt;p&gt;A common expectation, especially from employers, is that employees get better over time. They invest in their employees by educating them further, giving them responsibility, and letting them roam free from time to time. And this leads to them expecting a certain ambition of you to progress further and get better in your craft. But for you to get better, you need to be aware of where you are currently at. And this requires regular self-reflection. If you know where you are currently, you can plot a path to your goal. You will also, most likely, be able to assume whether you can solve a certain problem and when you need help. If you take too long to think about this question and then respond with a generic answer, it can indicate that you haven't spent much time on self-reflection recently.&lt;/p&gt;

&lt;p&gt;Even further, only a number is usually not the expected answer. You should give a reason as to why you answer with a specific number. "Oh, an 8" is an answer, but it doesn't tell why you have actually come to this conclusion. Interviewers really expect reasons. Don't underestimate this. They will judge you based on the answer and reasons you give. There is not even a right answer to this question because usually, no one will try to assess whether an 8 or 9 is justified. It's all about how you deliver it.&lt;/p&gt;




&lt;h2&gt;
  
  
  How to prepare for this question
&lt;/h2&gt;

&lt;p&gt;As you've previously read, it's all about how you deliver the answer. This requires some preparation on your side, like many other questions and interviews in general do. Take an hour or less while you prepare for your interview and rate yourself honestly. Do this based on the skill requirements of the job ad, at least. Create a table and rate how good are you at:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;JavaScript&lt;/li&gt;
&lt;li&gt;Rect/Vue/Angular/Svelte&lt;/li&gt;
&lt;li&gt;API Design&lt;/li&gt;
&lt;li&gt;Layouting&lt;/li&gt;
&lt;li&gt;etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This table should contain:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The skill&lt;/li&gt;
&lt;li&gt;Your rating&lt;/li&gt;
&lt;li&gt;A reason why this rating is justified and how you improved in it recently&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Bonus points for you if you try to imagine what else, besides each hard skill listed, might be interesting for this position.&lt;/p&gt;

&lt;p&gt;This prepares you at least for such questions that aim at anything possibly previously known. A neat side effect is that you actually work on improving your self-reflection skills. It might well happen that you get asked to rate yourself in something that has nearly nothing to do with the position you interview for. I personally hate when interviewers do this, but it happens. In this case, you will still be prepared to come up with an answer before it is too late. Also learn to say your thoughts aloud, so interviewers don't need to watch you silently thinking about a satisfying answer. This can give bonus points.&lt;/p&gt;




&lt;h2&gt;
  
  
  How to deal with this question in an interview
&lt;/h2&gt;

&lt;p&gt;If this question comes up, you will most likely be prepared for it. You will have self-reflected during your preparation and can honestly state your number and argument for why it is justified. Go ahead and deliver your answer. Show your interviewers that you are prepared for this kind of question and that you won't let such a tricky one fool you. You have self-reflected, and it's your right to show that you did.&lt;/p&gt;

&lt;p&gt;Let's now look at an example answer.&lt;/p&gt;

&lt;h3&gt;
  
  
  An example
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;"I think I'd give myself an 8. Due to the extensive work I did in my last projects where I worked as a Senior Developer, I could improve my skills a lot. I was especially able to work more with Promises, and async/await and understand this topic better. But I still think that even to get close to a 10, I will definitely need more exposure to lesser used language features like asynchronous generators and generators in general."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is an answer that contains everything an interviewer could ask for.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Rate yourself&lt;/li&gt;
&lt;li&gt;Give reasons for it&lt;/li&gt;
&lt;li&gt;Show you are aware you improved and&lt;/li&gt;
&lt;li&gt;also know that you still lack in some areas&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And that's it then? Well, yes and no. You can answer this question and finish your interview, but there is also a way to really crush it, which comes next.&lt;/p&gt;




&lt;h2&gt;
  
  
  How to crush this question
&lt;/h2&gt;

&lt;p&gt;Let's face it. Using numbers to rate yourself properly is nearly impossible. Let's take JavaScript, for example. What actually is a 10? I once learned the full ECMA spec, and I wouldn't give myself a 10. Even Brendan Eich said that he wouldn't call himself an expert in JavaScript anymore because he didn't put much work into catching up with the language's evolution. He stated he could, but he currently does other things.&lt;/p&gt;

&lt;p&gt;So, what then if numbers are a terrible indicator? If the question mostly exists to assess your self-awareness, why not think outside the box and give another answer that still satisfies your interviewer's expectations? Throw any numbers away and better state that you know relatively well where you stand.&lt;/p&gt;

&lt;p&gt;Let's say you previously worked as a frontend developer and also interview for another frontend developer role. Don't you think a lot of your experience is transferable? As long as you build SPAs, they will most likely consist of the same building parts no matter where you work. And as long as you are aware of what else a certain language or framework has to offer, you have an easier time stating what you haven't really worked with yet. The actual strategy now is to show that you know what you worked with and are aware of what you haven't worked with excessively or at all yet.&lt;/p&gt;

&lt;p&gt;Let's take a look at an example next to show you what crushing this question is about.&lt;/p&gt;

&lt;h3&gt;
  
  
  An example
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;"Thank you for this question. I would really like to give you an honest, self-reflective rating of my skills, but I think that numbers are a terrible indicator of skill level. You can ask three nearly identical developers how they rate themselves in the same skill, and you will get three very different answers.&lt;/p&gt;

&lt;p&gt;I think it is better to tell you where I have a lot of experience and where I believe I still need to improve. In the last few years, I worked the majority of my time on creating base components. This gave me extensive knowledge of functional React components and enabled me to get deep insights into React's rendering loop.&lt;/p&gt;

&lt;p&gt;Due to the nature of these base components and a user base of around 500 company-internal frontend engineers, I also tackled many accessibility issues and gained a deeper understanding of the field. But I have to admit that I couldn't work much with remote calls and the fetch API. I would even go so far as to say that I definitely need to improve my frontend data skills. This also holds true for server-side React. I wasn't able to work much with Next, for example.&lt;/p&gt;

&lt;p&gt;I do, however, highly believe that if you give me the chance and time to catch up, I will be able to do so relatively quickly while setting myself up to become a great contributor in my future team."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This answer should usually tell your interviewer much more than a common answer with a number and some reasons. It additionally shows that you are aware that certain "rating systems" are bad and that you still know how to show what you know and don't. You reflected deeply about your past and are even aware of what you might need in your future position. No interviewer can ask for more. If they do, you can still stand up and go. This company isn't worth it then.&lt;/p&gt;




&lt;h2&gt;
  
  
  Before You Leave
&lt;/h2&gt;

&lt;p&gt;If you would love to read even more content like this, feel free to visit me on &lt;a href="https://twitter.com/intent/follow?screen_name=oliverjumpertz"&gt;Twitter&lt;/a&gt; or &lt;a href="https://www.linkedin.com/in/oliverjumpertz/"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I'd love to count you as my ever-growing group of awesome friends!&lt;/p&gt;

</description>
      <category>interview</category>
      <category>techinterview</category>
    </item>
    <item>
      <title>How to tackle the dreaded question "Why should we hire you?" in your next interview</title>
      <dc:creator>Oliver Jumpertz</dc:creator>
      <pubDate>Fri, 30 Jul 2021 08:39:37 +0000</pubDate>
      <link>https://forem.com/oliverjumpertz/how-to-tackle-the-dreaded-question-why-should-we-hire-you-in-your-next-interview-1cea</link>
      <guid>https://forem.com/oliverjumpertz/how-to-tackle-the-dreaded-question-why-should-we-hire-you-in-your-next-interview-1cea</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;"Why should we hire you?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is another of those questions everyone interviewing hates. It spread from traditional jobs into the tech world, and even software developers have to deal with it.&lt;/p&gt;

&lt;p&gt;But you can turn this into a huge win, and this article shows you how.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why is this question asked?
&lt;/h2&gt;

&lt;p&gt;Like any of those pretty cliché questions regularly asked in interviews, interviewers try to determine whether you are a good fit. Your hard skills might have been assessed already or will be soon, but this one is about your character.&lt;/p&gt;

&lt;p&gt;Even before the term EQ (emotional intelligence) became popular, there was more to employees than only the skills they brought with them. People are human. They have a character. And they need to get along with other people.&lt;/p&gt;

&lt;p&gt;What interviewers try to assess here is how good you can sell yourself. And even more, they want to find out HOW you sell yourself. Interestingly, it can tell you a lot about someone else when you listen to how they talk about themselves. Is the candidate super arrogant? Are they introverts? Don't they find the words to describe themselves? Do they only speak in superlatives? Yea, you are somehow being profiled when you are asked to answer this question.&lt;/p&gt;

&lt;p&gt;With the answers you give, experienced HR managers can create a rough profile of you. They see whether you fit into the company culturally and how well you would fit into certain teams. It also tells a lot about how interested in the company itself you actually are. If you only talk about how awesome you are but never even mention how awesome your work would be for the company and how much you would like to contribute, you make a bad impression.&lt;/p&gt;




&lt;h2&gt;
  
  
  How to deal with this question
&lt;/h2&gt;

&lt;p&gt;If you want to make the most out of this question, you should do these two things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Prepare yourself before the interview&lt;/li&gt;
&lt;li&gt;Listen to the interview(s) closely, up to the point this question is asked&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Prepare yourself before the interview
&lt;/h3&gt;

&lt;p&gt;Your goal is most likely to get this job or see whether it is really something for you. No matter what your motivation is, prepare yourself as if you really wanted to get this job. If you find out that you actually want this job during the interview, you might be unprepared for a question like this and thus not come up with a great answer. No one likes missed chances. Especially not if they could have been avoided.&lt;/p&gt;

&lt;p&gt;What you should do:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Write down what you really look for in a job&lt;/li&gt;
&lt;li&gt;Double-check the job ad and see how good of a fit you are&lt;/li&gt;
&lt;li&gt;Check the company's background: What do they do? What do they tell about themselves?&lt;/li&gt;
&lt;li&gt;Look for company reviews on the internet, both from customers and employees&lt;/li&gt;
&lt;li&gt;Use a table or another form of notation to compare who you are with what the company is&lt;/li&gt;
&lt;li&gt;Craft an honest story that references the most important points where you and the company match&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This might seem much now, but let's go over it:&lt;/p&gt;

&lt;p&gt;You should first make clear for yourself what you are really looking for.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What would make you go to work every day?&lt;/li&gt;
&lt;li&gt;What would keep you motivated?&lt;/li&gt;
&lt;li&gt;What does a job have to offer you, so you are happy?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then see what the company states it is looking for. This is usually the job ad. Keep in mind that those are never perfect and should not be taken as the whole truth. But they are a good indicator.&lt;/p&gt;

&lt;p&gt;Next, look up what the company really stands for. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How do they represent themselves?&lt;/li&gt;
&lt;li&gt;What image do they present, and what do they do for society?&lt;/li&gt;
&lt;li&gt;What are their values?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You want to make sure that your values align with theirs. Marketing is one thing, but the truth usually lies deeper. A company can write anything they want on their website and in press releases. But what do customers and employees say when they review the company? Is there more you can learn about them?&lt;/p&gt;

&lt;p&gt;The next step is a little more difficult. You should somehow try to find out how well the company and you align. Do you match their criteria, and do they match yours? A table, like a pro/contra list, can help. But perhaps there is something that works better for you. Just make sure you have something written somewhere, black on white, so you don't try to cheat yourself.&lt;/p&gt;

&lt;p&gt;With all this information at hand, you can craft a story about why exactly YOU are a good fit for the company and why they should hire exactly YOU. This story should answer: &lt;strong&gt;What do you bring to the table that helps them, and what can they do for you?&lt;/strong&gt; Feel free to write multiple sentences already. This helps you learn the core of it up to the point that you could also answer this question freely during the interview. If bullet points serve you better, that's also fine.&lt;/p&gt;

&lt;h3&gt;
  
  
  Listen to the interview(s) closely, up to the point this question is asked
&lt;/h3&gt;

&lt;p&gt;Interviews usually bring up a lot of additional information you didn't know about previously. Perhaps you couldn't find it online, or it simply wasn't there. I can only recommend you take notes during an interview. This way, you have all this information at hand when you need it. Perhaps the interviewer tells you about an employee education program that they didn't list on their website. Or perhaps they tell you about their engagement in local communities, or even better, a new field they want to get their feet wet in.&lt;/p&gt;

&lt;p&gt;If something is interesting for you in it, definitely use it to your advantage. As soon as the question of why they should hire you comes up, you can use exactly this as one of the reasons they should hire you. You can sell them your honest interest in a certain topic as a reason to hire you. Why? Because it shows your ambition.&lt;/p&gt;

&lt;h4&gt;
  
  
  An example
&lt;/h4&gt;

&lt;p&gt;"I can give you several reasons why you should hire me. First of all, I tick many boxes regarding hard skills as I have some extended experience. For the rest, I can assure you that I will quickly catch up with these. I've already shown that I am a fast and avid learner in the past, and I plan that this won't be any different at your company. The next reason is that I deeply care for the cloud. Most of my personal work is based on it. I would love to bring my expertise in this field into the company and see whether I can help you advance there further. I would even go so far as to say that I am very motivated to become a core contributor to this field in your company within the next two years. Another reason is your engagement with your local community. I also regularly engage in my local community and think that our values align perfectly here. Perhaps I could help with my expertise and could equally learn from yours. During our interview, you mentioned that you currently try to rebuild your education program. As a content creator on the internet, I would really love to help with my expertise. I think I can contribute a lot to this area. And lastly, I believe that we have had an awesome interview so far and seem to align quite well. For me, we seem like we fit well culturally, and I'd love to be able to thrive in the one that you carefully built."&lt;/p&gt;

&lt;p&gt;This is only an example, but it should give an interviewer nearly anything they could ask for.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It gives them not one but multiple reasons to hire you&lt;/li&gt;
&lt;li&gt;It shows your ambition&lt;/li&gt;
&lt;li&gt;It showcases your interest to contribute&lt;/li&gt;
&lt;li&gt;It states you listened to the interview&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Keep one thing in mind: Your answer doesn't need to be perfect. But it should tick many of the boxes interviewers are looking for. Sell yourself as the best hire and make sure they understand that you think they are the best company for you right now.&lt;/p&gt;




&lt;h2&gt;
  
  
  Before You Leave
&lt;/h2&gt;

&lt;p&gt;If you would love to read even more content like this, feel free to visit me on &lt;a href="https://twitter.com/intent/follow?screen_name=oliverjumpertz"&gt;Twitter&lt;/a&gt; or &lt;a href="https://www.linkedin.com/in/oliverjumpertz/"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I'd love to count you as my ever-growing group of awesome friends!&lt;/p&gt;

</description>
      <category>interview</category>
      <category>techinterview</category>
    </item>
  </channel>
</rss>
