<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Spyros Argalias</title>
    <description>The latest articles on DEV Community by Spyros Argalias (@sargalias).</description>
    <link>https://dev.to/sargalias</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%2F124747%2F53b3fdce-5ddd-43b3-a8c5-0004b600fc51.jpeg</url>
      <title>DEV Community: Spyros Argalias</title>
      <link>https://dev.to/sargalias</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sargalias"/>
    <language>en</language>
    <item>
      <title>Defensive &amp; offensive programming</title>
      <dc:creator>Spyros Argalias</dc:creator>
      <pubDate>Sun, 16 Jan 2022 13:09:42 +0000</pubDate>
      <link>https://dev.to/programmingduck/defensive-offensive-programming-38gh</link>
      <guid>https://dev.to/programmingduck/defensive-offensive-programming-38gh</guid>
      <description>&lt;p&gt;Defensive programming is a term that many programmers have heard of. It's related to &lt;a href="https://programmingduck.com/articles/errors"&gt;error handling&lt;/a&gt; and having correct programs. For some programs, defensive programming is essential. For others, it may be useful to use here and there. Along with that, there's also offensive programming.&lt;/p&gt;

&lt;p&gt;In this article, we'll start by examining "normal programming". We'll examine it first because some people mistake it for defensive programming. However, this is something that you should do regardless of whether you do defensive programming or not.&lt;/p&gt;

&lt;p&gt;Then, we'll examine defensive programming, followed by offensive programming.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Normal programming
&lt;/h2&gt;

&lt;p&gt;Normal programming means to have all the checks that are necessary in your code. It also means to always handle certain types of errors.&lt;/p&gt;

&lt;h3&gt;
  
  
  Necessary checks in code
&lt;/h3&gt;

&lt;p&gt;Some code needs a lot of conditionals. It can feel like you're being "overly defensive" with the number of conditionals you have.&lt;/p&gt;

&lt;p&gt;One example of this is checking for &lt;code&gt;null&lt;/code&gt; (the billion-dollar mistake). &lt;a href="https://programmingduck.com/articles/nulls"&gt;Nulls and null checks&lt;/a&gt; are very tricky. Many codebases need &lt;code&gt;if&lt;/code&gt; statements for them all over the place.&lt;/p&gt;

&lt;p&gt;Another example is validating user input. You need to have many checks to ensure that user input is valid. Your program needs to handle it very harshly. Otherwise, you'll have security vulnerabilities.&lt;/p&gt;

&lt;p&gt;But that's not defensive programming.&lt;/p&gt;

&lt;p&gt;Rather, something like forgetting a single null check is a bug. They're not unnecessary checks that you do "just in case". They're necessary checks. The value will be &lt;code&gt;null&lt;/code&gt; sometimes and that's normal. If you forget a single one, you have a bug. No questions asked.&lt;/p&gt;

&lt;h3&gt;
  
  
  Necessary error handling
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://programmingduck.com/articles/errors"&gt;Error handling&lt;/a&gt; is very important in programs. You always need to consider &lt;a href="https://programmingduck.com/articles/error-responses"&gt;how your program should respond to errors&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This also depends on the kind of error.&lt;/p&gt;

&lt;p&gt;Generally, most programs handle "expected errors" which are out of their control. For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;failing to send a network request because the network connection dropped.&lt;/li&gt;
&lt;li&gt;failing to find a file because a user deleted it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It would be very bad for the user experience for a program to crash on these errors. Also, it's relatively easy to handle them.&lt;/p&gt;

&lt;p&gt;As a result, most programs handle these, even if they're not doing defensive programming. So, again, this is considered "normal programming", not defensive programming.&lt;/p&gt;

&lt;p&gt;A different kind of error is a bug. In most programs, these errors are considered "unrecoverable". The rule-of-thumb for most programs is to crash on these errors and to not handle them.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rEvXbHW6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uq2i20uhxcf0oto3sgnz.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rEvXbHW6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uq2i20uhxcf0oto3sgnz.jpg" alt="" width="880" height="587"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Defensive programming
&lt;/h2&gt;

&lt;p&gt;In my interpretation, defensive programming is about fault tolerance. It means going above and beyond to ensure that your program continues working. It's used for certain programs where you need maximum:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;availability&lt;/li&gt;
&lt;li&gt;safety&lt;/li&gt;
&lt;li&gt;security&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example use case of defensive programming
&lt;/h3&gt;

&lt;p&gt;One example of defensive programming, as &lt;a href="https://coder.today/tech/2017-11-09_nasa-coding-standards-defensive-programming-and-reliability-a-postmortem-static-analysis./"&gt;Adrian Georgescu writes on his post on NASA coding standards&lt;/a&gt;, is for code used in space exploration missions.&lt;/p&gt;

&lt;p&gt;That code is developed once and sent to space. If it goes wrong, that's billions of dollars worth of work lost.&lt;/p&gt;

&lt;p&gt;For that kind of code, you need to take extreme measures. The code must work correctly, without crashing, no matter what.&lt;/p&gt;

&lt;p&gt;This is very different to your average program. With your average program, bugs aren't generally a big problem. Your program may still be usable even if it's buggy. In the worst case, a problem can be fixed manually by calling customer service. If the program becomes unusable, you can crash it and restart it. If it's a back end program, there are probably multiple servers running it. If it's a client, the user can restart the program themselves. In a really bad case, you can update the server code. You can even go to a physical server manually and restart it.&lt;/p&gt;

&lt;p&gt;But, with certain critical software, you can't do that. The software has to always work properly.&lt;/p&gt;

&lt;p&gt;The problem is that people aren't perfect. We create bugs. Not to mention that other errors may occur that are outside of the program's control (such as operating system errors). This means that the program may fail.&lt;/p&gt;

&lt;p&gt;But, that's not an option with some software.&lt;/p&gt;

&lt;p&gt;As a result, you need to do everything in your power to prevent failure.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to do defensive programming
&lt;/h3&gt;

&lt;p&gt;Defensive programming primarily means doing everything possible to ensure your program is working correctly and will continue to work correctly. This can include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;having very good software development practices.&lt;/li&gt;
&lt;li&gt;having many checks in code to double and triple check that everything is working at all times.&lt;/li&gt;
&lt;li&gt;optionally, having error recovery mechanisms. That way, if something goes wrong, maybe the program can recover.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Good software development practices
&lt;/h4&gt;

&lt;p&gt;The first step is to make the code as bug-free and as easy to work with as possible.&lt;/p&gt;

&lt;p&gt;That means that you need things such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;very strict QA&lt;/li&gt;
&lt;li&gt;very thorough tests&lt;/li&gt;
&lt;li&gt;very thorough runtime monitoring&lt;/li&gt;
&lt;li&gt;very strict coding and development standards. In fact, you may ban certain patterns or language features altogether, such as recursion.&lt;/li&gt;
&lt;li&gt;good general software quality&lt;/li&gt;
&lt;li&gt;source code that's easy to understand&lt;/li&gt;
&lt;li&gt;software that behaves in a predictable manner&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Those points are important for all software. However, they're critical for defensive programming. After all, if your source code isn't well tested or easy to understand, it could have bugs. This defeats the point of defensive programming.&lt;/p&gt;

&lt;h4&gt;
  
  
  Extra checks
&lt;/h4&gt;

&lt;p&gt;Code with defensive programming tends to have many extra checks. The checks are there to catch bugs. They wouldn't be needed if the code was completely bug-free. Checks that aren't intended to catch bugs fall under "normal programming", not "defensive programming".&lt;/p&gt;

&lt;p&gt;You have conditionals in the code that check whether something, such as some state in the program, is valid. If a check fails, it shows a bug.&lt;/p&gt;

