<?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: Mikhail Levkovsky</title>
    <description>The latest articles on DEV Community by Mikhail Levkovsky (@mlevkov).</description>
    <link>https://dev.to/mlevkov</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%2F206348%2F16c6d7ec-625c-434e-b9c6-f95a8ed2e744.jpg</url>
      <title>DEV Community: Mikhail Levkovsky</title>
      <link>https://dev.to/mlevkov</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mlevkov"/>
    <language>en</language>
    <item>
      <title>Putting the Breaks on Downtime</title>
      <dc:creator>Mikhail Levkovsky</dc:creator>
      <pubDate>Thu, 21 Jan 2021 01:09:28 +0000</pubDate>
      <link>https://dev.to/mlevkov/putting-the-breaks-on-downtime-3g1e</link>
      <guid>https://dev.to/mlevkov/putting-the-breaks-on-downtime-3g1e</guid>
      <description>&lt;p&gt;It comes as no surprise that for any ecommerce business like SSENSE, there are peaks and valleys in terms of customer traffic. There are periods of relative calm, followed by high-traffic periods where we experience up to 80 times the regular traffic. This can come during drops of highly anticipated items — like sneakers — the start of our seasonal markdown periods, and around the holidays.&lt;/p&gt;

&lt;p&gt;It should come as even less of a surprise that when dealing with a microservice architecture, you have to pay extra attention to the resiliency of your stack to avoid any downtime. The last thing that any developer wants, is a domino effect of downward dependencies causing downtime of a service that’s 5 levels up.&lt;/p&gt;

&lt;p&gt;At SSENSE, we set an extremely aggressive target for ourselves of having a &lt;a href="https://medium.com/ssense-tech/the-what-and-why-behind-performance-metrics-part-ii-commitments-to-performance-reliability-4525b9386436"&gt;99.95%&lt;/a&gt; yield during these critical moments for the business. Among many steps that we took, handling graceful degradation was key in ensuring we did not experience any downtime during a critical period for the business.&lt;/p&gt;

&lt;p&gt;The degraded experiences were broken down into two main strategies:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Automatic degradation&lt;/li&gt;
&lt;li&gt;Planned degradation&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Automatic Degradation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To automatically degrade our services we leverage circuit breakers. The basic idea behind the circuit breaker is very simple. You wrap a protected function call in a circuit breaker object, which monitors for failures. Once the failures reach a certain threshold, the circuit breaker trips, and all further calls to the circuit breaker return with an error/empty or result/cached result, without the protected call being made at all.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6_0gzdCN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2AKkx_5AAJWev3FjHt" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6_0gzdCN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2AKkx_5AAJWev3FjHt" alt="circuit breaker"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The library we leveraged is called &lt;strong&gt;&lt;a href="https://www.npmjs.com/package/opossum"&gt;opossum.js&lt;/a&gt;&lt;/strong&gt;, an open source Hystrix compliant library. It's relatively easy to use, and is very configurable. The key attributes that we configured were:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The api timeout: If our function takes longer than this value seconds, trigger a failure.&lt;/li&gt;
&lt;li&gt;The error threshold percentage: When this error percentage of requests fail, trigger the circuit.&lt;/li&gt;
&lt;li&gt;The reset timeout: How long to wait before trying again.&lt;/li&gt;
&lt;li&gt;The rolling count timeout: Sets the duration of the statistical rolling window, in milliseconds. This is how long opossum keeps metrics for the circuit breaker to use and for publishing.&lt;/li&gt;
&lt;li&gt;The volume threshold: The minimum number of requests within the rolling statistical window that must exist before the circuit breaker can open.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Although opossum is extremely easy to use, in order to ensure it's adoption internally, we decided to simplify it even more and turn it into a decorator.&lt;br&gt;
This allowed us to wrap our gateway calls in a circuit breaker like so:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;To translate this decorator into plain English what it means is that:&lt;/p&gt;

&lt;p&gt;If after 3 errors within 10 seconds, we still get response times of 5+ seconds or 55%+ requests fail then open the circuit and call the fallback method. Try one call to the original product-service again after 15 seconds, if it works then close the circuit and go back to normal, if it fails keep the circuit open.&lt;/p&gt;

&lt;p&gt;We sprinkled these decorators across our code, and got seamless fallbacks if errors started happening within our stack.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Planned Degradation&lt;/strong&gt;&lt;br&gt;
The second step we took is to define our planned degradation and what that means for the customer experience. While the automated degradation was mostly decided within the tech team, the planned degradation was a collaboration between tech and product.&lt;/p&gt;

&lt;p&gt;Our goal when working on this was: "How can we protect our stack in advance from surges in traffic, by turning off some features while still maintaining an acceptable user experience?"&lt;/p&gt;

&lt;p&gt;This was cross functional work with multiple teams, stakeholders, tech leads, and project managers. To be able to roll this out we leveraged a tool that we use extensively at SSENSE: LaunchDarkly. We use LaunchDarkly internally to manage our feature flags to be able to enable/disable features in an instant, either for the whole user base or on a percentage based rollout.&lt;/p&gt;

&lt;p&gt;The next part was how to operationalize this in an efficient manner. The questions we had to ask were:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;When do we enable these features?&lt;/li&gt;
&lt;li&gt;When do we disable these features?&lt;/li&gt;
&lt;li&gt;What is the customer impact?&lt;/li&gt;
&lt;li&gt;What is the business impact?&lt;/li&gt;
&lt;li&gt;What services does this affect?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To answer the first two questions, we broke down our degraded features into three sections:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go Live: Turned on ~1 hour before a high-traffic event, turned off when traffic dies down.&lt;/li&gt;
&lt;li&gt;Amber: Prolonged drop in yield for key funnel services or stack experiencing important latency, but still responsive. Decided in a war room session when to trigger this.&lt;/li&gt;
&lt;li&gt;Code Red: If critical flows are down, these feature flags will be activated. Decided in a war room session when to trigger this.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The next three questions were answered by:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Digging into the code to look for dependencies.&lt;/li&gt;
&lt;li&gt;Inspecting our traces and flame graphs using Datadog.&lt;/li&gt;
&lt;li&gt;Regression testing with our QA team.&lt;/li&gt;
&lt;li&gt;Getting buy-in from stakeholders.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After all was said and done, here are a few examples of the planned degraded experiences we decided to go with:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Disable one of our widgets on the product display page (2% decrease in downstream traffic)&lt;/li&gt;
&lt;li&gt;Disable our personalization models for the mobile app (decrease latency by 4x-5x)&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Disable the counter on the Shopping Bag menu item (74% decrease in downstream traffic)&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rJUAytWS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AMSd_Q_uN5q02_zZfDdnwdQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rJUAytWS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AMSd_Q_uN5q02_zZfDdnwdQ.png" alt="before"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3KmKp38i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2A3ya0gfjwzrTM3X202kSRVg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3KmKp38i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2A3ya0gfjwzrTM3X202kSRVg.png" alt="after"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Display a static message for shipping rates in some use cases (decrease downstream calls by 500K/hour)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use internal cache of Wishlist items (will continue working even if downstream source of truth is non responsive)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Disable the Order History page&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JOnD5hC1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AnZ20nVQUSldvQ0kE2t98vQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JOnD5hC1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AnZ20nVQUSldvQ0kE2t98vQ.png" alt="before"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hTiIP2dp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2Aor8nRRKFPwWOaJPamoxmsQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hTiIP2dp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2Aor8nRRKFPwWOaJPamoxmsQ.png" alt="after"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Simplify the order confirmation page
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ox9TP5XK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2Aa_qSUKPWSCSaGOY4" alt="before"&gt;
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ofJLQpp_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2A92z4jsbQeMV-6fmOIX5TLQ.png" alt="after"&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Results&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Although these experiences showed a lot of promise during our load tests - with significant increases in throughput - it was time to put them to the test during our high traffic period.&lt;/p&gt;

&lt;p&gt;Was all this work worth it?&lt;/p&gt;

&lt;p&gt;Well, see for yourself.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--I5XvmtoW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2ACCXqGTREfaZyQOCD" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--I5XvmtoW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2ACCXqGTREfaZyQOCD" alt="results"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While I can't disclose the absolute number of checkouts, this relative graph will show just how much the checkouts grow during a high-traffic period - not including all the other types of traffic we receive. The stack performed flawlessly with no downtime and our YIELD goal of 99.95% was achieved.&lt;/p&gt;

&lt;p&gt;The degraded experiences that were baked in are definitely not the only reason we did not experience any downtime. Teams worked and continue to work tirelessly to ensure stability across the stack with a slew of different initiatives to stabilize and improve the resiliency of our stack.&lt;/p&gt;

&lt;p&gt;In the end, planning for degradation and handling it properly should be just one strategy in your arsenal when building a resilient product.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>javascript</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Test-Driven Development (TDD) and Why People Get it Wrong</title>
      <dc:creator>Mikhail Levkovsky</dc:creator>
      <pubDate>Sun, 01 Mar 2020 19:40:15 +0000</pubDate>
      <link>https://dev.to/mlevkov/test-driven-development-tdd-and-why-people-get-it-wrong-3kbh</link>
      <guid>https://dev.to/mlevkov/test-driven-development-tdd-and-why-people-get-it-wrong-3kbh</guid>
      <description>&lt;p&gt;Whether at school, work, or while watching a YouTube video almost every single developer has been introduced to Test-Driven Development, AKA TDD.&lt;/p&gt;

&lt;p&gt;For the following examples we will build out a calculator (for simplicity) in Typescript. However, all these principles are language agnostic.&lt;/p&gt;

&lt;p&gt;Most tutorials of TDD explain it in more or less the same way:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You receive a completely new feature request&lt;/li&gt;
&lt;li&gt;You write the test case&lt;/li&gt;
&lt;li&gt;Your test case fails&lt;/li&gt;
&lt;li&gt;You write the corresponding code&lt;/li&gt;
&lt;li&gt;You run your test again&lt;/li&gt;
&lt;li&gt;Your test passes&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The code sample might look something like this:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You would then update the method to make it work, ending up with something like this:&lt;/p&gt;


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


&lt;p&gt;Unfortunately, this is where the majority of TDD articles end, which in my opinion, misses the whole point of TDD.&lt;/p&gt;

&lt;p&gt;The real magic of TDD comes after the initial features are built in two main use cases:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Bugs: proving that one existed and proving that you fixed it.&lt;/li&gt;
&lt;li&gt;Evolution of features and proving that they meet the acceptance criteria.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Bugs
&lt;/h2&gt;

&lt;p&gt;Let's add some more functions to our calculator (and some new tests, of course) with a well-placed bug:&lt;/p&gt;


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


&lt;p&gt;Any seasoned developer (or anybody who has used a calculator before) will see that there is a bug in the subtract method, however, our unit tests still pass. &lt;em&gt;Dang!&lt;/em&gt; Looks like our unit test missed a pretty important use case, the fact that it will fail for any value outside of subtracting two zeroes. The initial reaction of any developer - myself included - would be to hop right in, fix the subtract method, and update our unit test.&lt;/p&gt;

&lt;p&gt;Well, if you're doing TDD you're actually taking the wrong approach. You're approach should be:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Write a proper test case to prove that the bug exists&lt;/li&gt;
&lt;li&gt;Fix the feature&lt;/li&gt;
&lt;li&gt;Run the passing test proving that you fixed it&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In terms of code changes, it would look something like this:&lt;/p&gt;


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


&lt;p&gt;You would then fix the code and run the tests again:&lt;/p&gt;


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


&lt;p&gt;By doing this two-step process, you not only improve your unit test suite, but you prove from a functional standpoint that you found the bug and resolved it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Evolution of Features
&lt;/h2&gt;

&lt;p&gt;As requirements and products grow, it's also a great time to remember your TDD best practices.&lt;/p&gt;

&lt;p&gt;As requirements and products grow, it's also a great time to remember your TDD best practices.&lt;/p&gt;

&lt;p&gt;Let's say your stakeholders come to you and say that they absolutely need a divide function for your calculator application. All the customers are &lt;br&gt;
clamouring for one and you need to develop it ASAP.&lt;/p&gt;