&lt;p&gt;At that point:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;if the program is in development, you can crash it and fix the bug. This is the same principle as using assertions, during development, in offensive programming.&lt;/li&gt;
&lt;li&gt;if the program is in production, you can run error recovery (if you've implemented it) so the program can continue working.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The common case is to crash the program and fix the bug. During development, you hope that the combination of tests and extra checks will catch all of the bugs. Then, when the program is in production, it should work as intended.&lt;/p&gt;

&lt;p&gt;Another benefit for these checks is that they catch errors early. The more checks you have that the intermediate state is correct, the sooner you'll catch bugs. That makes debugging easier. It also means that you can start error recovery earlier.&lt;/p&gt;

&lt;p&gt;Finally, you may be able to implement some error recovery. Then, if a check fails, you can run your error recovery code.&lt;/p&gt;

&lt;p&gt;You can have as many or as few checks as you need. You'll have to decide what to check based on risk analysis. Some important checks are probably results involving important calculations and data. Some less important checks are things like checking function arguments or constantly checking state after simple operations.&lt;/p&gt;

&lt;p&gt;Here are some examples of checks you might have:&lt;/p&gt;

&lt;h5&gt;
  
  
  Example with checking function arguments
&lt;/h5&gt;

&lt;p&gt;You can check whether a function is called with valid arguments. The arguments should have the correct type and range.&lt;/p&gt;

&lt;p&gt;Here's a code example:&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;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nonEmptyString&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;naturalInteger&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;typeof&lt;/span&gt; &lt;span class="nx"&gt;nonEmptyString&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;string&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="c1"&gt;// if it's not a string&lt;/span&gt;
    &lt;span class="nx"&gt;nonEmptyString&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="c1"&gt;// if it's the empty string&lt;/span&gt;
    &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isInteger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;naturalInteger&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="c1"&gt;// if it's not an integer&lt;/span&gt;
    &lt;span class="nx"&gt;naturalInteger&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="c1"&gt;// if it's not a natural integer (1 or more)&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// crash the program&lt;/span&gt;
    &lt;span class="c1"&gt;// or handle the error here&lt;/span&gt;
    &lt;span class="c1"&gt;// or throw an exception so some code higher up handles the error&lt;/span&gt;
    &lt;span class="c1"&gt;// or do anything else your error recovery implementation requires&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// code for normal function execution&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Example with checking the results of data calculations
&lt;/h5&gt;

&lt;p&gt;Another example is checking results involving data.&lt;/p&gt;

&lt;p&gt;Normally, you would only check some data when you first receive it. For example, if a user submits some data, you would check it to make sure it's valid.&lt;/p&gt;

&lt;p&gt;Then, you would work with that data. You might format it or transform it in some way. You would have tests to make sure that these processes work correctly.&lt;/p&gt;

&lt;p&gt;In theory, you shouldn't need to also check the final result. The initial data is valid. The code you process it with works correctly. Therefore, the end result should be correct.&lt;/p&gt;

&lt;p&gt;But, if you're doing defensive programming, you might have checks on the final result too.&lt;/p&gt;

&lt;h4&gt;
  
  
  Recovering from unexpected errors
&lt;/h4&gt;

&lt;p&gt;The steps mentioned so far try to reduce the number of bugs in your program. However, there might still be bugs. For that reason, you might want to implement error recovery.&lt;/p&gt;

&lt;p&gt;This might require a lot of thinking. It might even need to be part of your feature planning. This would be the case if the program needs to respond to a user while it's in the process of recovery. The user-facing behaviour will probably be determined in collaboration with a product manager, not just by the programmers.&lt;/p&gt;

&lt;p&gt;Also, error recovery might be a large part of the code. As a made-up example, consider a back end that accepts network requests for product orders. A server might error while processing the order. To handle that scenario, you might do things like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;have an initial server record the order information so it's not lost.&lt;/li&gt;
&lt;li&gt;have some recovery mechanism for the faulty server. E.g. some other process may restart it. Or, maybe the server can try to fix its own state internally.&lt;/li&gt;
&lt;li&gt;the order can be given to a different server, or maybe the erroring server can try to process it again after it's fixed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here are some more examples of possible recovery mechanisms. If something in the code fails:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;maybe you can try to manually fix or reset the state in the program.&lt;/li&gt;
&lt;li&gt;maybe you can try running the operation again. If the problem is a race condition, it may work next time.&lt;/li&gt;
&lt;li&gt;if it's a subprogram that's erroring, maybe you can restart it. If the problem is invalid state in the subprogram, then restarting it may work.&lt;/li&gt;
&lt;li&gt;maybe you can have a backup program hosted on a server. If the client is producing incorrect results, then maybe it can call on the server to do the calculation instead.&lt;/li&gt;
&lt;li&gt;maybe you can have a backup program with less features than the main program. If the main program is erroring, maybe run the backup program instead which only provides barebones operation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Of course, if a critical part of the program is buggy then maybe you can't do anything about it in runtime. The only solution may be to fix the code.&lt;/p&gt;

&lt;p&gt;You'll also need to have risk analysis. That's where you consider things like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;what code could have errors?&lt;/li&gt;
&lt;li&gt;how likely it is that it will have errors?&lt;/li&gt;
&lt;li&gt;what impact would the error have?&lt;/li&gt;
&lt;li&gt;what would it cost to prevent the error from ever happening or to implement recovery mechanisms for that error?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The idea is that recovery will need to be considered as a first class citizen and a requirement during the project.&lt;/p&gt;

&lt;p&gt;Note that these kinds of recovery measures are probably reserved for programs that really need defensive programming. For most normal programs, it's probably enough to simply restart a server or notify the user that something went wrong.&lt;/p&gt;

&lt;h3&gt;
  
  
  Downsides of defensive programming
&lt;/h3&gt;

&lt;p&gt;Defensive programming has significant downsides. For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it requires a lot more code. At the very least, you'll have many more conditions and checks than a similar program without defensive programming.&lt;/li&gt;
&lt;li&gt;performance can be worse. That's because the extra checks take time to execute.&lt;/li&gt;
&lt;li&gt;it makes the code harder to work with because there is a lot more code.&lt;/li&gt;
&lt;li&gt;error recovery can take a long time to plan for and implement.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  When to use defensive programming
&lt;/h3&gt;

&lt;p&gt;Whether or not you use defensive programming depends on your program.&lt;/p&gt;

&lt;p&gt;As mentioned earlier, some programs need maximum availability, reliability and security. Those types of programs may require a lot of defensive programming.&lt;/p&gt;

&lt;p&gt;For most other programs, you shouldn't need defensive programming. "Normal programming" should be enough. Nonetheless, you're free to use some defensive programming techniques around some key areas of the code. It's up to you to make the decision.&lt;/p&gt;

&lt;p&gt;Regardless of what you do, remember to be pragmatic. Use risk analysis. Consider:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;what could go wrong?&lt;/li&gt;
&lt;li&gt;how much chance is there of it going wrong?&lt;/li&gt;
&lt;li&gt;what would the impact be?&lt;/li&gt;
&lt;li&gt;how could you prevent it from going wrong?&lt;/li&gt;
&lt;li&gt;what would it cost to implement prevention or recovery?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then, use the right amount of defensive programming where necessary. Try to avoid overusing defensive programming if it's not necessary.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KnRwNwqj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wk4zyshwxvf2o6kr43sf.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KnRwNwqj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wk4zyshwxvf2o6kr43sf.jpg" alt="" width="880" height="587"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Offensive programming
&lt;/h2&gt;

&lt;p&gt;The goal of offensive programming is to catch bugs and crash early. As explained in &lt;a href="https://programmingduck.com/articles/error-responses"&gt;how to respond to errors&lt;/a&gt;, crashing early is helpful.&lt;/p&gt;

&lt;p&gt;It means that you are notified of bugs immediately. Also, the stack trace from the crash is closer to the source of the problem. This helps with debugging.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to do offensive programming
&lt;/h3&gt;

&lt;p&gt;To do offensive programming, you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;do normal programming&lt;/li&gt;
&lt;li&gt;don't recover from bugs (avoid defensive programming)&lt;/li&gt;
&lt;li&gt;write code in a way where bugs are obvious and easy to find&lt;/li&gt;
&lt;li&gt;immediately crash the program on bugs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Just like with normal programming, you still need conditionals for things that aren't bugs. For example, you need conditionals for &lt;code&gt;null&lt;/code&gt; checks.&lt;/p&gt;

&lt;p&gt;Similarly, you should probably handle errors which aren't bugs. For example when users provide invalid data, or when you're not able to find a file in the file system. Most of the time, it would be unreasonable to crash on them. In other words, you should probably follow the "normal programming" way of dealing with these.&lt;/p&gt;

&lt;p&gt;Also, you should write code in a way where bugs are easy to find. Here are some techniques for that.&lt;/p&gt;

&lt;h4&gt;
  
  
  Avoid fallback code and default values
&lt;/h4&gt;

&lt;p&gt;Things like default state, default arguments and fallback code can hide bugs.&lt;/p&gt;

&lt;p&gt;For example, you might call a function with incorrect arguments. You might have accidentally used &lt;code&gt;null&lt;/code&gt; instead of a string for an argument. That's a bug. However, due to default arguments, the argument will be a string anyway. The bug won't be caught and the program may do the wrong thing as a result.&lt;/p&gt;

&lt;p&gt;A similar thing applies to fallback code. One example is inheritance and subclassing. You may have forgotten to implement a method in a subclass. Then, you call the method and it executes the parent's method. That's unintended behaviour, which is a bug.&lt;/p&gt;

&lt;p&gt;To prevent this, avoid using things like default state, default values and fallback implementations.&lt;/p&gt;

&lt;h4&gt;
  
  
  Avoid checks on code that will crash on errors
&lt;/h4&gt;

&lt;p&gt;Sometimes, buggy code will crash on its own. You don't have to do anything extra. Leave the code as it is and let it crash.&lt;/p&gt;

&lt;p&gt;For example, consider the code below. &lt;code&gt;array&lt;/code&gt; should never be &lt;code&gt;null&lt;/code&gt;. If it's &lt;code&gt;null&lt;/code&gt;, that's a bug.&lt;/p&gt;

&lt;p&gt;If you have a defensive check around it, the code won't crash:&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;foo&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="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="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// code doesn't crash if array is null&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="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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But if you don't have a defensive check, the code will crash.&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;foo&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="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="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// code crashes if array is null&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You want the code to crash as early as possible. So, in this case, just leave it as it is without a defensive check.&lt;/p&gt;

&lt;h4&gt;
  
  
  Have conditionals or assertions to check for errors
&lt;/h4&gt;

&lt;p&gt;Contrary to the point above, some bugs won't cause the program to crash.&lt;/p&gt;

&lt;p&gt;For example, you might have some incorrect state in your program. Your program may not crash from that.&lt;/p&gt;

&lt;p&gt;As another example, some code may execute that shouldn't execute under normal circumstances.&lt;/p&gt;

&lt;p&gt;In these cases, you can use manual checks. Then, if you find something wrong, you can manually crash the program.&lt;/p&gt;

&lt;p&gt;For example:&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;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;switch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="c1"&gt;// do something&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bar&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="c1"&gt;// do something&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="c1"&gt;// this code should never execute, so crash the program if it does&lt;/span&gt;
      &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Default case should never execute.&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's another example with checking state:&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;getCurrentPlayerHealth&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;health&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;player&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;health&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;health&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="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;health&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// this condition should never evaluate to true, so crash the program if it does&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Player health should be between 0 and 100.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// continue normal function execution&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;More traditionally, these kinds of "bug checks" use assertions instead of conditionals.&lt;/p&gt;

&lt;p&gt;Assertions are bug-finding tools. If they fail, they signify a bug. Conditionals are control-flow tools. If a conditional "fails", it doesn't signify a bug. It means that a different block of code should execute instead.&lt;/p&gt;

&lt;p&gt;So, instead of using conditionals, you can use assertions. For details on how to do that, please see the documentation for your programming language.&lt;/p&gt;

&lt;p&gt;Here's a code example in JavaScript:&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;player&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;health&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="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;player&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;health&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;player&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// logs a stack trace if condition is false, along with the player object&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In some programming languages, assertions crash the program. However, in others, they don't crash it. They may only print an error message to the console or something. Both are usable. However, offensive programming recommends hard crashing when possible.&lt;/p&gt;

&lt;p&gt;Also, some programming languages allow you to turn off assertions in production for better performance.&lt;/p&gt;

&lt;h3&gt;
  
  
  Downsides of offensive programming
&lt;/h3&gt;

&lt;p&gt;Similar to defensive programming, offensive programming has downsides.&lt;/p&gt;

&lt;p&gt;One downside is having to avoid certain kinds of code like default arguments. Default arguments have valid use cases. They provide "reasonable defaults". They can make some code much easier to work with.&lt;/p&gt;

&lt;p&gt;Another downside is having to crash the program. As explained in &lt;a href="https://programmingduck.com/articles/error-responses"&gt;how to respond to errors&lt;/a&gt;, crashing on bugs is usually good. However, it might be something that you're not prepared to do in your application.&lt;/p&gt;

&lt;p&gt;Another downside is performance. Having assert statements throughout your code can significantly reduce performance.&lt;/p&gt;

&lt;p&gt;As a result, many programming languages don't crash when assertions fail. Also, they have the option of removing assertions from production code. With this option, you lose the benefits of offensive programming in production. You only gain the benefits during development. However, that alone can be very useful.&lt;/p&gt;

&lt;h3&gt;
  
  
  When to use offensive programming
&lt;/h3&gt;

&lt;p&gt;Offensive programming helps you catch bugs. That's a significant win.&lt;/p&gt;

&lt;p&gt;For this reason, it's good to use it during development. Generally, you'll put assert statements here and there to ensure that certain things are correct.&lt;/p&gt;

&lt;p&gt;As for production, it depends. Consider the pros and cons of offensive programming and make your decision.&lt;/p&gt;

&lt;p&gt;It's alright to only use offensive programming in development. After all, catching more bugs during development is better than nothing.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rhOO9MCm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rs3nn0vrdlty13qy3l4t.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rhOO9MCm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rs3nn0vrdlty13qy3l4t.jpg" alt="" width="880" height="587"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Be pragmatic
&lt;/h2&gt;

&lt;p&gt;When choosing your approach to handling errors, you need to be pragmatic.&lt;/p&gt;

&lt;p&gt;"Normal programming" is the minimum that you need to do for most programs.&lt;/p&gt;

&lt;p&gt;For some programs, you might use defensive programming. In particular, for programs that need high:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;availability&lt;/li&gt;
&lt;li&gt;security&lt;/li&gt;
&lt;li&gt;reliability&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But also understand the downsides. Primarily, the downsides are worse performance and longer development time.&lt;/p&gt;

&lt;p&gt;Offensive programming helps you catch bugs. This is useful during development (and even production).&lt;/p&gt;

&lt;p&gt;You can mix and match the approaches based on what you need. You can even use different methodologies in different areas of the code. It's up to you to decide.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final notes
&lt;/h2&gt;

&lt;p&gt;So that's it for this article. I hope that you found it useful.&lt;/p&gt;

&lt;p&gt;As always, if any points were missed, or if you disagree with anything, or have any comments or feedback then please leave a comment below.&lt;/p&gt;

&lt;p&gt;For the next steps, I recommend looking at the other articles in the &lt;a href="https://programmingduck.com/articles/errors"&gt;error handling series&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Alright, thanks and see you next time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Credits
&lt;/h2&gt;

&lt;p&gt;Image credits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Turtle in sea - Photo by Tanguy Sauvin from Pexels&lt;/li&gt;
&lt;li&gt;Turtle in shell - Photo by Hogr Othman on Unsplash&lt;/li&gt;
&lt;li&gt;Tiger - Photo by Samuele Giglio on Unsplash&lt;/li&gt;
&lt;li&gt;Squirrel - Photo by Pixabay from Pexels&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>coding</category>
    </item>
    <item>
      <title>Nulls and null checks - How to work safely with nulls in any codebase</title>
      <dc:creator>Spyros Argalias</dc:creator>
      <pubDate>Sat, 01 Jan 2022 14:24:28 +0000</pubDate>
      <link>https://dev.to/programmingduck/nulls-and-null-checks-how-to-work-safely-with-nulls-in-any-codebase-1oeh</link>
      <guid>https://dev.to/programmingduck/nulls-and-null-checks-how-to-work-safely-with-nulls-in-any-codebase-1oeh</guid>
      <description>&lt;p&gt;An important part of &lt;a href="https://programmingduck.com/articles/clean-code" rel="noopener noreferrer"&gt;clean code&lt;/a&gt; is handling nulls properly.&lt;/p&gt;

&lt;p&gt;Nulls have been a tricky problem in programming for decades.&lt;/p&gt;

&lt;p&gt;Tony Hoare, the inventor of the &lt;code&gt;null&lt;/code&gt; even called it a &lt;a href="https://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare/" rel="noopener noreferrer"&gt;billion-dollar mistake&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Semantically, nulls are necessary. They represent the absence of a value. For example, a user may fill in a form that has optional fields. They may leave the optional fields blank. That's one reason for nulls.&lt;/p&gt;

&lt;p&gt;The problem is that nulls can be difficult to work with and track.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  The problem with nulls
&lt;/h2&gt;

&lt;p&gt;Nulls are hard to track in a codebase. There are many things which:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;have properties that are &lt;code&gt;null&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;can return &lt;code&gt;null&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;need to check for &lt;code&gt;null&lt;/code&gt; before doing something&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you miss a single "null check", you have a bug. Your program might do the wrong thing or even crash.&lt;/p&gt;

&lt;p&gt;For example, here is some code that crashes if you forget to check for &lt;code&gt;null&lt;/code&gt; first:&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="c1"&gt;// this function crashes if the argument is null&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arrayOrNull&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;arrayOrNull&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code should have been like this instead:&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="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arrayOrNull&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;arrayOrNull&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&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;null&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;arrayOrNull&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The issue is that being 100% thorough with your null checks is very hard. It's extremely difficult, if not impossible, to keep track of every null.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Solutions for working with nulls
&lt;/h2&gt;

&lt;p&gt;Working with nulls is difficult. To make things easier, here are some possible solutions you could use. Some of them are bad and some of them are good. We'll go over each one.&lt;/p&gt;

&lt;p&gt;The solutions are to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;place a &lt;code&gt;null&lt;/code&gt; check around everything&lt;/li&gt;
&lt;li&gt;use try / catch instead of null checks&lt;/li&gt;
&lt;li&gt;return a default value instead of &lt;code&gt;null&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;use the null object pattern&lt;/li&gt;
&lt;li&gt;remember to check for every null&lt;/li&gt;
&lt;li&gt;use a programming language with a type system that can track null&lt;/li&gt;
&lt;li&gt;use something like the Option type&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is each one in more detail:&lt;/p&gt;

&lt;h3&gt;
  
  
  Place a null check around everything
&lt;/h3&gt;

&lt;p&gt;One solution for dealing with nulls is to always check for them, even when you shouldn't need to. Check "just in case". After all "It's better to have it and not need it than to need it and not have it." - George Ellis. Right?&lt;/p&gt;

&lt;p&gt;If this is your only way of ensuring that you don't miss null checks, then maybe...&lt;/p&gt;

&lt;p&gt;However, it's not an optimal solution. The problem is that something in your code might be &lt;code&gt;null&lt;/code&gt; when it's not supposed to be. In other words, you have a bug.&lt;/p&gt;

&lt;p&gt;But, if you have null checks where they're not needed, you'll silently ignore the bug. It will be swallowed up in a null check.&lt;/p&gt;

&lt;p&gt;For example:&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="c1"&gt;// car is never supposed to be null&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;car&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getWheels&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;In the code above, &lt;code&gt;car&lt;/code&gt; may be &lt;code&gt;null&lt;/code&gt; when it's not supposed to be. That's a bug. However, due to an unnecessary null check, the program won't crash. The bug will be silently ignored.&lt;/p&gt;

&lt;p&gt;But, if you didn't have the unnecessary null check, the program would crash.&lt;/p&gt;

&lt;p&gt;For example:&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="c1"&gt;// car is null due to a bug&lt;/span&gt;
&lt;span class="c1"&gt;// the program crashes&lt;/span&gt;
&lt;span class="nx"&gt;car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getWheels&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 a good scenario. As explained in &lt;a href="https://programmingduck.com/articles/error-responses" rel="noopener noreferrer"&gt;how to respond to errors&lt;/a&gt;, at the very least, you want to know that you have a bug. Crashing makes that clear, but silently ignoring bugs doesn't.&lt;/p&gt;

&lt;p&gt;In other words, you should probably avoid unnecessary null checks.&lt;/p&gt;

&lt;p&gt;Otherwise, if you want to do &lt;a href="https://programmingduck.com/articles/defensive-programming" rel="noopener noreferrer"&gt;defensive programming&lt;/a&gt;, you can have the extra null checks. However, put in some code that records the bug if the thing is actually &lt;code&gt;null&lt;/code&gt;. That way you can debug the problem later. (For more information please see &lt;a href="https://programmingduck.com/articles/error-recording" rel="noopener noreferrer"&gt;record errors to debug later&lt;/a&gt;.)&lt;/p&gt;

&lt;h3&gt;
  
  
  Use try / catch instead of null checks
&lt;/h3&gt;

&lt;p&gt;Conditionals vs try / catch is a debate that applies to all possibly invalid actions. For this reason, it's explained more thoroughly in &lt;a href="https://programmingduck.com/articles/control-flow-invalid-actions" rel="noopener noreferrer"&gt;control flow for invalid actions&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;That aside, try / catch won't solve the problem.&lt;/p&gt;

&lt;p&gt;You might forget to add try / catch blocks, just like you might forget null checks. In this case, your program could crash.&lt;/p&gt;

&lt;p&gt;Worse, an exception might be caught, unintentionally, by a different try / catch block. That's a silent bug. Silent bugs tend to be worse than crashes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Return a default value instead of null
&lt;/h3&gt;

&lt;p&gt;Another option is to avoid returning &lt;code&gt;null&lt;/code&gt;. Instead, return a default value of the relevant type.&lt;/p&gt;

&lt;p&gt;For example, you might have a function that would normally return a string or a null. Instead of null, return the empty string. Or, you might have a function that would normally return a positive number or null. Instead of null, return 0 or -1 (if 0 isn't a suitable default).&lt;/p&gt;

&lt;h4&gt;
  
  
  Benefits of default values
&lt;/h4&gt;

&lt;p&gt;Default values reduce the number of nulls in your code.&lt;/p&gt;

&lt;p&gt;In some cases, they also reduce the number of conditionals. This happens when you can treat the default value and the "normal" value the same way.&lt;/p&gt;

&lt;p&gt;For example, this code works whether &lt;code&gt;user.name&lt;/code&gt; is a normal value or the empty string.&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="nf"&gt;printUserGreeting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&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;formattedName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toUppercase&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;greeting&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`Hello &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;formattedName&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&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="nx"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But, if &lt;code&gt;user.name&lt;/code&gt; was sometimes &lt;code&gt;null&lt;/code&gt;, the function would need a null check to work.&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="nf"&gt;printUserGreeting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&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;name&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// null check&lt;/span&gt;
    &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello&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="k"&gt;else&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;formattedName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toUppercase&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;greeting&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`Hello &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;formattedName&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&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="nx"&gt;greeting&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;Returning default values can be good. However, there are downsides.&lt;/p&gt;

&lt;h4&gt;
  
  
  Downsides of default values
&lt;/h4&gt;

&lt;p&gt;One downside is that the semantic meaning of &lt;code&gt;null&lt;/code&gt; isn't being honoured. Semantically, &lt;code&gt;null&lt;/code&gt; means the absence of a value. It doesn't mean a legitimate value. In comparison, the empty string or the number 0 could be legitimate values. 0 or -1 could be the result of a math calculation. The empty string may be a delimiter provided to a function. They don't mean the absence of data.&lt;/p&gt;

&lt;p&gt;Another downside, related to the first, is that you lose information on whether the value represents null or a legitimate value. Sometimes it's important to differentiate between the two. You won't always be able to use the default value and a normal value in the same way.&lt;/p&gt;

&lt;p&gt;For example, consider JavaScript's &lt;code&gt;Array.prototype.indexOf()&lt;/code&gt; method. It returns either a natural number (0 or a positive integer), or -1 as a default value (instead of null). But, in most situations, you can never use the value -1. You'll need a conditional to see if the method returned -1 or a normal value. This defeats the point. From the point of view of your code, it might as well have been null.&lt;/p&gt;

&lt;p&gt;For example:&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="nf"&gt;findUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userArray&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;targetUser&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;index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;userArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;targetUser&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;===&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Sorry, the user could not be found&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="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`The target user is user number &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="s2"&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;Another downside is that you might have many functions. Each might need a different default value. In this case, you'll have a default value that works for one of them, but not for the others. Then, the other functions will need conditionals to check for the default value. Again, this defeats the point. It actually makes the code harder to work with. Checking for &lt;code&gt;null&lt;/code&gt; is easier than checking for "magic values".&lt;/p&gt;

&lt;p&gt;Just to finish off, some other downsides are that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;coming up with a default value can be difficult&lt;/li&gt;
&lt;li&gt;tracing the origin of a default value (in code) can be difficult&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Verdict for default values
&lt;/h4&gt;

&lt;p&gt;To summarize: This is a solution which can be helpful to use. However, be careful of the downsides. You'll need to use your own judgement for when to use this option.&lt;/p&gt;

&lt;p&gt;Personally, I don't use it too often.&lt;/p&gt;

&lt;p&gt;But, one "default" value that's often good to use is an empty collection. For example, an empty array, or an empty hashmap. This tends to have all of the benefits without the downsides. That's because it's semantically correct to say "yes, this thing &lt;em&gt;has a collection&lt;/em&gt;, it just &lt;em&gt;happens to be empty&lt;/em&gt;". Also, most code should be able to work with an empty collection in the same way as a non-empty collection.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use the null object pattern
&lt;/h3&gt;

&lt;p&gt;The null object pattern is similar to using default values (mentioned above).&lt;/p&gt;

&lt;p&gt;The difference is that it works with classes and objects, rather than primitive values like strings and numbers and such. It sets defaults for values (attributes) as well as behaviour (methods).&lt;/p&gt;

&lt;p&gt;You use the null object pattern by creating a null / empty / default object with the same interface as a normal object. The attributes and methods of this object would have default values and behaviour.&lt;/p&gt;

&lt;p&gt;For example, here is a normal &lt;code&gt;User&lt;/code&gt; class that you might have in your codebase:&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;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&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;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;updateName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;doSomething&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// code to do something&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;Here is an example &lt;code&gt;NullUser&lt;/code&gt; class that you might have (a null object):&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;class&lt;/span&gt; &lt;span class="nc"&gt;NullUser&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Guest&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// default value&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;id&lt;/span&gt; &lt;span class="o"&gt;=&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="c1"&gt;// default value&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;updateName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt; &lt;span class="c1"&gt;// do nothing (default behaviour)&lt;/span&gt;

  &lt;span class="nf"&gt;doSomething&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// do nothing, or do some other default behaviour&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The usage in code would be something like this: You might have some code that would normally return either &lt;code&gt;null&lt;/code&gt; or a normal object. Instead of returning &lt;code&gt;null&lt;/code&gt;, return the null object. This is analogous to returning a default value.&lt;/p&gt;

&lt;p&gt;For example, the code below sometimes returns &lt;code&gt;null&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="nf"&gt;findUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&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;targetUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;userId&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;targetUser&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;null&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;user&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;Instead, you can have this code, which returns a null object instead of &lt;code&gt;null&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="nf"&gt;findUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&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;targetUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;userId&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;targetUser&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;new&lt;/span&gt; &lt;span class="nc"&gt;NullUser&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;user&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;Then, whenever you use the null object or the normal object, you don't need a null check.&lt;/p&gt;

&lt;p&gt;To illustrate the point, here some example code &lt;strong&gt;without&lt;/strong&gt; the null object pattern:&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="c1"&gt;// class User is shown above&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Bob&lt;/span&gt;&lt;span class="dl"&gt;'&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;new&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Alice&lt;/span&gt;&lt;span class="dl"&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;findUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&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;targetUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;userId&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;targetUser&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;null&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;user&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="nf"&gt;printName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&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;user&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// null check here&lt;/span&gt;
    &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&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="s2"&gt;`Hello Guest`&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&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="s2"&gt;`Hello &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&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;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;main&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;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;findUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;printName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&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;Here is the same code, except it uses the null object pattern:&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="c1"&gt;// classes User and NullUser are shown above&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Bob&lt;/span&gt;&lt;span class="dl"&gt;'&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;new&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Alice&lt;/span&gt;&lt;span class="dl"&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;findUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&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;targetUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;userId&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;targetUser&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;new&lt;/span&gt; &lt;span class="nc"&gt;NullUser&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// instead of returning null, return a null object&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;user&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="nf"&gt;printName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// no null check&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&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="s2"&gt;`Hello &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&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;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;main&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;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;findUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;printName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&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;As for whether to use the null object pattern or not, similar points apply as for default values.&lt;/p&gt;

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

&lt;h3&gt;
  
  
  Remember to check for every null
&lt;/h3&gt;

&lt;p&gt;One way to be thorough with all of your checks is... to be thorough with all of your checks...&lt;/p&gt;

&lt;p&gt;Every time you work on code, be extremely careful with your null checks. You should understand where &lt;code&gt;null&lt;/code&gt; can appear and where it shouldn't appear (where it would be a bug).&lt;/p&gt;

&lt;p&gt;It's very difficult. Sometimes it might feel impossible. But, that's what you have to do if you're not using other solutions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use a programming language with a type system that can track null
&lt;/h3&gt;

&lt;p&gt;Type systems to the rescue.&lt;/p&gt;

&lt;p&gt;Some static type programming languages are able to track &lt;code&gt;null&lt;/code&gt; just like they can track any other type. Then, if something in the codebase could either be &lt;code&gt;null&lt;/code&gt; or another type, they force (or warn) you to have a null check.&lt;/p&gt;

&lt;p&gt;Some examples are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;C# with its nullable reference types&lt;/li&gt;
&lt;li&gt;TypeScript when the &lt;code&gt;strictNullChecks&lt;/code&gt; option is enabled&lt;/li&gt;
&lt;li&gt;Kotlin's nullable reference types&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Also, some of these languages have non-nullable types. They can prevent you from assigning &lt;code&gt;null&lt;/code&gt; to a variable altogether. This gives you a guarantee that a variable will never be &lt;code&gt;null&lt;/code&gt;, so you don't need a null check.&lt;/p&gt;

&lt;p&gt;For example, using TypeScript (with &lt;code&gt;strictNullChecks&lt;/code&gt; enabled):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// works&lt;/span&gt;
&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// doesn't work, you get a compilation error&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// doesn't work, you get a compilation error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, that variable will never be &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In summary, with some type systems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;you'll be forced, or reminded, to have null checks when you need them. This way, you can never forget a null check.&lt;/li&gt;
&lt;li&gt;you can declare some variables as non-nullable. This means that they'll never be null. The type system will be aware of that and notify you.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Personally, I think that this is a great option.&lt;/p&gt;

&lt;p&gt;(Credit to &lt;a href="https://twitter.com/nicolas_frankel" rel="noopener noreferrer"&gt;Nicolas Frankel&lt;/a&gt; for mentioning non-nullable types.)&lt;/p&gt;

&lt;h3&gt;
  
  
  Use the Option type
&lt;/h3&gt;

&lt;p&gt;The final option (no pun intended) is to use something like the Option type (also known as the Maybe type).&lt;/p&gt;

&lt;p&gt;This doesn't completely eliminate null checks. But, it reduces them a lot. Also, the few remaining null checks are in places where they're easy to work with. It's very difficult to forget to put them in.&lt;/p&gt;

&lt;p&gt;With the Option type, you have two null checks instead of a countless number of them.&lt;/p&gt;

&lt;p&gt;The null checks are in:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;the Option type itself&lt;/li&gt;
&lt;li&gt;the first function to return an Option type&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here's a (very) simplified implementation of the Option type:&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;class&lt;/span&gt; &lt;span class="nc"&gt;Option&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nullOrNormalValue&lt;/span&gt;&lt;span class="p"&gt;)&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;_value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nullOrNormalValue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fn&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;_value&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&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="p"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;fn&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;_value&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;new&lt;/span&gt; &lt;span class="nc"&gt;Option&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newValue&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;To do something with the Option type, you use the &lt;code&gt;map&lt;/code&gt; method and pass in a function. This should be familiar if you've ever used a &lt;code&gt;map&lt;/code&gt; function for arrays and such.&lt;/p&gt;

&lt;p&gt;The key point here is that the null check is inside the Option type. In other words, every single time you try to use that value, you get a null check for free. This means that, as long as you're working with the Option type, you can never forget your null checks.&lt;/p&gt;

&lt;p&gt;You also need a null check, or some other conditional, in the place where you'll return an Option for the first time.&lt;/p&gt;

&lt;p&gt;For example, here's a normal function that would normally return null or a normal value:&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="nf"&gt;getNextScheduledEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&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;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;scheduledEvents&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;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;null&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;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;scheduledEvents&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the same function, but now, it returns an Option.&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="nf"&gt;getNextScheduledEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&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;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;scheduledEvents&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;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="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Option&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&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;new&lt;/span&gt; &lt;span class="nc"&gt;Option&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;scheduledEvents&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After writing that code, you don't need any more null checks for the returned value.&lt;/p&gt;

&lt;p&gt;For example, here's what the code would look like without Option:&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="nf"&gt;getNextScheduledEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&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;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;scheduledEvents&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;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;null&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;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;scheduledEvents&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="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nextScheduledEvent&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;nextSceduledEvent&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// null check&lt;/span&gt;
    &lt;span class="c1"&gt;// do nothing&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="c1"&gt;// stuff&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="nf"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nextScheduledEvent&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;nextSceduledEvent&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// null check&lt;/span&gt;
    &lt;span class="c1"&gt;// do nothing&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="c1"&gt;// stuff&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="nf"&gt;baz&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nextScheduledEvent&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;nextSceduledEvent&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// null check&lt;/span&gt;
    &lt;span class="c1"&gt;// do nothing&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="c1"&gt;// stuff&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="nf"&gt;main&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;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;scheduledEvents&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;nextEventOption&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getNextScheduledEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nextScheduledEvent&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;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nextScheduledEvent&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;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;baz&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nextScheduledEvent&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;Notice that every function needs a null check.&lt;/p&gt;

&lt;p&gt;Here is the same code using Option:&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="nf"&gt;getNextScheduledEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&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;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;scheduledEvents&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;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="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Option&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;new&lt;/span&gt; &lt;span class="nc"&gt;Option&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;scheduledEvents&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="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;doubleEventPrice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// no null check&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;event&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="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="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// stuff, no null check&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// stuff, no null check&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;main&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;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;scheduledEvents&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;nextEventOption&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getNextScheduledEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nextEventOption&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;doubleEventPrice&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;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nextEventOption&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;foo&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;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nextEventOption&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bar&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;Notice the lack of null checks.&lt;/p&gt;

&lt;p&gt;Of course, this is a very simplified explanation. There is much more to using the Option type. A real implementation of Option would also be more much more complicated.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Which option should you use?
&lt;/h2&gt;

&lt;p&gt;We covered a lot of methods for dealing with nulls.&lt;/p&gt;

&lt;p&gt;It's up to you to choose the appropriate one for your codebase. You need to weigh the pros and cons of each. You also need to consider your preferences.&lt;/p&gt;

&lt;p&gt;Personally, I love the type system enforced null checks. Along with those, I might use default values or the null object pattern sometimes. As of the time of writing, I haven't used the Option type very much. However, many people are passionate about that one. It seems like a great solution.&lt;/p&gt;

&lt;p&gt;If you want, leave a comment below about which method you recommend and why.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final notes
&lt;/h2&gt;

&lt;p&gt;So that's it for this article. I hope that you found it useful.&lt;/p&gt;

&lt;p&gt;As always, if any points were missed, or if you disagree with anything, or have any comments or feedback then please leave a comment below.&lt;/p&gt;

&lt;p&gt;Alright, thanks and see you next time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Credits
&lt;/h2&gt;

&lt;p&gt;Image credits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Single box - Photo by Christopher Bill on Unsplash&lt;/li&gt;
&lt;li&gt;Two boxes - Photo by Karolina Grabowska from Pexels&lt;/li&gt;
&lt;li&gt;Sticky note - Photo by AbsolutVision on Unsplash&lt;/li&gt;
&lt;li&gt;Pointing to laptop - Photo by John Schnobrich on Unsplash&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>coding</category>
    </item>
    <item>
      <title>Exceptions vs error values</title>
      <dc:creator>Spyros Argalias</dc:creator>
      <pubDate>Wed, 29 Dec 2021 18:11:08 +0000</pubDate>
      <link>https://dev.to/programmingduck/exceptions-vs-error-values-2hp9</link>
      <guid>https://dev.to/programmingduck/exceptions-vs-error-values-2hp9</guid>
      <description>&lt;p&gt;Exceptions vs error values has been a debate in &lt;a href="https://programmingduck.com/articles/errors" rel="noopener noreferrer"&gt;error handling&lt;/a&gt; for ages. Some people have firm stances on them. For example, in the book &lt;a href="https://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882" rel="noopener noreferrer"&gt;Clean Code&lt;/a&gt;, Uncle Bob recommends exceptions. In his post on &lt;a href="https://www.joelonsoftware.com/2003/10/13/13/" rel="noopener noreferrer"&gt;Exceptions&lt;/a&gt;, Joel mentions that he prefers error values.&lt;/p&gt;

&lt;p&gt;Programming languages have also taken stances. Popular languages such as C# and Java traditionally use exceptions. Languages like Rust use error values.&lt;/p&gt;

&lt;p&gt;In this article we'll examine some of their similarities and differences. We'll also provide suggestions about when to use which.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Basic examples of exceptions and error values
&lt;/h2&gt;

&lt;p&gt;Just for a quick introduction, here are some examples of exceptions and error values.&lt;/p&gt;

&lt;p&gt;If you're already familiar with them, then please skip to the next section.&lt;/p&gt;

&lt;p&gt;Here's an example of throwing and catching an exception in C#:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Example&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;Bar&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IndexOutOfRangeException&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// handle error&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Bar&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;true&lt;/span&gt; &lt;span class="cm"&gt;/* some condition to check if something went wrong */&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;IndexOutOfRangeException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Some error message"&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="c1"&gt;// normal program execution&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;In the code above, &lt;code&gt;Bar&lt;/code&gt; throws an exception. The exception is caught and handled in &lt;code&gt;Foo&lt;/code&gt;, in the &lt;code&gt;catch&lt;/code&gt; block.&lt;/p&gt;

&lt;p&gt;Here's the same thing in JavaScript:&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="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// handle error&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="nf"&gt;bar&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="kc"&gt;true&lt;/span&gt; &lt;span class="cm"&gt;/* some condition */&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error message&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="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// normal program execution&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;Error values can be implemented in different ways. One way is for a function to return either an error or a normal value.&lt;/p&gt;

&lt;p&gt;For example:&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="nf"&gt;foo&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;bar&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;result&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// handle error&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="c1"&gt;// normal program execution&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="nf"&gt;bar&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="kc"&gt;true&lt;/span&gt; &lt;span class="cm"&gt;/* some condition */&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;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Error message&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="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;42&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;In the code above, &lt;code&gt;bar&lt;/code&gt; can return either an error or a normal value. &lt;code&gt;foo&lt;/code&gt; checks the return value. If it was an error, it handles it. Otherwise, it continues normal program execution.&lt;/p&gt;

&lt;p&gt;You can also use error values by returning a single object. The object should have fields for both the error and the normal return value. For example, you could use a tuple, or an object with properties. If there was an error, the &lt;code&gt;value&lt;/code&gt; should be empty. For example &lt;code&gt;{error: new Error('Message'), value: null}&lt;/code&gt;. If there wasn't an error, the &lt;code&gt;error&lt;/code&gt; value should be empty. For example &lt;code&gt;{error: null, value: 42}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here's a code example:&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="nf"&gt;foo&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;bar&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;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// handle error&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="c1"&gt;// normal program execution&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="nf"&gt;bar&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="kc"&gt;true&lt;/span&gt; &lt;span class="cm"&gt;/* some condition */&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="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Error message.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&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="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;42&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;In the code above, &lt;code&gt;bar&lt;/code&gt; always returns an object. If something goes wrong, the object will have a value in the &lt;code&gt;error&lt;/code&gt; field. Otherwise, the &lt;code&gt;error&lt;/code&gt; field will be &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Similarities between exceptions and error values
&lt;/h2&gt;

&lt;p&gt;Exceptions and error values are fairly similar. In fact, some newer programming languages such as Rust and Swift eliminate most of the differences between them.&lt;/p&gt;

&lt;p&gt;The most important thing about both of them is that they act as different return values from a function / method. The different return values should lead to different code execution paths.&lt;/p&gt;

&lt;p&gt;They also share a big downside. It's easy to mess up with both of them.&lt;/p&gt;

&lt;p&gt;With an exception, you may:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;forget to catch it&lt;/li&gt;
&lt;li&gt;wrongly assume that some code higher in the call stack will catch it&lt;/li&gt;
&lt;li&gt;accidentally catch it higher in the call stack in a place that's not prepared to handle it properly&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Also, you can completely avoid checking error values.&lt;/p&gt;

&lt;p&gt;It's very easy to forget or mess up. Even if you don't, someone else might. So, you have to be very diligent.&lt;/p&gt;

&lt;p&gt;Or, you can use a programming language that forces you to check all errors. (More on that later.)&lt;/p&gt;

&lt;h2&gt;
  
  
  Differences between exceptions and error values
&lt;/h2&gt;

&lt;p&gt;Exceptions and error values have some differences:&lt;/p&gt;

&lt;h3&gt;
  
  
  Performance
&lt;/h3&gt;

&lt;p&gt;Throwing and catching exceptions are commonly considered slow. Returning error values is fast.&lt;/p&gt;

&lt;p&gt;However, exceptions are supposed to be "exceptional" (thrown very rarely). In practice, this means that the performance of your application shouldn't be negatively affected by using them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Crashing the program vs silent bugs
&lt;/h3&gt;

&lt;p&gt;Uncaught exceptions crash the program. More rarely, exceptions can also result in silent bugs (if you catch them higher in the call stack without intending to).&lt;/p&gt;

&lt;p&gt;Unchecked error values result in silent bugs.&lt;/p&gt;

&lt;p&gt;Exceptions are better in this case. As explained in &lt;a href="https://programmingduck.com/articles/error-responses" rel="noopener noreferrer"&gt;how to respond to errors&lt;/a&gt;, crashing the program is a better default option than silent bugs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bubbling
&lt;/h3&gt;

&lt;p&gt;Exceptions can "bubble" up the call stack. An exception that's not caught in a &lt;code&gt;catch&lt;/code&gt; block will be thrown in the caller (the previous code in the call stack). If it's not caught there, the process will repeat. If it reaches the end of the call stack, the program will crash.&lt;/p&gt;

&lt;p&gt;Bubbling is both good and bad.&lt;/p&gt;

&lt;p&gt;The benefit is that it's very convenient. You can have a single try / catch block in some parent function. The exception will propagate to it and will be caught there.&lt;/p&gt;

&lt;p&gt;The downside is that the flow of execution is not explicit. You have to keep track of it yourself. You also have to remember which exceptions are caught where in the call stack.&lt;/p&gt;

&lt;p&gt;This can put you into a bad situation. Sometimes you might not remember or know if an exception will be caught or not, or where it will be caught, or by what.&lt;/p&gt;

&lt;p&gt;In comparison, error values are standard return values. If you want them to propagate, you have to propagate them manually. You have to manually return them across different functions / methods, all the way up the stack.&lt;/p&gt;

&lt;p&gt;The benefit of this is that it's very explicit. It's very easy to track and reason about. The downside is that it's very verbose. You need many return statements across many different function / method calls.&lt;/p&gt;

&lt;p&gt;Note that you can technically manually propagate exceptions if you want to. However, that's not common practice. For more details on this please see "checked exceptions" in a later section.&lt;/p&gt;

&lt;h3&gt;
  
  
  Suitability in functional programming
&lt;/h3&gt;

&lt;p&gt;Generally, exceptions are less common in functional programming.&lt;/p&gt;

&lt;p&gt;That's because functional programming promotes immutability and pure functions.&lt;/p&gt;

&lt;p&gt;With exceptions, sometimes you need to break immutability. For example, often, you need to declare variables outside of try / catch blocks and then mutate them in try / catch.&lt;/p&gt;

&lt;p&gt;Here's a code example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;try&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="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Something&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="c1"&gt;// do stuff with `a`&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// handle error&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&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="nf"&gt;close&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;Also, thrown exceptions are not standard return values. This messes up the "pure function" point.&lt;/p&gt;

&lt;h2&gt;
  
  
  Exceptions and error values in some newer languages
&lt;/h2&gt;

&lt;p&gt;Some newer languages, like Rust and Swift, change things up a bit.&lt;/p&gt;

&lt;p&gt;Most importantly, they force you to check all error values and thrown exceptions. This means that you can never forget to check for errors or to handle exceptions.&lt;/p&gt;

&lt;p&gt;In the case of Swift, it also makes exception bubbling more explicit. It still allows exceptions to propagate automatically. However, it requires intermediate functions (that an exception will propagate through), to be marked with the keyword "throws".&lt;/p&gt;

&lt;p&gt;This additional explicitness makes exceptions easier to track throughout your code.&lt;/p&gt;

&lt;p&gt;The downside is that it makes things more verbose.&lt;/p&gt;

&lt;p&gt;(Rust uses error values, which you have to propagate explicitly anyway.)&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Which should you use?
&lt;/h2&gt;

&lt;p&gt;Overall, it seems like this is a question of robustness and amount of safety measures vs verbosity.&lt;/p&gt;

&lt;p&gt;Enforcing error checking and having explicit error propagation have obvious benefits. It makes it much harder to forget to do your error handling. You'll have to intentionally ignore it to avoid it.&lt;/p&gt;

&lt;p&gt;However, verbosity has downsides too. It can can make code less readable. It can also make it harder to make large changes to code. This can be especially prominent if you're propagating everything manually.&lt;/p&gt;

&lt;p&gt;For example, imagine that you change a low-level function (or add a new one) to sometimes return an error value. That error may need to be handled at a higher-level function. This means that you'll need to add code to every intermediary function to keep propagating the error.&lt;/p&gt;

&lt;p&gt;That's a large change. In comparison, if you added an exception that bubbled automatically, you would just add a try / catch block at the high-level function and you'd be done.&lt;/p&gt;

&lt;p&gt;So it's up to you to decide where you stand on the safety measures vs verbosity scale.&lt;/p&gt;

&lt;p&gt;For maximum safety measures, you should probably use a language that forces you to check all errors and forces explicit propagation of them. The downside is that the error handling will be more verbose.&lt;/p&gt;

&lt;p&gt;One level lower in safety is to use error values. I regard these as more robust than throwing exceptions. That's because propagating error values is more explicit than bubbling exceptions. The downside is that there's more verbosity. Also, note that you need to be very diligent with these. If you forget to check an error, you'll get silent bugs. Unchecked error values are worse than uncaught exceptions.&lt;/p&gt;

&lt;p&gt;Otherwise, go for throwing "normal" exceptions (such as the ones in Java, C# and JavaScript). They're the least verbose. This doesn't mean that you can't create robust programs with them. It just means that it's up to you to be diligent with errors and to track everything.&lt;/p&gt;

&lt;p&gt;It's probably also a good idea to consider the convention in your programming language. Some programming languages prefer exceptions. Some others prefer error values.&lt;/p&gt;

&lt;p&gt;My personal preference is to lean towards higher safety for larger scoped and more critical projects. For smaller scoped projects, I lean towards less verbosity and more convenience (exceptions).&lt;/p&gt;

&lt;h2&gt;
  
  
  Final notes
&lt;/h2&gt;

&lt;p&gt;So that's it for this article. I hope that you found it useful.&lt;/p&gt;

&lt;p&gt;As always, if any points were missed, or if you disagree with anything, or have any comments or feedback then please leave a comment below.&lt;/p&gt;

&lt;p&gt;For the next steps, I recommend looking at the other articles in the &lt;a href="https://programmingduck.com/articles/errors" rel="noopener noreferrer"&gt;error handling series&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Alright, thanks and see you next time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Credits
&lt;/h2&gt;

&lt;p&gt;Images:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Duelling Legos - Photo by Stillness InMotion on Unsplash&lt;/li&gt;
&lt;li&gt;Typewriter and laptop - Photo by Glenn Carstens-Peters on Unsplash&lt;/li&gt;
&lt;li&gt;Post-it notes - Photo by Will H McMahan on Unsplash&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>coding</category>
    </item>
    <item>
      <title>Best practices for error catching and handling</title>
      <dc:creator>Spyros Argalias</dc:creator>
      <pubDate>Fri, 19 Nov 2021 17:41:19 +0000</pubDate>
      <link>https://dev.to/programmingduck/best-practices-for-error-catching-and-handling-4e3m</link>
      <guid>https://dev.to/programmingduck/best-practices-for-error-catching-and-handling-4e3m</guid>
      <description>&lt;p&gt;Catching and handling errors is an important part of &lt;a href="https://programmingduck.com/articles/errors" rel="noopener noreferrer"&gt;error handling&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Here are some best practices for it. Generally, it's good to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;be very thorough with your error checking&lt;/li&gt;
&lt;li&gt;do your error checking first&lt;/li&gt;
&lt;li&gt;handle errors at the earliest appropriate place&lt;/li&gt;
&lt;li&gt;(for exceptions) put the minimum code you can in your try blocks&lt;/li&gt;
&lt;li&gt;restore state and resources so the program can keep executing correctly&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here are the best practices in more detail.&lt;/p&gt;

&lt;p&gt;(Note: For this article, "error" is defined in &lt;a href="https://programmingduck.com/articles/error-meaning" rel="noopener noreferrer"&gt;Terminology - Errors and non-errors&lt;/a&gt;. It means anything you might throw an exception or return an error value for. It doesn't just mean an "unrecoverable error".)&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Be very thorough with your error checking
&lt;/h2&gt;

&lt;p&gt;Unfortunately, programmers aren't perfect. We create bugs and make mistakes. In fact, we do that quite often. That's why we have so many error correction tools to help us.&lt;/p&gt;

&lt;p&gt;So, in general, you should go by the assumption that everything in your program that can fail will fail. Other things that you didn't think of will also fail.&lt;/p&gt;

&lt;p&gt;To minimise the issue, you should be very thorough with your error checking. Always catch all possible exceptions and check all error values. Then, handle them appropriately.&lt;/p&gt;

&lt;h2&gt;
  
  
  Check for errors first
&lt;/h2&gt;

&lt;p&gt;This one is a stylistic convention.&lt;/p&gt;

&lt;p&gt;In your code, it's good to do your error checking before anything else. Leave the normal program execution for after.&lt;/p&gt;

&lt;p&gt;For example, in methods that throw exceptions, try to check for errors and throw the exception as early as possible.&lt;/p&gt;

&lt;p&gt;Here's a code example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Example&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;a&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="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt; &lt;span class="p"&gt;===&lt;/span&gt; &lt;span class="m"&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;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;InvalidArgumentException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Parameter {a} must not be the empty string."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="c1"&gt;// normal program execution&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;Method &lt;code&gt;Foo&lt;/code&gt; starts by doing its error checking. The normal code execution comes afterwards.&lt;/p&gt;

&lt;p&gt;Avoid doing something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Example&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// do some "normal program execution" that doesn't need parameter `a`&lt;/span&gt;

        &lt;span class="c1"&gt;// check parameter `a` right before you need it&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt; &lt;span class="p"&gt;===&lt;/span&gt; &lt;span class="m"&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;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;InvalidArgumentException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Parameter {a} must not be the empty string."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="c1"&gt;// more normal program execution&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;In this code, &lt;code&gt;Foo&lt;/code&gt; executes some code. Then, it does some error checking for parameter &lt;code&gt;a&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The same principle applies to checking error values. Try to check for errors before you continue your normal program execution.&lt;/p&gt;

&lt;p&gt;Here is a code example:&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="nf"&gt;foo&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;bar&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;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// handle error&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="c1"&gt;// normal code execution&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above first checks for errors. Then, it continues normal program execution.&lt;/p&gt;

&lt;p&gt;This organises your code into recognisable blocks. Errors first, normal code after. It makes your code easier to scan through and easier to understand.&lt;/p&gt;

&lt;p&gt;It also helps with debugging. When an error is thrown, less normal code will have ran. This reduces the amount of code you'll have to check through when debugging.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Handle errors at the first appropriate place
&lt;/h2&gt;

&lt;p&gt;Sometimes, you can't handle errors immediately. You might need to propagate them to higher level code.&lt;/p&gt;

&lt;p&gt;To showcase this, consider this example: You have a function that searches the file system for a file. If it finds it, it reads its contents. Otherwise, it throws an exception. How should the code handle this exception? Should it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;crash the program?&lt;/li&gt;
&lt;li&gt;create a new file?&lt;/li&gt;
&lt;li&gt;search for a backup file in a different location?&lt;/li&gt;
&lt;li&gt;notify the user that the file couldn't be found and ask them to try a different file?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The file system code doesn't know. Different programs will want different behaviour if the file isn't found. This means that the file system code can't have hardcoded behaviour to handle the exception.&lt;/p&gt;

&lt;p&gt;Instead, the exception should be handled at a more appropriate place. The place that can decide what to do about it. Often, that means some code higher up in the call stack. So, the exception (or error value) needs to propagate up to that place.&lt;/p&gt;

&lt;p&gt;For exceptions, that means that you should let the exception bubble up. Then, have a try / catch block at the place where you'll handle it.&lt;/p&gt;

&lt;p&gt;For error values, you'll have to manually return them until they reach the correct place in the call stack.&lt;/p&gt;

&lt;p&gt;Having said that, you should handle errors at the &lt;strong&gt;first&lt;/strong&gt; appropriate place. Don't propagate them higher than necessary. The earlier you handle errors, the closer they'll be to the code that raised them. This makes the execution flow of the code easier to track and understand.&lt;/p&gt;

&lt;p&gt;Here's a code example where we search the database for a record:&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="c1"&gt;// server.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;getSpriteById&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./myDatabase.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/:spriteId&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;spriteId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;spriteId&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;getSpriteById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;spriteId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// exception from database is handled here.&lt;/span&gt;
    &lt;span class="c1"&gt;// In this case, it responds with a 404.&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sendStatus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;404&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Sprite found&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;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/foo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;spriteId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;spriteId&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;getSpriteById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;spriteId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// exception from database is handled here.&lt;/span&gt;
    &lt;span class="c1"&gt;// In this case, it redirects&lt;/span&gt;
    &lt;span class="c1"&gt;// to another page for the user to fill in correct information&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;redirect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/form&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Data accepted&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="c1"&gt;// myDatabase.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;connectToDatabase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;connectionString&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;getSpriteById&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;spriteId&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="c1"&gt;// throws exception if it doesn't find the record&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sprite&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;spriteId&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;sprite&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;getSpriteById&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(By the way the code is pseudocode, don't expect it to work if you actually run it. However, it showcases the point.)&lt;/p&gt;

&lt;p&gt;In the example, the function &lt;code&gt;getSpriteById&lt;/code&gt; searches the database. If it doesn't find the record it's looking for, it throws an exception. It doesn't handle the error itself. Instead, the handlers in &lt;code&gt;server.js&lt;/code&gt; decide how to handle the error. Both handlers have try / catch blocks which handle the exception differently based on what they need.&lt;/p&gt;

&lt;h2&gt;
  
  
  (For exceptions) Be careful of how much code you put in your try blocks
&lt;/h2&gt;

&lt;p&gt;It's considered best practice to put as little code as possible in each try / catch block. This means that you may need multiple try / catch blocks, instead of just one.&lt;/p&gt;

&lt;p&gt;The benefits of this are that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it's easy to see which code raises which exceptions (and which code doesn't raise exceptions)&lt;/li&gt;
&lt;li&gt;it "separates concerns" more clearly. Each try / catch block is a separate piece of functionality. This makes it easier to refactor it into a separate function.&lt;/li&gt;
&lt;li&gt;it prevents the accidental swallowing of exceptions. This happens if you put some code in &lt;code&gt;try&lt;/code&gt; without thinking. That code might throw an exception that you weren't prepared to handle. However, it will be caught in &lt;code&gt;catch&lt;/code&gt; (and potentially handled incorrectly). Then, the program will continue executing, potentially producing the wrong result.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's also a solution for when certain lines of code can throw the same type of exception, but each case needs to be handled differently.&lt;/p&gt;

&lt;p&gt;On the flip side, having small try / catch blocks can make the code more verbose.&lt;/p&gt;

&lt;p&gt;In the end, you need to be pragmatic. Keep the benefits and downsides in mind. Then, make your decision. Usually, correctness and clarity are more important, even if they're more verbose. However, it's okay to make the code more concise sometimes, especially if you don't think correctness and clarity are too affected.&lt;/p&gt;

&lt;p&gt;For example, this code could be separated further, but is still pretty good:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;BufferedReader&lt;/span&gt; &lt;span class="n"&gt;bufferedReader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;bufferedReader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BufferedReader&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;FileReader&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"path"&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bufferedReader&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;readLine&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;doSomething&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bufferedReader&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;readLine&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;FileNotFoundException&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;IOException&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printStackTrace&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;bufferedReader&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;close&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;IOException&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printStackTrace&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the same example separated more:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;BufferedReader&lt;/span&gt; &lt;span class="n"&gt;bufferedReader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;bufferedReader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BufferedReader&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;FileReader&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"path"&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;

    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bufferedReader&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;readLine&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// we’re leaving this line here for simplicity, but depending&lt;/span&gt;
            &lt;span class="c1"&gt;// on how it works, it might need its own try block&lt;/span&gt;
            &lt;span class="n"&gt;doSomething&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bufferedReader&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;readLine&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;IOException&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printStackTrace&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;FileNotFoundException&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printStackTrace&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;bufferedReader&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;close&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;IOException&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printStackTrace&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second version would be necessary if each &lt;code&gt;catch&lt;/code&gt; block needed to have different code. Otherwise, you can choose either version.&lt;/p&gt;

&lt;p&gt;(The best solution is probably to use a &lt;code&gt;with&lt;/code&gt; / &lt;code&gt;using&lt;/code&gt; / &lt;code&gt;try-with-resources&lt;/code&gt; statement, or the equivalent for your programming language. It automatically closes the &lt;code&gt;bufferedReader&lt;/code&gt; at the end. The code above is just to showcase the point.)&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Restore state and resources
&lt;/h2&gt;

&lt;p&gt;If you successfully handle an error, then the program should be able to continue executing correctly. It should continue almost as though the error never happened.&lt;/p&gt;

&lt;p&gt;This means that you need to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;restore state to something correct&lt;/li&gt;
&lt;li&gt;close any side effects that were started by erroring code&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Restore state
&lt;/h3&gt;

&lt;p&gt;After recovering from an error, your program needs to have the correct state. If it doesn't, then you haven't really recovered.&lt;/p&gt;

&lt;p&gt;This means that you might need to fix or restore your program's state in your error handling code.&lt;/p&gt;

&lt;p&gt;Here's an example.&lt;/p&gt;

&lt;p&gt;Consider that you have a Boolean variable. The Boolean variable should start as &lt;code&gt;false&lt;/code&gt;. However, while some code is running, you set it to &lt;code&gt;true&lt;/code&gt;. At the end, you set it to &lt;code&gt;false&lt;/code&gt; again.&lt;/p&gt;

&lt;p&gt;But, if the program errors at some point, the variable won't be reset. This will leave your program in a bad state, even if the error is handled.&lt;/p&gt;

&lt;p&gt;Here's an example of some "dangerous code", which will have invalid state if an error occurs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;isBusy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleUserEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="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;isBusy&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;isBusy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// do something asynchronous which may throw an exception, for example:&lt;/span&gt;
    &lt;span class="c1"&gt;// await doSomething()&lt;/span&gt;
    &lt;span class="nx"&gt;isBusy&lt;/span&gt; &lt;span class="o"&gt;=&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If &lt;code&gt;handleUserEvent&lt;/code&gt; errors, the value of &lt;code&gt;isBusy&lt;/code&gt; will remain &lt;code&gt;false&lt;/code&gt; forever. &lt;code&gt;handleUserEvent&lt;/code&gt; won't be able to run properly again.&lt;/p&gt;

&lt;p&gt;For that reason, you need to manually reset the state if an error occurs.&lt;/p&gt;

&lt;p&gt;Here's a code example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;isBusy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleUserEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="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;isBusy&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;isBusy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// do something asynchronous which may throw an exception, for example:&lt;/span&gt;
      &lt;span class="c1"&gt;// await doSomething()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;isBusy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// fix the state&lt;/span&gt;
      &lt;span class="c1"&gt;// exception is sent higher up because there's no catch block&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="c1"&gt;// equivalent example&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleUserEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="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;isBusy&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;isBusy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// do something asynchronous which may throw an exception, for example:&lt;/span&gt;
      &lt;span class="c1"&gt;// await doSomething()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;isBusy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// fix the state&lt;/span&gt;
      &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;isBusy&lt;/span&gt; &lt;span class="o"&gt;=&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, if an error occurs in &lt;code&gt;handleUserEvent&lt;/code&gt;, there's no problem. The state is restored and &lt;code&gt;handleUserEvent&lt;/code&gt; will execute correctly afterwards.&lt;/p&gt;

&lt;p&gt;The same principle applies across all of your code in the call stack. Imagine that an error occurs in function &lt;code&gt;bar&lt;/code&gt;. But, you handle the error in function &lt;code&gt;foo&lt;/code&gt;, which is 5 function calls earlier in the call stack. For the program to be in a valid state, you need to make sure that you've fixed all of the state from &lt;code&gt;bar&lt;/code&gt; to &lt;code&gt;foo&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7xv31ggifxcg78uor5zj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7xv31ggifxcg78uor5zj.png" alt="Diagram of a call stack showing functions which may need to reset their state in case of errors."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Basically, it means that you may need many intermediate try / catch blocks in different functions in the call stack. You fix the state in each one. That way, if the error is handled higher up, the state of all the intermediate code has been fixed. They can run again as though nothing went wrong.&lt;/p&gt;

&lt;p&gt;For example:&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="c1"&gt;// handleUserEvent&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./foo.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// this is the top-level function&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleUserEvent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// handle the error at the top level&lt;/span&gt;
    &lt;span class="c1"&gt;// record error&lt;/span&gt;
    &lt;span class="c1"&gt;// display message to user that action didn't work&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="c1"&gt;// foo.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;bar&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./bar.js&lt;/span&gt;&lt;span class="dl"&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;isBusy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;foo&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;isBusy&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="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nx"&gt;isBusy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;isBusy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// restore this module's state&lt;/span&gt;
    &lt;span class="c1"&gt;// exception is sent further up because there is no catch block&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


&lt;span class="c1"&gt;// bar.js&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;isBusy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;bar&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;isBusy&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="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// do something asynchronous which may throw an exception, for example:&lt;/span&gt;
    &lt;span class="c1"&gt;// await doSomething()&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;isBusy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// restore this module's state&lt;/span&gt;
    &lt;span class="c1"&gt;// exception is sent further up because there is no catch block&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(Yes, I know the code in the examples is very contrived, but hopefully it illustrates the point 😅)&lt;/p&gt;

&lt;h3&gt;
  
  
  Close side effects
&lt;/h3&gt;

&lt;p&gt;Some side effects come in pairs. For example, if you open a file, you should also close it. It can be unsafe to leave it open.&lt;/p&gt;

&lt;p&gt;So, make sure that resources like that are properly released.&lt;/p&gt;

&lt;p&gt;If you use exceptions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;use &lt;code&gt;with&lt;/code&gt; blocks. These automatically close resources if an error occurs. Some examples are &lt;code&gt;with&lt;/code&gt; in Python, &lt;code&gt;try-with-resources&lt;/code&gt; in Java or &lt;code&gt;using&lt;/code&gt; in C#.&lt;/li&gt;
&lt;li&gt;otherwise, use &lt;code&gt;finally&lt;/code&gt; blocks (or their equivalent in different programming languages)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you're using error values, place the "closing" code somewhere appropriate. It should run both in the error case and the non-error case.&lt;/p&gt;

&lt;p&gt;Here's an example with &lt;code&gt;finally&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="c1"&gt;// pseudocode&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;openFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;foo.txt&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// open the resource&lt;/span&gt;
  &lt;span class="nf"&gt;writeToFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bar&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="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// code to handle exceptions&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// close the resource&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's an example with error values:&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="c1"&gt;// pseudocode&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;fileError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;openFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;foo.txt&lt;/span&gt;&lt;span class="dl"&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;fileError&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// handle error&lt;/span&gt;
  &lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;writeError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;writeToFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bar&lt;/span&gt;&lt;span class="dl"&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;writeError&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// handle error&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Final notes
&lt;/h2&gt;

&lt;p&gt;So that's it for this article. I hope that you found it useful.&lt;/p&gt;

&lt;p&gt;As always, if any points were missed, or if you disagree with anything, or have any comments or feedback then please leave a comment below.&lt;/p&gt;

&lt;p&gt;For the next steps, I recommend looking at the other articles in the &lt;a href="https://programmingduck.com/articles/errors" rel="noopener noreferrer"&gt;error handling series&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Alright, thanks and see you next time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Credits
&lt;/h2&gt;

&lt;p&gt;Image credits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Net - Photo by Raghavendra Saralaya on Unsplash&lt;/li&gt;
&lt;li&gt;Arrow - Photo by Hello I'm Nik on Unsplash&lt;/li&gt;
&lt;li&gt;Squirrel in treehouse - Photo by Joakim Honkasalo on Unsplash&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>coding</category>
    </item>
    <item>
      <title>Error recording - How to record errors in your application to debug later</title>
      <dc:creator>Spyros Argalias</dc:creator>
      <pubDate>Tue, 09 Nov 2021 13:48:53 +0000</pubDate>
      <link>https://dev.to/programmingduck/error-recording-how-to-record-errors-in-your-application-to-debug-later-24i9</link>
      <guid>https://dev.to/programmingduck/error-recording-how-to-record-errors-in-your-application-to-debug-later-24i9</guid>
      <description>&lt;p&gt;Recording errors is an important part of &lt;a href="https://programmingduck.com/articles/errors"&gt;error handling&lt;/a&gt;. In short, when certain errors occur in programs, you want to know about it. This is particularly important with bugs.&lt;/p&gt;

&lt;p&gt;You need to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;know that they occurred&lt;/li&gt;
&lt;li&gt;have useful information about them so you can debug them later&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;One way to do this is to use an error monitoring or logging service. Some examples are &lt;a href="https://newrelic.com/"&gt;New Relic&lt;/a&gt; and &lt;a href="https://www.splunk.com/"&gt;Splunk&lt;/a&gt;. These will automatically record any program errors and such.&lt;/p&gt;

&lt;p&gt;In particular, logging is very useful. It records a lot of information about what's happening in your program. This can help a lot with debugging.&lt;/p&gt;

&lt;p&gt;Alternatively, you can manually record information about errors.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  How to manually record errors
&lt;/h2&gt;

&lt;p&gt;The goal is to be able to view errors later. You can achieve that in any number of ways.&lt;/p&gt;

&lt;p&gt;One way is to manually record errors in a database.&lt;/p&gt;

&lt;p&gt;To do this, you can:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;prepare your database for recording errors&lt;/li&gt;
&lt;li&gt;set up some code to record errors to the database&lt;/li&gt;
&lt;li&gt;set up a global error handler to catch errors. (This error handler would call the code from step 2)&lt;/li&gt;
&lt;li&gt;set up an endpoint in the back end so the front end can record errors too. (This endpoint would call the code from step 2)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For example, you might use a &lt;a href="https://www.mongodb.com/"&gt;MongoDB database&lt;/a&gt; with a collection for errors. Every time an error occurs in your application, add information about it to the collection. You can organise the information in any way you like. For example, you could organise by the type of error or exception, by the error message, or by the last code in the call stack for the error.&lt;/p&gt;

&lt;p&gt;After setting that up, you could set up a global error handler. This error handler would be called on errors that occur in your application. In the handler, record the error in your database.&lt;/p&gt;

&lt;p&gt;Different frameworks and "environments" provide different ways to set up global error handlers. For example, in the Unity game engine, you can use &lt;code&gt;Application.logMessageReceived += Handler;&lt;/code&gt;. On the front end of a website, you can use &lt;code&gt;window.addEventListener('error', handler);&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Finally, you can set up an endpoint so that the front end can record errors too. Then, the front end can make a network request with information about the error it encountered.&lt;/p&gt;

&lt;p&gt;Here's an example call you could make from the front end:&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;handleError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;errorEvent&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="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;errorEvent&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;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://example.com/errors&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="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;error&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;handleError&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function &lt;code&gt;handleError&lt;/code&gt; above is executed any time an error occurs. It creates an object with useful information about the error. Then, it sends a network request to the back end. The back end will then record the information about the error so it can be viewed later.&lt;/p&gt;

&lt;h2&gt;
  
  
  What information to record
&lt;/h2&gt;

&lt;p&gt;You want as much useful information about the error as possible. This will help you debug it later.&lt;/p&gt;

&lt;p&gt;The article &lt;a href="https://docs.microsoft.com/en-us/dotnet/standard/exceptions/best-practices-for-exceptions"&gt;.NET best practices on exceptions&lt;/a&gt; has some guidelines for this. Adapted for both error values and exceptions, they are to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;use the predefined error types in your programming language if they're relevant. Only create custom types if the predefined ones don't apply.&lt;/li&gt;
&lt;li&gt;if you create custom error types:

&lt;ul&gt;
&lt;li&gt;they should usually be subclasses of the main error types (if you use an OOP language)&lt;/li&gt;
&lt;li&gt;they can optionally have custom properties (if they would be useful)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;use grammatically correct error messages. For example 'The file "foo.txt" could not be found.'.&lt;/li&gt;
&lt;li&gt;include a localised string message in every error (if your application is localised)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Final notes
&lt;/h2&gt;

&lt;p&gt;So that's it for this article. I hope that you found it useful.&lt;/p&gt;

&lt;p&gt;As always, if any points were missed, or if you disagree with anything, or have any comments or feedback then please leave a comment below.&lt;/p&gt;

&lt;p&gt;For the next steps, I recommend looking at the other articles in the &lt;a href="https://programmingduck.com/articles/errors"&gt;error handling series&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Alright, thanks and see you next time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Credits
&lt;/h2&gt;

&lt;p&gt;Recording photo - Photo by Krists Luhaers on Unsplash&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>devops</category>
    </item>
    <item>
      <title>How to respond to errors - Crashing, ignoring, recovering and their use cases</title>
      <dc:creator>Spyros Argalias</dc:creator>
      <pubDate>Tue, 02 Nov 2021 15:35:27 +0000</pubDate>
      <link>https://dev.to/programmingduck/how-to-respond-to-errors-crashing-ignoring-recovering-and-their-use-cases-25bo</link>
      <guid>https://dev.to/programmingduck/how-to-respond-to-errors-crashing-ignoring-recovering-and-their-use-cases-25bo</guid>
      <description>&lt;p&gt;What should a program do when it encounters errors? In my experience, a lot of programs hardly give any thought to this. Usually it's just the bare minimum of silently ignoring bugs and &lt;strong&gt;maybe&lt;/strong&gt; recording that they occurred.&lt;/p&gt;

&lt;p&gt;However, this is an important consideration in &lt;a href="https://programmingduck.com/articles/errors"&gt;error handling&lt;/a&gt;. A program should behave correctly when errors occur.&lt;/p&gt;

&lt;p&gt;In this article, we'll examine:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;possible responses to errors&lt;/li&gt;
&lt;li&gt;examples of error responses in common programs&lt;/li&gt;
&lt;li&gt;how to handle bugs in your program&lt;/li&gt;
&lt;li&gt;how to handle other errors in your program&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's dive in.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Response options
&lt;/h2&gt;

&lt;p&gt;There are different ways that you can respond to errors. You can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;crash the program&lt;/li&gt;
&lt;li&gt;silently ignore the error&lt;/li&gt;
&lt;li&gt;try to recover in some way&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Crashing the program is a good default option. There are multiple benefits to it.&lt;/p&gt;

&lt;p&gt;One benefit is that the program won't be doing the wrong thing. There have been multiple situations where software doing the wrong thing has been disastrous.&lt;/p&gt;

&lt;p&gt;This doesn't mean that crashing is good. It means that it's &lt;em&gt;probably&lt;/em&gt; better than silently ignoring bugs. For example, it's fine for a calculator program to crash. You can just restart it and continue what you were doing. But, if a calculator has errors and produces the wrong result, that can be a big problem.&lt;/p&gt;

&lt;p&gt;Another benefit to crashing is that it makes errors obvious. This means that you can debug the problem immediately.&lt;/p&gt;

&lt;p&gt;In addition, crashing as soon as possible means that the stack trace will be more helpful. It will point to the problem code. Instead, if you silently ignore the error, the program may not crash until later (if at all). At that point, it won't point to the real source of the error. This means that debugging will be harder.&lt;/p&gt;

&lt;p&gt;Another option is to silently ignore errors. Sometimes, this option is good. It depends on the user experience. Later on, we see a few examples where ignoring the error is probably the best option.&lt;/p&gt;

&lt;p&gt;The final option is to try to recover from the error in some way. The result should be as though the error never happened. The program should be able to continue executing correctly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NReNCP_P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aa6pdod0ywmizi8ce6tu.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NReNCP_P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aa6pdod0ywmizi8ce6tu.jpg" alt="" width="880" height="586"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Examples of responses to errors
&lt;/h2&gt;

&lt;p&gt;Here are some examples of how different programs may respond when encountering errors.&lt;/p&gt;

&lt;h3&gt;
  
  
  A program that's in early development
&lt;/h3&gt;

&lt;p&gt;If a program is in early development, it's probably fine to just crash on errors. This will make debugging easier.&lt;/p&gt;

&lt;h3&gt;
  
  
  Most desktop applications
&lt;/h3&gt;

&lt;p&gt;Most desktop applications crash when something goes wrong (if they can't recover in the background). This is usually fine. It's very easy for the user to start the program again.&lt;/p&gt;

&lt;p&gt;For programs that are "viewers" (such as Windows Photos), no data is lost.&lt;/p&gt;

&lt;p&gt;For programs which change data (such as Microsoft Word), very little tends to be lost. These programs tend to have autosave features to minimise lost data.&lt;/p&gt;

&lt;h3&gt;
  
  
  Error on "add to cart"
&lt;/h3&gt;

&lt;p&gt;Consider that you have an ecommerce website. There could be an error in the "add to cart" button where sometimes the product isn't being added to cart.&lt;/p&gt;

&lt;p&gt;How should that error be handled?&lt;/p&gt;

&lt;p&gt;For starters, you probably want to notify the user. It would be very bad if the user doesn't realise that a product is missing from their cart. They might go through checkout and order everything, wait for the item to arrive and never receive it. (I mean, I've done that without any errors to "add to cart" and I can tell you it's bad...)&lt;/p&gt;

&lt;p&gt;To notify the user, you could display a message to them. For example, you could tell them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the action failed&lt;/li&gt;
&lt;li&gt;that they should try again later&lt;/li&gt;
&lt;li&gt;that they should refresh the page and try again&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  A simple animation
&lt;/h3&gt;

&lt;p&gt;Consider that you have a simple website with a small animation that you trigger with JavaScript.&lt;/p&gt;

&lt;p&gt;For this website, if the animation fails to trigger, it's probably not a big deal.&lt;/p&gt;

&lt;p&gt;In this case, you essentially want to ignore the error silently. Don't notify the user that a problem occurred. The animation isn't important enough to notify and distract the user from what they're doing.&lt;/p&gt;

&lt;h3&gt;
  
  
  A single player video game
&lt;/h3&gt;

&lt;p&gt;If there is a bug in a single player video game, it probably doesn't matter. In most cases, it's very bad for the user experience for the game to crash. Having buggy gameplay is preferred to crashing. In this case, the best option is probably to silently ignore the bug.&lt;/p&gt;

&lt;h3&gt;
  
  
  An important system in an airplane
&lt;/h3&gt;

&lt;p&gt;For something life-critical, you would want to recover from errors very carefully and deliberately.&lt;/p&gt;

&lt;p&gt;This might mean having redundancy. For example, you might have backup systems so that one can take over if something goes wrong. Or you might have a live-monitoring program, which can restart and reinitialise other programs that have errored or crashed. Or any other number of things. You might also use &lt;a href="https://programmingduck.com/articles/defensive-programming"&gt;defensive programming&lt;/a&gt; to prevent certain programs from failing in the first place.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qaTFfSnW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iu7eeue0i3mo4zhvws0u.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qaTFfSnW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iu7eeue0i3mo4zhvws0u.jpg" alt="" width="880" height="587"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to handle bugs
&lt;/h2&gt;

&lt;p&gt;A bug is when something unexpected or obviously wrong happens in your program. It arises from faulty coding. It wouldn't be there if the code was correct.&lt;/p&gt;

&lt;p&gt;When handling bugs (or any error), you need to consider:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;whether the bug is recoverable or not&lt;/li&gt;
&lt;li&gt;the user experience&lt;/li&gt;
&lt;li&gt;the development time for different responses&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Also, regardless of what you do, you should &lt;a href="https://programmingduck.com/articles/error-recording"&gt;record errors to debug them later&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is the bug recoverable (without crashing)?
&lt;/h3&gt;

&lt;p&gt;Some bugs are impossible to recover from. For example, there's nothing you can do if some important code always fails. The only solution is to fix the code.&lt;/p&gt;

&lt;p&gt;However, some bugs may be recoverable.&lt;/p&gt;

&lt;p&gt;One example of possibly recoverable bugs are intermittent bugs. These are bugs that only occur under certain circumstances. This includes race conditions or errors that only happen with specific state.&lt;/p&gt;

&lt;p&gt;With some effort, you may be able to handle these without restarting the main program. For example, if an operation fails, you could:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;try to run the operation again. If the problem is a race condition, it may work next time.&lt;/li&gt;
&lt;li&gt;try to restart a faulty subprogram in the background. Then retry the operation afterwards.&lt;/li&gt;
&lt;li&gt;try to manually fix the state of the program to something that works&lt;/li&gt;
&lt;li&gt;offload the erroring operation to a server&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Another example may be something like running out of memory. Even this can be recoverable sometimes.&lt;/p&gt;

&lt;p&gt;However, one issue is that you may not know that your program has a particular bug. After all, if you knew about the bug, then the best solution would be to fix it. So, if you don't know about the bug, you might not have error handling for it.&lt;/p&gt;

&lt;p&gt;The exception is if you're doing &lt;a href="https://programmingduck.com/articles/defensive-programming"&gt;defensive programming&lt;/a&gt;. In this case, you'll have error handling "just in case". You won't actually know of whether you have a bug or not. Instead, you'll implement error handling pre-emptively for all kinds of possible bugs.&lt;/p&gt;

&lt;p&gt;So, in summary:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;some bugs aren't recoverable&lt;/li&gt;
&lt;li&gt;some bugs are recoverable but you won't have error handling for them&lt;/li&gt;
&lt;li&gt;some bugs are recoverable and you'll have error handling for them&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The user experience
&lt;/h3&gt;

&lt;p&gt;As shown in the examples above, different programs need to respond to errors differently. Sometimes, it's fine to crash the program. The user can restart it or another process can restart it automatically. At other times, you can silently ignore the error. In other cases, you may need to do everything in your power to recover.&lt;/p&gt;

&lt;h3&gt;
  
  
  Effort in implementing different error responses
&lt;/h3&gt;

&lt;p&gt;In general, even though some bugs are recoverable, recovery can be extremely difficult. Some of the issues with it are explained in &lt;a href="https://programmingduck.com/articles/defensive-programming"&gt;defensive programming&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In comparison, crashing the program is very easy. Also, this usually fixes errors just as well (if not better) than manually recovering.&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary of handling bugs
&lt;/h3&gt;

&lt;p&gt;Generally, when you encounter bugs, the rule of thumb is to crash the program. The most important reason for this is because defensive programming can be very difficult to implement.&lt;/p&gt;

&lt;p&gt;At other times, it's okay to ignore the bugs. For example, when the area of the codebase that errored is insignificant.&lt;/p&gt;

&lt;p&gt;Recovering from bugs is rare. It's only reserved for defensive programming. Defensive programming is mostly used for software where uptime and correctness are extremely valuable.&lt;/p&gt;

&lt;p&gt;Finally, regardless of what you do, remember to &lt;a href="https://programmingduck.com/articles/error-recording"&gt;record errors to debug them later&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--c-3mbsKg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gfz4b1nmx9zksdse7hla.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--c-3mbsKg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gfz4b1nmx9zksdse7hla.jpg" alt="" width="880" height="587"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to handle other errors
&lt;/h2&gt;

&lt;p&gt;"Other errors" are things which aren't bugs in your program. These can include things like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;failing to send a network request because the network connection dropped&lt;/li&gt;
&lt;li&gt;failing to read a file from the filesystem because the file was deleted by the user, manually, a few milliseconds ago&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These "errors" are normal and expected. They are things that any program may encounter. There is nothing that your program can do to completely prevent them.&lt;/p&gt;

&lt;p&gt;Once again, you need to consider:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;whether the error is recoverable or not&lt;/li&gt;
&lt;li&gt;the user experience&lt;/li&gt;
&lt;li&gt;the development time for different responses&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In general, many of these errors are both recoverable and easy to recover from.&lt;/p&gt;

&lt;p&gt;For example, consider that a network request failed to send because the user lost internet connection. It's relatively easy to show the user a message. Then, you can ask them try again after ensuring they're connected to the internet. This also results in a good user experience. In comparison, crashing the program would be a very bad user experience.&lt;/p&gt;

&lt;p&gt;As another example, you might try to write to a file that doesn't exist. In this case, an appropriate and easy solution might be to create the file first.&lt;/p&gt;

&lt;p&gt;So, overall, the common advice for these errors is to handle and recover from them. Of course, the details depend on your program. Some of these errors may be unrecoverable. In this case, crashing the program may be a better option.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final notes
&lt;/h2&gt;

&lt;p&gt;So that's it for this article. I hope that you found it useful.&lt;/p&gt;

&lt;p&gt;As always, if any points were missed, or if you disagree with anything, or have any comments or feedback then please leave a comment below.&lt;/p&gt;

&lt;p&gt;For the next steps, I recommend looking at the other articles in the &lt;a href="https://programmingduck.com/articles/errors"&gt;error handling series&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Alright, thanks and see you next time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Credits
&lt;/h2&gt;

&lt;p&gt;Image credits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Signs photo - Photo by Alexander Schimmeck on Unsplash&lt;/li&gt;
&lt;li&gt;Jenga photo - Photo by Michał Parzuchowski on Unsplash&lt;/li&gt;
&lt;li&gt;Person photo - Photo by Sebastian Herrmann on Unsplash&lt;/li&gt;
&lt;li&gt;Chess photo - Photo by Nothing Ahead from Pexels&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>programming</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>learning</category>
    </item>
    <item>
      <title>Naming (in code) - The ultimate guide and reference</title>
      <dc:creator>Spyros Argalias</dc:creator>
      <pubDate>Mon, 21 Jun 2021 15:04:27 +0000</pubDate>
      <link>https://dev.to/programmingduck/naming-in-code-the-ultimate-guide-and-reference-1hhe</link>
      <guid>https://dev.to/programmingduck/naming-in-code-the-ultimate-guide-and-reference-1hhe</guid>
      <description>&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/MsqQCfq5xNY"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;a href="https://programmingduck.com/articles/programming-principles" rel="noopener noreferrer"&gt;Programming principles&lt;/a&gt; tell us that naming in code is important. This article aims to be a complete learner's guide for naming in code. It also aims to be a reference about naming that you can refer to in the future if you need to.&lt;/p&gt;

&lt;p&gt;For certain things, such as naming methods, there are different naming conventions. We'll mention a few so that you're aware of how they work and the options you can choose from.&lt;/p&gt;

&lt;p&gt;Overall, we'll examine:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;the motivation for wanting good names&lt;/li&gt;
&lt;li&gt;general naming tips for all code&lt;/li&gt;
&lt;li&gt;established naming conventions for specific things such as variables and classes&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Motivation for good names
&lt;/h2&gt;

&lt;p&gt;The motivation for good names comes from clean code and programming principles. Code should be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;immediately obvious and easy to understand&lt;/li&gt;
&lt;li&gt;easy to change&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Being easy to understand helps because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;code that's easy to understand will be faster to understand. This means that you can work faster. You'll spend less time trying to understand old code and more time writing new code.&lt;/li&gt;
&lt;li&gt;code will be possible to understand. In comparison, if some code is difficult to understand, you may not understand it even after spending a long time reading it. Someone who's less experienced may have even worse luck. Additionally, you may misunderstand how the code works, especially if you're not fully paying attention that day. Misunderstanding code makes it very easy to create bugs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Good names help with both cases.&lt;/p&gt;

&lt;h3&gt;
  
  
  How good names make code faster to understand
&lt;/h3&gt;

&lt;p&gt;If something is well-named, then you don't need further details to understand it. This saves you time.&lt;/p&gt;

&lt;p&gt;For example, consider:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a function named &lt;code&gt;printHelloToTheScreen&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;a function named &lt;code&gt;multiplyBy2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;a constant named &lt;code&gt;PI&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;a constant named &lt;code&gt;MAXIMUM_ALLOWED_LOGIN_ATTEMPTS&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;a static class or module named &lt;code&gt;Math&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;a variable named &lt;code&gt;circumference&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;a variable named &lt;code&gt;userInfo&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In a codebase, you would have a pretty good idea of what those do just from the name.&lt;/p&gt;

&lt;h3&gt;
  
  
  How good names make code easier to understand
&lt;/h3&gt;

&lt;p&gt;When reading code, you have to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;read (parse) what it does&lt;/li&gt;
&lt;li&gt;understand why it does it, or rather, understand what it's trying to do conceptually&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For example, it's not enough to think "this code removes initial whitespace from the string, then it sends a network request". Instead, you have to understand "this code formats the username and then sends a password reset request".&lt;/p&gt;

&lt;p&gt;Here's a code example that's difficult to understand:&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="nf"&gt;foo&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="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;PI&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;c&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="nx"&gt;c&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What's the &lt;code&gt;8&lt;/code&gt; there for? What's &lt;code&gt;b&lt;/code&gt;? Why does it return a minimum of &lt;code&gt;1&lt;/code&gt;? What's the purpose of this function?&lt;/p&gt;

&lt;p&gt;If you're trying to do something such as change the area, you don't know if this function is relevant or not. Even if you suspect it is, you don't know what it does or why.&lt;/p&gt;

&lt;p&gt;Something like this would be much better:&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="nf"&gt;calculateRemainingArea&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;radius&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reductionFactor&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;area&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;PI&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;radius&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;const&lt;/span&gt; &lt;span class="nx"&gt;remainingArea&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;area&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;AREA_REDUCTION_RESISTANCE&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;reductionFactor&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;remainingArea&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;MINIMUM_AREA&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;So, good names help, because &lt;strong&gt;they provide meaning&lt;/strong&gt;. They help you understand what the code does and why it does it.&lt;/p&gt;

&lt;p&gt;To get the benefit, all you have to do is give a good name to the thing.&lt;/p&gt;

&lt;p&gt;As another example, someone may not understand &lt;code&gt;PI * a ** 2&lt;/code&gt;. They may think "&lt;code&gt;PI&lt;/code&gt;, &lt;code&gt;a&lt;/code&gt;, power of 2... What the heck is that??". To make it easier, all you have to do is replace that line with &lt;code&gt;const circleArea = PI * radius ** 2&lt;/code&gt;. It helps a lot.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  General naming tips for code
&lt;/h2&gt;

&lt;p&gt;In short, a good name is something that immediately tells you what something is or does. It's not surprising. It doesn't take effort to understand it. You understand it without thinking about it.&lt;/p&gt;

&lt;p&gt;A bad name is something that you read and you wonder "what's that", or "what does that do?". Something that requires you to think more.&lt;/p&gt;

&lt;p&gt;When writing names, they should be understandable by someone that's never seen this code before. They need to make sense, be consistent and be sufficiently descriptive.&lt;/p&gt;

&lt;p&gt;Here are some pointers to accomplish this.&lt;/p&gt;

&lt;h3&gt;
  
  
  Have clear naming conventions
&lt;/h3&gt;

&lt;p&gt;Your codebase should have clear conventions. This is probably the most important point in this article. If you're already following a convention, it's probably best to stick to it, even if an alternative may be more descriptive.&lt;/p&gt;

&lt;p&gt;Conventions apply consistency. &lt;a href="https://programmingduck.com/articles/programming-principles" rel="noopener noreferrer"&gt;Programming principles&lt;/a&gt; tell us that consistency is very important.&lt;/p&gt;

&lt;p&gt;It allows you to work faster. You can make certain assumptions about how the code works and what certain things mean. Additionally, when thinking of a name for something, the convention may already have a rule for it. This makes things easier.&lt;/p&gt;

&lt;p&gt;Also, not following conventions can cause mistakes. You may assume that a common convention is being followed in the codebase. If you're wrong, then you may have the wrong idea about how some code works. At that point, it's very easy to create bugs.&lt;/p&gt;

&lt;p&gt;Ideally, you should follow conventions that already exist in your programming language or framework. This makes things easier for new developers to the company. Some examples are the &lt;a href="https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/naming-guidelines" rel="noopener noreferrer"&gt;.NET naming guidelines&lt;/a&gt; and &lt;a href="https://github.com/airbnb/javascript" rel="noopener noreferrer"&gt;Airbnb JavaScript style guide&lt;/a&gt;. Otherwise, you can also create your own custom conventions for your project or company.&lt;/p&gt;

&lt;h3&gt;
  
  
  Prefer descriptive names
&lt;/h3&gt;

&lt;p&gt;Always consider, will the next person who looks at this name understand it easily?&lt;/p&gt;

&lt;h4&gt;
  
  
  Use full words
&lt;/h4&gt;

&lt;p&gt;Generally, full words are easier to understand. For example &lt;code&gt;calculateArea&lt;/code&gt; or &lt;code&gt;createWindow&lt;/code&gt;. Abbreviated words may be harder to understand. In general, avoid names like &lt;code&gt;calcArea&lt;/code&gt; or &lt;code&gt;cWin&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Avoid very short variable names
&lt;/h4&gt;

&lt;p&gt;In particular, avoid single-letter or very short variables, such as &lt;code&gt;d&lt;/code&gt;. It could mean anything: the &lt;code&gt;document&lt;/code&gt;, &lt;code&gt;data&lt;/code&gt;, &lt;code&gt;date&lt;/code&gt;, or &lt;code&gt;days&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The exception to this is when the name is used in a very small scope and what it represents is immediately obvious. In this case, it's easy to look one or two lines up and see where it was defined. Here's a code example using the variable &lt;code&gt;e&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="nf"&gt;sendData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* sends a network request */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleFormSubmission&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&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;formElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&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;formData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FormData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;formElement&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;sendData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;formData&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;Here, the variable &lt;code&gt;e&lt;/code&gt; represents the event object. We know this because the name &lt;code&gt;handleFormSubmission&lt;/code&gt; and the function signature represent an event handler. &lt;code&gt;e&lt;/code&gt; is only used one line under where it is defined, so it's still easy to work with.&lt;/p&gt;

&lt;p&gt;Nevertheless, I would still personally use &lt;code&gt;event&lt;/code&gt;. I consider readability more important than minor saving of keystrokes like that.&lt;/p&gt;

&lt;p&gt;Another acceptable example is using &lt;code&gt;i&lt;/code&gt; in for loops and such. That's a convention that everyone understands.&lt;/p&gt;

&lt;h4&gt;
  
  
  Acronyms
&lt;/h4&gt;

&lt;p&gt;If an acronym is very common, then it's okay to use it. Some examples are the acronyms HTML, UI, IO, OS, JSON, XML, HTTP.&lt;/p&gt;

&lt;p&gt;If an acronym is uncommon, then prefer the fully expanded name. If in doubt, you should probably use the full version.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conciseness matters
&lt;/h3&gt;

&lt;p&gt;Names that are sufficiently descriptive come first. However, that doesn't mean that you need super long names.&lt;/p&gt;

&lt;p&gt;Super long names, such as a variable named &lt;code&gt;integerNumberOfEnginesInObject&lt;/code&gt; are difficult to work with. &lt;code&gt;engineCount&lt;/code&gt; would be sufficient.&lt;/p&gt;

&lt;p&gt;In general, if you can have the same clarity with a shorter name, use the shorter name.&lt;/p&gt;

&lt;h3&gt;
  
  
  Consider the context
&lt;/h3&gt;

&lt;p&gt;The name of the surrounding context can provide useful clues for the purpose of something. This means that sometimes, you can get away with using shorter names.&lt;/p&gt;

&lt;p&gt;For example, if you have a class called &lt;code&gt;Users&lt;/code&gt;, then the method for creating a new user can be called &lt;code&gt;create&lt;/code&gt;. The usage of it would be something like &lt;code&gt;users.create()&lt;/code&gt; (where &lt;code&gt;users&lt;/code&gt; is an instance of &lt;code&gt;Users&lt;/code&gt;). That's sufficiently descriptive. You don't need to call the method &lt;code&gt;createUser&lt;/code&gt; in this case.&lt;/p&gt;

&lt;h3&gt;
  
  
  Casing
&lt;/h3&gt;

&lt;p&gt;The prevalent casings in programming (excluding HTML and CSS) are pascal case, camel case and snake case. The one you use depends on the conventions for your programming language or framework.&lt;/p&gt;

&lt;h4&gt;
  
  
  General format of each casing
&lt;/h4&gt;

&lt;p&gt;Snake casing is lower cased and uses underscores to separate words. For example &lt;code&gt;this_is_snake_case&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In pascal casing, every word begins with a capital letter. For example &lt;code&gt;ThisIsPascalCase&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Camel casing is similar to pascal casing, except that the first word begins with a lowercase letter. For example &lt;code&gt;thisIsCamelCase&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Casing for acronyms
&lt;/h4&gt;

&lt;p&gt;For acronyms, the conventions vary.&lt;/p&gt;

&lt;p&gt;On the front end, it seems that acronyms are always fully capitalised, regardless of length. Some examples are &lt;code&gt;performance.toJSON()&lt;/code&gt;, &lt;code&gt;XMLDocument&lt;/code&gt;, &lt;code&gt;HTMLCollection&lt;/code&gt; and &lt;code&gt;DOMString&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;However, in some other languages, such as the .NET languages, the convention is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;if an acronym is only two letters long, the second letter should have the same casing (upper or lower case) as the first one. For example, &lt;code&gt;UIInput&lt;/code&gt; (pascal case), &lt;code&gt;uiInput&lt;/code&gt; (camel case) and &lt;code&gt;fooUIInput&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;if an acronym is three letters or longer, only the first letter should be capitalised if necessary. For example &lt;code&gt;JsonFoo&lt;/code&gt; (pascal case), &lt;code&gt;jsonFoo&lt;/code&gt; (camel case).&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Capitalisation for compound words
&lt;/h4&gt;

&lt;p&gt;It's a convention to treat compound words as one word in terms of capitalisation. For example, use &lt;code&gt;callback&lt;/code&gt; and &lt;code&gt;endpoint&lt;/code&gt; instead of &lt;code&gt;callBack&lt;/code&gt; and &lt;code&gt;endPoint&lt;/code&gt;. You can find a thorough list of common compound words, used in programming, on the &lt;a href="https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/capitalization-conventions" rel="noopener noreferrer"&gt;.NET naming guidelines on capitalisation&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Prefer semantic naming
&lt;/h3&gt;

&lt;p&gt;Semantic naming means to name something after its purpose or meaning. In order of priority, this means:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;what the purpose of it is, or what it does&lt;/li&gt;
&lt;li&gt;how it does it&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As a bonus, it also results in code that is less likely to change in the future.&lt;/p&gt;

&lt;p&gt;When naming things, consider: what's the most important thing about the thing you're naming? What's the most suitable name for someone else to understand it conceptually?&lt;/p&gt;

&lt;p&gt;Usually, users care more about what something is doing, rather than how it's doing it. Although, sometimes, it's the "how" that's the most important.&lt;/p&gt;

&lt;p&gt;Here are some examples of semantic naming.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example with a variable name
&lt;/h4&gt;

&lt;p&gt;If you have a variable that holds a collection of users, the important part is that it contains users. Whether it's a list or a map is less important. Not to mention that your IDE and type declarations provide that information anyway.&lt;/p&gt;

&lt;p&gt;Therefore, the name:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;users&lt;/code&gt; would be suitable&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;userList&lt;/code&gt; is less preferable. The "list" part is less important than the &lt;code&gt;users&lt;/code&gt; part. Also, if you change the data structure in the future you'll have to update the variable name.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;userCollection&lt;/code&gt; is also less preferable, because the "collection" part is less important than the "user" part. However, at least you won't have to update the variable name if you change the data structure in the future.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Example with interfaces and implementations
&lt;/h4&gt;

&lt;p&gt;In OOP code, you tend to have interfaces with potentially multiple implementations. An interface may be called something semantic and generic, such as &lt;code&gt;Sorter&lt;/code&gt;, for a sorting algorithm. That's the significant thing about it, the fact that it sorts. The how isn't important at the interface level.&lt;/p&gt;

&lt;p&gt;The different implementations can be called after the sorting algorithm they implement. That's the important thing about them and the only thing that separates them from one another. It's the information a user of that specific class would want. For example &lt;code&gt;BubbleSort&lt;/code&gt;, &lt;code&gt;MergeSort&lt;/code&gt;, &lt;code&gt;Quicksort&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example with a method for sorting
&lt;/h4&gt;

&lt;p&gt;Imagine that you have a class containing a collection of something. You also have a method for sorting it, say, alphabetically.&lt;/p&gt;

&lt;p&gt;If you only have one sorting method, then it would be preferable to name it &lt;code&gt;sort&lt;/code&gt;. A name such as &lt;code&gt;sortWithQuicksort&lt;/code&gt; or &lt;code&gt;quicksort&lt;/code&gt; would be unnecessary information that the user doesn't care about. If you think about it, the caller of the code wants to sort. They aren't particularly interested in the specific algorithm your class uses. (The only exception is if your class is an actual bottleneck to performance or something, but that's a different topic.)&lt;/p&gt;

&lt;p&gt;Additionally, in the future, you may change the sorting algorithm that the class uses to merge sort. In that case, the name &lt;code&gt;sortWithQuicksort&lt;/code&gt; wouldn't make sense any more.&lt;/p&gt;

&lt;p&gt;The solution is to name your public method &lt;code&gt;sort&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The fact that you're using quicksort is still important. Developers working on the implementation of the class would want to know that. Some options for that are to have a private method named &lt;code&gt;quicksort&lt;/code&gt;, or import and use a &lt;code&gt;quicksort&lt;/code&gt; function from somewhere else in the codebase.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// pseudocode&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Users&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// the public method with a name that's useful to callers&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="nf"&gt;Sort&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="nf"&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="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// the private method with a name that's userful to someone working on this class&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;_quicksort&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/* implementation of quicksort */&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;h4&gt;
  
  
  Example with front end CSS
&lt;/h4&gt;

&lt;p&gt;In front end CSS, there are a few approaches. The approach you choose decides what's important about the name of your class.&lt;/p&gt;

&lt;p&gt;When using the BEM naming convention, the important thing is the purpose of the element, not the styling. For example, you would use the CSS class &lt;code&gt;page-heading&lt;/code&gt; instead of &lt;code&gt;large-heading-red&lt;/code&gt;. That's because, tomorrow, the styling of the page heading may change. At that point, the non-semantic name won't make sense anymore, but the semantic name will.&lt;/p&gt;

&lt;p&gt;If you're using a UI component library, then the styling of the component is more important than the purpose of the component. For example &lt;code&gt;button-primary&lt;/code&gt; instead of &lt;code&gt;add-to-cart-button&lt;/code&gt;. That's because classes like &lt;code&gt;button-primary&lt;/code&gt; are the main ones you'll be working with throughout your code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Other tips from Uncle Bob
&lt;/h3&gt;

&lt;p&gt;Some tips that are mentioned in &lt;a href="https://www.oreilly.com/library/view/clean-code-a/9780136083238/" rel="noopener noreferrer"&gt;Uncle Bob's book Clean Code&lt;/a&gt; are to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;make meaningful distinctions. Avoid variables that seem similar or the same, such as &lt;code&gt;accountInfo&lt;/code&gt; or &lt;code&gt;accountData&lt;/code&gt;. A user of that code won't be able to tell the difference.&lt;/li&gt;
&lt;li&gt;use pronounceable names. Names should be easy to read out loud. Some examples to avoid are names such as &lt;code&gt;genymdhms&lt;/code&gt; and &lt;code&gt;modymdhms&lt;/code&gt;. Better names would be &lt;code&gt;generationTimestamp&lt;/code&gt; and &lt;code&gt;modificationTimestamp&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;use searchable names. Names should be easy to search using your code editor. Essentially, avoid names that are one or two letters long, as searching for them will return too many matches.&lt;/li&gt;
&lt;li&gt;avoid using cute / offensive words. Avoid names such as &lt;code&gt;whack()&lt;/code&gt; and &lt;code&gt;seeYouLaterFiles()&lt;/code&gt;. Be professional and consistent.&lt;/li&gt;
&lt;li&gt;pick one word per concept. For example, don't mix the words "fetch", "get" and "retrieve" for the same kind of operation throughout your codebase. Pick one of them and use it consistently.&lt;/li&gt;
&lt;li&gt;avoid puns. Avoid using the same word for different concepts. For example, if in one class &lt;code&gt;add()&lt;/code&gt; adds two numbers, in a different class &lt;code&gt;add()&lt;/code&gt; shouldn't insert into a list.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Other tips from the .NET naming conventions
&lt;/h3&gt;

&lt;p&gt;.NET makes some additional recommendations. Some of them are specifically for .NET languages. However, you may want to keep them in mind anyway.&lt;/p&gt;

&lt;p&gt;The recommendations are to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;prefer naturally readable names. For example, use &lt;code&gt;HorizontalAlignment&lt;/code&gt; instead of &lt;code&gt;AlignmentHorizontal&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;avoid using names that conflict with common keywords in other programming languages&lt;/li&gt;
&lt;li&gt;prefer semantic names rather than language specific names. For example, use &lt;code&gt;GetLength&lt;/code&gt; instead of &lt;code&gt;GetInt&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;prefer generic CLR type names instead of language-specific names. For example name a method &lt;code&gt;ToInt64&lt;/code&gt; instead of &lt;code&gt;ToLong&lt;/code&gt;. That's because methods like these may be used in other CLR-compatible languages where the data type &lt;code&gt;long&lt;/code&gt; doesn't exist. Therefore, that name won't make sense in those languages. However, &lt;code&gt;Int64&lt;/code&gt; exists and makes sense for all CLR languages.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;h2&gt;
  
  
  Naming conventions for specific use cases
&lt;/h2&gt;

&lt;p&gt;Here are some common naming conventions for things such as variables, functions, classes, etc.&lt;/p&gt;

&lt;h3&gt;
  
  
  Variables
&lt;/h3&gt;

&lt;p&gt;Variables are just names or labels for values and objects. Some general conventions for them are that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;they should be nouns. For example &lt;code&gt;car&lt;/code&gt;, &lt;code&gt;user&lt;/code&gt;, &lt;code&gt;userLocation&lt;/code&gt;, &lt;code&gt;student&lt;/code&gt;, &lt;code&gt;result&lt;/code&gt;, &lt;code&gt;area&lt;/code&gt;, &lt;code&gt;player&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;they generally use camel case or snake case, depending on your programming language&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The rules change a bit for constants and variables holding Boolean values.&lt;/p&gt;

&lt;h4&gt;
  
  
  Constants
&lt;/h4&gt;

&lt;p&gt;Some programming languages write constants fully capitalised and with snake case. This includes JavaScript, Python and Java. Some example constants are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Number.EPSILON&lt;/li&gt;
&lt;li&gt;Math.PI&lt;/li&gt;
&lt;li&gt;NUMBER_OF_LIVES&lt;/li&gt;
&lt;li&gt;MAX_ATTEMPTS_BEFORE_LOCKOUT&lt;/li&gt;
&lt;li&gt;SOME_SPECIAL_VALUE&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here, "constant" refers to special values. These are values that don't depend on runtime. They could easily be placed in a configuration file, away from your application code.&lt;/p&gt;

&lt;p&gt;"Constant" doesn't refer to a normal local variable that just happens to be immutable. Those follow the same conventions as normal variables.&lt;/p&gt;

&lt;p&gt;Here are some examples of "constants" and "normal variables that just happen to be immutable":&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;final MAX_ATTEMPS_BEFORE_LOCKOUT = 5&lt;/code&gt; (this could be in a configuration file, it's a special constant)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;final float result = SomeClass.factorial(someNumber)&lt;/code&gt; (this is just a local variable that happens to be immutable. It varies at runtime depending on &lt;code&gt;someNumber&lt;/code&gt;. It can't be placed in a configuration file)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;const URL_ENDPOINT = '/ajax/foo'&lt;/code&gt; (this could be in a configuration file, it's a special constant)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;const filteredArray = array.filter(a =&amp;gt; !a)&lt;/code&gt; (this is just a local variable that happens to be immutable. It varies at runtime depending on &lt;code&gt;array&lt;/code&gt;. It can't be placed in a configuration file)&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Booleans
&lt;/h4&gt;

&lt;p&gt;For variables that have Boolean values, the convention is to phrase them as a question. Start them with a predicate such as "is", "has", "had" and "can". These clearly denote that the variable holds a Boolean.&lt;/p&gt;

&lt;p&gt;Some example Boolean variables are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;isValid&lt;/li&gt;
&lt;li&gt;isComplete&lt;/li&gt;
&lt;li&gt;isActive&lt;/li&gt;
&lt;li&gt;isDisabled&lt;/li&gt;
&lt;li&gt;isLoading&lt;/li&gt;
&lt;li&gt;hasRan&lt;/li&gt;
&lt;li&gt;hasArrived&lt;/li&gt;
&lt;li&gt;hasDescendants&lt;/li&gt;
&lt;li&gt;canTakeDamage&lt;/li&gt;
&lt;li&gt;canDrive&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In addition to being readable standalone, they read nicely in conditional statements:&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;hasLoaded&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;doSomething&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="nf"&gt;doSomethingElse&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;In comparison, if you don't use a predicate, you would use a name such as &lt;code&gt;complete&lt;/code&gt;. This is both a verb and an adjective. It can denote a number of things. It could be a function that you run to complete something, or a variable that holds things that have already completed, or a Boolean that states whether something has completed or not, or an event name. What it represents is more ambiguous, so prefer predicates instead.&lt;/p&gt;

&lt;p&gt;A verb like &lt;code&gt;completing&lt;/code&gt; is slightly better. It can't denote a function, because the "ing" in "completing" means that something is already happening. It's not something that you can start running now (like a function call). However, it can still be any of the other options. Overall, it's still preferable to use predicates.&lt;/p&gt;

&lt;h3&gt;
  
  
  Functions
&lt;/h3&gt;

&lt;p&gt;Functions are units of code that do something. Conventions for function names are that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;they should be verbs&lt;/li&gt;
&lt;li&gt;they generally use camel case or snake case, depending on your programming language&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some example function names are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;createReport&lt;/li&gt;
&lt;li&gt;createTodo&lt;/li&gt;
&lt;li&gt;getX&lt;/li&gt;
&lt;li&gt;divide&lt;/li&gt;
&lt;li&gt;sendRequest&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For functions that return a Boolean, a common convention is for them to start with a predicate. This is similar to variables that contain Boolean values.&lt;/p&gt;

&lt;p&gt;Some example names for functions that return Booleans are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;isValid&lt;/li&gt;
&lt;li&gt;hasSignedIn&lt;/li&gt;
&lt;li&gt;isSignedIn&lt;/li&gt;
&lt;li&gt;isFormatted&lt;/li&gt;
&lt;li&gt;canDrive&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Another convention I've seen is for "transformer" or "converter" functions. These are functions that convert from one thing to another. They usually begin with "to", followed by the type they're converting to.&lt;/p&gt;

&lt;p&gt;Some examples of transformer function names are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;toLowerCase&lt;/li&gt;
&lt;li&gt;toUpperCase&lt;/li&gt;
&lt;li&gt;toArray&lt;/li&gt;
&lt;li&gt;toString&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Classes
&lt;/h3&gt;

&lt;p&gt;Classes are units of code that contain methods and attributes.&lt;/p&gt;

&lt;p&gt;Some conventions for classes are that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;they use Pascal case&lt;/li&gt;
&lt;li&gt;they should be nouns (or noun phrases)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some example class names are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Observable&lt;/li&gt;
&lt;li&gt;Student&lt;/li&gt;
&lt;li&gt;Printer&lt;/li&gt;
&lt;li&gt;Player&lt;/li&gt;
&lt;li&gt;ImageSprite&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  General property names
&lt;/h4&gt;

&lt;p&gt;In general, attributes are named similarly to variables. Methods are named similarly to functions.&lt;/p&gt;

&lt;p&gt;However, as mentioned earlier, the name of the class provides some context.&lt;/p&gt;

&lt;p&gt;This means that you may be able to use a less specific name for a method / attribute than you would use for the equivalent function / variable.&lt;/p&gt;

&lt;p&gt;For example, inside a class &lt;code&gt;Users&lt;/code&gt;, you may have a method named &lt;code&gt;create&lt;/code&gt;. The usage of it would be something like &lt;code&gt;users.create()&lt;/code&gt; (where &lt;code&gt;users&lt;/code&gt; is an instance of &lt;code&gt;Users&lt;/code&gt;), which is sufficiently descriptive.&lt;/p&gt;

&lt;h4&gt;
  
  
  Casing and prefixing for properties
&lt;/h4&gt;

&lt;p&gt;In terms of casing and prefixes, different languages and frameworks have different conventions.&lt;/p&gt;

&lt;p&gt;For example, the &lt;a href="https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html" rel="noopener noreferrer"&gt;Java naming conventions&lt;/a&gt; mention that methods and attributes should be camel cased.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://www.python.org/dev/peps/pep-0008/" rel="noopener noreferrer"&gt;PEP 8 Python naming convention&lt;/a&gt; also mentions camel casing. However, it adds that private properties should be prefixed with an underscore. For example (&lt;code&gt;_privateProperty&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/coding-conventions" rel="noopener noreferrer"&gt;C# coding conventions&lt;/a&gt; seem the strictest. They state that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;public properties should be pascal cased (for example &lt;code&gt;PublicMethod&lt;/code&gt; or &lt;code&gt;PublicVariable&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;private or internal properties should be prefixed with underscore and be camel cased (for example &lt;code&gt;private void _privateMethod&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;private or internal static properties should be prefixed with &lt;code&gt;s_&lt;/code&gt; and be camel cased (for example &lt;code&gt;private static foo s_workerQueue&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;private or internal thread properties should be prefixed with &lt;code&gt;t_&lt;/code&gt; and be camel cased (for example &lt;code&gt;private static foo t_timeSpan&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If, for whatever reason, you were creating your own convention, I would personally recommend:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;pascal casing for methods and camel casing for attributes (this is the convention used in the Unity game engine). The reason for this is be to differentiate between Boolean attributes (such as &lt;code&gt;isValid&lt;/code&gt;) and methods that return Booleans (such as &lt;code&gt;IsValid(data)&lt;/code&gt;). As a second choice, I would use camel case for methods.&lt;/li&gt;
&lt;li&gt;underscore prefix for non-public properties&lt;/li&gt;
&lt;li&gt;possibly (not sure yet) the &lt;code&gt;s&lt;/code&gt; and &lt;code&gt;t&lt;/code&gt; prefixes from C# for the relevant things. As the &lt;a href="https://www.python.org/dev/peps/pep-0020/" rel="noopener noreferrer"&gt;Zen of Python&lt;/a&gt; says "Namespaces are one honking great idea -- let's do more of those!" (I consider prefixes to have a similar effect)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Interfaces
&lt;/h3&gt;

&lt;p&gt;Names for interfaces are similar to class names. They use pascal case and are usually nouns. Sometimes they can be named with an adjective, for example "Readable".&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.oreilly.com/library/view/clean-code-a/9780136083238/" rel="noopener noreferrer"&gt;In the book Clean Code&lt;/a&gt;, Uncle Bob recommends avoiding the &lt;code&gt;I&lt;/code&gt; prefix for interfaces. The &lt;a href="https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html" rel="noopener noreferrer"&gt;Java naming conventions&lt;/a&gt; recommend the same. For example &lt;code&gt;interface Foo&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The C# coding conventions recommend prefixing interfaces with I. For example &lt;code&gt;interface IFoo&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Personally, I have a minor preference for avoiding the prefix. That's because, as a user of the code, I'm not particularly interested on whether I'm working with an interface or a class.&lt;/p&gt;

&lt;h3&gt;
  
  
  Enums
&lt;/h3&gt;

&lt;p&gt;The Java convention is to treat enums like classes with constants. This means to name the enum type with pascal case and the fields fully uppercase.&lt;/p&gt;

&lt;p&gt;C# also treats them the same as classes. This means to name both the enum type and the fields using pascal case.&lt;/p&gt;

&lt;p&gt;Personally, I prefer the Java convention because it differentiates between constants and other values.&lt;/p&gt;

&lt;h3&gt;
  
  
  Events, event handlers, messaging systems and commands
&lt;/h3&gt;

&lt;p&gt;There are a few things to consider regarding events and their relevant functions.&lt;/p&gt;

&lt;h4&gt;
  
  
  Event names
&lt;/h4&gt;

&lt;p&gt;Event names:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;refer to an action&lt;/li&gt;
&lt;li&gt;are normally named using verbs. For example "downloaded", "loaded", "deleted", "damaged", "moved".&lt;/li&gt;
&lt;li&gt;generally use present tense for events that fire before an action starts. For example, before submitting a password reset request, you might fire an event named "passwordResetRequestSubmitting".&lt;/li&gt;
&lt;li&gt;generally use past tense for events that fire after an action completes. For example, after submitting a password reset request, you might fire an event named "passwordResetRequestSubmitted".&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In my experience, these conventions are common across many languages. However, there isn't much official documentation for them. One exception is with &lt;a href="https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/names-of-type-members" rel="noopener noreferrer"&gt;.NET, where they formally state these guidelines&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For the actual name of the event, you can use whatever makes sense to you. If your event is created in a class, the class name may provide sufficient context. For example, if you have a class &lt;code&gt;Shape&lt;/code&gt;, you may have an event named &lt;code&gt;areaChanged&lt;/code&gt;. The usage would be something like &lt;code&gt;myShape.areaChanged += someHandler&lt;/code&gt; or &lt;code&gt;myShape.areaChanged.subscribe(someHandler)&lt;/code&gt;. The event name "areaChanged" is sufficiently descriptive in this case.&lt;/p&gt;

&lt;p&gt;In terms of casing, follow the convention in your programming language. C# uses pascal casing for public members (including events). Most other languages use camel casing.&lt;/p&gt;

&lt;p&gt;Some example event names are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;fileDownloading / fileDownloaded&lt;/li&gt;
&lt;li&gt;userRegistering / userRegistered&lt;/li&gt;
&lt;li&gt;uiElementXUpdating / uiElementXUpdated&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Event handlers
&lt;/h4&gt;

&lt;p&gt;For event handler functions, there are a few conventions. In front end, &lt;a href="https://reactjs.org/docs/handling-events.html" rel="noopener noreferrer"&gt;React tends to use the prefix "handle"&lt;/a&gt;. C# recommends the suffix "EventHandler". Other languages may have other conventions.&lt;/p&gt;

&lt;p&gt;Some example names of event handler functions are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;handleUserSubscribed / userSubscribedHandler / UserSubscribedEventHandler&lt;/li&gt;
&lt;li&gt;handleFileDownloaded / fileDownloadedHandler / FileDownloadedEventHandler&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;My personal preference is to use the "handle" prefix. This keeps the function a verb, which is the convention for functions.&lt;/p&gt;

&lt;h4&gt;
  
  
  "On" functions
&lt;/h4&gt;

&lt;p&gt;Functions that exist solely to raise events tend to have the name &lt;code&gt;on&amp;lt;EventName&amp;gt;&lt;/code&gt;. For example "onAreaChanged".&lt;/p&gt;

&lt;p&gt;One case for using these is in classes that may be derived.&lt;/p&gt;

&lt;p&gt;Here's a C# example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Foo&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;event&lt;/span&gt; &lt;span class="n"&gt;EventHandler&lt;/span&gt; &lt;span class="n"&gt;FileDownloaded&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OnFileDownloaded&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;EventArgs&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;EventHandler&lt;/span&gt; &lt;span class="n"&gt;handler&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;FileDownloaded&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="n"&gt;handler&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;FileDownloaded&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="n"&gt;e&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;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Something&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;OnFileDownloaded&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;EventArgs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Empty&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;Here, we have a method named &lt;code&gt;OnFileDownloaded&lt;/code&gt; whose only purpose is to raise the event.&lt;/p&gt;

&lt;p&gt;This convention may work a bit differently in front end, but it still follows the general idea. For example, in React, you can name props something like "onClick" or "onSomeEvent". Event handler functions that you define inside a component can use the "handle" prefix.&lt;/p&gt;

&lt;p&gt;Here's a React example:&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&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;ParentComponent&lt;/span&gt; &lt;span class="o"&gt;=&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleButtonClicked&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* do something */&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;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ChildComponent&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleButtonClicked&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="err"&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;ChildComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;onClick&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;clicked&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;foo&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="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleClick&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;Click&lt;/span&gt; &lt;span class="nx"&gt;me&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, each component creates event handler functions prefixed with "handle". The child component has the prop named "onClick". In the child component, the &lt;code&gt;handleClick&lt;/code&gt; function calls the &lt;code&gt;onClick&lt;/code&gt; prop.&lt;/p&gt;

&lt;h4&gt;
  
  
  More global message names
&lt;/h4&gt;

&lt;p&gt;PubSub (message bus) and analytics have similarities to local events, but they are more global. They span a much larger area of the codebase. The can even span multiple applications (as might be the case for analytics).&lt;/p&gt;

&lt;p&gt;With these, it's important to use more specific names, otherwise you won't know what they refer to.&lt;/p&gt;

&lt;p&gt;A good way to do this is to use namespaces and prefixes, along with specified delimiters.&lt;/p&gt;

&lt;p&gt;For example, with PubSub event names, you can have a namespace for the relevant area of the codebase. The format of the event name can be &lt;code&gt;&amp;lt;areaOfCodebase&amp;gt;/&amp;lt;eventName&amp;gt;&lt;/code&gt;. For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ui/themeChanged&lt;/li&gt;
&lt;li&gt;ui/fontSizeChanged&lt;/li&gt;
&lt;li&gt;player/damaged&lt;/li&gt;
&lt;li&gt;player/poweredUp&lt;/li&gt;
&lt;li&gt;user/registering&lt;/li&gt;
&lt;li&gt;user/registered&lt;/li&gt;
&lt;li&gt;user/deleted&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And, as explained on &lt;a href="https://davidwells.io/blog/clean-analytics" rel="noopener noreferrer"&gt;clean analytics by David Wells&lt;/a&gt;, for analytics you could use a format like &lt;code&gt;&amp;lt;Source&amp;gt;:&amp;lt;object&amp;gt;_&amp;lt;actionName&amp;gt;&lt;/code&gt;. For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;site:newsletter_subscribed&lt;/li&gt;
&lt;li&gt;app:site_deployed&lt;/li&gt;
&lt;li&gt;cli:user_login&lt;/li&gt;
&lt;li&gt;api:site_created&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are just examples. In your own codebase, you can use as many namespaces as you want. The namespaces can be anything.&lt;/p&gt;

&lt;p&gt;Likewise, the delimiter(s) can be anything you want. Some examples are "/", "::", ":", "_", or even no delimiter.&lt;/p&gt;

&lt;h4&gt;
  
  
  Commands
&lt;/h4&gt;

&lt;p&gt;Commands are  written similarly to functions. They are verbs in an imperative mood. They are also used in PubSub. The notes about namespaces and delimiters, mentioned above, apply to them.&lt;/p&gt;

&lt;p&gt;Commands also usually expect a response. In other words, a command such as "CreateUser" will have a response message such as "CreateUserResult", "CreateUserSucceeded" or even "UserCreatedNotification". Overall, I'm not aware of strong conventions for these, so you can probably use whatever you like.&lt;/p&gt;

&lt;p&gt;My personal preference for response names comes from &lt;a href="https://jimmybogard.com/message-naming-conventions/" rel="noopener noreferrer"&gt;Jimmy Bogard's post on message naming conventions&lt;/a&gt;. I generally append "Result", "Reply" or "Response" to the original command name.&lt;/p&gt;

&lt;p&gt;Some example formats for command names and their namespaces are &lt;code&gt;&amp;lt;Verb&amp;gt;&amp;lt;Subject&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;subject&amp;gt;/&amp;lt;verb&amp;gt;&lt;/code&gt;. For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Possible commands are "RegisterUser" or "user/register". Possible responses are "registerUserResponse" or "user/register_result"&lt;/li&gt;
&lt;li&gt;Possible commands are "DamagePlayer", "player/damage". Possible responses are "DamagePlayerResponse", "player/damage_result"&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  File names
&lt;/h3&gt;

&lt;p&gt;For file names, you need to consider conventions for casing, as well as what to name the file based on the code it contains.&lt;/p&gt;

&lt;h4&gt;
  
  
  File name casing conventions and delimiters
&lt;/h4&gt;

&lt;p&gt;For file naming, there are different conventions depending on the language, framework and style guide you follow.&lt;/p&gt;

&lt;p&gt;Many conventions recommend file names that are all lower case. Words can be separated with either hyphens (-) or underscores (_). For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;in HTML, the convention is all lower case, with hyphens as separators. Underscores are far less common. One reason for this is because the HTML file name may reflect in the URL (especially with older servers). Underscores in URLs are far less common than hyphens.&lt;/li&gt;
&lt;li&gt;in CSS, the convention is all lower case with hyphens or underscores as separators&lt;/li&gt;
&lt;li&gt;In Python, PEP 8 recommends file names to be all lower case, with underscores as separators&lt;/li&gt;
&lt;li&gt;the Google JavaScript style guide recommends file names to be all lower case, with underscores or hyphens as separators&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Between hyphens and underscores, you can use either. Both are acceptable. In general, I prefer to use hyphens to be consistent with my CSS classes (which conventionally use hyphens) and for the reasons mentioned for the HTML. However, if you commonly use snake case in your programming language, or if you don't write HTML and CSS, it might feel more consistent to use underscores instead of hyphens.&lt;/p&gt;

&lt;p&gt;Some example file names are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;index.html, index.css, index.js&lt;/li&gt;
&lt;li&gt;third-party-analytics.js&lt;/li&gt;
&lt;li&gt;enemy-mover.js&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Along with that, there are other conventions that recommend camel or pascal case for your files. For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;C# and Java recommend naming your file the same as the main thing in your file. This means using pascal case, same as your classes and interfaces.&lt;/li&gt;
&lt;li&gt;the AirBnB JavaScript style guide recommends naming your file the same thing as your default export. Again, this means using camel or pascal case, at least for your JavaScript files.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://reactjs.org/docs/faq-structure.html" rel="noopener noreferrer"&gt;React takes it a step further and recommends naming all of your files consistently in each folder&lt;/a&gt;. For example:

&lt;ul&gt;
&lt;li&gt;MyComponent.js&lt;/li&gt;
&lt;li&gt;MyComponent.css&lt;/li&gt;
&lt;li&gt;MyComponent.test.js (test files tend to have the special extension .test.js)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;So which should you choose? First, consider if one of the conventions is more common than the others for your programming language or framework. That's the natural choice. Otherwise, you can do whatever you like. My personal recommendation is to choose the naming convention that best matches the code in your files. For example, when working on the front end, the convention is for everything in your HTML and CSS to be lower case with hyphens as separators. Therefore, you might want to use that as the naming convention.&lt;/p&gt;

&lt;p&gt;As another example, when working on React applications that use CSS modules, you might prefer to write CSS using pascal case and underscores. This makes it easier to use your CSS in your JavaScript, for example &lt;code&gt;styles.Foo_bar&lt;/code&gt; (where &lt;code&gt;Foo_bar&lt;/code&gt; is your CSS class), as hyphens aren't allowed in JavaScript. In this case, it may feel more natural to name your files using pascal case.&lt;/p&gt;

&lt;h4&gt;
  
  
  Choosing a file name based on the code it contains
&lt;/h4&gt;

&lt;p&gt;In general, you want to name your file based on its purpose.&lt;/p&gt;

&lt;p&gt;This is supported by the C# naming conventions and Java naming conventions. They state that your file name should be the same as the main thing you define in your file. For example, if you define a class Foo, your file name should be Foo.cs or Foo.java.&lt;/p&gt;

&lt;p&gt;The AirBnB JavaScript style guide also agrees with this. It states that your file should be named after your default export. Whether or not you use default exports is a separate matter. But the point is the same. It means to name it after the purpose, or the most important thing in your file. Normally (but not always) that's thing that you would &lt;code&gt;export default&lt;/code&gt;, if you were using export default.&lt;/p&gt;

&lt;p&gt;One exception is if your default export is named "main", "setup" or something similar. It doesn't make sense for a file to be called "main", especially if many files have a similar default export. In that case, consider what the purpose of the file is. An alternative is to consider what the equivalent OOP code would be.&lt;/p&gt;

&lt;p&gt;For example, consider that you have a class that deals with carousel functionality. Your OOP code would probably be a class named "Carousel". In comparison, if you write it using functions, it might look 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;function&lt;/span&gt; &lt;span class="nf"&gt;handleChangeToNextSlide&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// code to change to next slide when user clicks the relevant button&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// find DOM elements that are supposed to be carousels&lt;/span&gt;
  &lt;span class="c1"&gt;// set up event listeners on those elements&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;main&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the equivalent OOP code, the code in &lt;code&gt;main&lt;/code&gt; would be in the constructor of the class &lt;code&gt;Carousel&lt;/code&gt;. In this case, my recommendation would be to name the file &lt;code&gt;carousel&lt;/code&gt;. That's its true purpose. Alternatively, you could also change &lt;code&gt;main&lt;/code&gt; to &lt;code&gt;setupCarousel&lt;/code&gt; or something and name the file after that.&lt;/p&gt;

&lt;p&gt;As for some other cases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If your file defines multiple classes: Well, most style guides would tell you to avoid files with multiple classes. Consider whether you can separate each class into its own file. Although, if you only have one public class, then that's fine. Name the file after the one public class, not the private ones.&lt;/li&gt;
&lt;li&gt;If you use functions instead of classes, there might be some cases where none of the functions would suitable to export default. Again, you need to consider what the purpose of the file is. For example, if you have some functions that are used in your test files, then perhaps "test-utilities" would be a good name. Alternatively, you can consider what the equivalent OOP code would be. You would probably have a static class with static methods. Name your file after the name of that static class.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Package names and namespaces
&lt;/h3&gt;

&lt;p&gt;In practice, many packages don't follow naming conventions at all. That's because a developer can upload a package with whatever name they want. There usually aren't many checks.&lt;/p&gt;

&lt;p&gt;However, there are naming conventions for packages and for different package repositories.&lt;/p&gt;

&lt;p&gt;Java and &lt;a href="https://search.maven.org/" rel="noopener noreferrer"&gt;Maven&lt;/a&gt; use the format of &lt;code&gt;&amp;lt;groupID&amp;gt;&amp;lt;artifactID&amp;gt;&lt;/code&gt;. The group ID part is generally a reversed domain name. For example, if your domain is example.com, the group ID would be "com.example". It can have subgroups (additional namespaces). For example "com.example.plugins". The artifactID is the name of the jar. For example "junit" or "spring-web". In general, artifact IDs tend to be fully lower case with hyphens to separate words.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.npmjs.com/" rel="noopener noreferrer"&gt;NPM (node package manager)&lt;/a&gt; tends to use direct package names, such as "react-dom", or prefixed with a namespace or company name, such as "&lt;a class="mentioned-user" href="https://dev.to/babel"&gt;@babel&lt;/a&gt;/preset-env". They tend to be fully lower case with hyphens to separate words.&lt;/p&gt;

&lt;p&gt;With &lt;a href="https://www.nuget.org/" rel="noopener noreferrer"&gt;NuGet&lt;/a&gt; (package repository for .NET), the number of namespaces vary, just like with Maven and subgroups. There are many packages with a single name, such as "Moq". There are also many packages of the format &lt;code&gt;&amp;lt;CompanyName&amp;gt;.&amp;lt;Package&amp;gt;&lt;/code&gt;, such as "AWSSDK.Core". There are also packages with many namespaces, such as "Microsoft.Extensions.FileProviders.Abstractions". If you want to follow the example of the packages released by Microsoft, then use pascal case, optionally a company prefix and as many namespaces as you need.&lt;/p&gt;

&lt;p&gt;Namespaces (in code) seem to follow similar concepts and conventions to packages.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final tips
&lt;/h2&gt;

&lt;p&gt;Remember that naming is hard.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;There are only two hard things in Computer Science: cache invalidation and naming things. - Phil Karlton&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;However, spending some time to come up with a reasonable name is usually worth it.&lt;/p&gt;

&lt;p&gt;Also, as always, be pragmatic. It's okay to break these conventions once in a while. They are only here to help you make good and consistent decisions most of the time. For example, it's okay if you feel that adding the data structure to the name will help, such as &lt;code&gt;userList&lt;/code&gt;. It's up to you to decide what's best for your project.&lt;/p&gt;

&lt;p&gt;In addition, you probably can't spend an unreasonable amount of time coming up with good names. So, sometimes, if you've already spent too long, you might need to use the best name you've come up with so far and move on.&lt;/p&gt;

&lt;p&gt;Overall, the most important thing from this article is that you understand the principles behind naming. In particular, that names should make the code easy to understand. If you understand that, then you'll be fine. You'll be able to come up with your own solutions and conventions even in unfamiliar situations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final notes
&lt;/h2&gt;

&lt;p&gt;So that's it for the article. I hope that you found it useful.&lt;/p&gt;

&lt;p&gt;If you've encountered any awful names in code you've worked on, please leave a comment with them!&lt;/p&gt;

&lt;p&gt;Also, if you want to discuss anything, disagree with anything, or have any feedback in general, please leave a comment below.&lt;/p&gt;

&lt;p&gt;Alright, see you next time :)&lt;/p&gt;

&lt;h2&gt;
  
  
  Image credits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Cover photo (modified) - Photo by Paul Stollery on Unsplash&lt;/li&gt;
&lt;li&gt;Clear Bulb Beside White Notepad on White Surface - Photo by Burak Kebapci on Pexels&lt;/li&gt;
&lt;li&gt;Green leaf with white card - Photo by Helena Hertz on Unsplash&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>programming</category>
      <category>learning</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Carousel slider tutorial with HTML, CSS and JavaScript</title>
      <dc:creator>Spyros Argalias</dc:creator>
      <pubDate>Tue, 01 Jun 2021 12:01:10 +0000</pubDate>
      <link>https://dev.to/programmingduck/carousel-slider-tutorial-with-html-css-and-javascript-1024</link>
      <guid>https://dev.to/programmingduck/carousel-slider-tutorial-with-html-css-and-javascript-1024</guid>
      <description>&lt;p&gt;In this post we'll look at how to make a simple carousel with HTML, CSS and JavaScript. We will use good code practices, keep accessibility in mind and also consider how we can test the carousel.&lt;/p&gt;

&lt;p&gt;The carousel will be a "moving carousel". Slides will move in from left to right, or right to left, with a transition. It won't be an in-place carousel where a slide fades out while another one fades-in.&lt;/p&gt;

&lt;p&gt;If you prefer a video version, here it is. It goes into much more detail than this post.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Basic functionality
&lt;/h2&gt;

&lt;p&gt;We'll start with the basic functionality. That's the basic HTML, CSS and JavaScript.&lt;/p&gt;

&lt;h3&gt;
  
  
  HTML
&lt;/h3&gt;

&lt;p&gt;We'll keep the HTML fairly simple. We basically need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a container for the carousel&lt;/li&gt;
&lt;li&gt;the carousel controls&lt;/li&gt;
&lt;li&gt;the slides&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We won't focus very much on the HTML head or anything other than the carousel. The rest is standard stuff.&lt;/p&gt;

&lt;p&gt;As for the actual carousel, here is some HTML we can use.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
&lt;span class="c"&gt;&amp;lt;!-- Import font-awesome somewhere in the HTML --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"&lt;/span&gt; &lt;span class="na"&gt;integrity=&lt;/span&gt;&lt;span class="s"&gt;"sha512-iBBXm8fW90+nuLcSKlbmrPcLa0OT92xO1BIsZ+ywDWZCvqsWgccV3gFoRBv0z+8dLJgyAHIhR35VZc2oM/gI1w=="&lt;/span&gt; &lt;span class="na"&gt;crossorigin=&lt;/span&gt;&lt;span class="s"&gt;"anonymous"&lt;/span&gt; &lt;span class="na"&gt;referrerpolicy=&lt;/span&gt;&lt;span class="s"&gt;"no-referrer"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"./index.css"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"carousel"&lt;/span&gt; &lt;span class="na"&gt;data-carousel&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"carousel-buttons"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt;
        &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"carousel-button carousel-button_previous"&lt;/span&gt;
        &lt;span class="na"&gt;data-carousel-button-previous&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"fas fa-chevron-circle-left"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt;
        &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"carousel-button carousel-button_next"&lt;/span&gt;
        &lt;span class="na"&gt;data-carousel-button-next&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"fas fa-chevron-circle-right"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"slides"&lt;/span&gt; &lt;span class="na"&gt;data-carousel-slides-container&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"slide"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="c"&gt;&amp;lt;!-- Anything can be here. Each slide can have any content --&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;h2&amp;gt;&lt;/span&gt;Slide 1 heading&lt;span class="nt"&gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Slide 1 content
      &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"slide"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="c"&gt;&amp;lt;!-- Anything can be here. Each slide can have any content --&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;h2&amp;gt;&lt;/span&gt;Slide 2 heading&lt;span class="nt"&gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Slide 2 content
      &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the head, we are linking font awesome and also our custom styles CSS file.&lt;/p&gt;

&lt;p&gt;In the body:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;we have an outer &lt;code&gt;div&lt;/code&gt; for the entire carousel.&lt;/li&gt;
&lt;li&gt;we have two buttons, one for "previous slide" and one for "next slide". The buttons use font-awesome icons.&lt;/li&gt;
&lt;li&gt;we have a &lt;code&gt;div&lt;/code&gt; for the slides. Inside that, we have a &lt;code&gt;div&lt;/code&gt; for each slide. The content inside each slide is irrelevant to us, it can be anything.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As for the &lt;code&gt;data-&lt;/code&gt; attributes, those are what we'll use as selectors in JavaScript.&lt;/p&gt;

&lt;p&gt;I personally prefer using &lt;code&gt;data-&lt;/code&gt; attributes for JavaScript because I want to separate concerns. For example, classes are standard to use for CSS. When someone tries to change the styling of the carousel in the future, they may replace the class name for a more descriptive one. They may also change some CSS modifier classes or something. I don't want them to be paranoid that if they change the CSS they may break the JavaScript, or the automated tests, or the asynchronous content insertions, or anything else. I want them to feel safe when working with the CSS.&lt;/p&gt;

&lt;p&gt;This means, that I do not use classes to select elements with JavaScript.&lt;/p&gt;

&lt;p&gt;An exception to this is if you use classes with a prefix such as &lt;code&gt;js-&lt;/code&gt;. E.g. &lt;code&gt;&amp;lt;div class="js-carousel"&amp;gt;&amp;lt;/div&amp;gt;&lt;/code&gt;, which are exclusively for JavaScript use. That achieves the same result.&lt;/p&gt;

&lt;p&gt;But my preference is to use &lt;code&gt;data-&lt;/code&gt; attributes. That's what &lt;code&gt;data-carousel&lt;/code&gt; and the others are for.&lt;/p&gt;

&lt;h3&gt;
  
  
  CSS
&lt;/h3&gt;

&lt;p&gt;Our CSS:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;is going to have the basic styling for our carousel&lt;/li&gt;
&lt;li&gt;is going to have the mechanism for changing the slides&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The way our carousel will work is by having all slides horizontally next to each other. However, only one slide will show at a time. That's because every slide, except the one that's visible, will be overflowing outside of the top-level carousel &lt;code&gt;div&lt;/code&gt;. That &lt;code&gt;div&lt;/code&gt; will have &lt;code&gt;overflow: hidden&lt;/code&gt;, so nothing that's overflowing will show.&lt;/p&gt;

&lt;p&gt;We'll decide which slide is currently showing with the line &lt;code&gt;transform: translateX(/* something */)&lt;/code&gt;. That way, we'll translate the &lt;code&gt;slides&lt;/code&gt; div, so that only the correct slide is visible.&lt;/p&gt;

&lt;p&gt;Here is the CSS.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.carousel&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="py"&gt;--current-slide&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c"&gt;/* we set position relative so absolute position works properly for the buttons */&lt;/span&gt;
  &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;relative&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;overflow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;hidden&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.carousel-button&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c"&gt;/* vertically centering the buttons */&lt;/span&gt;
  &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;absolute&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;top&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;50%&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;translateY&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;-50%&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nl"&gt;z-index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c"&gt;/* basic styling */&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0.5rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;50%&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;transparent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1.5rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;pointer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nl"&gt;transition&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt; &lt;span class="m"&gt;0.1s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.carousel-button&lt;/span&gt;&lt;span class="nd"&gt;:hover&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;rgba&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.carousel-button_next&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c"&gt;/* The "next slide button" will be at the right */&lt;/span&gt;
  &lt;span class="nl"&gt;right&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.slides&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;transition&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;transform&lt;/span&gt; &lt;span class="m"&gt;0.5s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;translateX&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;calc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;-100%&lt;/span&gt; &lt;span class="err"&gt;*&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--current-slide&lt;/span&gt;&lt;span class="p"&gt;)));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.slide&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="m"&gt;100%&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;@media&lt;/span&gt; &lt;span class="n"&gt;screen&lt;/span&gt; &lt;span class="n"&gt;and&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;min-width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;768px&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nc"&gt;.carousel-button&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1rem&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;With this CSS, every &lt;code&gt;div&lt;/code&gt; has its default width of 100%. This means that the carousel will take the full width of its parent container. Every slide will also take up the full width of the carousel.&lt;/p&gt;

&lt;h4&gt;
  
  
  Controls
&lt;/h4&gt;

&lt;p&gt;In the &lt;code&gt;carousel-button&lt;/code&gt; class, we provide some simple styling for the buttons. We're using font-awesome icons, so we give them a font-size so they're large and visible. We also remove some of the default button styling (things like borders and background color).&lt;/p&gt;

&lt;p&gt;Also, we position the buttons in the middle (vertically) of the entire carousel. We do this by using the &lt;code&gt;position: absolute; top: 50%; transform: translateY(-50%);&lt;/code&gt; trick.&lt;/p&gt;

&lt;h4&gt;
  
  
  Changing slides
&lt;/h4&gt;

&lt;p&gt;The trick for how the carousel actually changes slides is the CSS in &lt;code&gt;.slides&lt;/code&gt; and &lt;code&gt;.slide&lt;/code&gt;. In &lt;code&gt;.slide&lt;/code&gt;, we make each slide have 100% of the width of the carousel. This is done with the &lt;code&gt;flex&lt;/code&gt; property. In other words, one slide will take up the entire width of the carousel.&lt;/p&gt;

&lt;p&gt;Since &lt;code&gt;.slides&lt;/code&gt; is &lt;code&gt;display: flex;&lt;/code&gt;, all of the slides will be horizontally next to each other. This means that one slide will take up the entire width of the carousel and all other slides will overflow horizontally next to it. The carousel div has &lt;code&gt;overflow: hidden;&lt;/code&gt;, so none of the overflowing slides will show.&lt;/p&gt;

&lt;p&gt;At some point, using JavaScript, we'll move the &lt;code&gt;.slides&lt;/code&gt; div to the right or left. This means that the slides will move, so a different slide will be visible inside the carousel.&lt;/p&gt;

&lt;p&gt;The declaration &lt;code&gt;transform: translateX(calc(-100% * var(--current-slide)));&lt;/code&gt; is our movement mechanism. Here we're saying to move the slides container -100% (the full-width of the carousel, or the full width of a slide) to the left (the negative sign means to the left), as many times as the slide index we're on.&lt;/p&gt;

&lt;p&gt;For example, if we're on slide index 0 (first slide), &lt;code&gt;-100% * 0&lt;/code&gt; = 0, so we don't translate at all and the first slide is visible.&lt;/p&gt;

&lt;p&gt;If we're on slide 1, then &lt;code&gt;-100% * 1&lt;/code&gt; = -100%, so we translate 100% (one slide width) to the left. This means that we're displaying slide index 1 (the second slide).&lt;/p&gt;

&lt;p&gt;We'll set the &lt;code&gt;--current-slide&lt;/code&gt; property using JavaScript.&lt;/p&gt;

&lt;h3&gt;
  
  
  JavaScript
&lt;/h3&gt;

&lt;p&gt;Our JavaScript needs to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;handle events for the two buttons (switch to previous slide and next slide)&lt;/li&gt;
&lt;li&gt;work independently for any number of different carousels on the page&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is the JavaScript.&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;modulo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;mod&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="nx"&gt;mod&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;result&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;result&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;mod&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;result&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;setUpCarousel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;carousel&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;handleNext&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;currentSlide&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;modulo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentSlide&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;numSlides&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;changeSlide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentSlide&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;handlePrevious&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;currentSlide&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;modulo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentSlide&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;numSlides&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;changeSlide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentSlide&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;changeSlide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;slideNumber&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;carousel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;--current-slide&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;slideNumber&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// get elements&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;buttonPrevious&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;carousel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;[data-carousel-button-previous]&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;buttonNext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;carousel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;[data-carousel-button-next]&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;slidesContainer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;carousel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;[data-carousel-slides-container]&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// carousel state we need to remember&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;currentSlide&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;numSlides&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;slidesContainer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;children&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="c1"&gt;// set up events&lt;/span&gt;
  &lt;span class="nx"&gt;buttonPrevious&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;handlePrevious&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;buttonNext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;handleNext&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;carousels&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelectorAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;[data-carousel]&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;carousels&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="nx"&gt;setUpCarousel&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code may be appear a bit confusing because of the nested functions. If you're not used to this syntax, then here is a class alternative for the &lt;code&gt;setUpCarousel&lt;/code&gt; function which does exactly the same thing.&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;class&lt;/span&gt; &lt;span class="nx"&gt;Carousel&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;carousel&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// find elements&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;carousel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;carousel&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;buttonPrevious&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;carousel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;[data-carousel-button-previous]&lt;/span&gt;&lt;span class="dl"&gt;'&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;buttonNext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;carousel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;[data-carousel-button-next]&lt;/span&gt;&lt;span class="dl"&gt;'&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;slidesContainer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;carousel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;[data-carousel-slides-container]&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// state&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;currentSlide&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;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;numSlides&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="nx"&gt;slidesContainer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;children&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="c1"&gt;// add events&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;buttonPrevious&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&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;handlePrevious&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bind&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;buttonNext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&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;handleNext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bind&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;handleNext&lt;/span&gt;&lt;span class="p"&gt;()&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;currentSlide&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;modulo&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;currentSlide&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;numSlides&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;carousel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;--current-slide&lt;/span&gt;&lt;span class="dl"&gt;'&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;currentSlide&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;handlePrevious&lt;/span&gt;&lt;span class="p"&gt;()&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;currentSlide&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;modulo&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;currentSlide&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;numSlides&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;carousel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;--current-slide&lt;/span&gt;&lt;span class="dl"&gt;'&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;currentSlide&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;carousels&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelectorAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;[data-carousel]&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;carousels&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="nx"&gt;carousel&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Carousel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;carousel&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Basically, we're holding some state, the &lt;code&gt;currentSlide&lt;/code&gt; and the &lt;code&gt;numSlides&lt;/code&gt; variables. We're also holding references to some HTML elements, such as the carousel element, because we'll need them when changing slides. Finally, we add event listeners to the buttons.&lt;/p&gt;

&lt;p&gt;When the user clicks on the "next slide" button, we run the &lt;code&gt;handleNext&lt;/code&gt; function. The call to &lt;code&gt;modulo(currentSlide, numSlides)&lt;/code&gt; sets &lt;code&gt;currentSlide&lt;/code&gt; to the correct index for the next slide. So, if there are 5 slides, and we're on slide index 0, it will set &lt;code&gt;currentSlide&lt;/code&gt; to 1. But, if we're already on slide index 4 (the fifth and final slide), then the next slide index is 0, not 5. The modulo function takes care of the wrapping back to 0 for us.&lt;/p&gt;

&lt;p&gt;Really, we could have used the &lt;code&gt;%&lt;/code&gt; (modulo) operator for this. The reason why we have the &lt;code&gt;modulo&lt;/code&gt; function is because &lt;code&gt;%&lt;/code&gt; doesn't play well with negative numbers. &lt;code&gt;-1 % 5&lt;/code&gt; evaluates to &lt;code&gt;-1&lt;/code&gt;, rather than &lt;code&gt;4&lt;/code&gt; (the index of the slide we would actually want). We created our own &lt;code&gt;modulo&lt;/code&gt; function to handle that case.&lt;/p&gt;

&lt;p&gt;Finally, we set the CSS property &lt;code&gt;--current-slide&lt;/code&gt; to the correct number. Then, the CSS changes the visible slide by translating the slides &lt;code&gt;div&lt;/code&gt; appropriately.&lt;/p&gt;

&lt;p&gt;The independence of different carousels on the page happens because we use &lt;code&gt;querySelector&lt;/code&gt; on the parent carousel element, not on the &lt;code&gt;document&lt;/code&gt;. This means that, for example, &lt;code&gt;carouselElement1.querySelector([data-carousel-button-next])&lt;/code&gt;, will only get the button inside that carousel element. Whereas &lt;code&gt;document.querySelector('[data-carousel-button-next]')&lt;/code&gt; would get the first matching element it finds on the page, rather than the target carousel.&lt;/p&gt;

&lt;h2&gt;
  
  
  Accessibility
&lt;/h2&gt;

&lt;p&gt;At the moment, this carousel is very unfriendly to screen reader users. You'll need to actually use a screen reader and listen to it to hear it for yourself (or watch the accessibility section of the embedded video), but basically:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it doesn't mention anything about the content being a carousel&lt;/li&gt;
&lt;li&gt;for the buttons, it just says "button" and nothing else (because the buttons don't have text or a label)&lt;/li&gt;
&lt;li&gt;on "auto read", it reads through all of the content of every slide, as though it was a normal web page full of text (because we're not telling it to only read the visible slide)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To fix those issues, we need to go to the &lt;a href="https://www.w3.org/TR/wai-aria-practices-1.1/"&gt;WAI-ARIA authoring practices document&lt;/a&gt;. There is a section for carousels. We just go to it and follow the instructions. It's actually not too difficult. It has step-by-step instructions for us.&lt;/p&gt;

&lt;p&gt;In the end, our HTML looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt;
  &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"carousel"&lt;/span&gt;
  &lt;span class="na"&gt;aria-role=&lt;/span&gt;&lt;span class="s"&gt;"group"&lt;/span&gt;
  &lt;span class="na"&gt;aria-roledescription=&lt;/span&gt;&lt;span class="s"&gt;"carousel"&lt;/span&gt;
  &lt;span class="na"&gt;aria-label=&lt;/span&gt;&lt;span class="s"&gt;"Student testimonials"&lt;/span&gt;
  &lt;span class="na"&gt;data-carousel&lt;/span&gt;
&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"carousel-buttons"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt;
      &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"carousel-button carousel-button_previous"&lt;/span&gt;
      &lt;span class="na"&gt;aria-label=&lt;/span&gt;&lt;span class="s"&gt;"Previous slide"&lt;/span&gt;
      &lt;span class="na"&gt;data-carousel-button-previous&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"fas fa-chevron-circle-left"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt;
      &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"carousel-button carousel-button_next"&lt;/span&gt;
      &lt;span class="na"&gt;aria-label=&lt;/span&gt;&lt;span class="s"&gt;"Next slide"&lt;/span&gt;
      &lt;span class="na"&gt;data-carousel-button-next&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"fas fa-chevron-circle-right"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt;
    &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"slides"&lt;/span&gt;
    &lt;span class="na"&gt;aria-live=&lt;/span&gt;&lt;span class="s"&gt;"polite"&lt;/span&gt;
    &lt;span class="na"&gt;data-carousel-slides-container&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt;
      &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"slide"&lt;/span&gt;
      &lt;span class="na"&gt;aria-role=&lt;/span&gt;&lt;span class="s"&gt;"group"&lt;/span&gt;
      &lt;span class="na"&gt;aria-roledescription=&lt;/span&gt;&lt;span class="s"&gt;"slide"&lt;/span&gt;
      &lt;span class="na"&gt;aria-hidden=&lt;/span&gt;&lt;span class="s"&gt;"false"&lt;/span&gt;
      &lt;span class="na"&gt;aria-labelledby=&lt;/span&gt;&lt;span class="s"&gt;"bob"&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;h2&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"bob"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Bob&lt;span class="nt"&gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt;
      &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"slide"&lt;/span&gt;
      &lt;span class="na"&gt;aria-role=&lt;/span&gt;&lt;span class="s"&gt;"group"&lt;/span&gt;
      &lt;span class="na"&gt;aria-roledescription=&lt;/span&gt;&lt;span class="s"&gt;"slide"&lt;/span&gt;
      &lt;span class="na"&gt;aria-hidden=&lt;/span&gt;&lt;span class="s"&gt;"true"&lt;/span&gt;
      &lt;span class="na"&gt;aria-labelledby=&lt;/span&gt;&lt;span class="s"&gt;"alice"&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;h2&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"alice"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Alice&lt;span class="nt"&gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A quick summary of what we did is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;we added an &lt;code&gt;aria-role&lt;/code&gt;, &lt;code&gt;aria-roledescription&lt;/code&gt; and &lt;code&gt;aria-label&lt;/code&gt; to the carousel &lt;code&gt;div&lt;/code&gt;. Now, the screen reader says something like "Student testimonials carousel", immediately indicating that this is a carousel and what content it represents.&lt;/li&gt;
&lt;li&gt;for each button, we added an &lt;code&gt;aria-label&lt;/code&gt;. Now the screen reader says something like "button previous slide", instead of just "button". (An alternative technique here would be to add "screen-reader only text". This is text that exists in the HTML but is hidden visually using particular methods.)&lt;/li&gt;
&lt;li&gt;we added an &lt;code&gt;aria-role&lt;/code&gt; and &lt;code&gt;aria-roledescription&lt;/code&gt; to each slide. Now the screen reader knows when it's entering a slide or leaving a slide and it will notify the user as necessary.&lt;/li&gt;
&lt;li&gt;we also added a label to each slide using &lt;code&gt;aria-labelledby&lt;/code&gt;. This is the same as &lt;code&gt;aria-label&lt;/code&gt; except that you point it to some text that already exists on the page, using an HTML ID. In this case, since our label already exists on the page (the heading for each slide), we used &lt;code&gt;aria-labelledby&lt;/code&gt; instead of &lt;code&gt;aria-label&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;we added &lt;code&gt;aria-hidden="true"&lt;/code&gt; to the hidden slides. Now the screen reader won't read them.&lt;/li&gt;
&lt;li&gt;we added an &lt;code&gt;aria-live&lt;/code&gt; region. Now the screen reader will re-read the content of the carousel whenever there are changes (when the user changes the slide).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are some other aria attributes that would be useful, but I'm ignoring them for now because they're not mentioned in the carousel part of the WAI-ARIA authoring practices. One example is &lt;a href="https://tink.uk/using-the-aria-controls-attribute/"&gt;aria-controls&lt;/a&gt;. If you want to learn more about these, it might be worth looking at the &lt;a href="https://www.w3.org/TR/wai-aria-practices-1.1/"&gt;WAI-ARIA authoring practices&lt;/a&gt; in your own time. If you want to learn more about accessibility in general, I've written a learning guide in &lt;a href="https://programmingduck.com/articles/accessibility"&gt;Web accessibility - Everything you need to know&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Our JavaScript needs some updates as well. Specifically, when we change slides, we need to change the &lt;code&gt;aria-hidden&lt;/code&gt; property to &lt;code&gt;false&lt;/code&gt; for the new active slide. We also need to hide the previous slide that we're no longer looking at.&lt;/p&gt;

&lt;p&gt;Here is some example code we can use:&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;changeSlide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;slideNumber&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// change current slide visually&lt;/span&gt;
  &lt;span class="nx"&gt;carousel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;--current-slide&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;slideNumber&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// handle screen reader accessibility&lt;/span&gt;
  &lt;span class="c1"&gt;// here we're getting the elements for the previous slide, current slide and next slide&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;previousSlideNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;modulo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;slideNumber&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;numSlides&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;nextSlideNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;modulo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;slideNumber&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;numSlides&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;previousSlide&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;slidesContainer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;previousSlideNumber&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;currentSlideElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;slidesContainer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;slideNumber&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;nextSlide&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;slidesContainer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;nextSlideNumber&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

  &lt;span class="c1"&gt;// here, we're hiding the previous and next slides and unhiding the current slide&lt;/span&gt;
  &lt;span class="nx"&gt;previousSlide&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;aria-hidden&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;nextSlide&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;aria-hidden&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;currentSlideElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;aria-hidden&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Testing
&lt;/h2&gt;

&lt;p&gt;What ways are there to test something like this?&lt;/p&gt;

&lt;p&gt;In short, I would write end-to-end tests for it. I would hesitate to write unit tests for it.&lt;/p&gt;

&lt;p&gt;Here's why.&lt;/p&gt;

&lt;p&gt;An end-to-end test shows you that the entire thing works correctly.&lt;/p&gt;

&lt;p&gt;Depending on your test framework, you could do things like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;check that only a particular &lt;code&gt;div&lt;/code&gt; (slide) is visible on the page, and the others aren't&lt;/li&gt;
&lt;li&gt;check that the correct &lt;code&gt;div&lt;/code&gt; (slide) is visible after pressing the next / previous slide button&lt;/li&gt;
&lt;li&gt;check that the transition for changing slides works correctly&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But if you unit test, you can only check that your JavaScript works correctly.&lt;/p&gt;

&lt;p&gt;You could do a test where you set up some HTML, then run your JavaScript and finally check that the resulting HTML is what you expect.&lt;/p&gt;

&lt;p&gt;Or you could do something like spy on your JavaScript code, run your JavaScript and ensure your spies were called.&lt;/p&gt;

&lt;p&gt;With the first unit test example (where you check the final HTML), the problem is that, while your tests may be passing, your carousel may not be working. For example, someone may have changed how the CSS works. They may have renamed the property &lt;code&gt;--current-slide&lt;/code&gt; to &lt;code&gt;--index&lt;/code&gt; or whatever else. Maybe they changed the entire CSS mechanism for changing the slides (for example, to improve performance).&lt;/p&gt;

&lt;p&gt;In this case, your JavaScript will be executing without errors and the tests will be passing, but the carousel won't be working.&lt;/p&gt;

&lt;p&gt;The tests won't provide confidence that your code works.&lt;/p&gt;

&lt;p&gt;The only thing they'll do is freeze your JavaScript implementation. This is the scenario where you've already checked the carousel yourself, manually, in the browser. You think "I can see that it's working, let me write some unit tests for it that check that the JavaScript is doing X". What this does, is it prevents anyone from accidentally changing the JavaScript in the future. If they do so, the tests will fail.&lt;/p&gt;

&lt;p&gt;But, it also makes intentional changes more difficult. Now, if you want to change the implementation in the future, you need to change your CSS, JavaScript and your 10 tests. This is one of the reasons why people dislike unit tests. They make changes to the implementation more difficult (at least with unit tests like these).&lt;/p&gt;

&lt;p&gt;So, for these reasons, I would personally recommend writing end-to-end tests instead. Now, if you really want to prevent accidental changes in the JavaScript, that's fine. You need to do what you need to do. It's up to you to decide if the peace of mind is worth the downsides and the time it takes to write those tests.&lt;/p&gt;

&lt;p&gt;As for the other scenario of unit testing, where you check that your spies were called, I just don't see a benefit to that. With those tests, you're not even testing that your JavaScript is doing what you think. You could break the JavaScript implementation in the future and your tests would still pass, as long as you're calling the same functions.&lt;/p&gt;

&lt;p&gt;But, those are just my thoughts on the matter. I'm open to differences in opinion. Please leave a comment below if you think I'm missing something.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final notes
&lt;/h2&gt;

&lt;p&gt;So that's it. I hope that you found this article useful.&lt;/p&gt;

&lt;p&gt;If you want a fuller view of the code, &lt;a href="https://github.com/Programming-Duck/carousel"&gt;here is the code repository&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Please note that this is not meant to be production-ready. The code can be cleaned up more. It can probably be made more appropriate to what you need to use. Etc.&lt;/p&gt;

&lt;p&gt;This is just a little tutorial to show you the general idea on how to make a simple carousel.&lt;/p&gt;

&lt;p&gt;If you have any feedback, anything that was missed or could have been done better, or anything else, please leave a comment below.&lt;/p&gt;

&lt;p&gt;Alright, thanks very much and see you next time.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>codenewbie</category>
      <category>a11y</category>
    </item>
    <item>
      <title>Clean code &amp; programming principles – The ultimate beginner’s guide</title>
      <dc:creator>Spyros Argalias</dc:creator>
      <pubDate>Fri, 14 May 2021 11:18:14 +0000</pubDate>
      <link>https://dev.to/programmingduck/clean-code-programming-principles-the-ultimate-beginner-s-guide-5605</link>
      <guid>https://dev.to/programmingduck/clean-code-programming-principles-the-ultimate-beginner-s-guide-5605</guid>
      <description>&lt;p&gt;This article is the beginner's introductory guide to programming principles.&lt;/p&gt;

&lt;p&gt;First we're going to examine what good code is. The qualities of good code. That's because those qualities come before programming principles. Programming principles are just guidelines to help us apply those qualities to code.&lt;/p&gt;

&lt;p&gt;Afterwards, we'll examine the most important programming principles, one-by-one, at an introductory level.&lt;/p&gt;

&lt;p&gt;Hopefully, this article will feel less like "have small functions" and more like "these are the qualities you want in code, for reasons 1, 2 and 3. So as you can see, small functions help you achieve those in ways X, Y and Z".&lt;/p&gt;

&lt;p&gt;I believe that this kind of understanding is more beneficial than just knowing some arbitrary rules. They're especially helpful if you've been stuck on how to apply certain programming principles in the past. Knowing how they help and what they're trying to achieve should help you apply them even in unfamiliar situations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Target audience
&lt;/h2&gt;

&lt;p&gt;I believe that this article is suitable for all audiences.&lt;/p&gt;

&lt;p&gt;If you're a beginner developer, some of the things mentioned in this article may be too abstract. But, some others should be useful immediately. Nevertheless, this article will give you an understanding that will help you very much in the future, even if you don't understand all of it now.&lt;/p&gt;

&lt;p&gt;If you're an intermediate-level developer, you'll probably gain the most benefit. You are probably writing medium to large programs. You've got the hang of the basics. Now, you need to learn how to write code that scales (in size). This is what programming principles help you with.&lt;/p&gt;

&lt;p&gt;If you're an advanced-level developer, you'll probably know most of these things already. However, you might enjoy this article nonetheless.&lt;/p&gt;




&lt;h2&gt;
  
  
  Qualities of good code
&lt;/h2&gt;

&lt;p&gt;What is good code?&lt;/p&gt;

&lt;p&gt;To answer that question, first we need to examine the requirements of code. Then, the qualities that we (people) need for something to be easy to work with. After that, the qualities of good code become obvious.&lt;/p&gt;

&lt;p&gt;If you want to skip the discussion, here are the conclusions:&lt;/p&gt;

&lt;p&gt;The requirements of code are that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it should work as intended, without bugs&lt;/li&gt;
&lt;li&gt;it should be built as quickly and efficiently as possible (without sacrificing quality) (just like all products)&lt;/li&gt;
&lt;li&gt;it should be easy and fast to work with and modify (for the next time you need to work with it)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some of our limitations are that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;we can't remember too much at any one time. This means that we won't remember that modifying X will break Y and Z.&lt;/li&gt;
&lt;li&gt;we find complicated things disproportionally more difficult than simple things&lt;/li&gt;
&lt;li&gt;making multiple similar changes is very error-prone for us&lt;/li&gt;
&lt;li&gt;we have bad days where we are bored, can't focus and don't pay too much attention&lt;/li&gt;
&lt;li&gt;we always make mistakes, no matter what. This means that we need tests (manual or automated) and other error-catching things.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;From those two, after a bit of reasoning, we conclude that code should:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;be simple (because we're bad with complicated things)&lt;/li&gt;
&lt;li&gt;be immediately understandable (so we can understand it quickly and make changes faster. Also so we don't misunderstand it and create bugs, especially if we're not really focusing that day)&lt;/li&gt;
&lt;li&gt;be organised (so we can understand the project structure easier and find the files we need to modify faster)&lt;/li&gt;
&lt;li&gt;be independent (so we can make reasonable changes to X without breaking 1,000 other things in the project)&lt;/li&gt;
&lt;li&gt;have minimal duplication (because we're bad with repetitive changes. They're also slower)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;More details and explanations are below. If you're not interested, please skip to the next section.&lt;/p&gt;

&lt;h3&gt;
  
  
  Requirements of code
&lt;/h3&gt;

&lt;p&gt;Software is a product. Businesses hire programmers to build software products. It's not abstract art (usually). It's something built for a specific purpose.&lt;/p&gt;

&lt;p&gt;From a business perspective, products:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;have to be fit for purpose and work as intended&lt;/li&gt;
&lt;li&gt;should be as cheap and efficient as possible to create (without sacrificing quality)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The same applies to software.&lt;/p&gt;

&lt;p&gt;But software has some unique aspects. It needs constant modification. That's because software is often never "finished". Companies may be requesting new features for decades after initial release. Also, there may be bugs that need fixing at any time. Finally, during development, programmers constantly modify the code.&lt;/p&gt;

&lt;p&gt;Therefore, for the software product to be as efficient and cheap as possible to create and maintain, the code needs to be easy and fast to work with and modify.&lt;/p&gt;

&lt;p&gt;Not to mention that being easy to work with means less bugs due to changes.&lt;/p&gt;

&lt;p&gt;So, the requirements of code are that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it should work as intended, without bugs&lt;/li&gt;
&lt;li&gt;it should be built as quickly and efficiently as possible (without sacrificing quality)&lt;/li&gt;
&lt;li&gt;it should be easy and fast to work with and modify (for the next time you need to work with it)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For even more detail on this, please see the post &lt;a href="https://programmingduck.com/articles/requirements-of-software" rel="noopener noreferrer"&gt;requirements of software&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Human limitations and bad code
&lt;/h3&gt;

&lt;p&gt;Code can be difficult to work with because of our limitations.&lt;/p&gt;

&lt;p&gt;Here are some of our limitations and what we can do to counter them.&lt;/p&gt;

&lt;h4&gt;
  
  
  Memory
&lt;/h4&gt;

&lt;p&gt;We can't remember too much at any one time.  The quote about short term memory and &lt;a href="https://en.wikipedia.org/wiki/The_Magical_Number_Seven,_Plus_or_Minus_Two" rel="noopener noreferrer"&gt;the magical number 7 plus or minus 2&lt;/a&gt; comes to mind.&lt;/p&gt;

&lt;p&gt;To counter that, we need code to be sufficiently independent (decoupled) and without hidden dependencies. That way, when we're modifying code, we won't accidentally break it due to forgetting to also update a dependency that we didn't remember existed.&lt;/p&gt;

&lt;h4&gt;
  
  
  We like things simple
&lt;/h4&gt;

&lt;p&gt;Complicated things are disproportionally more difficult for us. This is partly because we need to keep in mind many things about them at once. Therefore, we should make code simple and easy to work with.&lt;/p&gt;

&lt;h4&gt;
  
  
  We are impatient
&lt;/h4&gt;

&lt;p&gt;We get impatient, skim things often, have bad days and get bored.&lt;/p&gt;

&lt;p&gt;To counter that, we should make code simple, easy to understand and easy to work with.&lt;/p&gt;

&lt;h4&gt;
  
  
  We are bad with repetitive work
&lt;/h4&gt;

&lt;p&gt;Repetition is error-prone for us, particularly if every repetition is slightly different.&lt;/p&gt;

&lt;p&gt;Repetitive work means more chances to make an error. Also, probably due to impatience and lack of focus, we're more likely to rush this type of work. We don't usually provide the necessary care and attention to every single change. To help, we should minimise repetitive work.&lt;/p&gt;

&lt;h4&gt;
  
  
  We make mistakes
&lt;/h4&gt;

&lt;p&gt;We make mistakes often and in all areas of life. This includes programming, mathematics, engineering, art, design and everything else.&lt;/p&gt;

&lt;p&gt;Therefore, we always need to double check our work. As a result, we use practices like code reviews and automated testing. We also use tools to statically analyse our code.&lt;/p&gt;

&lt;h3&gt;
  
  
  How we should work on software
&lt;/h3&gt;

&lt;p&gt;We should work on software deliberately. We should know and understand as much as possible about the code we're currently working on. This means that we'll be as certain as possible that we're doing the right thing and that we won't break anything.&lt;/p&gt;

&lt;p&gt;In comparison, if we're just trying things at random, we're not certain that they'll work. Most of the things we try won't work, except the last one (at which point we'll stop). Also, we'll only know whether they work or not because of our tests. We'll probably manually test everything we try.&lt;/p&gt;

&lt;p&gt;This is problematic, because, since we're not really sure what we're doing, we might have broken other things that we won't think to test.&lt;/p&gt;

&lt;p&gt;So, to minimize the chance of error, it's important to understand as much as possible about what we're doing.&lt;/p&gt;

&lt;p&gt;The best way to do that is to make code simple, easy to understand and easy to work with.&lt;/p&gt;

&lt;h3&gt;
  
  
  How code should be
&lt;/h3&gt;

&lt;p&gt;Everything we've examined so far points to a certain way for how code should be. Code should:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;be simple (because we're bad with complicated things)&lt;/li&gt;
&lt;li&gt;be immediately understandable (so we can understand it quickly and make changes faster. Also so we don't misunderstand it and create bugs, especially if we're not really focusing that day)&lt;/li&gt;
&lt;li&gt;be organised (so we can understand the project structure easier and find the files we need to modify faster)&lt;/li&gt;
&lt;li&gt;be independent (so we can make reasonable changes to X without breaking 1,000 other things in the project)&lt;/li&gt;
&lt;li&gt;have minimal duplication (because we're bad with repetitive changes. They're also slower)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Next, let's examine the programming principles.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Be pragmatic - The most important principle
&lt;/h2&gt;

&lt;p&gt;Not just in programming, but pretty much everything in life, being pragmatic is essential.&lt;/p&gt;

&lt;p&gt;It means to remember the true goal of what you're trying to accomplish, maximise that, and not get side-tracked.&lt;/p&gt;

&lt;p&gt;In programming, your aims are to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;have code that works correctly&lt;/li&gt;
&lt;li&gt;make your changes as quickly and efficiently as possible&lt;/li&gt;
&lt;li&gt;make the code easy and fast to work with for the next time someone works on it&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The programming principles are guidelines to help you do that. But, your aims come first. If a programming principle will be detrimental to your aims, you shouldn't apply it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Don't apply principles to the extreme
&lt;/h3&gt;

&lt;p&gt;For example, having code that's short is commonly considered a good thing. It has many benefits which we'll examine later. But you should never make your code shorter if it will make it more difficult to understand and work with.&lt;/p&gt;

&lt;p&gt;Don't play "code golf", where you use complicated syntax and mathematical tricks to make the code as short as possible. That makes the code more complicated and more difficult to understand.&lt;/p&gt;

&lt;p&gt;In other words, have code that's short (the guideline), but only if it makes the code simpler and easier to understand (your aims).&lt;/p&gt;

&lt;h3&gt;
  
  
  Balance time spent refactoring
&lt;/h3&gt;

&lt;p&gt;Additionally, you need to make your changes in a reasonable timeframe. You've got to balance how much time you spend refactoring code against how much benefit it will provide.&lt;/p&gt;

&lt;p&gt;For example, if you have some code that's very difficult to understand, you absolutely should refactor it. It might take a few hours, but it's probably worth it. It will make your project easier to work with in the long-term. You'll reclaim the time you spent refactoring through higher efficiency in the future.&lt;/p&gt;

&lt;p&gt;But, if you have some code that's almost perfect, don't spend 3 days refactoring it only to make it slightly better. You would have spent 3 days for almost no benefit. Instead, you could have used that time in better ways. You could have written a new feature, or refactored a more suitable part of the codebase.&lt;/p&gt;

&lt;p&gt;The point here is: You need to prioritise based on value. That usually means keeping code pretty clean and refactoring when needed. But it probably doesn't mean spending an unreasonable amount of time refactoring for almost no benefit.&lt;/p&gt;

&lt;h3&gt;
  
  
  YAGNI
&lt;/h3&gt;

&lt;p&gt;Another important thing to talk about is YAGNI. It stands for "you ain't gonna need it".&lt;/p&gt;

&lt;p&gt;It warns you against coding things in anticipation of features you might need in the future. For a simple contrived example, you may create a function &lt;code&gt;foo&lt;/code&gt;, which has the parameter &lt;code&gt;bar&lt;/code&gt;. But you might think "feature X might be added in the future, which will need a parameter &lt;code&gt;baz&lt;/code&gt;, so let me add it to the function now".&lt;/p&gt;

&lt;p&gt;In general, you want to be wary of doing that. Firstly, that feature is probably never going to be needed. Secondly, you increase the complexity of the code today, making it harder to work with. Thirdly, if that feature is needed in the future, you might code it differently to how you anticipate today.&lt;/p&gt;

&lt;p&gt;Instead, code the simplest solution for what you need today. Then, make the changes needed for that feature when it's needed (if ever).&lt;/p&gt;

&lt;p&gt;This is optimal, because you won't needlessly waste your time or make the codebase more complicated. Even if you did predict a feature correctly, it will be much faster to code it when you need it compared to all of the time you would have spent coding everything prematurely.&lt;/p&gt;

&lt;h3&gt;
  
  
  Personal recommendations
&lt;/h3&gt;

&lt;p&gt;Create a fairly simple solution for what you need today, that is simple to understand and work with.&lt;/p&gt;

&lt;p&gt;Write clean code and maintain your code so it's fairly clean. Refactoring may take time upfront, but it pays off in the long-term because the code is easier to work with.&lt;/p&gt;

&lt;p&gt;Only apply programming principles if they'll make your code better and easier to work with.&lt;/p&gt;

&lt;p&gt;If you're newer to programming principles, consider applying them more heavily than necessary when you practice. You'll get practice applying them and you'll get a feel for when you've taken them too far.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  KISS (keep it simple stupid) and the principle of least astonishment
&lt;/h2&gt;

&lt;p&gt;KISS (keep it simple stupid) is another principle that's universal to most things in life. It means that your code should be simple and easy to understand.&lt;/p&gt;

&lt;p&gt;The principle of least astonishment is also important. It means that things should work exactly as you expect them to, they shouldn't be surprising. It's a cousin to KISS.&lt;/p&gt;

&lt;p&gt;If you don't keep things simple and easy to understand, then:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;everything takes longer to understand&lt;/li&gt;
&lt;li&gt;sometimes you might not understand how things work, even after spending a lot of time on them&lt;/li&gt;
&lt;li&gt;you might &lt;strong&gt;misunderstand&lt;/strong&gt; how things work. Then, if you modify the software, you could easily create bugs.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How to apply KISS and the principle of least astonishment
&lt;/h3&gt;

&lt;p&gt;Here are some guidelines for making your code simple and easy to understand.&lt;/p&gt;

&lt;h4&gt;
  
  
  Default to writing dumb code, avoid writing clever code
&lt;/h4&gt;

&lt;p&gt;Dumb code is simple code. Clever code is probably not simple code.&lt;/p&gt;

&lt;p&gt;Really clever code is not simple, it's difficult to understand and it's tricky. People will &lt;strong&gt;misunderstand&lt;/strong&gt; it and create bugs as a result.&lt;/p&gt;

&lt;h4&gt;
  
  
  Keep code short and concise
&lt;/h4&gt;

&lt;p&gt;Shorter code is more likely to be simple.&lt;/p&gt;

&lt;p&gt;Short code means that units, such as functions and classes, do less things. That means they're simpler and easier to understand.&lt;/p&gt;

&lt;h4&gt;
  
  
  Use good names
&lt;/h4&gt;

&lt;p&gt;If you have a well-named function, you can understand what it does from the name, without reading the function body. The same applies to all code. This makes your work faster and easier.&lt;/p&gt;

&lt;p&gt;The name also provides meaning, which helps you decipher code faster.&lt;/p&gt;

&lt;p&gt;For example, if you see the code &lt;code&gt;2 * Math.PI * radius&lt;/code&gt;, you may not understand what it's doing and why, even after reading it. You may look at it and be like "what? PI, radius?? What is this???".&lt;/p&gt;

&lt;p&gt;But, if you see &lt;code&gt;const circleArea = 2 * Math.PI * radius&lt;/code&gt;, straight away you're like "oh I get it. It's calculating the area of the circle, of courseeeee. No wonder PI and radius are there...".&lt;/p&gt;

&lt;h4&gt;
  
  
  Always consider the programmer reading the code for the first time
&lt;/h4&gt;

&lt;p&gt;This is the person you're trying to optimise the code for. The colleague who has never worked on this code before, or even yourself, 6 months from now, when you've forgotten what this code does and how it works.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Any code of your own that you haven't looked at for six or more months might as well have been written by someone else." - Eagleson's law&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Consider that when you're writing the code, you know what the code needs to do and you just code it. But the person reading the code for the first time, has to parse what the code is doing &lt;strong&gt;and also has to understand why it's doing it&lt;/strong&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Consider immutability (never reassigning the values of variables)
&lt;/h4&gt;

&lt;p&gt;Immutability provides a guarantee that a value will never change.&lt;/p&gt;

&lt;p&gt;This makes the code simpler to understand, because you don't have to trace through the code for the history of the variable, just in case it happened to change anywhere in your codebase.&lt;/p&gt;

&lt;h4&gt;
  
  
  Follow existing conventions
&lt;/h4&gt;

&lt;p&gt;Code that follows existing conventions is unsurprising. Code that breaks conventions can be very unexpected. Someone who skims the code may not realise that it doesn't follow the convention, so they may misunderstand how it works.&lt;/p&gt;

&lt;p&gt;Try to follow conventions which already exist in your codebase. Conventions which exist in your language or framework are less essential to follow, but also recommended.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Separation of concerns
&lt;/h2&gt;

&lt;p&gt;Separation of concerns means to organise functionality well in code.&lt;/p&gt;

&lt;p&gt;Code should be separated into sensible units (modules, classes, functions and methods). Someone looking at the code should immediately understand what the particular unit does.&lt;/p&gt;

&lt;p&gt;For example, if you have a &lt;code&gt;Circle&lt;/code&gt; class, an &lt;code&gt;Enumerable&lt;/code&gt; interface or a &lt;code&gt;Math&lt;/code&gt; object or module, you tend to have a pretty good idea of what each does and contains. You would expect to find &lt;code&gt;Math.PI&lt;/code&gt;, or &lt;code&gt;Math.pow(base, exponent)&lt;/code&gt; (these methods exist in the JavaScript &lt;code&gt;Math&lt;/code&gt; object). However, you wouldn't expect to find &lt;code&gt;Math.printHelloToTheScreen()&lt;/code&gt; or &lt;code&gt;Math.produceAccountingReport()&lt;/code&gt;. The methods in the latter example would be unexpected, which would break the principles of KISS and least astonishment.&lt;/p&gt;

&lt;p&gt;In addition, units should be small and only do one thing (also known as the &lt;a href="https://en.wikipedia.org/wiki/Single-responsibility_principle" rel="noopener noreferrer"&gt;single responsibility principle&lt;/a&gt;). Another way of thinking about this is that different concerns should be separated at a granular level.&lt;/p&gt;

&lt;p&gt;For example, you shouldn't have a god-class called &lt;code&gt;Shape&lt;/code&gt; that has functionality for all possible shapes within it. Instead, you should have a small class for each shape.&lt;/p&gt;

&lt;p&gt;This code is the bad version:&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="c1"&gt;// Bad god class&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Shape&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;typeOfShape&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;length1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;length2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// length2 is an optional parameter&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;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;typeOfShape&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;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;circle&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;radius&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;length1&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="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;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;square&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;length1&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="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;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;rectangle&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;length1&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="nx"&gt;length2&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// And so on for many more shapes&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;getArea&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;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;circle&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="k"&gt;return&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;PI&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="nx"&gt;radius&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="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&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;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;square&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="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;width&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="nx"&gt;width&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="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;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;rectangle&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="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;width&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="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// And so on for many more shapes&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the good version:&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="c1"&gt;// Good small and simple classes&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Circle&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;radius&lt;/span&gt;&lt;span class="p"&gt;)&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;radius&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;radius&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;getArea&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="mi"&gt;2&lt;/span&gt; &lt;span class="o"&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;PI&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="nx"&gt;radius&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="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Rectangle&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;width&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="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;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;width&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="nx"&gt;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;getArea&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;width&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="nx"&gt;length&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;Here is another example.&lt;/p&gt;

&lt;p&gt;This code is the bad version:&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="c1"&gt;// Function does too many things&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sendData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;formattedData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&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="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;5&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;formattedData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;every&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isInteger&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;formattedData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;every&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isLessThan1000&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;foo.com&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="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;formattedData&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;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// code to submit error&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code is the better version:&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="c1"&gt;// Functionality is separated well over multiple functions&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sendData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;formattedData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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="nf"&gt;isValid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;formattedData&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;foo.com&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="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;formattedData&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;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;sendError&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;data&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;square&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isGreaterThan5&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="nf"&gt;isValid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;every&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isInteger&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;every&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isLessThan1000&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="nf"&gt;sendError&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// code to submit error&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The idea that you should have small, specific units applies to all code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Advantages of small units
&lt;/h3&gt;

&lt;p&gt;Smaller, more specific units, have multiple advantages.&lt;/p&gt;

&lt;h4&gt;
  
  
  Better code organisation
&lt;/h4&gt;

&lt;p&gt;Technically, with the god-class &lt;code&gt;Shape&lt;/code&gt;, you know where to go to find the circle functionality, so the organisation is not too bad.&lt;/p&gt;

&lt;p&gt;But, with the more specific units of &lt;code&gt;Circle&lt;/code&gt; and &lt;code&gt;Rectangle&lt;/code&gt;, you can find functionality faster and easier.&lt;/p&gt;

&lt;p&gt;It's less obvious with the &lt;code&gt;sendData&lt;/code&gt; example, but the same thing applies. Say you want to find the functionality for validating the data. You can find that instantly in the second version. There is a function clearly named &lt;code&gt;isValid&lt;/code&gt;. &lt;code&gt;sendData&lt;/code&gt; also calls &lt;code&gt;isValid(formattedData)&lt;/code&gt;, which labels where the data is validated.&lt;/p&gt;

&lt;p&gt;However, in the first version of &lt;code&gt;sendData&lt;/code&gt;, you'll have to spend more time reading through the details of &lt;code&gt;sendData&lt;/code&gt; to find it. Also, the part where the data is validated isn't labelled. You'll have to both parse the code and &lt;strong&gt;recognise&lt;/strong&gt; the line which does the data validation. If you're not familiar with the code, this may be difficult.&lt;/p&gt;

&lt;p&gt;In summary, smaller units provide better organisation.&lt;/p&gt;

&lt;h4&gt;
  
  
  Simplicity and understandability
&lt;/h4&gt;

&lt;p&gt;If you examine the &lt;code&gt;Shape&lt;/code&gt; example, you'll see that the code there is quite long and complex. It's difficult to follow. In comparison, the classes &lt;code&gt;Circle&lt;/code&gt; and &lt;code&gt;Rectangle&lt;/code&gt; are super simple. As a result, they're much easier to understand.&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;sendData&lt;/code&gt; example, understanding what &lt;code&gt;sendData&lt;/code&gt; does is easier in the second version. It almost reads like English:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Format data&lt;/li&gt;
&lt;li&gt;If the data is valid: fetch&lt;/li&gt;
&lt;li&gt;Else: sendError&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You also don't have to read the implementation of the separate functions, such as &lt;code&gt;isValid&lt;/code&gt;, because their names tell you what they do.&lt;/p&gt;

&lt;p&gt;All of the smaller functions are simpler too. They are clearly labelled (which helps you understand them even if the implementation is complicated) and they only do a small thing.&lt;/p&gt;

&lt;p&gt;In general, smaller units have less code and do less things. This applies the KISS principle, which makes code easier to read and understand.&lt;/p&gt;

&lt;h4&gt;
  
  
  Easier changes
&lt;/h4&gt;

&lt;p&gt;Code that does fewer things is easier to change than code which does many things.&lt;/p&gt;

&lt;p&gt;At the very least, the code you need to change isn't surrounded by other code that you need to carefully avoid changing. Also, you need to understand the code before changing it, which is easier with small units.&lt;/p&gt;

&lt;p&gt;Consider the god-class &lt;code&gt;Shape&lt;/code&gt; example. The code for the functionality of all the shapes is entangled together. If you try to change the code for the circle, you could accidentally modify something else and create a bug. Also, the functionality for circle exists in multiple different methods inside &lt;code&gt;Shape&lt;/code&gt;. You'll have to jump around and change multiple different things.&lt;/p&gt;

&lt;p&gt;On the other hand, &lt;code&gt;Circle&lt;/code&gt; and &lt;code&gt;Rectangle&lt;/code&gt; are very easy to change. Unrelated code is nowhere to be found. You can't break any other shape by accident.&lt;/p&gt;

&lt;p&gt;The same applies to the &lt;code&gt;sendData&lt;/code&gt; example.&lt;/p&gt;

&lt;p&gt;In the second version, if you want to change the data validation, you change the code in &lt;code&gt;isValid&lt;/code&gt; and you're finished. You can't break any unrelated code, because there isn't any.&lt;/p&gt;

&lt;p&gt;However, in the first version, since a lot of unrelated code is placed together, you might accidentally change something else by accident.&lt;/p&gt;

&lt;h4&gt;
  
  
  Easier to test
&lt;/h4&gt;

&lt;p&gt;In general, if a unit does less stuff, it's easier to test than if it does more stuff.&lt;/p&gt;

&lt;h4&gt;
  
  
  Easier to reuse
&lt;/h4&gt;

&lt;p&gt;If a unit does one specific thing, it's immediately reusable any time you need that one thing. However, if a unit does 10 things, or even 2 things, it's generally not reusable unless you need all of those things.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to apply separation of concerns
&lt;/h3&gt;

&lt;p&gt;To apply separation of concerns, you extract functionality.&lt;/p&gt;

&lt;p&gt;For example, with &lt;code&gt;Shape&lt;/code&gt;, if you extract all of the relevant code for the circle functionality into its own class, you end up with &lt;code&gt;Circle&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here is a more step-by-step process.&lt;/p&gt;

&lt;p&gt;Here is &lt;code&gt;Shape&lt;/code&gt; again for reference.&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;class&lt;/span&gt; &lt;span class="nc"&gt;Shape&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;typeOfShape&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;length1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;length2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// length2 is an optional parameter&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;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;typeOfShape&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;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;circle&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;radius&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;length1&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="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;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;square&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;length1&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="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;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;rectangle&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;length1&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="nx"&gt;length2&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// And so on for many more shapes&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;getArea&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;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;circle&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="k"&gt;return&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;PI&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="nx"&gt;radius&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="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&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;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;square&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="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;width&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="nx"&gt;width&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="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;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;rectangle&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="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;width&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="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// And so on for many more shapes&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;Let's define a class called &lt;code&gt;Circle&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;class&lt;/span&gt; &lt;span class="nc"&gt;Circle&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From &lt;code&gt;Shape&lt;/code&gt;, let's extract only the constructor functionality that's relevant to circle. That's the part inside the &lt;code&gt;constructor&lt;/code&gt; method and inside the &lt;code&gt;if (this.type === 'circle')&lt;/code&gt; conditional.&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;class&lt;/span&gt; &lt;span class="nc"&gt;Circle&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;radius&lt;/span&gt;&lt;span class="p"&gt;)&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;radius&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;radius&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;Repeat for the &lt;code&gt;getArea&lt;/code&gt; function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Circle&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;radius&lt;/span&gt;&lt;span class="p"&gt;)&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;radius&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;radius&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;getArea&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="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;PI&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="nx"&gt;radius&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="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;And so on for all the other methods which might be in &lt;code&gt;Shape&lt;/code&gt;. Afterwards, repeat for the other shapes.&lt;/p&gt;

&lt;p&gt;The same process applies for &lt;code&gt;sendData&lt;/code&gt;, although in this case we're not completely replacing &lt;code&gt;sendData&lt;/code&gt; like we did with &lt;code&gt;Shape&lt;/code&gt; and &lt;code&gt;Circle&lt;/code&gt;. Instead, we're extracting functionality into separate functions and calling them inside &lt;code&gt;sendData&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example, the code to format data was moved into the &lt;code&gt;formatData&lt;/code&gt; function and the code to check if the data is valid was moved into the &lt;code&gt;isValid&lt;/code&gt; function.&lt;/p&gt;

&lt;h3&gt;
  
  
  When to apply separation of concerns
&lt;/h3&gt;

&lt;p&gt;Now that you understand the "why" and "how" of separation of concerns, when should you apply it?&lt;/p&gt;

&lt;p&gt;Generally, you want "small, specific units that only do one thing".&lt;/p&gt;

&lt;p&gt;However, the definition of "one thing" varies, it depends on context.&lt;/p&gt;

&lt;p&gt;If you were to show the god-class &lt;code&gt;Shape&lt;/code&gt; to someone, they might rightfully say that it only does one thing. "It handles shapes".&lt;/p&gt;

&lt;p&gt;Someone else may say that &lt;code&gt;Shape&lt;/code&gt; does a lot of things. "It handles circles, rectangles and so on. That's multiple things".&lt;/p&gt;

&lt;p&gt;Both claims are correct. It all depends on what level of abstraction you consider.&lt;/p&gt;

&lt;p&gt;In general, it's good to consider small levels of abstraction. You want units that do small, specific things.&lt;/p&gt;

&lt;p&gt;That's because, as already examined, smaller units have more benefits than larger units.&lt;/p&gt;

&lt;p&gt;So, here are some guidelines.&lt;/p&gt;

&lt;h4&gt;
  
  
  When code feels large and complicated
&lt;/h4&gt;

&lt;p&gt;If you feel that some code is difficult to understand, or too large, try extracting some units out of it.&lt;/p&gt;

&lt;h4&gt;
  
  
  Can you keep extracting?
&lt;/h4&gt;

&lt;p&gt;Robert Martin has a technique that he calls &lt;a href="https://sites.google.com/site/unclebobconsultingllc/one-thing-extract-till-you-drop" rel="noopener noreferrer"&gt;"extract till you drop"&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In short, you keep extracting functionality until there is no reasonable way of extracting any more.&lt;/p&gt;

&lt;p&gt;As you write code, consider: "Can I extract some more functionality from this unit, into a separate unit?"&lt;/p&gt;

&lt;p&gt;If it's possible to extract further, then consider doing so.&lt;/p&gt;

&lt;p&gt;See &lt;a href="https://sites.google.com/site/unclebobconsultingllc/one-thing-extract-till-you-drop" rel="noopener noreferrer"&gt;Robert Martin's blog post on extract till you drop&lt;/a&gt; for more information on this technique.&lt;/p&gt;

&lt;h4&gt;
  
  
  Reasons to change
&lt;/h4&gt;

&lt;p&gt;Consider, what reasons does this code have to change?&lt;/p&gt;

&lt;p&gt;Code which is placed together, which has different reasons to change (different parts may change at different times), is bad, as we've already examined.&lt;/p&gt;

&lt;p&gt;The solution is to move code with different reasons to change into separate units.&lt;/p&gt;

&lt;p&gt;Consider the &lt;code&gt;Shape&lt;/code&gt; example. &lt;code&gt;Shape&lt;/code&gt; will change when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;anything needs changing for circles&lt;/li&gt;
&lt;li&gt;anything needs changing for rectangles&lt;/li&gt;
&lt;li&gt;anything needs changing on any other shape&lt;/li&gt;
&lt;li&gt;a new shape needs to be added or removed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the &lt;code&gt;sendData&lt;/code&gt; example, &lt;code&gt;sendData&lt;/code&gt; could change if:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the formatting of the data needs to change&lt;/li&gt;
&lt;li&gt;the validation of the data needs to change&lt;/li&gt;
&lt;li&gt;the data in the error request needs to change&lt;/li&gt;
&lt;li&gt;the endpoint (URL) of the error request needs to change&lt;/li&gt;
&lt;li&gt;the data in the &lt;code&gt;sendData&lt;/code&gt; request needs to change&lt;/li&gt;
&lt;li&gt;the endpoint (URL) of the &lt;code&gt;sendData&lt;/code&gt; request needs to change&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All of these reasons are indicators that you may want to separate that functionality.&lt;/p&gt;

&lt;h4&gt;
  
  
  Who (which role in the company) may want to change this code
&lt;/h4&gt;

&lt;p&gt;This is another flavour of "what reasons does this code have to change".&lt;/p&gt;

&lt;p&gt;It asks who (which role in the company) may want to change the code.&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;sendData&lt;/code&gt; example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;developers may want to change something about the URL endpoints of the requests or the bodies of the requests&lt;/li&gt;
&lt;li&gt;accountants may want to change the data validation in the future&lt;/li&gt;
&lt;li&gt;a product owner who uses the submitted data to generate reports could want to format the data differently in the future&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both of these questions (what could change and who may want changes) try to point out different concerns in the code, that may benefit from separation.&lt;/p&gt;

&lt;h4&gt;
  
  
  Be pragmatic
&lt;/h4&gt;

&lt;p&gt;The final point is to be pragmatic.&lt;/p&gt;

&lt;p&gt;You don't have to separate everything to the extreme. The goal is to have code that's easy to work with.&lt;/p&gt;

&lt;p&gt;For example, you don't need to enforce every function in your codebase to be at maximum 4 lines long (which is possible to do). You would end up with hundreds of miniscule functions. They may be harder to work with than more reasonably sized functions, that are an average of 4 to 8 lines long.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Principle of least knowledge
&lt;/h2&gt;

&lt;p&gt;In software, it's beneficial to minimise knowledge. This includes the knowledge that code has of other code (dependencies), as well as the knowledge you need to work with particular areas of code.&lt;/p&gt;

&lt;p&gt;In other words, you want software to be decoupled and easy to work with. Making changes shouldn't break seemingly unrelated code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Knowledge in code
&lt;/h3&gt;

&lt;p&gt;In programming, knowledge means dependencies.&lt;/p&gt;

&lt;p&gt;If some code (call it module A), &lt;strong&gt;knows&lt;/strong&gt; about some other code (call it module B), it means that it &lt;strong&gt;uses&lt;/strong&gt; that other code. It depends on it.&lt;/p&gt;

&lt;p&gt;If some code is being used elsewhere, that means that there are &lt;strong&gt;limitations on how you can change it&lt;/strong&gt;, otherwise you would break the code that uses it.&lt;/p&gt;

&lt;p&gt;Without discipline and control, this is where you can get into a chain reaction of propagating changes. The situation where you just wanted to make a small change and had to modify every file in the system to do so. You changed A, which was used by B and C so you had to change both of those to accommodate your changes to A. In turn B and C were used in other places which you also had to change. And so on.&lt;/p&gt;

&lt;p&gt;Every change is error-prone, multiple cascading changes are much worse.&lt;/p&gt;

&lt;p&gt;Additionally, you need to actually remember or know that these dependencies exist. This is quite difficult to do, especially when dependencies propagate far and wide throughout your code. But if you don't remember, you won't make all of the required changes and you'll immediately introduce bugs.&lt;/p&gt;

&lt;p&gt;That's why you need to minimise knowledge in your code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Modifications to code
&lt;/h3&gt;

&lt;p&gt;Here are the possible changes you can make to already-existing code.&lt;/p&gt;

&lt;h4&gt;
  
  
  No change to contract
&lt;/h4&gt;

&lt;p&gt;The only change you can make with no propagating changes, is a change that doesn't affect anything else in the codebase.&lt;/p&gt;

&lt;p&gt;For example:&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="c1"&gt;// Original&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// After change&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&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="s2"&gt;`Hello &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&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;These two functions are equivalent from a caller's point of view. They have the same contract. If you change from one version to the other, nothing else in the codebase needs to change, because nothing could possibly be affected by this change.&lt;/p&gt;

&lt;h4&gt;
  
  
  Changing the contract of a "private" function
&lt;/h4&gt;

&lt;p&gt;The next best case is when you change the contract of a private function. Something that's not public to the majority of the codebase. In this case, if you change the contract, the code that is affected is very small.&lt;/p&gt;

&lt;p&gt;For example, consider this Circle class:&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="c1"&gt;// Circle.js&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Circle&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;radius&lt;/span&gt;&lt;span class="p"&gt;)&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;radius&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;radius&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;getArea&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="nf"&gt;_privateCalculation&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;radius&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;_privateCalculation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;radius&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="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;PI&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;radius&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="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Circle&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, consider that we want to delete &lt;code&gt;_privateCalculation&lt;/code&gt;. Here is the code after the change:&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="c1"&gt;// Circle.js&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Circle&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;radius&lt;/span&gt;&lt;span class="p"&gt;)&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;radius&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;radius&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;getArea&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="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;PI&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="nx"&gt;radius&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="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Circle&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we deleted &lt;code&gt;_privateCalculation&lt;/code&gt;, &lt;code&gt;getArea&lt;/code&gt; was affected. As a result, we also had to modify &lt;code&gt;getArea&lt;/code&gt; to accommodate the changes. However, since &lt;code&gt;_privateCalculation&lt;/code&gt; wasn't used anywhere else in the codebase and since &lt;code&gt;getArea&lt;/code&gt; didn't change its contract, we're finished. Nothing else in the codebase needs to be modified.&lt;/p&gt;

&lt;h4&gt;
  
  
  Changing the contract of a public function
&lt;/h4&gt;

&lt;p&gt;The pattern continues in the same way. If you change the contract of anything, you'll have to modify &lt;strong&gt;everything&lt;/strong&gt; that uses it to accommodate. If you change more contracts as a result, you'll have to modify even more things. And so on.&lt;/p&gt;

&lt;p&gt;For example, if you delete &lt;code&gt;getArea&lt;/code&gt;, you'll have to update all of the code in the codebase that uses it. Since &lt;code&gt;getArea&lt;/code&gt; is a public function, many things could be using it.&lt;/p&gt;

&lt;p&gt;In general, you want to prevent these situations.&lt;/p&gt;

&lt;p&gt;The only real way to prevent them is to separate concerns properly. You need to organise your code into sensible units that make sense for your project. If done well, that minimises the chance that you'll need to change the contract of those units in the future.&lt;/p&gt;

&lt;p&gt;For example, what is the chance that the &lt;code&gt;Circle&lt;/code&gt; class needs to change its contract? It's very low.&lt;/p&gt;

&lt;p&gt;Other than that, keep everything you can private, so that very little is affected when you need to change code.&lt;/p&gt;

&lt;p&gt;Now, changes to public things are necessary sometimes. That's life. It could be due to new requirements, or due to large refactors. You'll deal with them when you need to, but hopefully it won't be too often.&lt;/p&gt;

&lt;h3&gt;
  
  
  More tips
&lt;/h3&gt;

&lt;p&gt;The principle of least knowledge has many more applications. They all deal with making code independent to changes and with minimizing the mental knowledge you need to work with code.&lt;/p&gt;

&lt;p&gt;Other applications of this principle include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the interface segregation principle. This keeps interfaces small. It means that code which uses an interface depends on less things. It allows for easier future changes such as splitting a class based on its interfaces or creating a smaller separate class for an interface.&lt;/li&gt;
&lt;li&gt;the law of Demeter. This prevents functions / methods from depending on long chains of object compositions.&lt;/li&gt;
&lt;li&gt;immutability. This eliminates changes to variables. It means that you don't need to track how the variable has changed over time. It reduces the knowledge you need to work.&lt;/li&gt;
&lt;li&gt;only accessing things in the local scope (or maybe instance scope). Global things are accessible by many things in the codebase. Changing them may break many things. It's also difficult to track how they change over time, because many things can change them. However, local things are more "private". This makes tracking changes easier.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;h2&gt;
  
  
  Abstraction and don't repeat yourself (DRY)
&lt;/h2&gt;

&lt;p&gt;DRY (don't repeat yourself) is a core principle in programming.&lt;/p&gt;

&lt;p&gt;It says that if you have multiple instances of similar code, you should refactor them into a single abstraction. That way you'll end up with just one instance of the code, rather than multiple.&lt;/p&gt;

&lt;p&gt;To accommodate the differences, the resulting abstraction accepts arguments.&lt;/p&gt;

&lt;h3&gt;
  
  
  Motivation for DRY
&lt;/h3&gt;

&lt;p&gt;One of the reasons for DRY is to cut down the time you need to write code. If you already have an abstraction for X functionality, then you can import it and use it, rather than re-code it from scratch every time you need it.&lt;/p&gt;

&lt;p&gt;Another reason is to make changes easier. As already mentioned, we're bad with repetitive work. If code is DRY, then you only have to make a specific change in one place. If code isn't DRY then you have to make a similar change in multiple places. Making a single change is safer and faster than making multiple similar changes.&lt;/p&gt;

&lt;p&gt;Additionally, keeping code DRY applies separation of concerns. The abstraction will have to be placed in a sensible place in the codebase (good for code organisation). Also, the implementation of the abstraction is separated from the caller.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to apply abstraction and DRY
&lt;/h3&gt;

&lt;p&gt;Here are some guidelines for applying DRY.&lt;/p&gt;

&lt;h4&gt;
  
  
  Combine similar code into a single abstraction
&lt;/h4&gt;

&lt;p&gt;Whenever you find multiple instances of the same or similar code, combine them into a single abstraction. If there are slight differences between the instances, accept arguments to handle them.&lt;/p&gt;

&lt;p&gt;You've probably done this a vast number of times throughout your career.&lt;/p&gt;

&lt;p&gt;To illustrate the point, let's use the function &lt;code&gt;map&lt;/code&gt; as an example. &lt;code&gt;map&lt;/code&gt; is a function that handles this common process:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a new, empty, array&lt;/li&gt;
&lt;li&gt;Iterate over an array with a for-loop&lt;/li&gt;
&lt;li&gt;Run some functionality on every value&lt;/li&gt;
&lt;li&gt;Push the resulting value to the new array&lt;/li&gt;
&lt;li&gt;After the for-loop ends, return the new array&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This process is very common. It appears all the time in many codebases.&lt;/p&gt;

&lt;p&gt;Here is what it normally looks like using a for-loop.&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="nf"&gt;double&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&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;x&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="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;doubleArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&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;result&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="nx"&gt;arr&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;element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;transformedElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;double&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;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;transformedElement&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;result&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;arr&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;doubleArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In addition to the function &lt;code&gt;doubleArray&lt;/code&gt;, there would be many other functions that are almost exactly the same. The only differences would be the array they iterate over and the transformation they make on each element.&lt;/p&gt;

&lt;p&gt;So, take the common parts from those functions and put them into a separate function called &lt;code&gt;map&lt;/code&gt;. Accept arguments for the things that are different every time, the array and the transformation to run on each element.&lt;/p&gt;

&lt;p&gt;Here is the resulting code.&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="nf"&gt;map&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;transformationFn&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;result&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="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="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;element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;transformedElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;transformationFn&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;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;transformedElement&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;result&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;Then, in every function in your codebase similar to &lt;code&gt;doubleArray&lt;/code&gt;, use &lt;code&gt;map&lt;/code&gt; instead.&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="nf"&gt;double&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&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;x&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="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;doubleArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&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="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;double&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;arr&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;double&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(Of course, arrays in JavaScript already have a built-in method for &lt;code&gt;map&lt;/code&gt;, so you wouldn't need to create a standalone &lt;code&gt;map&lt;/code&gt; function. This was just for illustrative purposes.)&lt;/p&gt;

&lt;p&gt;You can do the same with any other code. Any time you encounter similar code, combine it into a single abstraction and accept arguments for any differences.&lt;/p&gt;

&lt;h4&gt;
  
  
  Rule of three
&lt;/h4&gt;

&lt;p&gt;The rule of three is a precaution against combining functionality too early.&lt;/p&gt;

&lt;p&gt;It states that you should combine functionality into a single abstraction if there are three occurrences of it. Don't combine if there are only two occurrences.&lt;/p&gt;

&lt;p&gt;That's because the instances of code you might combine, may diverge (each may change differently) in the future.&lt;/p&gt;

&lt;p&gt;For example, consider this code:&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="nf"&gt;validateUsername&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&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;str&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;6&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="nf"&gt;validatePassword&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&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;str&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;6&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;It would probably be a mistake to combine the duplicate functionality into its own abstraction, like so:&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="c1"&gt;// combined too early&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;validateUsername&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&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="nf"&gt;validate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&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="nf"&gt;validatePassword&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&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="nf"&gt;validate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&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="nf"&gt;validate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&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;str&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;6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The problem is that, in the future, &lt;code&gt;validateUsername&lt;/code&gt; and &lt;code&gt;validatePassword&lt;/code&gt; may change differently. It's not difficult to see how that may happen.&lt;/p&gt;

&lt;p&gt;For example, in the future, &lt;code&gt;validateUsername&lt;/code&gt; may need to check that there are no special characters, while the password may require special characters.&lt;/p&gt;

&lt;p&gt;Obviously you could make both scenarios work in the &lt;code&gt;validate&lt;/code&gt; function using conditionals, but it would be messier than if you had kept the functionality separate.&lt;/p&gt;

&lt;p&gt;This is why we use the rule of three. Waiting until the third occurrence makes it more likely that the similar functionality is significant rather than coincidental. It means that things are less likely to diverge in the future.&lt;/p&gt;

&lt;p&gt;It also makes it so that if one of the three instances of similar code diverges, you can separate it and still keep the abstraction for the other two. On the other hand, if you combined functionality on the second occurrence, then had to separate them out again, you would have to revert both of them.&lt;/p&gt;

&lt;p&gt;In summary, refactoring on the second occurrence is more likely to be a waste of time.&lt;/p&gt;

&lt;p&gt;Of course, the rule of three is just a guideline. Remember to be pragmatic and do what's best for your project. Some similar instances of code may be changing in the same way every time. Or maybe they are each quite complicated to change, and you have to make a similar change to both every time. In that case, it may be more beneficial for your project to combine them into a single abstraction, even if you have to ignore the rule of three.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Side effects
&lt;/h2&gt;

&lt;p&gt;The last thing we're going to look at is side effects. These aren't a single principle, but a combination of many principles + being pragmatic.&lt;/p&gt;

&lt;p&gt;(And no, they're not just the domain of functional programming. It's essential for all code to handle side effects properly.)&lt;/p&gt;

&lt;p&gt;In programming, the general definition of a side effect is anything that changes the state of the system. This includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;changing the value of a variable&lt;/li&gt;
&lt;li&gt;logging to the console&lt;/li&gt;
&lt;li&gt;modifying the DOM&lt;/li&gt;
&lt;li&gt;modifying the database&lt;/li&gt;
&lt;li&gt;any mutation whatsoever&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It also includes "actions" that may not be viewed as mutations, such as sending data over the network.&lt;/p&gt;

&lt;p&gt;I also say that accessing non-local scope is a side effect. It may not be in the official definition, but it's as unsafe as other side effects, especially if the variable you're trying to access is mutable. After all, if you access a global variable whose value isn't what you expect, you have a bug, even if the code in question doesn't modify it.&lt;/p&gt;

&lt;p&gt;All code needs "side effects" to be useful. For example, you have to modify the database or the DOM at some point.&lt;/p&gt;

&lt;p&gt;But side effects can be dangerous. They need to be handled carefully.&lt;/p&gt;

&lt;h3&gt;
  
  
  The danger of side effects
&lt;/h3&gt;

&lt;p&gt;Side effects are not directly harmful, but they can be indirectly harmful.&lt;/p&gt;

&lt;p&gt;For example, code A and B might both depend on the value of a global variable. You might change the value of the global variable, because you want to influence code A. But, you don't remember that code B will be affected as well. As a result, you now have a bug.&lt;/p&gt;

&lt;p&gt;These hidden dependencies, where you change one thing and something else breaks, can be very difficult to remember, track and manage.&lt;/p&gt;

&lt;p&gt;Another example is changing the DOM. The DOM can be thought of as just a global object with state. The problem is that, if different pieces of code affect the DOM at different times, in non-compatible ways, there can be bugs. Maybe code A depends on element X to be there, but code B deleted that entire section altogether just before code A ran.&lt;/p&gt;

&lt;p&gt;Perhaps you've encountered bugs like these in your work as well.&lt;/p&gt;

&lt;p&gt;Additionally, side effects break most of the principles we've covered so far:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;KISS and the principle of least astonishment&lt;/li&gt;
&lt;li&gt;principle of least knowledge (because code affects other, seemingly unrelated code)&lt;/li&gt;
&lt;li&gt;separation of concerns (because concerns are not necessarily self-contained or well-organised)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One important thing to understand however, is that side effects are not inherently harmful. They only cause bugs if we code them incorrectly. They are code we write which happens to be incompatible with other code we write. We write code A and then we write code B which breaks code A under certain circumstances.&lt;/p&gt;

&lt;p&gt;The main danger of side effects is that they're generally very difficult to track. The reason for that is because tracking global state, which anything can modify at any time, is very difficult. If uncontrolled, how could you possibly track changes made to the DOM over time? You may have to track so many things that it just wouldn't be feasible.&lt;/p&gt;

&lt;p&gt;Asynchronicity and race conditions also add to the complexity and difficulty of tracking side effects.&lt;/p&gt;

&lt;p&gt;Another downside of side effects is that code with side effects is generally harder to test.&lt;/p&gt;

&lt;h3&gt;
  
  
  Handling side effects
&lt;/h3&gt;

&lt;p&gt;Even though side effects are dangerous, they can be handled effectively.&lt;/p&gt;

&lt;h4&gt;
  
  
  Be pragmatic
&lt;/h4&gt;

&lt;p&gt;The most important point, as always, is to be pragmatic.&lt;/p&gt;

&lt;p&gt;You don't have to avoid all side effects to the extreme. You are only required to be careful with potentially incompatible code.&lt;/p&gt;

&lt;p&gt;For example, immutability is a good way to avoid many types of side effects. However, immutability makes little difference in the local scope of functions.&lt;/p&gt;

&lt;p&gt;For example, here are two functions that do the same thing. One uses immutability and the other doesn't.&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="nf"&gt;factorial1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&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;result&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="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;1&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;n&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;result&lt;/span&gt; &lt;span class="o"&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;result&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="nf"&gt;factorial2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&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;n&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&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="k"&gt;return&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;factorial2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example, &lt;code&gt;factorial1&lt;/code&gt; uses mutation. The values of &lt;code&gt;result&lt;/code&gt; and &lt;code&gt;i&lt;/code&gt; both change during execution.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;factorial2&lt;/code&gt; uses immutability. The values of the variables inside it never change during function execution.&lt;/p&gt;

&lt;p&gt;But it makes no difference. Other than some language limitations of recursion (which we'll ignore for this example), for all intents and purposes, &lt;code&gt;factorial1&lt;/code&gt; and &lt;code&gt;factorial2&lt;/code&gt; are exactly the same from the perspective of the caller.&lt;/p&gt;

&lt;p&gt;In fact, people tend to be less comfortable with recursion, so &lt;code&gt;factorial2&lt;/code&gt; could actually be the worse choice depending on your team.&lt;/p&gt;

&lt;p&gt;So be pragmatic and do what's best for your project.&lt;/p&gt;

&lt;h4&gt;
  
  
  Immutability
&lt;/h4&gt;

&lt;p&gt;Having said that, immutability is an easy way to avoid a large portion of side effects.&lt;/p&gt;

&lt;p&gt;By never modifying variables in your code unnecessarily, you remove a large problem. You won't have things changing unexpectedly. You also won't have to track the lifecycle of variables to know what values they contain.&lt;/p&gt;

&lt;p&gt;When starting with immutability, start simple. Then, over time, try to make as many things immutable in your work as possible.&lt;/p&gt;

&lt;p&gt;Instead of modifying a variable, create a new variable for the new value. Instead of modifying an object, create a new object with the new values you want.&lt;/p&gt;

&lt;p&gt;For example:&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="c1"&gt;// Example 1 - Don't do this&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;doubleArray&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="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="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="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;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="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="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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// mutates the original 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;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&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="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="nf"&gt;doubleArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Example 2 - Do this&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;double&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&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;x&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="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;doubleArray&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="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="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;double&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// returns a new array, without modifying the original&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;arr&lt;/span&gt; &lt;span class="o"&gt;=&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="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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;doubleArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In example 1, the original array is modified.&lt;/p&gt;

&lt;p&gt;In example 2 the original array is not modified. &lt;code&gt;doubleArray&lt;/code&gt; creates and returns a new array with the doubled values. Outside of the function, we create the new variable &lt;code&gt;result&lt;/code&gt; to hold the new array.&lt;/p&gt;

&lt;h5&gt;
  
  
  Immutability performance concerns
&lt;/h5&gt;

&lt;p&gt;Immutability may be slightly worse for performance. However, you probably shouldn't worry about that, because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;you shouldn't do premature optimisation for performance. Don't worry about performance except for the bottlenecks in your code.&lt;/li&gt;
&lt;li&gt;in most cases, immutability won't have a significant impact on performance&lt;/li&gt;
&lt;li&gt;you can use a performant immutable data structures library, such as Immer for JavaScript. It converts some operations from Big-O(n) time (such as copying an entire object) to Big-O(1) time.&lt;/li&gt;
&lt;li&gt;you can be pragmatic. You don't have to apply immutability in places where it would bottleneck performance.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Also, in some cases, immutability can improve performance by making things easier to run in parallel.&lt;/p&gt;

&lt;h4&gt;
  
  
  Avoid non-local scope
&lt;/h4&gt;

&lt;p&gt;Avoid accessing or modifying things that are not &lt;strong&gt;exclusively&lt;/strong&gt; in the local scope of your functions or methods. This means that it's probably okay to modify variables that originated in your local scope, but not variables which were passed in as arguments (originated outside of the local scope).&lt;/p&gt;

&lt;p&gt;If necessary, it's alright to mutate things up to instance or module scope.&lt;/p&gt;

&lt;p&gt;The further away from local scope you go, the more dangerous it gets, because things become more global. This makes things harder to track and introduces far-reaching dependencies in your code.&lt;/p&gt;

&lt;p&gt;Wherever possible:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;pass things in explicitly as arguments&lt;/li&gt;
&lt;li&gt;stick as close to local-scope as possible&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example:&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="c1"&gt;// Example 1 - Don't do this&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;doubleResult&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;result&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="c1"&gt;// Accesses and mutates a variable outside of the local scope&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;doubleResult&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Example 2 - Do this&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;double&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&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;n&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="c1"&gt;// Accesses parameter which is in local scope. Doesn't mutate anything&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;initialValue&lt;/span&gt; &lt;span class="o"&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;double&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;p&gt;In example 1, &lt;code&gt;doubleResult&lt;/code&gt; accesses &lt;code&gt;result&lt;/code&gt;, which is a variable outside of its local scope. It also mutates it, changing its value. Now, if any other code in the codebase accesses &lt;code&gt;result&lt;/code&gt;, it will see the new value.&lt;/p&gt;

&lt;p&gt;In example 2, &lt;code&gt;double&lt;/code&gt; only accesses its parameter, which is part of its local scope. It doesn't mutate any values outside of its local scope.&lt;/p&gt;

&lt;p&gt;In a real codebase, something resembling example 1 could be very difficult to track. The &lt;code&gt;result&lt;/code&gt; variable may be defined much further away from both the &lt;code&gt;doubleResult&lt;/code&gt; function as well as the function call. This makes it harder to track the value of &lt;code&gt;result&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Also, if &lt;code&gt;result&lt;/code&gt; isn't what you expect, you have a bug. For example, you may have already called &lt;code&gt;doubleResult&lt;/code&gt; 3 times but you may not remember.&lt;/p&gt;

&lt;p&gt;Overall, in example 1, you can't predict what a function that uses &lt;code&gt;result&lt;/code&gt; will do unless you know the exact value of &lt;code&gt;result&lt;/code&gt; at that time. To do this, you'll need to search and trace through the entire codebase to keep track of &lt;code&gt;result&lt;/code&gt; at all times.&lt;/p&gt;

&lt;p&gt;In the second example, &lt;code&gt;initialValue&lt;/code&gt; is always 5, so there are never any surprises. Also you can see what the function is doing immediately and can easily predict what will happen.&lt;/p&gt;

&lt;h4&gt;
  
  
  Be extremely careful
&lt;/h4&gt;

&lt;p&gt;Sometimes you can't just rely on immutability. For example, at some point, you must mutate the DOM or the database, or make a call to a third party API, or run some sort of side effect. As already mentioned, asynchronicity only adds to the problem.&lt;/p&gt;

&lt;p&gt;In this case, you just have to be extremely careful.&lt;/p&gt;

&lt;p&gt;Side effects are probably where the majority of the bugs in your codebase exist. They're the hardest code to understand and track.&lt;/p&gt;

&lt;p&gt;Regardless of what you do to try and manage them, you must always invest the required time and attention to them.&lt;/p&gt;

&lt;h4&gt;
  
  
  Separate pure and impure functionality
&lt;/h4&gt;

&lt;p&gt;For the most part, try to separate code with side effects and code without side effects. Your functions shouldn't both perform side effects and have "pure" code. They should do one or the other (within reason).&lt;/p&gt;

&lt;p&gt;This is also known as the &lt;a href="https://en.wikipedia.org/wiki/Command%E2%80%93query_separation" rel="noopener noreferrer"&gt;command-query separation principle&lt;/a&gt;. It's also an application of separation of concerns.&lt;/p&gt;

&lt;p&gt;For starters, something like writing to the database is very different to calculating what to write to the database. Those two concerns can change independently and for different reasons. As we examined in separation of concerns, they should be separated.&lt;/p&gt;

&lt;p&gt;Further, pure functions are generally easy to understand, reuse and test. Functions with side effects are not. Therefore, for your codebase to be easy to work with, you probably want as many functions as possible to be pure. This means that you should separate your pure functionality from your side effects.&lt;/p&gt;

&lt;p&gt;For example, instead of 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;function&lt;/span&gt; &lt;span class="nf"&gt;double&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&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;x&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="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;doubleArrayAndDisplayInDOM&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;// this function does a non-trivial calculation / operation and performs a side effect&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;doubled&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="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;double&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// (pretend this is a non-trivial calculation / operation)&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#foo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;doubled&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// writing to the DOM is a side effect&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;doubleArrayAndDisplayInDOM&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Do 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;function&lt;/span&gt; &lt;span class="nf"&gt;double&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&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;x&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="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;doubleArray&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;// this function only does a calculation / operation&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="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;double&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="nf"&gt;displayInDom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// this function only performs a side effect&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#foo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;content&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="nf"&gt;main&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;doubled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;doubleArray&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="nf"&gt;displayInDom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;doubled&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;h4&gt;
  
  
  Clear areas of responsibility
&lt;/h4&gt;

&lt;p&gt;As much as possible, you need to make sure that your code doesn't have conflicts. Code which performs side effects shouldn't conflict with other code performing other side effects at different times.&lt;/p&gt;

&lt;p&gt;A good way to do this is to have distinct areas of responsibility in your code.&lt;/p&gt;

&lt;p&gt;For example, if code A modifies element X in the DOM, then it should ideally be the only code which modifies that part of the DOM. All other code that needs to influence X should talk to code A to do so. That way tracking changes to element X is as easy as possible.&lt;/p&gt;

&lt;p&gt;Additionally, try to organise code dependencies well. For example, code A shouldn't run if any other code runs which would conflict with it. Also, code A shouldn't run if the state that it depends on isn't there or isn't what code A expects.&lt;/p&gt;

&lt;h4&gt;
  
  
  Side effects in pairs
&lt;/h4&gt;

&lt;p&gt;For side effects which come in pairs (e.g. open / close file), the function that started the side effect should also finish it.&lt;/p&gt;

&lt;p&gt;For example, instead of 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="cm"&gt;/* Note, this is pseudocode */&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;openFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fileName&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;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fileName&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;file&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;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;openFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;foo.txt&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="cm"&gt;/* Lots of other code in-between */&lt;/span&gt;

&lt;span class="nf"&gt;doStuffToFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Do 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="cm"&gt;/* Note, this is pseudocode */&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;useFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fileName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fn&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;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fileName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;useFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;foo.txt&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;doStuffToFile&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Robert Martin calls this technique "passing a block". The function &lt;code&gt;useFile&lt;/code&gt; both opens and closes the file, so it doesn't leave an open file pointer in the system.&lt;/p&gt;

&lt;p&gt;This ensures that the file will be closed when it's no longer needed.&lt;/p&gt;

&lt;p&gt;As for the functionality to perform on the file, that's passed into the function. It's the parameter &lt;code&gt;fn&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This ensures that you won't forget to finish the side effect later. It also provides good code organisation and makes the code easy to understand and track. The entire side effect is fully handled in one place.&lt;/p&gt;

&lt;h4&gt;
  
  
  Consider using a framework or functional programming language
&lt;/h4&gt;

&lt;p&gt;As with immutability, the best option might be to avoid side effects as much as possible.&lt;/p&gt;

&lt;p&gt;To help with this, you can consider delegating some of them to a framework, library, or functional programming language.&lt;/p&gt;

&lt;p&gt;For example, for working with the DOM, you can use a library such as &lt;a href="https://reactjs.org/" rel="noopener noreferrer"&gt;React&lt;/a&gt; (or one of the many alternatives).&lt;/p&gt;

&lt;p&gt;Something like React handles all of the DOM-related side effects. Then, in your application, you just write pure functions. You don't modify the DOM directly. Instead, your functions generate an object that represents what the DOM should look like.&lt;/p&gt;

&lt;p&gt;This is good for you, because working with pure functions is much easier than working with side effects.&lt;/p&gt;

&lt;p&gt;As for actually modifying the DOM, those side effects still occur, but they're React's problem now.&lt;/p&gt;

&lt;p&gt;Additionally, the parent / child hierarchy of React ensures that your DOM manipulations won't conflict with each other and cause problems. For example, React code involving element X won't run if element X won't actually exist. This is an example of good organisation and structure in your code to prevent conflicts with other side effects.&lt;/p&gt;

&lt;p&gt;Of course, there are many more pros and cons to using something like this. But it's just an option for you to consider.&lt;/p&gt;




&lt;h2&gt;
  
  
  Further reading
&lt;/h2&gt;

&lt;p&gt;That was a high-level overview of what I consider to be the most important concepts for writing good code. I hope that this article helped you understand the reasoning, motivation and overview behind clean code and programming principles. Hopefully, this knowledge will help you when you go on to learn more programming principles, or find more practical examples of them.&lt;/p&gt;

&lt;p&gt;For the next step, I recommend learning clean code and programming principles more practically. Use a resource that explains the concepts with many examples and applications in code.&lt;/p&gt;

&lt;p&gt;I highly recommend looking into content created by &lt;a href="https://twitter.com/unclebobmartin" rel="noopener noreferrer"&gt;Robert Martin&lt;/a&gt;. For the "quick", free version, I found his lectures &lt;a href="https://www.youtube.com/watch?v=SVRiktFlWxI" rel="noopener noreferrer"&gt;Coding a better world together part 1&lt;/a&gt; and &lt;a href="https://www.youtube.com/watch?v=qnq9syXUuFE" rel="noopener noreferrer"&gt;Coding a better world together part 2&lt;/a&gt; to be some of the best programming videos I've ever watched. For more detail you might want to check out his book &lt;a href="https://www.amazon.co.uk/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882/" rel="noopener noreferrer"&gt;Clean Code&lt;/a&gt; or his videos &lt;a href="https://cleancoders.com/" rel="noopener noreferrer"&gt;Clean Coders&lt;/a&gt; (start with the fundamentals series and the SOLID principles). I've learned a lot from Robert Martin's resources. I especially like that he explains the principles very practically, giving many practical examples of each and a lot of information in general.&lt;/p&gt;

&lt;p&gt;I also found the book &lt;a href="https://www.amazon.co.uk/Pragmatic-Programmer-Andrew-Hunt/dp/020161622X" rel="noopener noreferrer"&gt;The Pragmatic Programmer&lt;/a&gt; very good. Some of the details are outdated, but the concepts are not. That book truly hammers in the concept of being pragmatic. If anyone reads the &lt;a href="https://www.amazon.co.uk/Pragmatic-Programmer-journey-mastery-Anniversary/dp/0135957052/" rel="noopener noreferrer"&gt;20th anniversary edition of The Pragmatic Programmer&lt;/a&gt; please let me know what you thought. It's on my list but I haven't read it yet.&lt;/p&gt;

&lt;p&gt;I'm sure there are other amazing resources as well, but these are the ones I'm familiar with and can personally recommend.&lt;/p&gt;

&lt;p&gt;Finally, I recommend thinking about the programming principles yourself. Challenge them, consider where they might be useful or not be. Spend time on your own and consider everything that this article discussed.&lt;/p&gt;

&lt;p&gt;Alright, if you have any comments, feedback, or even counter-arguments to what this article discussed, please let me know in the comments. I'm always happy for a discussion. See you next time.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Challenge - Function that only runs with no arguments (intermediate / advanced functional programming)</title>
      <dc:creator>Spyros Argalias</dc:creator>
      <pubDate>Tue, 04 May 2021 09:59:27 +0000</pubDate>
      <link>https://dev.to/programmingduck/challenge-function-that-only-runs-with-no-arguments-intermediate-advanced-functional-programming-46b1</link>
      <guid>https://dev.to/programmingduck/challenge-function-that-only-runs-with-no-arguments-intermediate-advanced-functional-programming-46b1</guid>
      <description>&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/Sz-m3i66DZs"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;This is an intermediate / advanced functional programming challenge I had to solve in an interview. I found it very interesting and it had me thinking for a while.&lt;/p&gt;

&lt;p&gt;If you're interested in a brain teaser, have a go. You can also complete it using classes and methods if you prefer, but we'll use functions in this article.&lt;/p&gt;

&lt;p&gt;(If you don't know a lot about functional programming and want to look at that first, then check out &lt;a href="https://programmingduck.com/articles/functional-programming-introduction"&gt;functional programming - the ultimate beginner's guide&lt;/a&gt;.)&lt;/p&gt;

&lt;h2&gt;
  
  
  The challenge
&lt;/h2&gt;

&lt;p&gt;There were two parts to the challenge. Part 1 and part 2. Each part increases in difficulty.&lt;/p&gt;

&lt;p&gt;I've also added a small part 3.&lt;/p&gt;

&lt;h3&gt;
  
  
  Challenge, part 1
&lt;/h3&gt;

&lt;p&gt;Design a function that you can keep calling in a chain as long as you keep passing in arguments. For example, &lt;code&gt;foo(argument1)(argument2)&lt;/code&gt;. During this time, it shouldn't do anything except collect the arguments. Finally, when you call the function without an argument, it should call a real function, such as &lt;code&gt;console.log&lt;/code&gt;, with the arguments you've passed in so far.&lt;/p&gt;

&lt;p&gt;Here is a code example showing how this function should behave:&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;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// nothing happens&lt;/span&gt;
&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// nothing happens&lt;/span&gt;

&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// logs an empty line (calls console.log())&lt;/span&gt;
&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)();&lt;/span&gt; &lt;span class="c1"&gt;// logs 'a' (calls console.log('a'))&lt;/span&gt;
&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)()&lt;/span&gt; &lt;span class="c1"&gt;// logs 'a b' (calls console.log('a', 'b'))&lt;/span&gt;
&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;c&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)()&lt;/span&gt; &lt;span class="c1"&gt;// logs 'a b c' (calls console.log('a', 'b', 'c'))'&lt;/span&gt;

&lt;span class="c1"&gt;// optionally, the function can accept more than one argument at once&lt;/span&gt;
&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)()&lt;/span&gt; &lt;span class="c1"&gt;// same as foo('a')('b')()&lt;/span&gt;
&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;c&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)()&lt;/span&gt; &lt;span class="c1"&gt;// same as foo('a')('b')('c')()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Challenge, part 2
&lt;/h3&gt;

&lt;p&gt;Implement a function similar to question 1, but make it "stateless"?&lt;/p&gt;

&lt;p&gt;Different function calls shouldn't share state with each other.&lt;/p&gt;

&lt;p&gt;For example:&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&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;ab&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;b&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;abc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ab&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;c&lt;/span&gt;&lt;span class="dl"&gt;'&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="c1"&gt;// logs 'a' (calls console.log('a'))&lt;/span&gt;
&lt;span class="nx"&gt;abc&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// logs 'a b c' (calls console.log('a', 'b', 'c'))&lt;/span&gt;
&lt;span class="nx"&gt;ab&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// logs 'a b' (calls console.log('a', 'b'))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The difference compared to part 1, is that calling &lt;code&gt;a('b')&lt;/code&gt; shouldn't also add the argument &lt;code&gt;'b'&lt;/code&gt; to the call for &lt;code&gt;a()&lt;/code&gt;. &lt;code&gt;a()&lt;/code&gt; should log &lt;code&gt;'a'&lt;/code&gt;, not &lt;code&gt;'a b'&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Challenge, part 3
&lt;/h3&gt;

&lt;p&gt;Make a function similar to the one for part 2, but the first time it's called it must be called with a single argument. That argument should be the function it will run in the end.&lt;/p&gt;

&lt;p&gt;Then, as normal, the function should keep accepting and collecting arguments. When it's finally called without an argument, it should execute the function you passed in as the first argument with the arguments it has collected.&lt;/p&gt;

&lt;p&gt;For example:&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;log&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;actWhenCalledWithoutArguments&lt;/span&gt;&lt;span class="p"&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="kd"&gt;const&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;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&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;ab&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;b&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;abc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ab&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;c&lt;/span&gt;&lt;span class="dl"&gt;'&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="c1"&gt;// logs an empty line (calls console.log())&lt;/span&gt;
&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// logs 'a' (calls console.log('a'))&lt;/span&gt;
&lt;span class="nx"&gt;abc&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// logs 'a b c' (calls console.log('a', 'b', 'c'))&lt;/span&gt;
&lt;span class="nx"&gt;ab&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// logs 'a b' (calls console.log('a', 'b'))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Hints - Part 1
&lt;/h2&gt;

&lt;p&gt;So what would your solution to these questions be? Before reading on, try solving them on your own.&lt;/p&gt;

&lt;p&gt;If you need help, read on for some hints.&lt;/p&gt;

&lt;h3&gt;
  
  
  Hint 1 - Currying
&lt;/h3&gt;

&lt;p&gt;For the first hint, notice that these functions sound a lot like currying. Curried functions also accept arguments multiple times, before finally executing the "real functionality" with all of the arguments they've collected. This is very similar to what you're trying to do in this challenge.&lt;/p&gt;

&lt;p&gt;In particular, the &lt;code&gt;curry&lt;/code&gt; utility seems very relevant. That's a function that takes a normal function, which isn't curried, and turns it into a curried function. In its implementation, it has a mechanism for storing arguments and then calling a function with all of the stored arguments later.&lt;/p&gt;

&lt;p&gt;So, you may want to start by implementing the &lt;code&gt;curry&lt;/code&gt; utility first. Looking at its source code may be helpful for the challenges.&lt;/p&gt;

&lt;p&gt;Feel free to try to implement it yourself.&lt;/p&gt;

&lt;p&gt;Otherwise, here is an example implementation:&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;curry&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;arity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fn&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="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&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;args&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;arity&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;fn&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&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;gatherMoreArgs&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;moreArgs&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;execute&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;moreArgs&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;gatherMoreArgs&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;execute&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// example usage&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;foo&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="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&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="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="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;d&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;curriedFoo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;curry&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;foo&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="nx"&gt;curriedFoo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;c&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;d&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// logs 'a b c d' (console.logs('a', 'b', 'c', 'd'))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Hint 2 - More functions
&lt;/h3&gt;

&lt;p&gt;For the challenges, you don't need to have just one top-level function. Just like the &lt;code&gt;curry&lt;/code&gt; utility, you can have more functions. The &lt;code&gt;curry&lt;/code&gt; utility itself is made up of 3 nested functions in total.&lt;/p&gt;

&lt;p&gt;If you're stuck on your solution, explore what you can do with 1 function, 2 functions or even 3 functions, nested or not.&lt;/p&gt;

&lt;h3&gt;
  
  
  Hint 3 - How to gather arguments for part 1
&lt;/h3&gt;

&lt;p&gt;This hint is more spoiler-heavy. It talks about a possible implementation for part 1.&lt;/p&gt;

&lt;p&gt;One way to gather the arguments is to have an array. Then, every time the function is called with arguments, push them to that array.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution - Part 1
&lt;/h2&gt;

&lt;p&gt;Alright, if you're ready, here's the solution for part 1:&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;logWhenCalledWithoutArguments&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;argsSoFar&lt;/span&gt; &lt;span class="o"&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;execute&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;moreArgs&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;moreArgs&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;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;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="nx"&gt;argsSoFar&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;argsSoFar&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;moreArgs&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;execute&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;execute&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&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;logWhenCalledWithoutArguments&lt;/code&gt; is a higher order function (a function that returns another function). This is needed so that it can create a private array (&lt;code&gt;argsSoFar&lt;/code&gt;), that exists only for that invocation of &lt;code&gt;logWhenCalledWithoutArguments&lt;/code&gt;. That way, different calls of &lt;code&gt;logWhenCalledWithoutArguments&lt;/code&gt; won't share the same arguments.&lt;/p&gt;

&lt;p&gt;Other than that, the &lt;code&gt;execute&lt;/code&gt; function does the real work. First, it checks whether it was called without arguments. If that's the case, then it calls &lt;code&gt;console.log&lt;/code&gt; with all of the arguments collected so far. Otherwise, it adds the new arguments to &lt;code&gt;argsSoFar&lt;/code&gt;. Then, it returns itself so that the user can call the function again and keep providing arguments.&lt;/p&gt;

&lt;p&gt;At the end of &lt;code&gt;logWhenCalledWithoutArguments&lt;/code&gt;, we immediately call the &lt;code&gt;execute&lt;/code&gt; function and return its result. That way, we start checking and collecting arguments from the first call.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hint - Part 2 - Making it stateless
&lt;/h2&gt;

&lt;p&gt;This is another spoiler hint.&lt;/p&gt;

&lt;p&gt;Part 2 of the solution can't share state like part 1 did. With the solution for part 1, this code wouldn't work:&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;logWhenCalledWithNoArguments&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&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;ab&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;'&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="c1"&gt;// incorrectly logs 'a b'&lt;/span&gt;
&lt;span class="nx"&gt;ab&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// correctly logs 'a b'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So you can't use a shared array like in part 1.&lt;/p&gt;

&lt;p&gt;Instead, you have to find a way to pass in the arguments across different calls using closures.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution - Part 2
&lt;/h2&gt;

&lt;p&gt;Alright, if you're ready, here's the solution to part 2.&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;logWhenCalledWithoutArguments&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;firstArgs&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;gatherArgs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;execute&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;moreArgs&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;moreArgs&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;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="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="nx"&gt;args&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;gatherArgs&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;moreArgs&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;execute&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;firstExecute&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;gatherArgs&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;firstExecute&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;firstArgs&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function has a lot of similarities to the solution for part 1. The main difference is that, instead of capturing arguments in an array, we use the function &lt;code&gt;gatherArgs&lt;/code&gt; to hold the arguments.&lt;/p&gt;

&lt;p&gt;Also, just like before, we run the &lt;code&gt;execute&lt;/code&gt; function straight away, to check if we got an argument on the first call.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution - Part 3
&lt;/h2&gt;

&lt;p&gt;For part 3, the first call must be a single argument, the function to execute at the end. Everything else is the same.&lt;/p&gt;

&lt;p&gt;If you're ready, here's the solution:&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;actWhenCalledWithoutArguments&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fn&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;gatherArgs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;execute&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;moreArgs&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;moreArgs&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;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="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&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;gatherArgs&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;moreArgs&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;execute&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;gatherArgs&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The solution is very similar to the solution for part 2.&lt;/p&gt;

&lt;p&gt;The main difference is that we don't need to check for an argument on the first call. As a result, we don't need to run &lt;code&gt;execute&lt;/code&gt; straight away. All we have to do is return it.&lt;/p&gt;

&lt;p&gt;The other difference is that we replace the call to &lt;code&gt;console.log&lt;/code&gt; with &lt;code&gt;fn&lt;/code&gt; (the first argument).&lt;/p&gt;

&lt;p&gt;Other than that, everything else is the same.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final notes
&lt;/h2&gt;

&lt;p&gt;Anyway, that's it for this challenge.&lt;/p&gt;

&lt;p&gt;I hope you enjoyed it.&lt;/p&gt;

&lt;p&gt;Let me know if you completed it, and whether you thought it was easy or difficult, in the comments. It's not the kind of thing you have to implement at work every day.&lt;/p&gt;

&lt;p&gt;Also, if you have any similar challenges, or if you have any tips or feedback for this one, then please leave a comment.&lt;/p&gt;

&lt;p&gt;Alright, see you next time.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>functional</category>
      <category>learning</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How to write clean code units (functions and methods)</title>
      <dc:creator>Spyros Argalias</dc:creator>
      <pubDate>Mon, 26 Apr 2021 17:35:16 +0000</pubDate>
      <link>https://dev.to/programmingduck/how-to-write-clean-code-units-functions-and-methods-16l2</link>
      <guid>https://dev.to/programmingduck/how-to-write-clean-code-units-functions-and-methods-16l2</guid>
      <description>&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/LfEMBnUrMuU"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Code units at the edges of your application should be small, independent and only do one thing.&lt;/p&gt;

&lt;p&gt;This is a direct application of many established &lt;a href="https://programmingduck.com/articles/programming-principles-introduction"&gt;programming principles&lt;/a&gt;. This includes KISS, separation of concerns, single responsibility principle and many more.&lt;/p&gt;

&lt;p&gt;So, to have "good code", apply this principle where possible.&lt;/p&gt;

&lt;p&gt;This article will show you how to apply it. It will also examine why this principle is important and how it makes your code better.&lt;/p&gt;

&lt;p&gt;To showcase, we'll use units at the edge of an application. They're easier to use to illustrate this point. But, after you learn the guidelines, you can apply them to any kind of code unit.&lt;/p&gt;

&lt;p&gt;Overall, the main point of this article can be summarized in the following example. Example 1 has a large &lt;code&gt;makeRequest&lt;/code&gt; function, which is worse than example 2. In example 2, that function has been separated into two smaller and more specific functions.&lt;/p&gt;

&lt;p&gt;Example 1, the bad version:&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;main&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;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getData&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nx"&gt;makeRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;makeRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;isValid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://myfakeapi.com/&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="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://errormonitoringservice.com/&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="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;Example 2, the good version:&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;main&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;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getData&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;isValid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;makeRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;reportError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;makeRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://myfakeapi.com/&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="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;reportError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://errormonitoringservice.com/&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="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;Let's examine why example 1 is worse.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note: In this article, a unit refers to a function / method / module / class. We'll be using functions, but any of them can be used.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Small, independent units
&lt;/h2&gt;

&lt;p&gt;An "edge" unit of code is a fairly small piece of functionality that doesn't have any dependencies. It does some fairly low-level stuff and it doesn't call any other functions to help it. It's at the extremities, the very edges, of your application.&lt;/p&gt;

&lt;p&gt;It's safe code that you call to help you do something.&lt;/p&gt;

&lt;p&gt;When you call it, you know what it's going to do and you know that it's not going to break anything.&lt;/p&gt;

&lt;p&gt;It should be like a well-tested library that you've imported into your project. It does something small and specific and you expect it to work 100% of the time.&lt;/p&gt;

&lt;p&gt;To do that, these kinds of units:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;should be small&lt;/li&gt;
&lt;li&gt;should only do one small, specific thing&lt;/li&gt;
&lt;li&gt;should be independent&lt;/li&gt;
&lt;li&gt;shouldn't have side effects, unless the only purpose of the unit is to perform a side effect&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Examples of good code units
&lt;/h2&gt;

&lt;p&gt;Here are some examples of these kinds of good units:&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;add&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="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;propertyName&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;object&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;propertyName&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;appendElementToBody&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="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;append&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="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;doubleSpeed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;speed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;speed&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="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;incrementSpeedDamaged&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;speed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;speed&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.5&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;incrementSpeed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;speed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;speed&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that these units:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;don't have conditionals (if / else statements)&lt;/li&gt;
&lt;li&gt;do very little&lt;/li&gt;
&lt;li&gt;don't read / write to anything except their parameters (except for &lt;code&gt;appendElementToBody&lt;/code&gt;, because the &lt;code&gt;document&lt;/code&gt; object is a global singleton)&lt;/li&gt;
&lt;li&gt;only have side effects if they do nothing else&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In comparison, here are some units that don't follow these guidelines:&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;valueToAdd&lt;/span&gt; &lt;span class="o"&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;function&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&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;valueToAdd&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;x&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;object&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* has some properties here*/&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;getProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;propertyName&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;object&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;propertyName&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;appendElementToBody&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="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;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;foo&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="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// do nothing&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;append&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="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;shouldDouble&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&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;doubleSpeed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;gameObject&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;shouldDouble&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;speed&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="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;incrementSpeed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;isDamaged&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;isDamaged&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;speed&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mf"&gt;0.5&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;gameObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;speed&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We'll examine each of them in detail, including what makes them good or bad.&lt;/p&gt;

&lt;p&gt;But first, let's examine the advantages and disadvantages of the guidelines in general. What are the benefits that you gain from the good code examples, rather than the bad ones?&lt;/p&gt;

&lt;h2&gt;
  
  
  Benefits of good code units
&lt;/h2&gt;

&lt;p&gt;If you follow the guidelines, you get the benefits of good code. Things such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;code that is easy to understand&lt;/li&gt;
&lt;li&gt;code that works correctly, predictably, without unintended consequences&lt;/li&gt;
&lt;li&gt;code that is easy to reuse&lt;/li&gt;
&lt;li&gt;code that is easy to change&lt;/li&gt;
&lt;li&gt;code that is easy to test&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you use the bad versions, you get the opposite. Things such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;code that is harder to understand&lt;/li&gt;
&lt;li&gt;code that is not predictable, can have unintended consequences, is harder to track and easier to get wrong&lt;/li&gt;
&lt;li&gt;code that is not reusable&lt;/li&gt;
&lt;li&gt;code that is brittle and hard to change&lt;/li&gt;
&lt;li&gt;code that's much harder to test&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Next, let's see how the examples given affect these benefits / disadvantages.&lt;/p&gt;

&lt;h2&gt;
  
  
  Examining examples of code units and their benefits
&lt;/h2&gt;

&lt;p&gt;Let's go through each example one by one. Some will be more trivial and faster than others.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example: add
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;add&lt;/code&gt; function is trivially simple.&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;add&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="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, it showcases the point of good units well. This function is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;extremely simple to understand&lt;/li&gt;
&lt;li&gt;reusable every time you need it&lt;/li&gt;
&lt;li&gt;extremely easy to test&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One thing you may be wondering is "so what"? Why should you have an &lt;code&gt;add&lt;/code&gt; function when you can just add things inline when you need to?&lt;/p&gt;

&lt;p&gt;Well, let's just say that there are many valid reasons to have one. For example, you may need to pass it into a higher order function like &lt;code&gt;map&lt;/code&gt;, or to use partial application.&lt;/p&gt;

&lt;p&gt;In addition, &lt;code&gt;add&lt;/code&gt; just showcases the principle. Instead of &lt;code&gt;add&lt;/code&gt; you might have some real functionality which works exactly like &lt;code&gt;add&lt;/code&gt; internally. For example, you may have a function &lt;code&gt;formUserGreeting(username, userFlair)&lt;/code&gt;, which may concatenate (add) the &lt;code&gt;username&lt;/code&gt; and &lt;code&gt;userFlair&lt;/code&gt; together.&lt;/p&gt;

&lt;p&gt;Here is the bad version of the &lt;code&gt;add&lt;/code&gt; code:&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;valueToAdd&lt;/span&gt; &lt;span class="o"&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;function&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&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;valueToAdd&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This version is much worse.&lt;/p&gt;

&lt;p&gt;For starters, it has a weird signature which you might not expect. If you were working in some file &lt;code&gt;foo&lt;/code&gt; and you imported this function to use it, you probably wouldn't remember or expect it to work the way that it does. It would confuse you for a moment until you examined the function closer.&lt;/p&gt;

&lt;p&gt;This breaks the principle of least astonishment (one of the fundamental principles). When something works differently to how you expect, it's easy to create bugs.&lt;/p&gt;

&lt;p&gt;This function is also more difficult to understand. You have to spend additional time to read the source code of this function before you understand how it works.&lt;/p&gt;

&lt;p&gt;Also, it's not reusable. It always adds 5 to the number you provide. This means that you can never reuse it unless you want to add 5.&lt;/p&gt;

&lt;p&gt;So overall, it's much worse.&lt;/p&gt;

&lt;p&gt;To create the good version, make sure that the function it only accesses its local scope. It should receive everything it needs to work as an argument. It shouldn't access anything else.&lt;/p&gt;

&lt;p&gt;Finally, it takes no effort to have the better version, so you might as well have it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example: getProperty
&lt;/h3&gt;

&lt;p&gt;Next is the &lt;code&gt;getProperty&lt;/code&gt; example.&lt;/p&gt;

&lt;p&gt;Here is the code for the good version:&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;getProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;propertyName&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;object&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;propertyName&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;Here is the code for the bad version:&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;object&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* has some properties here*/&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;getProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;propertyName&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;object&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;propertyName&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The benefits / disadvantages are the same as the &lt;code&gt;add&lt;/code&gt; example.&lt;/p&gt;

&lt;p&gt;The good version is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;100% predictable&lt;/li&gt;
&lt;li&gt;easy to understand&lt;/li&gt;
&lt;li&gt;easy to reuse&lt;/li&gt;
&lt;li&gt;easy to test&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The bad version has a signature that a developer may not expect until they look at the code. It's also not reusable if you want to work with a different object.&lt;/p&gt;

&lt;p&gt;To get the good version, write the function in a way where it doesn't read anything outside of its local scope.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example: appendElementToDom
&lt;/h3&gt;

&lt;p&gt;Now we're starting to examine functions that may seem more realistic. These are functions that you probably have in your codebase (or something similar to them).&lt;/p&gt;

&lt;p&gt;Here is the good version:&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;appendElementToBody&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="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;append&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the bad version:&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;appendElementToBody&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="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;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;foo&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="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// do nothing&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;append&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second version of the code is concerning. It has a conditional that's not obvious to a user of the function unless they look at its source code.&lt;/p&gt;

&lt;p&gt;Consider, if you use a function named &lt;code&gt;appendElementToBody&lt;/code&gt;, what would you expect it to do?&lt;/p&gt;

&lt;p&gt;You would probably expect it to append an HTML element to the body element, 100% of the time, not just some of the time.&lt;/p&gt;

&lt;p&gt;Also consider, when you import a library to use in a project, you expect it to do what it says on the tin. You don't expect it to have hidden conditions where it sometimes does what you expect, other times it doesn't do anything and other times it does something different altogether.&lt;/p&gt;

&lt;p&gt;The problem with this code is the following scenario:&lt;/p&gt;

&lt;p&gt;Tomorrow, you realise that have a bug in your program. It turns out that whenever a user creates a particular todo list item, it doesn't get added to the DOM. Maybe it doesn't get added to the database either (you may have a similar condition there).&lt;/p&gt;

&lt;p&gt;In this situation, unless you specifically remember how the &lt;code&gt;appendElementToBody&lt;/code&gt; works (read: you already know where the bug is), it will probably take you a few hours to find the bug.&lt;/p&gt;

&lt;p&gt;Most likely, you're going to trace the code from the start, from where the user clicks "submit" for the new todo. The &lt;code&gt;appendElementToBody&lt;/code&gt; is the last function that gets run, so you might not examine it for a long time.&lt;/p&gt;

&lt;p&gt;Now, this example is very small and trivial. It's unlikely that you'll run into trouble for checking if an element has an ID of &lt;code&gt;foo&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But it's not difficult to see how something like this can become a problem under different circumstances. You may have more complicated conditions. You may also have conditions in many functions all over your codebase.&lt;/p&gt;

&lt;p&gt;Something, at some point, will cause a bug. In the meantime, there could already be bugs without anyone realising.&lt;/p&gt;

&lt;p&gt;Anyway, that's enough of a rant. The point is, don't do this.&lt;/p&gt;

&lt;h4&gt;
  
  
  Possible improvements
&lt;/h4&gt;

&lt;p&gt;Your unit functions should be 100% predictable and do one small thing. They shouldn't have conditionals in them. That's not their responsibility or where that conditional logic should be.&lt;/p&gt;

&lt;p&gt;Most of all, they shouldn't have implicit (unexpected and non-obvious) conditions like this.&lt;/p&gt;

&lt;p&gt;Explicit conditionals are at least predictable. Something like this would be better:&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;appendElementToBody&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;excludedSelectors&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="nx"&gt;excludedSelectors&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;selector&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;excludedSelectors&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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;selector&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="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// exit the function and do nothing&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;append&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A better option may be to change the name of the function so its functionality is obvious:&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;maybeAppendElementToBody&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;excludedSelectors&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="nx"&gt;excludedSelectors&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;selector&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;excludedSelectors&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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;selector&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="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// exit the function and do nothing&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;append&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this version, the function acts predictably. It does nothing for particular selectors, but at least you expect that.&lt;/p&gt;

&lt;p&gt;But, for the best improvements, consider:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;rethinking your program design so you don't need the condition&lt;/li&gt;
&lt;li&gt;putting the condition in a higher-level function. "Move the logic up", so to speak, to a more appropriate place.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, you could have something 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="c1"&gt;// Extremely simple TODO creator with very basic code&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;todos&lt;/span&gt; &lt;span class="o"&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;handleNewTodoSubmit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="c1"&gt;// read the DOM to see what the user has typed as the TODO title&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#todo-input&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// condition is checked here (albeit slightly altered to the original)&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;doesTodoTitleAlreadyExist&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;title&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;todo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;createTodoObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;todos&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;todo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;displayTodo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todo&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;doesTodoTitleAlreadyExist&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;title&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;hasTargetTitle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todo&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;todo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;title&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;todos&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="nx"&gt;hasTargetTitle&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// returns true if any of a todo in the array has the same title&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;createTodoObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;title&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;title&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;displayTodo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todo&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;todoElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;createTodoElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;appendElementToBody&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todoElement&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;createTodoElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todo&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;todoElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;div&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;todoElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;todoElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&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;todoElement&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;appendElementToBody&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="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;append&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="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;todoForm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#todo-form&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;todoForm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;submit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;handleNewTodoSubmit&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 example code, every function, including &lt;code&gt;appendElementToBody&lt;/code&gt;, does what you expect 100% of the time.&lt;/p&gt;

&lt;p&gt;The validation of the todo was moved from &lt;code&gt;appendElementToBody&lt;/code&gt; to &lt;code&gt;handleNewTodoSubmit&lt;/code&gt;. This is a much more appropriate place for it.&lt;/p&gt;

&lt;p&gt;The correct way to think about it is that the todo should not be created if it already exists. That's the domain of the &lt;code&gt;handleNewTodoSubmit&lt;/code&gt; function, not of the &lt;code&gt;appendElementToBody&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;In other words, the check is now at a place where you would expect it to be. This means that debugging will be easier if there is a problem, because you'll find the relevant code faster.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example: doubleSpeed
&lt;/h3&gt;

&lt;p&gt;Code for the good version of &lt;code&gt;doubleSpeed&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;doubleSpeed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;speed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;speed&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Code for the bad version of &lt;code&gt;doubleSpeed&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;let&lt;/span&gt; &lt;span class="nx"&gt;shouldDouble&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&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;doubleSpeed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;gameObject&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;shouldDouble&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;currentSpeed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;speed&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;speed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;currentSpeed&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="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This example is the same as the &lt;code&gt;appendElementToBody&lt;/code&gt; example.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;doubleSpeed&lt;/code&gt; should do what it says on the tin. It shouldn't have implicit conditions where it does what you expect sometimes and nothing at other times. That's unexpected and can only lead to trouble.&lt;/p&gt;

&lt;p&gt;Instead, some code higher up should decide if it needs to call it in the first place. Then it can either call it or not call it.&lt;/p&gt;

&lt;p&gt;The benefits of the good version of the code are that it's:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;predictable, easy to track and less likely to have weird bugs that depend on weird state and time&lt;/li&gt;
&lt;li&gt;easy to understand&lt;/li&gt;
&lt;li&gt;reusable. You can reuse this function anywhere in the codebase. However, you can't reuse the bad version unless you need the exact same condition.&lt;/li&gt;
&lt;li&gt;easy to test. The bad version is practically impossible to test (because your test file can't modify the variable &lt;code&gt;shouldDouble&lt;/code&gt;, unless you do a lot of work to circumvent that).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example: incrementSpeed
&lt;/h3&gt;

&lt;p&gt;This example showcases why you should avoid having Boolean parameters.&lt;/p&gt;

&lt;p&gt;Here is the good version of the code:&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;incrementSpeedDamaged&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;speed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;speed&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.5&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;incrementSpeed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;speed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;speed&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the bad version of the code:&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;incrementSpeed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;isDamaged&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;isDamaged&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;speed&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="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;speed&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mf"&gt;0.5&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;Does the Boolean parameter matter?&lt;/p&gt;

&lt;p&gt;Yes, it does. Not a tremendous amount in this example, but it's definitely worse.&lt;/p&gt;

&lt;p&gt;One problem with Boolean parameters is that they multiply the number of code paths in the function. In other words, there is an &lt;code&gt;if / else&lt;/code&gt; statement in there.&lt;/p&gt;

&lt;p&gt;For example:&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="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;booleanParameter&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;booleanParameter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;doSomething&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;doSomethingElse&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;Every additional Boolean parameter can double the number of possible code paths.&lt;/p&gt;

&lt;p&gt;For example, with two Boolean parameters, this is what the code may look like. Pay particular attention to the &lt;code&gt;sendData&lt;/code&gt; function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;sendData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;isValid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;isDataFormatted&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;isDataValid&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;isDataFormatted&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;formatData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://myfakeapi.com&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="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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="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;isDataFormatted&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;formatInvalidData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://myfakeapi.com/errors&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="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;formatData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;formatInvalidData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Error: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;data&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;main&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;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;123&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// get data from somewhere&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isDataValid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;validateData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;isDataFormatted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;sendData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;isDataValid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;isDataFormatted&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;sendData&lt;/code&gt; function is quite complicated. It's difficult to understand and read through it. It has nested conditionals, which make code harder to understand and work with.&lt;/p&gt;

&lt;p&gt;It's also not reusable, unless you need those exact conditions and arguments elsewhere. In particular, if you need if more conditions tomorrow, you'll need to add even more code to &lt;code&gt;sendData&lt;/code&gt; to handle them. This means that &lt;code&gt;sendData&lt;/code&gt; may grow over time, and become even more complicated.&lt;/p&gt;

&lt;p&gt;It's also difficult to test. You need tests covering each possible code path.&lt;/p&gt;

&lt;p&gt;In short, it's difficult to work with and it can become even more complicated in the future.&lt;/p&gt;

&lt;p&gt;The better version is to have simple unit functions, that only do one thing, without conditionals. For example:&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;sendData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://myfakeapi.com&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="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;reportDataError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://myfakeapi.com/errors&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="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;formatData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;formatIvalidData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Error: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;data&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;main&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;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;123&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// get data from somewhere&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isDataValid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;validateData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;isDataValid&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;formattedData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;formatData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;sendData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;formattedData&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;formattedData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;formatInvalidData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;reportDataError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;formattedData&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;Notice that the &lt;code&gt;sendData&lt;/code&gt; function is now trivially simple.&lt;/p&gt;

&lt;p&gt;You may be thinking "but those conditions just moved to the &lt;code&gt;main&lt;/code&gt; function, isn't that the same thing?" That's a fair argument. However, this code still has some advantages. In this version:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the unit functions are simple and easy to understand&lt;/li&gt;
&lt;li&gt;the unit functions are reusable all over the codebase. If you need to handle new conditions, you can handle them in a different high-level function like &lt;code&gt;main&lt;/code&gt; and still reuse the small unit functions.&lt;/li&gt;
&lt;li&gt;the unit functions are trivial to test&lt;/li&gt;
&lt;li&gt;the program in general is easier to modify or extend if you need more functionality&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A more important reason is how the good version of the code can grow tomorrow, versus the bad version of the code.&lt;/p&gt;

&lt;p&gt;For example, if new conditions arise tomorrow, the good version of the code may end up 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="c1"&gt;// We've kept the unit functions like sendData, but they're omitted for brevity&lt;/span&gt;

&lt;span class="c1"&gt;// More simple functions for new use-cases&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;validateDataADifferentWay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;validateSpecialData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;main1&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;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;123&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// get data from somewhere&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isDataValid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;validateData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;isDataValid&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;formattedData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;formatData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;sendData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;formattedData&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;formattedData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;formatInvalidData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;reportDataError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;formattedData&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;main2&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;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;123&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// get data from somewhere, it should always be valid&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;speciallyFormattedData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;formatDataADifferentWay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;sendData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;speciallyFormattedData&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;main3&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;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;123&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// get data from somewhere&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isDataValid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;validateSpecialData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;isDataValid&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;formattedData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;formatData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;formattedData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;formatInvalidData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;reportDataError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;formattedData&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;This is quite good.&lt;/p&gt;

&lt;p&gt;The unit functions we had are still 100% the same. We handle the new conditions in the different &lt;code&gt;main&lt;/code&gt; functions which aren't too complicated. For new, specific functionality, we've created the new unit functions &lt;code&gt;validateSpecialData&lt;/code&gt; and &lt;code&gt;formatDataADifferentWay&lt;/code&gt;. (We've omitted the implementations for brevity.)&lt;/p&gt;

&lt;p&gt;However, the bad version of the code wouldn't fare so well. Every new condition would be handled in &lt;code&gt;sendData&lt;/code&gt;. As a result, &lt;code&gt;sendData&lt;/code&gt; would become much more complicated.&lt;/p&gt;

&lt;p&gt;Consider this example where we add a Boolean parameter &lt;code&gt;needsSpecialFormatting&lt;/code&gt;. It's a flag which says that we should format the data in a different way:&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;sendData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;isValid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;isDataFormatted&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;needsSpecialFormatting&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;isValid&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;isDataFormatted&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;needsSpecialFormatting&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;formatDataADifferentWay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;formatData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://myfakeapi.com&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="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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="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;isDataFormatted&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;needsSpecialFormatting&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;formattedData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;formatDataADifferentWay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;formattedData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;formatInvalidData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://myfakeapi.com/errors&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="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;main1&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;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;123&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// get data from somewhere&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isDataValid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;validateData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;isDataFormatted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;sendData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;isDataValid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;isDataFormatted&lt;/span&gt;&lt;span class="p"&gt;,&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;main2&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;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;123&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// get data from somewhere, it will always be valid&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;speciallyFormattedData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;formatDataADifferentWay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;sendData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;main3&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;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;123&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// get data from somewhere&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isDataValid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;validateSpecialData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;isDataValid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;sendData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&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;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;sendData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, with one more Boolean parameter, &lt;code&gt;sendData&lt;/code&gt; is becoming much more complicated. Things would become even worse as more parameters are added.&lt;/p&gt;

&lt;p&gt;On top of that, even the call for &lt;code&gt;sendData(data, true, false, false)&lt;/code&gt; is difficult to look at. It's a mental exercise trying to match each Boolean to the parameter it represents. It's possible to improve this by making &lt;code&gt;sendData&lt;/code&gt; accept an object instead, but it's still more effort than the simple version.&lt;/p&gt;

&lt;p&gt;In addition, what &lt;code&gt;sendData&lt;/code&gt; does may be unexpected at first glance by a programmer who's not familiar with the code. As mentioned earlier, a programmer would expect that function to send some data and call it a day, not to do anything else. After all, the function's name is &lt;code&gt;sendData&lt;/code&gt;, not &lt;code&gt;send_data_if_valid_otherwise_report_error_and_also_format_the_data_if_needed&lt;/code&gt; (used underscore case to make it easier to read).&lt;/p&gt;

&lt;p&gt;Finally, this function is breaking many of the programming principles, because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it does many things, which breaks separation of concerns / single responsibility principle&lt;/li&gt;
&lt;li&gt;it's not simple, which breaks KISS&lt;/li&gt;
&lt;li&gt;it has many conditions with logic coupled together, which makes it more error prone to change. This breaks the goal of programming principles themselves, which is that code should be easy to work with.&lt;/li&gt;
&lt;li&gt;it's not reusable for different conditions unless you add even more logic. This breaks the open-closed principle.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So instead, prefer small unit functions that only do one thing. If you have to pass a Boolean to a function, consider splitting it into two functions instead. One will handle the &lt;code&gt;true&lt;/code&gt; case and the other will handle the &lt;code&gt;false&lt;/code&gt; case.&lt;/p&gt;

&lt;h2&gt;
  
  
  Linking back to programming principles
&lt;/h2&gt;

&lt;p&gt;The main thing to keep in mind is that these guidelines are just applications of the core programming principles. That includes KISS, the principle of least astonishment, separation of concerns / single responsibility principle and handling side effects well.&lt;/p&gt;

&lt;p&gt;All of those principles point towards functions that tend to be small, only do one thing, are reusable, easy to understand, easy to change and easy to test.&lt;/p&gt;

&lt;p&gt;Additionally, someone who understands those principles well would naturally create code units like the ones described in this article.&lt;/p&gt;

&lt;p&gt;So the point of this article isn't necessarily to be prescriptive about how to create small units. Instead, think of it as an example for how to apply those principles in this situation.&lt;/p&gt;

&lt;p&gt;In other words, it's a specific use-case to help you become more familiar with these principles in general. That way, you can apply them everywhere, without having to learn how to handle an infinite number of individual use-cases like this one.&lt;/p&gt;

&lt;p&gt;So, to write even better code, I recommend looking at the programming principles more closely. To do that, you can have a look at &lt;a href="https://programmingduck.com/articles/programming-principles-introduction"&gt;clean code and programming principles - the ultimate beginner's guide&lt;/a&gt;, which is a crash course on some fundamental programming principles.&lt;/p&gt;

&lt;h2&gt;
  
  
  Applying these guidelines to other code units
&lt;/h2&gt;

&lt;p&gt;We examined functions at the edge of an application because those can afford to be simple. Other functions may be more complicated.&lt;/p&gt;

&lt;p&gt;As shown in the examples, higher-level functions can have conditionals and they can be longer.&lt;/p&gt;

&lt;p&gt;As nice as it would be to avoid conditionals altogether, that's just not possible.&lt;/p&gt;

&lt;p&gt;Every real program needs to do different stuff under different circumstances. The very best case is to format your conditionals differently and to put them in a more appropriate place, so that they're easy to work with.&lt;/p&gt;

&lt;p&gt;Also, it's not possible for all of your functions to truly only do one small thing. The only functions with that luxury tend to be the functions at the very edge of your application. For everything else, it's more likely that they'll do a few things, say, 3 things, at an appropriate level of abstraction, in a way that it can be described as one thing.&lt;/p&gt;

&lt;p&gt;For example:&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;handleFormSubmit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// necessary to handle form submission with JavaScript, rather than HTML&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getDataFromForm&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;formattedData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;formatData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;sendData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;formattedData&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;handleFormSubmit&lt;/code&gt; function does 4 things. It has 4 lines of code after all. However, you can also think of it as doing one thing. "It handles the form submission", that's one thing. Both are correct, it depends on what level of abstraction you consider.&lt;/p&gt;

&lt;p&gt;So, since you can't just avoid conditionals and since your functions can't just do one thing, what can you do? All you can do is apply programming principles. A.k.a. do anything you can to ensure that your code is correct and easy to change.&lt;/p&gt;

&lt;p&gt;At any given time, consider if your code is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;easy to understand&lt;/li&gt;
&lt;li&gt;easy to reuse&lt;/li&gt;
&lt;li&gt;easy to change&lt;/li&gt;
&lt;li&gt;easy to test&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Be pragmatic
&lt;/h2&gt;

&lt;p&gt;As always, remember to be pragmatic. In this article, we examined how to write and structure good code units at the edge of your application.&lt;/p&gt;

&lt;p&gt;That's the ideal, but the ideal might not always be realistic. If you can write code units like this without much effort, then do it. But if that's not possible, well, don't postpone a critical feature by 1 month because you want to refactor every code unit in your codebase. That wouldn't make sense or be realistic.&lt;/p&gt;

&lt;p&gt;Instead, do the best you can and be pragmatic. You probably need to balance good code (which makes future development easier) and releasing features in an appropriate timeframe for your work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final notes
&lt;/h2&gt;

&lt;p&gt;That's it for this article.&lt;/p&gt;

&lt;p&gt;I hope you found it useful and I hope that the concepts and the reasons for them made sense.&lt;/p&gt;

&lt;p&gt;What are your opinions on how code units should be, particularly units at the "edge" of your application? Is there anything you disagree with? Anything that wasn't covered? If there is anything, please leave a comment below.&lt;/p&gt;

&lt;p&gt;Otherwise, for more details on how to write clean code and apply programming principles, please check out &lt;a href="https://programmingduck.com/articles/programming-principles-introduction"&gt;clean code and programming principles - the ultimate beginner's guide&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>learning</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to make accessible websites - The ultimate guide</title>
      <dc:creator>Spyros Argalias</dc:creator>
      <pubDate>Wed, 17 Feb 2021 10:59:38 +0000</pubDate>
      <link>https://dev.to/programmingduck/how-to-make-accessible-websites-the-ultimate-guide-3f22</link>
      <guid>https://dev.to/programmingduck/how-to-make-accessible-websites-the-ultimate-guide-3f22</guid>
      <description>&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/Ik3axRPhLmU"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;em&gt;For everything you need to know about accessibility, check out &lt;a href="https://programmingduck.com/articles/accessibility/"&gt;Web accessibility - Everything you need to know, on Programming Duck&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Disclaimer: I'm not a lawyer. This article only represents my personal opinion and current understanding. It is not legal advice. Please consult a lawyer for information on the legal aspects of accessibility.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Ensuring that your website is accessible shouldn't be difficult. A little bit of effort can take you a long way in your day-to-day work.&lt;/p&gt;

&lt;p&gt;To start with, if you're completely new to accessibility, learning some of the basics will be very helpful. Please see &lt;a href="https://programmingduck.com/articles/learn-accessibility"&gt;how to learn accessibility&lt;/a&gt; for some useful resources to start with.&lt;/p&gt;

&lt;p&gt;Afterwards, here is a simple process you can use to apply accessibility as you work:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Learn about your legal accessibility requirements&lt;/li&gt;
&lt;li&gt;Use semantic HTML&lt;/li&gt;
&lt;li&gt;Check accessibility checklists frequently to ensure you've applied accessibility correctly in your work&lt;/li&gt;
&lt;li&gt;Test your website's accessibility to ensure you're doing it correctly&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Optionally, you can also:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Make accessibility part of the standards and the development process&lt;/li&gt;
&lt;li&gt;Educate people about accessibility&lt;/li&gt;
&lt;li&gt;Hire specialists if you need more help&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here are more details on each part of the process.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LqiET8Zf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1wt46o5c8gf2f3scdh75.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LqiET8Zf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1wt46o5c8gf2f3scdh75.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Legal requirements
&lt;/h2&gt;

&lt;p&gt;Before starting, you should know about your legal requirements.&lt;/p&gt;

&lt;p&gt;In general, for most companies, you should meet the WCAG 2.1 (or more likely, the latest published version) level AA accessibility standards. Level A is probably not sufficient. Level AA is the standard. Level AAA is "aspirational". It's generally not a legal requirement to meet it, however it's great if you can.&lt;/p&gt;

&lt;p&gt;Additionally, you &lt;strong&gt;may&lt;/strong&gt; need an accessibility statement depending on your country's laws. Even if you don't, &lt;a href="https://www.w3.org/WAI/planning/statements/"&gt;WCAG mentions that there are many good reasons to have one&lt;/a&gt;. For information on accessibility statements, please see the &lt;a href="https://www.w3.org/WAI/planning/statements/"&gt;WCAG article on developing an accessibility statement&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use semantic HTML
&lt;/h2&gt;

&lt;p&gt;Using semantic HTML is the most important thing you can do for accessibility. An easy way to do this is to scan through the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element"&gt;HTML element reference on MDN&lt;/a&gt;. It lists all of the HTML elements and says what they should be used for. You can also visit a dedicated page for every element for much more information about it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use useful checklists
&lt;/h2&gt;

&lt;p&gt;As you work, keep referring to these useful checklists:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://webaim.org/standards/wcag/checklist"&gt;WebAIM WCAG checklist&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.w3.org/TR/wai-aria-practices-1.1/"&gt;WAI-ARIA authoring practices&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.w3.org/WAI/standards-guidelines/wcag/"&gt;WCAG documents&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.w3.org/WAI/WCAG21/quickref/"&gt;How to meet WCAG (quick reference)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.w3.org/WAI/WCAG21/Techniques/"&gt;Techniques for WCAG&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


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

&lt;h3&gt;
  
  
  WebAIM WCAG checklist
&lt;/h3&gt;

&lt;p&gt;The WebAIM WCAG checklist provides great recommendations on the most important things you can do for accessibility. It's one of the simpler checklists in this section. As a result, it's great to refer to frequently and for the majority of your work.&lt;/p&gt;

&lt;h3&gt;
  
  
  WAI-ARIA authoring practices
&lt;/h3&gt;

&lt;p&gt;The WAI-ARIA authoring practices provide guidelines for custom widgets created with JavaScript (things like accordions, custom dropdowns, etc.). Following these guidelines is very important for the accessibility of these widgets. Therefore, I recommend always referring to this resource whenever you're working on something like that.&lt;/p&gt;

&lt;p&gt;Additionally, this resource provides guidance on naming for particular elements. Some elements require a name, while for others a name is recommended, but not required. Thankfully, if you use semantic HTLM, the majority (if not all) of the elements that require a name will automatically get one associated to them from their content. In other words, you shouldn't need to check this section very often.&lt;/p&gt;

&lt;p&gt;However, the naming section provides additional recommendations for naming elements. If you want to improve the experience of screen reader users even further, feel free to check this section once in a while and apply some of its recommendations.&lt;/p&gt;

&lt;h3&gt;
  
  
  WCAG
&lt;/h3&gt;

&lt;p&gt;WCAG includes the official specification that's mentioned in the legal requirements. To be 100% compliant, you have to check against that.&lt;/p&gt;

&lt;p&gt;However, this resource seems more difficult to work from than the others. For this reason, you may find it easier to work from the WebAIM resource in your day-to-day work. Then, you can check this resource when you want to do a more comprehensive accessibility audit.&lt;/p&gt;

&lt;h3&gt;
  
  
  Frequency of using the checklists
&lt;/h3&gt;

&lt;p&gt;As for the frequency of using these checklists, you have different options. You can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Refer to them every time you're working on something relevant and want additional information&lt;/li&gt;
&lt;li&gt;Scan over the documents every once in a while, perhaps once a week or so. That way, you can recall the work you did that week and make sure that you haven't missed any accessibility requirements.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5iTtW10u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/53cko3jnxqhw65rbum1c.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5iTtW10u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/53cko3jnxqhw65rbum1c.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Test
&lt;/h2&gt;

&lt;p&gt;To really be sure that your website is accessible, you need to test it.&lt;/p&gt;

&lt;p&gt;There are different kinds of accessibility tests that you can do. They vary in complexity and time required. They can also be manual or automated.&lt;/p&gt;

&lt;h3&gt;
  
  
  Manual tests
&lt;/h3&gt;

&lt;p&gt;Here are some things that you should consider testing manually.&lt;/p&gt;

&lt;h4&gt;
  
  
  Zoom
&lt;/h4&gt;

&lt;p&gt;Test how your website looks when you use browser zoom. The WCAG standard currently requires your page to be readable and functional at 200% zoom. However, feel free to test higher than that.&lt;/p&gt;

&lt;p&gt;Consider also testing your website with OS-level zoom (a zoom setting applied in your operating system settings).&lt;/p&gt;

&lt;p&gt;Consider also testing your website with an application such as &lt;a href="https://www.zoomtext.com/"&gt;ZoomText&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Screen readers
&lt;/h4&gt;

&lt;p&gt;Test your website with a screen reader.&lt;/p&gt;

&lt;p&gt;For more thorough testing, consider testing your website with different common combinations of operating systems, screen readers and browsers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Mac or iOS with Voiceover and Safari&lt;/li&gt;
&lt;li&gt;Windows with Jaws or NVDA&lt;/li&gt;
&lt;li&gt;ChromeOS with ChromeVox and Chrome&lt;/li&gt;
&lt;li&gt;Android with accessibility options enabled and Chrome&lt;/li&gt;
&lt;li&gt;Linux with Orca&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Keyboard navigation and interactivity
&lt;/h4&gt;

&lt;p&gt;Test the keyboard navigation and interactivity of your website. Pay particular attention to things that work with JavaScript, such as custom widgets, modals, etc.&lt;/p&gt;

&lt;p&gt;Ensure that keyboard navigation is sensible, easy and clear. The user should know exactly where the focus is every time.&lt;/p&gt;

&lt;p&gt;Also ensure that the entire website can be operated using just the keyboard. The only exception to this rule is if that's practically impossible, for example if you have functionality for free-hand drawing.&lt;/p&gt;

&lt;h4&gt;
  
  
  Vision deficiencies
&lt;/h4&gt;

&lt;p&gt;Some browsers have a vision deficiency simulator. If you're using Chrome, here's &lt;a href="https://addyosmani.com/blog/emulate-vision-deficiencies-devtools/"&gt;Andy Osmani's tutorial on the vision deficiencies simulator in Chrome&lt;/a&gt;. Here's an &lt;a href="https://developer.mozilla.org/en-US/docs/Tools/Accessibility_inspector/Simulation"&gt;article for using color vision simulator in Firefox&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Alternatively you can use a browser extension like &lt;a href="https://chrome.google.com/webstore/detail/nocoffee/jjeeggmbnhckmgdhmgdckeigabjfbddl"&gt;NoCoffee vision simulator&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Document outline
&lt;/h4&gt;

&lt;p&gt;The document outline refers to the headings of a webpage. Specifically, it refers to their heading level and their order.&lt;/p&gt;

&lt;p&gt;Heading levels should descend in order, without jumps.&lt;/p&gt;

&lt;p&gt;For example, this is fine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;h1&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;h2&amp;gt;&lt;/span&gt;h2&lt;span class="nt"&gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;h2&amp;gt;&lt;/span&gt;h2&lt;span class="nt"&gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;h3&amp;gt;&lt;/span&gt;h3&lt;span class="nt"&gt;&amp;lt;/h3&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, the following is not fine, because it skips / jumps from an h2 to an h4:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;h1&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;h2&amp;gt;&lt;/span&gt;h2&lt;span class="nt"&gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;h4&amp;gt;&lt;/span&gt;h4&lt;span class="nt"&gt;&amp;lt;/h4&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;h2&amp;gt;&lt;/span&gt;h2&lt;span class="nt"&gt;&amp;lt;/h4&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An easy way to test these is to install a browser extension like &lt;a href="https://wave.webaim.org/"&gt;WAVE&lt;/a&gt; or &lt;a href="https://chrome.google.com/webstore/detail/outliner-a-chrome-extensi/eegpjjnajnplmkigmoglgbgpibgkkepo"&gt;Outliner&lt;/a&gt;. They can show you the document outline very clearly, without you having to look at the source code of the page.&lt;/p&gt;

&lt;h3&gt;
  
  
  Accessibility testing tools
&lt;/h3&gt;

&lt;p&gt;Here are some tools that you can consider using for accessibility testing.&lt;/p&gt;

&lt;p&gt;Third-party services / analysers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://tenon.io/"&gt;Tenon&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Page testing tools:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Chrome developer tools:

&lt;ul&gt;
&lt;li&gt;Accessibility tab&lt;/li&gt;
&lt;li&gt;Rendering tab -&amp;gt; Vision deficiencies simulator&lt;/li&gt;
&lt;li&gt;Lighthouse&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Browser extensions:

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://wave.webaim.org/"&gt;WAVE&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://chrome.google.com/webstore/detail/outliner-a-chrome-extensi/eegpjjnajnplmkigmoglgbgpibgkkepo"&gt;Outliner&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.deque.com/axe/"&gt;Axe&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://khan.github.io/tota11y/"&gt;Tota11y&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


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

&lt;p&gt;Build tools:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/GoogleChrome/lighthouse"&gt;Lighthouse&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/dequelabs/axe-core"&gt;axe-core&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.npmjs.com/package/cypress-axe"&gt;cypress-axe&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/github/accessibilityjs"&gt;accessibilityjs&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Also consider accessibility code linters for the technologies you work with. One example is the &lt;a href="https://www.npmjs.com/package/eslint-plugin-jsx-a11y"&gt;eslint-plugin-jsx-a11y&lt;/a&gt; for JSX.&lt;/p&gt;

&lt;h3&gt;
  
  
  Unit, integration and end-to-end tests for accessibility
&lt;/h3&gt;

&lt;p&gt;In rare cases, you may want to write unit, integration or end-to-end tests for accessibility.&lt;/p&gt;

&lt;p&gt;For example, consider that you've created a custom widget. You may want to have an end-to-end test for it to make sure that the widget can receive focus when the user presses &lt;code&gt;TAB&lt;/code&gt; on their keyboard.&lt;/p&gt;

&lt;h3&gt;
  
  
  Recommendations for getting started with accessibility testing
&lt;/h3&gt;

&lt;p&gt;When it comes to accessibility testing, there are many things you could test and many tools you could use.&lt;/p&gt;

&lt;p&gt;To keep things simple, consider starting with the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use lighthouse to run audits on pages on your website. Then fix any errors it mentions.&lt;/li&gt;
&lt;li&gt;Do some manual testing for keyboard navigation, screen readers, the document outline and zoom.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As you get more comfortable, you can try out additional tools. For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Try installing the WAVE browser extension or an alternative.&lt;/li&gt;
&lt;li&gt;Try setting up some automated accessibility testing with Lighthouse, axe-core or an alternative.&lt;/li&gt;
&lt;li&gt;Try installing some relevant code linters, such as eslint-plugin-jsx-a11y.&lt;/li&gt;
&lt;li&gt;And so on.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Alternatively, if you have the budget, you can also use a service like Tenon or even hire an accessibility consultant.&lt;/p&gt;

&lt;h2&gt;
  
  
  Make accessibility a standard and part of the development process
&lt;/h2&gt;

&lt;p&gt;It's useful to make accessibility an official part of your standards and development process. That way, it won't be neglected.&lt;/p&gt;

&lt;p&gt;This is similar to how you would make anything a standard in your codebase.&lt;/p&gt;

&lt;p&gt;You may declare in your standards documents that accessibility is important and that all work is expected to meet the WCAG 2.1 AA specification, or at least the WebAIM recommendations and ARIA authoring practices recommendations.&lt;/p&gt;

&lt;p&gt;You could also make it part of your development process:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You could set up automated accessibility tests to be run on the pull request for every story.&lt;/li&gt;
&lt;li&gt;You could require developers to test the accessibility of their work before they submit a new feature.&lt;/li&gt;
&lt;li&gt;You could run a small accessibility audit once a month, or maybe a comprehensive audit once a year.&lt;/li&gt;
&lt;li&gt;And so on.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1z4eN2D4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4gbp9ed5oe926t357f55.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1z4eN2D4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4gbp9ed5oe926t357f55.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Educate people whose work ends up on the front end
&lt;/h2&gt;

&lt;p&gt;As explained in &lt;a href="https://programmingduck.com/articles/accessibility"&gt;Accessibility - Everything you need to know&lt;/a&gt;, front end developers need to know more about accessibility than anyone else.&lt;/p&gt;

&lt;p&gt;However, all work that ends up on the front end can affect accessibility. This includes work done by designers, content creators and other job roles. Therefore, it's useful for them to know about accessibility too.&lt;/p&gt;

&lt;p&gt;For this reason, it may be good for front end developers to educate them.&lt;/p&gt;

&lt;p&gt;Additionally, developers can collaborate with them or advise them on creating standards that ensures their work is accessible. For example, they may advise copywriters (people who write text on the website) that their work needs to follow a reasonable document outline.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hire third-party accessibility consultants
&lt;/h2&gt;

&lt;p&gt;Even if the developers in the company know about accessibility, they're not specialists. They could be missing things that only a specialist may know of. Even if they're not missing any legal requirements, a specialist may be able to enhance accessibility in ways that the developers may not think of.&lt;/p&gt;

&lt;p&gt;So, if you want to be extremely thorough with accessibility, you may have to hire a third-party accessibility consultant or a specialist.&lt;/p&gt;

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

&lt;p&gt;Accessibility work doesn't have to be difficult.&lt;/p&gt;

&lt;p&gt;Here is a simple process you can use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Learn about your legal accessibility requirements&lt;/li&gt;
&lt;li&gt;Use semantic HTML&lt;/li&gt;
&lt;li&gt;Check accessibility checklists frequently to ensure you've applied accessibility correctly in your work&lt;/li&gt;
&lt;li&gt;Test your website's accessibility to ensure you're doing it correctly&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Optionally, you can also:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Make accessibility part of the standards and the development process&lt;/li&gt;
&lt;li&gt;Educate and advise people on accessibility&lt;/li&gt;
&lt;li&gt;Hire specialists if you need more help&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Final notes
&lt;/h2&gt;

&lt;p&gt;That's it for this article.&lt;/p&gt;

&lt;p&gt;Please leave a comment if you have any feedback, anything I missed that might help others, or even anything you disagree with.&lt;/p&gt;

&lt;p&gt;Next, if you want to know more about accessibility, please see the article &lt;a href="https://programmingduck.com/articles/accessibility"&gt;Web accessibility - Everything you need to know&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>a11y</category>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