&lt;p&gt;If you're not doing TDD, then you might jump right into coding and add the divide function. However, since we are TDD fanboys and girls, the approach will be different.&lt;/p&gt;

&lt;p&gt;The steps we will take are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Add the unit test for division&lt;/li&gt;
&lt;li&gt;Run it and have it fail&lt;/li&gt;
&lt;li&gt;Program the functionality&lt;/li&gt;
&lt;li&gt;Run it and have it pass&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Our code changes will look something like this:&lt;/p&gt;


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


&lt;p&gt;Now we add the functionality of division, so our code change will look something like:&lt;/p&gt;


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


&lt;p&gt;The advantage here is two-fold. The first is that you can sit with your PM and define the correct inputs and outputs, and then code against them with confidence. The second, which is one of the more underrated uses of unit tests, is that by writing the unit test first you will be able to see very early on if your code needs to be refactored.&lt;/p&gt;

&lt;p&gt;This example is very simple and straightforward, but imagine the unit test was mocking several calls and database connections, and now you had to add an HTTP call. By starting with a unit test, you will be able to tell almost immediately that the unit is getting way too complicated and has too many dependencies. This will allow you to proactively refactor your code and build it in a scalable and testable way.&lt;/p&gt;

&lt;p&gt;If you were to start coding first, you would only hit the mocking and dependency roadblock in your unit tests towards the end of the development cycle.&lt;/p&gt;

&lt;p&gt;Hopefully this sheds some insight into doing TDD not just at the beginning but throughout the whole product life cycle.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Originally written for&lt;/em&gt; &lt;a href="https://medium.com/ssense-tech/test-driven-development-tdd-and-why-people-get-it-wrong-9c8b80536838"&gt;https://medium.com/ssense-tech/test-driven-development-tdd-and-why-people-get-it-wrong-9c8b80536838&lt;/a&gt;&lt;br&gt;
Editorial reviews Deanna Chow, Liela Touré, &amp;amp; Prateek Sanyal.&lt;/p&gt;

</description>
      <category>tdd</category>
      <category>codequality</category>
      <category>typescript</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Getting Started with Lambda functions, SLS and Node</title>
      <dc:creator>Mikhail Levkovsky</dc:creator>
      <pubDate>Mon, 02 Sep 2019 01:20:04 +0000</pubDate>
      <link>https://dev.to/mlevkov/managing-and-deploying-aws-lambda-functions-with-sls-4ofh</link>
      <guid>https://dev.to/mlevkov/managing-and-deploying-aws-lambda-functions-with-sls-4ofh</guid>
      <description>&lt;p&gt;At work my team had an interesting task of processing certain files daily as they were uploaded to an S3 bucket. &lt;br&gt;
Instead of having an application deployed and perpetually running in the background, we decided to try out AWS Lambda.&lt;/p&gt;

&lt;p&gt;This article will give an overview of how we setup our Lambda from beginning to end. I'll review how we manage the cloud formation stack with SLS, explore ways to set it up for different environments, and finally go over its deployment.&lt;/p&gt;

&lt;p&gt;First, you'll need a few things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;An AWS account&lt;/li&gt;
&lt;li&gt;Node v8.x (at a minimum) installed on your machine&lt;/li&gt;
&lt;li&gt;AWS CLI (Command Line Interface) installed on your machine&lt;/li&gt;
&lt;li&gt;SLS CLI installed on your machine&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In order to manage our Lambda deployment successfully, I decided to use the &lt;a href="https://serverless.com/"&gt;Serverless Library&lt;/a&gt;. This library is extremely powerful and allows us to essentially manage our entire stack with YAML files.&lt;/p&gt;

&lt;p&gt;In our use case, we wanted to create an S3 bucket that would trigger a specific method in a Lambda function upon receiving a file. All of this is defined in a few lines of a YAML file. Let's take a look.&lt;/p&gt;

&lt;p&gt;First, in our project we added a serverless.yml file with the following parameters:&lt;/p&gt;


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


&lt;h3&gt;
  
  
  pssst I tweet about code stuff all the time. If you have questions about how to level up your dev skills give me a follow &lt;a href="https://twitter.com/mlevkov"&gt;@mlevkov&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;The first part of the serverless.yml file establishes some basic requirements (i.e. what region in AWS to deploy, your AWS profile, etc…), the second part is where the fun starts.&lt;/p&gt;

&lt;p&gt;This file declares that the &lt;strong&gt;s3Handler&lt;/strong&gt; function inside of the app file will be triggered when a .csv file is created in the &lt;code&gt;mybucket&lt;/code&gt; S3 bucket.&lt;/p&gt;

&lt;p&gt;The last part of the file declares the plug-ins, which allow us to use TypeScript and run our serverless deployment locally.&lt;/p&gt;

&lt;p&gt;To give you an idea of the code that will be processing the files, here is a simplified set of snippets to demonstrate the flow:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;app.ts&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;app/controllers/S3Controller&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;And what does this deployment look like once it's on AWS?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--B07dhU-C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2AljoH46WAkcfW8-bm" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--B07dhU-C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2AljoH46WAkcfW8-bm" alt="img"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On the left you have the S3 trigger which is activated when .csv files are uploaded. In the middle you have the &lt;code&gt;jb-recommendation&lt;/code&gt; Lambda, and on the right you have your Amazon CloudWatch Logs and the S3 bucket where your Lambda function will be uploaded to.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deployment
&lt;/h2&gt;

&lt;p&gt;SLS makes deployment easy as pie.&lt;/p&gt;

&lt;p&gt;First, let's setup your local AWS profile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;aws configure - profile localdev
AWS Access Key ID [None]: &amp;lt;ENTER YOUR ACCESS KEY&amp;gt;
AWS Secret Access Key [None]: &amp;lt;ENTER YOUR SECRET KEY&amp;gt;
Default region name [None]: &amp;lt;ENTER 'us-east-1'&amp;gt;
Default output format [None]: &amp;lt;ENTER 'text'&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;After which, you just run &lt;em&gt;sls deploy&lt;/em&gt; and you're good to go.&lt;/p&gt;
&lt;h2&gt;
  
  
  Environment variables
&lt;/h2&gt;

&lt;p&gt;What we did earlier will deploy your application, but chances are that you'd want some environment specific variables to isolate &lt;code&gt;development&lt;/code&gt;, &lt;code&gt;QA&lt;/code&gt;, and &lt;code&gt;production&lt;/code&gt; environments.&lt;/p&gt;

&lt;p&gt;Here's how I recommend introducing these:&lt;/p&gt;

&lt;p&gt;The first step is to create a folder called configurations and create 3 separate YAML files: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;dev&lt;/li&gt;
&lt;li&gt;qa&lt;/li&gt;
&lt;li&gt;prod&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We won't add anything too complicated to these files, just a change in &lt;br&gt;
the Node environment to ensure that our environments work as expected.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;/configuration/dev&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;NODE_ENV: 'development'
profile: 'localdev'
region: 'us-west-2'
stage: 'dev'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;/configuration/qa&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;NODE_ENV:'qa'
profile: 'qa'
region: 'us-west-2'
stage: 'qa'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;/configuration/prod&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;NODE_ENV:'prod'
profile: 'prod'
region: 'us-west-2'
stage: 'prod'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now that we have separate environment variables, let's modify our serverless file to use them.&lt;/p&gt;


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



&lt;p&gt;We changed our serverless file to also include custom variables such as stage and configuration. Now when we deploy, we can specify the stage which in turn will select the appropriate configuration file:&lt;/p&gt;

&lt;p&gt;To toggle environments, all we have to do is add the &lt;code&gt;-s [env]&lt;/code&gt; flag as follows:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sls deploy -s [dev/qa/prod]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;-s&lt;/code&gt; stands for the stage you want to deploy.&lt;br&gt;
This will automagically create everything necessary for your entire CloudFormation infrastructure. It creates the S3 bucket, creates the S3 trigger events, deploys our Lambda function (hosted in a different s3 bucket) and adds the cloud formation logs.&lt;/p&gt;

&lt;p&gt;With a few YAML files we were able to deploy our Node application, create our S3 buckets, and setup the right events for 3 separate environments. Hopefully this article helps to provide context around when and how to integrate Lambda into your stack.&lt;/p&gt;

&lt;p&gt;If you want to level up your coding skills, I'm putting together a playbook that includes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;30+ common code smells &amp;amp; how to fix them&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;15+ design pattern practices &amp;amp; how to apply them&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;20+ common JS bugs &amp;amp; how to prevent them&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Get early access to the &lt;a href="https://javascriptplaybook.com/"&gt;Javascript playbook&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>typescript</category>
      <category>node</category>
      <category>webdev</category>
    </item>
    <item>
      <title>5 easy wins for cleaner Javascript code 🧹</title>
      <dc:creator>Mikhail Levkovsky</dc:creator>
      <pubDate>Tue, 27 Aug 2019 04:59:38 +0000</pubDate>
      <link>https://dev.to/mlevkov/5-easy-wins-for-cleaner-javascript-code-13of</link>
      <guid>https://dev.to/mlevkov/5-easy-wins-for-cleaner-javascript-code-13of</guid>
      <description>&lt;p&gt;Imagine an empty and clean kitchen sink. It's so shiny you can see your reflection inside of it. If you had a dirty plate, you would probably feel pretty bad just dropping it into the sink, right? You would clean it and put it away.&lt;br&gt;
Now what if your sink is full to the brim, with a bunch of nasty food particles floating around in that nasty water. In that case, you would just throw in your plate because, well, one more plate can't hurt. Unfortunately that is how we treat our code bases as well. Instead of tidying up our code base, we just sometimes throw in more and more code smells.&lt;br&gt;
Well, below are 5 things you can do to start tidying up your code base right now 🚀.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;N.B.&lt;/strong&gt; For some reason, the gists sometimes render in a really weird order. If the code doesn't line up with what I'm writing about, refreshing the page seems to fix it. Sorry about that! 😕&lt;/p&gt;
&lt;h2&gt;
  
  
  1. Use let and const and forget var
&lt;/h2&gt;

&lt;p&gt;You should no longer be using var, as it can easily introduce variable shadowing and can lead to a lot of confusion. If you need a value that doesn’t change use const. If you need a variable that doesn't change but you will initialize it in the constructor use readonly. If you need a variable who's value does change use let.&lt;/p&gt;


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


&lt;h3&gt;
  
  
  pssst I tweet about code stuff all the time. If you have questions about how to level up your dev skills give me a follow &lt;a href="https://twitter.com/mlevkov" rel="noopener noreferrer"&gt;@mlevkov&lt;/a&gt;
&lt;/h3&gt;

&lt;h2&gt;
  
  
  2. Always use string templates
&lt;/h2&gt;

&lt;p&gt;When concatenating strings you should always stick to string templates instead of the concatenation operator. This will make your life much easier as it allows for multiline strings, reduces any errors if your strings have quotes and is generally a lot easier to read. Here is how it would look like when we try to create a database connection string without string templates and with. Think of the kitchen sink. Try to keep your code as tidy as possible.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&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%2Fi.imgflip.com%2F38z5y3.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%2Fi.imgflip.com%2F38z5y3.jpg" alt="string templates"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Object shorthand should be used when possible
&lt;/h2&gt;

&lt;p&gt;Javascript and Typescript goes to great lengths to reduce verbosity. One of my favourite things is that when creating an object with keys, you can use the shorthand annotation to assign your variables to the right keys. Let’s look at an example of us creating a user object in a different way.&lt;/p&gt;


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


&lt;h2&gt;
  
  
  4. Merge Your Imports
&lt;/h2&gt;

&lt;p&gt;When importing either your own modules or from installed libraries there are certain conventions to follow. Some of them are less important than others. Personally I don’t care if the imports are in alphabetical order or not. However, if you’re importing multiple things from the same module, you should merge them into one. This will keep your code tidy and keep your imports from being all over the place.&lt;/p&gt;


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


&lt;h2&gt;
  
  
  5. Loop through your iterables properly
&lt;/h2&gt;

&lt;p&gt;If you have an iterable, such as an array or a list, and you want to go through the values, you should use the &lt;em&gt;for of&lt;/em&gt; instead of the &lt;em&gt;for in&lt;/em&gt;. You should use the &lt;em&gt;for in&lt;/em&gt; if you want to iterate through the properties (e.g. keys in an array) as opposed to the values. Take for example this method in the Playlist object that will list out all of the names. &lt;/p&gt;


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


&lt;p&gt;There you have it, 5 easy tips that you can use to keep your code base nice and tidy.&lt;/p&gt;

&lt;p&gt;If you want to level up your coding skills, I'm putting together a playbook that includes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;30+ common code smells &amp;amp; how to fix them&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;15+ design pattern practices &amp;amp; how to apply them&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;20+ common JS bugs &amp;amp; how to prevent them&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Get early access to the &lt;a href="https://javascriptplaybook.com/" rel="noopener noreferrer"&gt;Javascript playbook&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>typescript</category>
      <category>webdev</category>
      <category>codequality</category>
    </item>
    <item>
      <title>Thinking in Redux (when all you’ve known is MVC)</title>
      <dc:creator>Mikhail Levkovsky</dc:creator>
      <pubDate>Tue, 20 Aug 2019 13:44:37 +0000</pubDate>
      <link>https://dev.to/mlevkov/thinking-in-redux-when-all-you-ve-known-is-mvc-4f16</link>
      <guid>https://dev.to/mlevkov/thinking-in-redux-when-all-you-ve-known-is-mvc-4f16</guid>
      <description>&lt;p&gt;When rolling out a mobile app, one of the first decisions you usually make is: what language do we use? After some deliberation, and when you come to the realization that you don't want to learn Kotlin and Swift, you finally settle on React-Native. From my own experience, learning a new “language” or framework isn’t a huge issue, but man oh man, did react-native and redux give me a hard time. This article doesn’t explain how React-Native works (as that is not the hard part). The purpose of the next few paragraphs is to help anybody reading this transition from “thinking in MVC” to “thinking in Redux”. Hope it helps.&lt;/p&gt;

&lt;h2&gt;
  
  
  React-Native and Redux?
&lt;/h2&gt;

&lt;p&gt;As soon as you start learning about react-native (or react), you are about 3 stack overflow questions or medium posts away before somebody mentions redux.&lt;br&gt;
You were so happy. You started to understand state vs props, you know what componentDidMount does and you even understand how to properly create your components so they are re-usable. Now all of a sudden you found yourself on egghead.io, and some guy is talking about stores, reducer compositions, actions and mapping state to props.&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%2Fmiro.medium.com%2Fmax%2F370%2F1%2AZkpuItpBQjkDu1mXDbR35A.gif" 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%2Fmiro.medium.com%2Fmax%2F370%2F1%2AZkpuItpBQjkDu1mXDbR35A.gif" alt="confused"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You also realize that while before you were able to do: &lt;strong&gt;$(“.my-button”).click();&lt;/strong&gt; to get a button to do something; it has now been about 3 hours and your one button doesn’t do anything.&lt;/p&gt;
&lt;h2&gt;
  
  
  Some Analogies
&lt;/h2&gt;

&lt;p&gt;If you are coming from an MVC (or MVVC) world, you are used to models, views and controllers (duh). However in &lt;a href="https://redux.js.org/introduction/getting-started" rel="noopener noreferrer"&gt;Redux&lt;/a&gt; we are dealing with actions, reducers, stores and components. Trying to “translate” MVC to Redux is tricky but here is how I would do it:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Actions = Controller.&lt;/strong&gt; Think of your actions as the controller. Whenever you want something to happen in your app (i.e. load some data, change a isLoading flag from true to false…) you will have to dispatch an action. Just like in MVC where you would have to call a controller endpoint.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reducer = Model.&lt;/strong&gt; Sort of. Your reducers will be in charge of holding the current state of your application (i.e. user info, information loaded from the api, items you want to display…). It will also be the part that decides what to do when an action is called. While in MVC you might have a model with the method setName(), with Redux you would have a reducer handle an action to set the name in the state.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stores = ???.&lt;/strong&gt; The store is Redux specific and doesn’t really have an equivalent in MVC. Not to worry though. This part is taken care off behind the scenes. The store is like a container for the state that aggregates all of the reducers. It has a method to the get the current state, and exposes ways to subscribe to the state changes (using the “connect()” method). This is what will allow you to call actions and pass them in as props to your components.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Components = Views.&lt;/strong&gt; Components are kind of like your smart views. They display the information that they get from the state. I recommend splitting up your components into two parts. One just for the presentational part (dumb components) and one to handle all of the actions and state changes (smart components).&lt;/p&gt;
&lt;h3&gt;
  
  
  pssst I tweet about code stuff all the time. If you have questions about how to level up your dev skills give me a follow &lt;a href="https://twitter.com/mlevkov" rel="noopener noreferrer"&gt;@mlevkov&lt;/a&gt;
&lt;/h3&gt;
&lt;h2&gt;
  
  
  Moving From MVC Thinking to Redux Thinking
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F670%2F1%2AxORdWwOFLR-6D4ghvUa6AA.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%2Fmiro.medium.com%2Fmax%2F670%2F1%2AxORdWwOFLR-6D4ghvUa6AA.png" alt="Typical MVC. When life was easy."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see (and know from experience) in the diagram above data can flow two ways. You press a button in your view, it sends a message to the controller and that updates the model. The model changes some value, returns the value to the controller, and the controller refreshes the view. Easy peezy!&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%2Fmiro.medium.com%2Fmax%2F541%2F1%2AZX00M-DmsrigKap7wzGoQQ.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%2Fmiro.medium.com%2Fmax%2F541%2F1%2AZX00M-DmsrigKap7wzGoQQ.png" alt="Redux flow. Life sucks now"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With Redux things work a little differently. Let’s say you have a component and you want to do something when a button gets pressed. Where do you start? Here is how I go about it.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Define your Action&lt;/li&gt;
&lt;li&gt;Define your Reducer&lt;/li&gt;
&lt;li&gt;Define the Actions as a Prop in your Component&lt;/li&gt;
&lt;li&gt;Wire it up in your View&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here is a simple code example to explain these concepts. In this example I will show how to edit a text input and when a user presses a button it will call an action to save it.&lt;/p&gt;

&lt;p&gt;First let’s start with the &lt;strong&gt;Action&lt;/strong&gt; file.&lt;/p&gt;


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


&lt;p&gt;Now onto our &lt;strong&gt;Reducer&lt;/strong&gt;. Basically, the reducer has to handle the actions that come in.&lt;/p&gt;


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


&lt;p&gt;Notice how the &lt;strong&gt;constants.MODIFY_NAME&lt;/strong&gt; and &lt;strong&gt;constants.SAVE_NAME&lt;/strong&gt; are exactly what is being returned by our actions in the type field. That is how you let the reducer know what action is happening.&lt;/p&gt;

&lt;p&gt;Now to define our “smart” &lt;strong&gt;component&lt;/strong&gt;. Really all this means is this is the component that will define the call to the actions.&lt;/p&gt;


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


&lt;p&gt;Now for the easiest part. You create your &lt;strong&gt;presentational component&lt;/strong&gt; that the user will interact with (the V in MVC).&lt;/p&gt;


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


&lt;p&gt;And that’s it! You still have to do some basic boilerplate setup stuff, but I hope this clears up how to think in redux.&lt;br&gt;
This was something that tripped me up for a little while (i.e. what information was being passed where and how…) so I’m hoping to save you guys some time and heartache.&lt;/p&gt;

&lt;p&gt;If you want to level up your coding skills, I’m putting together a playbook that includes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;30+ common code smells &amp;amp; how to fix them&lt;/li&gt;
&lt;li&gt;15+ design pattern practices &amp;amp; how to apply them&lt;/li&gt;
&lt;li&gt;20+ common JS bugs &amp;amp; how to prevent them&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Get early access to the &lt;a href="https://javascriptplaybook.com" rel="noopener noreferrer"&gt;Javascript playbook&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>codenewbie</category>
      <category>webdev</category>
      <category>react</category>
    </item>
    <item>
      <title>The Holy Trinity of Functional Programming: Map, Filter and Reduce</title>
      <dc:creator>Mikhail Levkovsky</dc:creator>
      <pubDate>Sat, 17 Aug 2019 21:36:20 +0000</pubDate>
      <link>https://dev.to/mlevkov/the-holy-trinity-map-filter-and-reduce-381e</link>
      <guid>https://dev.to/mlevkov/the-holy-trinity-map-filter-and-reduce-381e</guid>
      <description>&lt;p&gt;You've watched the Youtube videos, you've done the tutorials on Pluralsight and you consider yourself a Javascript expert.&lt;/p&gt;

&lt;p&gt;However, now you want to hang with all of the cool kids and talk about functional programming. &lt;em&gt;For loops&lt;/em&gt; are for suckers (well not really), higher order functions are all the rage and you want to understand what this means. Well the very first step is understanding the 3 most popular methods which are: Map, Filter and Reduce.&lt;/p&gt;

&lt;p&gt;It's important to understand how and when to use these methods, but more importantly, you also need to know when to avoid them.&lt;/p&gt;

&lt;p&gt;To setup some context, let's pretend we have an application that saves basic user information. Let's assume our User entity has the following properties: id, firstName, lastName, date of birth, email, an avatar url, a username and a flag to tell us if they are active or not.&lt;/p&gt;

&lt;h2&gt;
  
  
  User.ts
&lt;/h2&gt;


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


&lt;p&gt;If the annotations are not familiar don't worry about it. It's not important for the example and is just some TypeORM specific annotations.&lt;/p&gt;

&lt;p&gt;To explore the concepts of map, filter and reduce, we will create a class with the following methods:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. standardizeAvatars 👉 Accepts an array of users, and adds a base url to all of the avatar urls&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;2. getActiveUsers 👉 Accepts an array of users, and returns only the active ones.&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;3. getAllAges 👉 Accepts an array of users, and gets the total age of everyone in the array.&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  pssst I tweet about code stuff all the time. If you have questions about how to level up your dev skills give me a follow &lt;a href="https://twitter.com/mlevkov"&gt;@mlevkov&lt;/a&gt;
&lt;/h3&gt;
&lt;h2&gt;
  
  
  Map 🗺
&lt;/h2&gt;

&lt;p&gt;The .map function goes through every single element of an array and applies a passed in function to each element. There is one &lt;strong&gt;VERY VERY&lt;/strong&gt; important point to pay attention to that's not talked about is that the .map function goes through EVERY 👏 SINGLE 👏 ELEMENT 👏 and returns a brand new array with the modified values.&lt;/p&gt;

&lt;p&gt;That means you can't break out of the loop AND you will duplicate every single element of the array. If you have a huge collection that takes up a lot of memory, maybe using a good old fashion for loop is better.&lt;/p&gt;

&lt;p&gt;Here is an example of how to convert a for loop to .map.&lt;/p&gt;


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


&lt;h2&gt;
  
  
  Filter 🥅
&lt;/h2&gt;

&lt;p&gt;The .filter function goes through every single element of an array and checks if that element returns true or false when passed into the passed in function. If it returns true, we keep that element, otherwise we don't. Phew that's a mouthful. Don't worry there's an example below.&lt;/p&gt;

&lt;p&gt;Just like .map, the .filter function goes through EVERY 👏 SINGLE 👏 ELEMENT 👏 and returns a new array with just the elements that didn't get filtered out. So while your collection might shrink in size (or might not), you will still go through every single element without the option to &lt;em&gt;break&lt;/em&gt; or &lt;em&gt;continue&lt;/em&gt;. If you are doing some heavy computation then on every element that you want to keep, then consider using a for loop.&lt;/p&gt;

&lt;p&gt;Here is an example of how to convert use .filter and the same example with a for loop.&lt;/p&gt;


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


&lt;h2&gt;
  
  
  Reduce 👨‍👦
&lt;/h2&gt;

&lt;p&gt;The .reduce function will also go through every single element of an array BUT in this case it will not return you another collection, but a single element. In fact, you can say it "reduces" your collection to one item. &lt;/p&gt;

&lt;p&gt;The .reduce function accepts a custom function that you make and a placeholder value that keeps track of the operations. This will guarantee that your collection will shrink in size.&lt;/p&gt;


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


&lt;p&gt;There you have it, how to use the holy trinity of functional programming.&lt;/p&gt;

&lt;p&gt;If you want to level up your coding skills, I'm putting together a playbook that includes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;30+ common code smells &amp;amp; how to fix them&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;15+ design pattern practices &amp;amp; how to apply them&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;20+ common JS bugs &amp;amp; how to prevent them&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Get early access to the &lt;a href="https://javascriptplaybook.com/"&gt;Javascript playbook&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>typescript</category>
      <category>webdev</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>5 easy wins to refactor even the worst legacy code</title>
      <dc:creator>Mikhail Levkovsky</dc:creator>
      <pubDate>Sat, 10 Aug 2019 20:10:32 +0000</pubDate>
      <link>https://dev.to/mlevkov/5-easy-wins-to-refactor-even-the-worst-legacy-code-2bp5</link>
      <guid>https://dev.to/mlevkov/5-easy-wins-to-refactor-even-the-worst-legacy-code-2bp5</guid>
      <description>&lt;p&gt;So you just joined a new company, you're excited to learn the newest technology and work on some super cool new projects and then BAM, you have to first learn and navigate the legacy system.&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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F32jdr0l6qcv3zmmk9t7t.jpeg" 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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F32jdr0l6qcv3zmmk9t7t.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All of a sudden the excitement drains from your body as you're navigating helper file after helper file, unable to make heads or tails of the code base.&lt;/p&gt;

&lt;p&gt;In this post I will go over 5 VERY common code smells and how to fix them. If you think something is missing, please check out my previous post, &lt;a href="https://dev.to/mlevkov/5-easy-wins-to-refactor-even-the-ugliest-code-14lb"&gt;5 easy wins to refactor even the ugliest code&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  1) Conditionals should each get their own line
&lt;/h2&gt;

&lt;p&gt;Generally speaking your code will be a lot easier to read if each statement has its own line. The exception to the rule is to combine the else (or else/if) with the ending of the previous if. However, if you are writing a new if statement, it’s important to put it on a new line. This will prevent any future errors, as it may not be obvious that the two if statements are not logically connected.&lt;/p&gt;


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


&lt;h2&gt;
  
  
  2) Properly annotate your optional parameters
&lt;/h2&gt;

&lt;p&gt;Optional parameters can be found in most programming language. Typescript uses the '?', Java uses the 'Optional' type, in PHP you can just assign a default value to a method parameter. In Typescript/Javascript, just like keeping the default clause last in a switch statement, it’s important to follow convention to ease development. When it comes to optional parameters, it’s preferable to use the ‘?’ over the undefined definition.&lt;/p&gt;


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


&lt;h3&gt;
  
  
  pssst I tweet about code stuff all the time. If you have questions about how to level up your dev skills give me a follow &lt;a href="https://twitter.com/mlevkov" rel="noopener noreferrer"&gt;@mlevkov&lt;/a&gt;
&lt;/h3&gt;

&lt;h2&gt;
  
  
  3) Watch out for “Dead Stores”
&lt;/h2&gt;

&lt;p&gt;A dead store is when you assign a value to a variable but is then re-assigned without actually using the original value. Calculating or setting a value, without actually using it is at best a waste of resources, and at worst a bug in our code. For the following examples, let's assume we have an array of music tracks and we want to calculate the total runtime of all the songs.&lt;br&gt;
A little added bonus in the following example, is the use of the reduce function to get our value.&lt;/p&gt;


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


&lt;h2&gt;
  
  
  4) Don’t Invert Your Booleans
&lt;/h2&gt;

&lt;p&gt;One thing to keep in mind as you are developing, is that you are coding for humans, and not for the compilers. It’s better to keep things as simple and as humanly readable as possible. It’s way too complicated to read when you invert the result of a boolean expression, just use the opposite comparison instead.&lt;/p&gt;


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


&lt;h2&gt;
  
  
  5) Use Templates. Don't concatenate!
&lt;/h2&gt;

&lt;p&gt;When concatenating strings you should always stick to string templates instead of the concatenation operator. This will make your life much easier as it allows for multiline strings, reduces any errors if your strings have quotes and is generally a lot easier to read. Here is how it would look like when we try to create a TypeORM connection string without string templates and with.&lt;/p&gt;


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


&lt;p&gt;There you have it, 5 more easy tips that you can apply to almost any codebase.&lt;/p&gt;

&lt;p&gt;If you want to level up your coding skills, I'm putting together a playbook that includes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;30+ common code smells &amp;amp; how to fix them&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;15+ design pattern practices &amp;amp; how to apply them&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;20+ common JS bugs &amp;amp; how to prevent them&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Get early access to the &lt;a href="https://javascriptplaybook.com/" rel="noopener noreferrer"&gt;Javascript playbook&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>refactorit</category>
      <category>typescript</category>
    </item>
    <item>
      <title>5 easy wins to refactor even the ugliest code</title>
      <dc:creator>Mikhail Levkovsky</dc:creator>
      <pubDate>Sat, 03 Aug 2019 19:43:15 +0000</pubDate>
      <link>https://dev.to/mlevkov/5-easy-wins-to-refactor-even-the-ugliest-code-14lb</link>
      <guid>https://dev.to/mlevkov/5-easy-wins-to-refactor-even-the-ugliest-code-14lb</guid>
      <description>&lt;p&gt;Writing clean code can be a challenge when you start on a new project. Trying to clean up code in an already existing application without breaking anything is similar to this:&lt;/p&gt;


&lt;blockquote class="ltag__twitter-tweet"&gt;
      &lt;div class="ltag__twitter-tweet__media ltag__twitter-tweet__media__video-wrapper"&gt;
        &lt;div class="ltag__twitter-tweet__media--video-preview"&gt;
          &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HfdHr-Aw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://pbs.twimg.com/ext_tw_video_thumb/1150872436944048130/pu/img/giafOKzc_JB_HusD.jpg" alt="unknown tweet media content"&gt;
          &lt;img src="/assets/play-butt.svg" class="ltag__twitter-tweet__play-butt" alt="Play butt"&gt;
        &lt;/div&gt;
        &lt;div class="ltag__twitter-tweet__video"&gt;
          
            
          
        &lt;/div&gt;
      &lt;/div&gt;

  &lt;div class="ltag__twitter-tweet__main"&gt;
    &lt;div class="ltag__twitter-tweet__header"&gt;
      &lt;img class="ltag__twitter-tweet__profile-image" src="https://res.cloudinary.com/practicaldev/image/fetch/s---MkW3tSf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://pbs.twimg.com/profile_images/1126920897481175041/z9oVsqFL_normal.jpg" alt="Heavenly @EVO 🍑 profile image"&gt;
      &lt;div class="ltag__twitter-tweet__full-name"&gt;
        Heavenly @EVO 🍑
      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__username"&gt;
        @heavenlycontrol
      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__twitter-logo"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--P4t6ys1m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/twitter-f95605061196010f91e64806688390eb1a4dbc9e913682e043eb8b1e06ca484f.svg" alt="twitter logo"&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__body"&gt;
      If you pointed a gun to my head and told me to finish this level i would tell you to pull the trigger 
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__date"&gt;
      02:10 AM - 16 Jul 2019
    &lt;/div&gt;


    &lt;div class="ltag__twitter-tweet__actions"&gt;
      &lt;a href="https://twitter.com/intent/tweet?in_reply_to=1150950714090278916" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-reply-action.svg" alt="Twitter reply action"&gt;
      &lt;/a&gt;
      &lt;a href="https://twitter.com/intent/retweet?tweet_id=1150950714090278916" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-retweet-action.svg" alt="Twitter retweet action"&gt;
      &lt;/a&gt;
      55961
      &lt;a href="https://twitter.com/intent/like?tweet_id=1150950714090278916" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-like-action.svg" alt="Twitter like action"&gt;
      &lt;/a&gt;
      164660
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/blockquote&gt;


&lt;p&gt;I have been a technical lead for a few years and during that time I have seen my fair share of spaghetti code that I had to maintain, or worst extend. Through many painstakingly frustrating hours, and a dozen rubber ducks or so I have developed a few tips and tricks to help refactor even the ugliest code bases.&lt;/p&gt;

&lt;p&gt;I put together a list of 5 quick wins that you can either apply to any code base or keep in mind if you're starting a new project.&lt;/p&gt;

&lt;p&gt;Each win will start with a bad code sample followed by a good one with an explanation of what changed.&lt;/p&gt;

&lt;h2&gt;
  
  
  1) No Invariant Methods
&lt;/h2&gt;

&lt;p&gt;A method can be considered pretty much useless if it always returns the exact same thing no matter what is passed in.&lt;br&gt;
As you can see in the example invariant-method-bad.ts, getName will always return the exact same "NO NAME FOUND" message.&lt;br&gt;
We fix this in the invariant-method-good.ts method by simply returning the result that we intended to from the get go.&lt;/p&gt;


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


&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BBaZUjjv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/7i6ugvxnxpu1yvya6rfj.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BBaZUjjv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/7i6ugvxnxpu1yvya6rfj.jpg" alt="me during code reviews" title="me during code reviews"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2) No magic numbers or strings
&lt;/h2&gt;

&lt;p&gt;Magic numbers and strings are hardcoded values that you can find sprinkled throughout your code. If not done carefully you will end up repeating the exact same values across your entire code base, which will make changing your code, debugging and testing it a nightmare.&lt;br&gt;
As you can see below in magic-strings-bad.ts we have a hardcoded string in our method. In the file right below it, magic-strings-good.ts, we fixed this issue by refactoring it to a private read only class variable.&lt;/p&gt;


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


&lt;p&gt;A good question to ask regarding magic numbers and strings, is "Where should I put them?". Here is how I decide where to put them:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Is the value only in one file? 👉 a constant in the top of the file&lt;/li&gt;
&lt;li&gt;Is the value used across multiple files? 👉 a specific enum class that groups the constants logically&lt;/li&gt;
&lt;li&gt;Is the value used when the application boots (e.g. an api url, a timeout threshold…) 👉 a properties file&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  pssst I tweet about code stuff all the time. If you have questions about how to level up your dev skills give me a follow &lt;a href="https://twitter.com/mlevkov"&gt;@mlevkov&lt;/a&gt;
&lt;/h3&gt;

&lt;h2&gt;
  
  
  3) Keep the default clause last
&lt;/h2&gt;

&lt;p&gt;A switch statement can contain a default clause to handle unexpected value. Considering you are most likely working collaboratively on a code base, it's important to follow conventions to ease communication.&lt;br&gt;
Considering the example below, just moving the default clause all the way to the bottom will already save a lot of frustration for your peers.&lt;/p&gt;


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


&lt;h2&gt;
  
  
  4) Clean up your redundant variables
&lt;/h2&gt;

&lt;p&gt;Keep an eye out when you don't need to instantiate a variable at all. Redundant variables take up memory, clutter up your code, make it harder to debug and add new features. Try to keep your code as tidy as possible. In the examples below, it's painfully obvious we don't need to create an instance of a BackgroundImage each time, and can just assign the value for each case.&lt;/p&gt;


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


&lt;h2&gt;
  
  
  5) Forget the else, return if you can
&lt;/h2&gt;

&lt;p&gt;An important concept in keeping code clean is cyclomatic complexity. Cyclomatic complexity is basically how many loops, conditionals or function calls do you have nested in our code. Really high cyclomatic complexity makes code nearly impossible to wrap your head around. You will definitely need a dozen rubber ducks if it gets out of control.&lt;br&gt;
Let's simplify the example from above by going from a switch statement to a tidy series of ifs.&lt;/p&gt;


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


&lt;p&gt;This last example keeps the flow of logic dead simple. There is no alternative flows that you have to map out, everything is very straight forward and becomes much easier to digest.&lt;/p&gt;

&lt;p&gt;There you have it, 5 easy tips that you can apply to almost any codebase.&lt;/p&gt;

&lt;p&gt;If you want to level up your coding skills, I'm putting together a playbook that includes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;30+ common code smells &amp;amp; how to fix them&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;15+ design pattern practices &amp;amp; how to apply them&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;20+ common JS bugs &amp;amp; how to prevent them&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Get early access to the &lt;a href="https://javascriptplaybook.com/"&gt;Javascript playbook&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>typescript</category>
      <category>refactorit</category>
      <category>codenewbie</category>
    </item>
  </channel>
</rss>
