<?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: Albert Mulia Shintra</title>
    <description>The latest articles on DEV Community by Albert Mulia Shintra (@chenxeed).</description>
    <link>https://dev.to/chenxeed</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%2F303454%2F4f70aa53-305a-43c6-962b-d22173a61b6a.jpeg</url>
      <title>DEV Community: Albert Mulia Shintra</title>
      <link>https://dev.to/chenxeed</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/chenxeed"/>
    <language>en</language>
    <item>
      <title>What is your thought process to develop the extendable application?</title>
      <dc:creator>Albert Mulia Shintra</dc:creator>
      <pubDate>Tue, 02 Nov 2021 05:55:55 +0000</pubDate>
      <link>https://dev.to/chenxeed/what-is-your-thought-process-to-build-the-extendable-app-like-vscode-3ja3</link>
      <guid>https://dev.to/chenxeed/what-is-your-thought-process-to-build-the-extendable-app-like-vscode-3ja3</guid>
      <description>&lt;p&gt;Hi devs!&lt;/p&gt;

&lt;p&gt;I have a project that requires collaborative effort to build the application. The project codebase foundation should allow external developer to develop features without having to modify the foundation codebase.&lt;/p&gt;

&lt;p&gt;A reference for similar application that provide such extension is like VSCode Editor, Canva, Figma, or even Browser Extension. Please kindly share other apps that you think is similar as well.&lt;/p&gt;

&lt;p&gt;I appreciate any sharing or advice from your thought process to build this kind of application properly. Please leave me any feedback or questions if you need more detail. Thank you!&lt;/p&gt;

</description>
      <category>help</category>
      <category>javascript</category>
      <category>architecture</category>
      <category>discuss</category>
    </item>
    <item>
      <title>The Pragmatic Programmer highlighted points: Ch7. While We're Coding</title>
      <dc:creator>Albert Mulia Shintra</dc:creator>
      <pubDate>Mon, 04 Jan 2021 17:55:56 +0000</pubDate>
      <link>https://dev.to/chenxeed/the-pragmatic-programmer-highlighted-points-ch7-while-we-re-coding-4bl7</link>
      <guid>https://dev.to/chenxeed/the-pragmatic-programmer-highlighted-points-ch7-while-we-re-coding-4bl7</guid>
      <description>&lt;h1&gt;
  
  
  Disclaimer
&lt;/h1&gt;

&lt;p&gt;This article is just highlighting the tip of the iceberg from the original book. Please &lt;a href="https://pragprog.com/titles/tpp20/the-pragmatic-programmer-20th-anniversary-edition/"&gt;read the book&lt;/a&gt; for better detail and examples given by the author.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Listen to Your Lizard Brain
&lt;/h2&gt;

&lt;p&gt;Our instincts, aka the lizard brain, are simply a response to patterns packed into our nonconscious brain. Whatever their source, instincts share one thing: they have no words.&lt;/p&gt;

&lt;p&gt;The trick is first to notice it is happening, and then to work out why.&lt;/p&gt;

&lt;p&gt;When you are coding, the code is trying to tell you something. It's saying that this is harder than it should be. Whatever the reason, your lizard brain is sensing feedback from the code, and it's desperately trying to get you to listen.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Listen to Your Inner Lizard&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;First, stop what you're doing. Give yourself a little time and space to let your brain organize itself. Eventually, they may bubble up to the conscious level, and you have one of those &lt;em&gt;a ha!&lt;/em&gt; moments.&lt;/p&gt;

&lt;p&gt;If that's not working, try to externalizing the issue by writing a doodle of your code, or talking to your coworker or the rubber duck. You may have moments when you're explaining the problem and suddenly realized something.&lt;/p&gt;

&lt;p&gt;If it's still not coming, then it's time for action by creating a prototype. If you're working on existing code and it's pushing back, then stash it away somewhere and prototype up something similar instead. While prototyping, if that nagging doubt suddenly crystallizes into a solid concern, then address it.&lt;/p&gt;

&lt;p&gt;Not just our code, but we also deal with existing code, often written by other people. If you can see what drove them to write code that way, you may find the job of understanding it becomes a lot easier.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Programming by Coincidence
&lt;/h2&gt;

&lt;p&gt;We should avoid programming by coincidence, relying on luck and accidental successes, in favor of programming deliberately.&lt;/p&gt;

&lt;p&gt;If we don't know why the code is failing because we didn't know why it worked in the first place.&lt;/p&gt;

&lt;p&gt;If it worked, why should we take the risk of messing with something that's working? We can think of several reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It may not be really working, it might just look like it is&lt;/li&gt;
&lt;li&gt;The boundary condition you rely on maybe just an accident, as it might behave differently in different circumstances&lt;/li&gt;
&lt;li&gt;Undocumented behavior may change with the next release&lt;/li&gt;
&lt;li&gt;Additional and unnecessary calls make your code slower&lt;/li&gt;
&lt;li&gt;Additional calls increase the risk of introducing new bugs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Human beings are designed to see patterns and causes, even when it's just a coincidence. Don't assume it, prove it.&lt;/p&gt;

&lt;p&gt;Finding an answer that happens to fit is not the same as the right answer. Assumptions that aren't based on well-established facts are the bane of all projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Algorithm Speed
&lt;/h2&gt;

&lt;p&gt;Pragmatic Programmers estimate their resources that algorithms use such as time, processor, memory, and so on.&lt;/p&gt;

&lt;p&gt;Normally, the size of the input will affect the algorithm: the larger the input, the longer the running time or the more memory used.&lt;/p&gt;

&lt;p&gt;We find that whenever we write anything containing loops or recursive calls, we subconsciously check the runtime and memory requirements.&lt;/p&gt;

&lt;p&gt;The Big-O notation, written &lt;em&gt;O&lt;/em&gt;(), is a mathematical way of dealing with approximations.&lt;/p&gt;

&lt;p&gt;Big-O is never going to give you actual numbers for time or memory or whatever: it simply tells you how these values will change as the input changes.&lt;/p&gt;

&lt;p&gt;Address the potential problems and try to find a better approach to improve the algorithm speed. Then, test your estimates to see if it improve.&lt;/p&gt;

&lt;p&gt;You also need to be pragmatic about choosing appropriate algorithms, the fastest one is not always the best for the job. It's always a good idea to make sure an algorithm really is a bottleneck before investing your precious time trying to improve it.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Refactoring
&lt;/h2&gt;

&lt;p&gt;Code needs to evolve; it's not a static thing. Rather than construction, the software is more like &lt;em&gt;gardening&lt;/em&gt;, it is more organic than concrete.&lt;/p&gt;

&lt;p&gt;Refactoring is defined by Martin Fowler as a:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The critical part of this definition is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The activity is disciplined, not a free-for-all&lt;/li&gt;
&lt;li&gt;External behavior doesn't change&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In order to guarantee that the external behavior hasn't changed, you need good, automated unit testing that validates the behavior of the code.&lt;/p&gt;

&lt;p&gt;You refactor when you've learned something when you understand something better than you did last year, yesterday, or even just ten minutes ago.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Refactor early, refactor often&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Refactoring, as with most things, is easier to do while the issues are small, as an ongoing activity while coding. It shouldn't take a week to refactor, that's a full-on rewrite.&lt;/p&gt;

&lt;p&gt;Martin Fowler offers simple tips to refactor without doing more harm than good:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Don't try to refactor and add functionality at the same time&lt;/li&gt;
&lt;li&gt;Make sure you have good tests before you begin refactoring&lt;/li&gt;
&lt;li&gt;Take short, deliberate steps, and test after each step to avoid prolonged debugging&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  5. Test to Code
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Testing is Not About Finding Bugs&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We believe that the major benefits of testing happens when you think about and write the tests, not when you run them.&lt;/p&gt;

&lt;p&gt;Thinking about testing made us reduce coupling in our code and increase flexibility. Thinking about writing a test for our method made us look at it from the outside as if we're a client of the code, not the author.&lt;/p&gt;

&lt;p&gt;We think this is probably the biggest benefit offered by testing: testing is vital feedback that guides your coding.&lt;/p&gt;

&lt;p&gt;The basic cycle of TDD (Test Driven Development) is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Decide on a small piece of functionality you want to add&lt;/li&gt;
&lt;li&gt;Write a test that will pass once that functionality is implemented&lt;/li&gt;
&lt;li&gt;Run all tests. Verify that the only failure is the one you just wrote&lt;/li&gt;
&lt;li&gt;Write the smallest amount of code needed to get the test to pass, and verify it&lt;/li&gt;
&lt;li&gt;Refactor your code: see if there's a way to improve on what you just wrote, and make sure the test is still passed when you're done&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you follow the TDD workflow, you'll guarantee that you always have tests for your code.&lt;/p&gt;

&lt;p&gt;We like to think of unit testing as &lt;em&gt;testing against contract&lt;/em&gt;. We want to avoid creating a "time bomb", something that sits around unnoticed and blows up at an awkward moment later in the project. By emphasizing testing against contract, we can try to avoid as many of those downstream disasters as possible.&lt;/p&gt;

&lt;p&gt;Even the best sets of tests are unlikely to find all the bugs. This means you'll often need to test a piece of software once it has been deployed, with real-world data flowing through its veins.&lt;/p&gt;

&lt;p&gt;Log files containing trace messages are one such mechanism. Another mechanism for getting inside running code is the "hot-key" sequence or magic URL, to show a debugging message on the fly. This isn't something you normally would reveal to end-users, but it can be very handy for the help desk.&lt;/p&gt;

&lt;p&gt;Treat test code with the same care as any production code. Keep it decoupled, clean, and robust.&lt;/p&gt;

&lt;p&gt;Testing, design, coding - it's all programming&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Property-Based Testing
&lt;/h2&gt;

&lt;p&gt;There could be incorrect assumptions while writing unit tests. The code passes the tests because it does what it is supposed to do, based on your understanding.&lt;/p&gt;

&lt;p&gt;Once we work out our contracts and invariants, we can use them to automate our testing.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Use Property-Based Testing to Validate Your Assumptions&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;At this point, the book gives more concrete examples that a test may only cover commonly known scenarios and doesn't cater to bad assumptions. Thus we can improve the test by adding more property to test those assumptions.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;One suggestion is that when a property-based test fails, find out what parameters it was passing to the test function, and then use those values to create a separate, regular, unit test.&lt;/p&gt;

&lt;p&gt;We believe that property-based testing is complementary to unit testing: they address different concerns, and each brings its own benefits.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Stay Safe Out There
&lt;/h2&gt;

&lt;p&gt;As a matter of fact, you do need to be paranoid as spies or dissidents, every day.&lt;/p&gt;

&lt;p&gt;There are cases where hundreds of records stolen at once, billions of dollars in losses. It's not because the attackers were terribly clever or competent, it's because the developers were careless.&lt;/p&gt;

&lt;p&gt;The next thing you have to do is analyze the code for ways it can go wrong and add those to your test suite.&lt;/p&gt;

&lt;p&gt;The survival time of an unpatched, outdated system on the open net is measured in minutes, or even less.&lt;/p&gt;

&lt;p&gt;Pragmatic Programmers have a healthy amount of paranoia. There are a handful of basic principles that you should always bear in mind:&lt;/p&gt;

&lt;h3&gt;
  
  
  a. Minimize Attack Surface Area
&lt;/h3&gt;

&lt;p&gt;Simple, smaller code is better. Less code means fewer bugs, fewer opportunities for a crippling security hole.&lt;/p&gt;

&lt;p&gt;Never trust data from an external entity, always sanitize it before passing it on to a database, view rendering, or other processing.&lt;/p&gt;

&lt;p&gt;By nature, any user anywhere in the world can call unauthenticated services, so limit the opportunity for a DoS (Denial of Service) attack at the very least.&lt;/p&gt;

&lt;p&gt;If an account with deployment credentials is compromised, your entire product is compromised.&lt;/p&gt;

&lt;p&gt;Don't give away information that may reveal credential knowledge, for example: "Password is used by another user".&lt;/p&gt;

&lt;p&gt;Make sure any testing window and runtime exception reporting is protected from spying eyes.&lt;/p&gt;

&lt;h3&gt;
  
  
  b. Principle of Least Privilege
&lt;/h3&gt;

&lt;p&gt;Don't automatically grab the highest permission level, such as &lt;em&gt;root&lt;/em&gt; or &lt;em&gt;Administrator&lt;/em&gt;. If needed, then take it, do the minimum amount of work, and relinquish your permission quickly to reduce the risk.&lt;/p&gt;

&lt;h3&gt;
  
  
  c. Secure Defaults
&lt;/h3&gt;

&lt;p&gt;The default settings on your app, or for your users on your site, should be the most secure values.&lt;/p&gt;

&lt;h3&gt;
  
  
  d. Encrypt Sensitive Data
&lt;/h3&gt;

&lt;p&gt;Don't leave personally identifiable information, financial data, passwords, or other credentials in plain text.&lt;/p&gt;

&lt;h3&gt;
  
  
  e. Maintain Security Updates
&lt;/h3&gt;

&lt;p&gt;You need that security patch, but as a side effect, it breaks some portion of your application. Don't decide to wait, apply security patches quickly.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Naming Things
&lt;/h2&gt;

&lt;p&gt;Your brain treats written words as something to be respected. We need to make sure the names we use to live up to this.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;There are only two hard things in computer science: cache invalidation and naming things.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It's important the everyone on the team knows what the common names are being used and that they use them consistently.&lt;/p&gt;

&lt;p&gt;When you see a name that no longer expresses the intent, or is misleading or confusing, then fix it.&lt;/p&gt;

&lt;p&gt;If for some reason you can't change the now-wrong name, then you've got a bigger problem: an ETC (Easy to Change) violation. Fix that first, then change the offending name.&lt;/p&gt;

&lt;p&gt;Make renaming easy, and do it often.&lt;/p&gt;




&lt;p&gt;This chapter is quite long but they are very insightful for us to be aware of while we're coding. What's the struggle you face while coding? Please share it in the comment section and I'll be happy to learn from you!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>books</category>
      <category>writing</category>
      <category>productivity</category>
    </item>
    <item>
      <title>The Pragmatic Programmer highlighted points: Ch6. Concurrency</title>
      <dc:creator>Albert Mulia Shintra</dc:creator>
      <pubDate>Sat, 12 Dec 2020 16:33:51 +0000</pubDate>
      <link>https://dev.to/chenxeed/the-pragmatic-programmer-highlighted-points-ch6-concurrency-42pd</link>
      <guid>https://dev.to/chenxeed/the-pragmatic-programmer-highlighted-points-ch6-concurrency-42pd</guid>
      <description>&lt;h1&gt;
  
  
  Disclaimer
&lt;/h1&gt;

&lt;p&gt;This article is just highlighting the tip of the iceberg from the original book. Please &lt;a href="https://pragprog.com/titles/tpp20/the-pragmatic-programmer-20th-anniversary-edition/" rel="noopener noreferrer"&gt;read the book&lt;/a&gt; for better detail and examples given by the author.&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;Concurrency is when the execution of the two pieces of code act as if they run at the same time.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;Breaking Temporal Coupling&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;Temporal Coupling&lt;/em&gt; happens when your code imposes a sequence of things that are not required to solve the problem at hand.&lt;/p&gt;

&lt;p&gt;For example: &lt;em&gt;Tick&lt;/em&gt; doesn't need to come before the &lt;em&gt;tock&lt;/em&gt; if you want to stay flexible.&lt;/p&gt;

&lt;p&gt;We need to allow for concurrency and to think about decoupling any time or order dependencies.&lt;/p&gt;

&lt;p&gt;To find out what can happen at the same time, and what must happen in a strict order, we can use a notation such as &lt;strong&gt;the activity diagram&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The book gives a great example of the activity diagram by simulating the steps of preparing a &lt;em&gt;Piña colada&lt;/em&gt;. Instead of running the step one by one, it can run some activities in parallel, like running the blender while preparing the glasses and the pink umbrellas.&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%2Fi.ibb.co%2Fh984HB4%2Fvirgin-pina-colada-recipe-2097115-05-5b0d8124ff1b7800364356ae.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%2Fi.ibb.co%2Fh984HB4%2Fvirgin-pina-colada-recipe-2097115-05-5b0d8124ff1b7800364356ae.png" alt="pina-colada"&gt;&lt;/a&gt; &lt;a href="https://www.thespruceeats.com/virgin-pina-colada-recipe-2097115" rel="noopener noreferrer"&gt;source&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we're designing for concurrency, we're hoping to find activities that take time, but not time in our code.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Shared State Is Incorrect State
&lt;/h2&gt;

&lt;p&gt;Imagine two customers looking at the last pie on the display case and both are calling the server at the same time. One of them is going to be &lt;del&gt;error&lt;/del&gt; disappointed.&lt;/p&gt;

&lt;p&gt;This could happen when the process of reading the state and retrieving the value is asynchronous. The problem here is that neither process can guarantee that its view of that memory is consistent.&lt;/p&gt;

&lt;p&gt;This is all because fetching and then updating the pie count is not an atomic operation: the underlying value can change in the middle.&lt;/p&gt;

&lt;p&gt;To make it atomic, we can consider using &lt;em&gt;semaphore&lt;/em&gt;. A semaphore is simply a &lt;em&gt;thing&lt;/em&gt; that only one person can own at a time. When both processes run at the same time, one of them will proceed while the other will be suspended until the semaphore becomes available again.&lt;/p&gt;

&lt;p&gt;Not only shared state, but problems can also pop up anywhere where your code shares mutable resources: files, databases, external services, and so on. Whenever two or more instances of your code can access some resource at the same time, you're looking at a potential problem.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Random Failures are often Concurrency Issues&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Most languages have library support for some kind of exclusive access to shared resources. You could also argue that functional languages, with their tendency to make data immutable, make concurrency simpler. However, they still face the same challenges, because at some point they are forced to step into the real, mutable world.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Actors and Processes
&lt;/h2&gt;

&lt;p&gt;Actors and processes offer interesting ways of implementing concurrency without the burden of synchronizing access to shared memory.&lt;/p&gt;

&lt;p&gt;An &lt;em&gt;actor&lt;/em&gt; is an independent virtual processor with its own local (and private) state.&lt;/p&gt;

&lt;p&gt;A &lt;em&gt;process&lt;/em&gt; is typically a more general-purpose virtual processor, often implemented by the operating system to facilitate concurrency.&lt;/p&gt;

&lt;p&gt;There are a few things that you won't find in the definition of actors:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There's no single thing that's in control&lt;/li&gt;
&lt;li&gt;The only state in the system is held in messages and in the local state of each actor&lt;/li&gt;
&lt;li&gt;All messages are one way, there's no concept of replying&lt;/li&gt;
&lt;li&gt;An actor processes each message to completion, and only processes one message at a time&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Use Actors For Concurrency Without Shared State&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;IN the actor model, there's no need to write any code to handle concurrency, as there is no shared state.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Blackboards
&lt;/h2&gt;

&lt;p&gt;Imagine a murder case, and detectives are investigating the case by combining all the facts, statements, and clues in a single &lt;strong&gt;blackboard&lt;/strong&gt;. As the data accumulate, a detective might notice a connection and post that observation as well. This process keeps going on until the case is closed.&lt;/p&gt;

&lt;p&gt;Some key features of the blackboard approach are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;None of the detectives needs to know of the existence of any other detective&lt;/li&gt;
&lt;li&gt;The detectives may be trained in a different background, but they're all towards the same goal to solve the case&lt;/li&gt;
&lt;li&gt;Different detectives can come and go during the process&lt;/li&gt;
&lt;li&gt;There is no restriction on what may be placed on the blackboard&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Computer-based blackboard systems were originally used in AI applications where the problems to be solved were late and complex, such as speech recognition, knowledge-based reasoning system, and so on.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Use Blackboards to Coordinate Workflow&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The actor and/or blackboard and/or microservice approach to architecture removes a whole class of potential concurrency problems from your applications, but that benefit comes at a cost.  These approaches are harder to reason about because a lot of the action is indirect.&lt;/p&gt;

&lt;p&gt;You'll find it helpful to keep a central repository of message formats and/or APIs, particularly if the repository can generate the code and documentation for you. You'll also need good tooling to be able to trace messages and facts as they progress through the system.&lt;/p&gt;




&lt;p&gt;Did you face a conflict state problem in your application before? Any bugs you traced are hard to reproduce or randomly occurred? Hopefully, the concept of Concurrency helps to address the issue better. Please leave your comment if you experienced one, I would be happy to learn as well!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>books</category>
      <category>writing</category>
      <category>productivity</category>
    </item>
    <item>
      <title>The Pragmatic Programmer highlighted points: Ch5. Bend or Break</title>
      <dc:creator>Albert Mulia Shintra</dc:creator>
      <pubDate>Mon, 07 Dec 2020 18:26:04 +0000</pubDate>
      <link>https://dev.to/chenxeed/the-pragmatic-programmer-highlighted-points-ch5-bend-or-break-3am1</link>
      <guid>https://dev.to/chenxeed/the-pragmatic-programmer-highlighted-points-ch5-bend-or-break-3am1</guid>
      <description>&lt;h1&gt;
  
  
  Disclaimer
&lt;/h1&gt;

&lt;p&gt;This article is just highlighting the tip of the iceberg from the original book. Please &lt;a href="https://pragprog.com/titles/tpp20/the-pragmatic-programmer-20th-anniversary-edition/"&gt;read the book&lt;/a&gt; for better detail and examples given by the author.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Decoupling
&lt;/h2&gt;

&lt;p&gt;Coupling is the enemy of change because it links together things that must change in parallel.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Decoupled Code is Easier To Change&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here are some symptoms of coupling:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Wacky dependencies between unrelated modules or libraries&lt;/li&gt;
&lt;li&gt;"Simple" change to one module break stuff elsewhere in the system&lt;/li&gt;
&lt;li&gt;Developers are afraid to change code because they aren't sure what might be affected&lt;/li&gt;
&lt;li&gt;Meetings where everyone has to attend because no one is sure who will be affected by a change&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One way to look at this is to think about responsibilities. The fix for that is to apply something we call:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Tell, don't Ask&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The principle says that you shouldn't make decisions based on the internal state of an object and then update the object.&lt;/p&gt;

&lt;p&gt;The book also recommends us to not chain method calls, unless the things you're chaining are really unlikely to change. But in practice, anything in your application should be considered likely to change.&lt;/p&gt;

&lt;p&gt;Another coupling issue is also due to globalization, as globally accessible data is an insidious source of coupling between application components. When you make code reusable, you give it clean interfaces, decoupling it from the rest of your code.&lt;/p&gt;

&lt;p&gt;If you have a singleton with a bunch of exported instance variables, it's still just global data, with a longer name. You can still make a singleton by hiding all the data behind methods. For example, instead of &lt;code&gt;Config.log_level&lt;/code&gt;, it could be &lt;code&gt;Config.getLogLevel()&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Juggling the Real World
&lt;/h2&gt;

&lt;p&gt;We'll start off with the concept of an &lt;em&gt;event&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Whatever the source, if we write applications that respond to events, and adjust what they do based on those events, those applications will work better in the real world.&lt;/p&gt;

&lt;p&gt;These are the recommended four strategies:&lt;/p&gt;

&lt;h3&gt;
  
  
  a. Finite State Machine (FSM)
&lt;/h3&gt;

&lt;p&gt;A state machine is basically just a specification of how to handle events. The neat thing about FSM is that we can express them purely as data.&lt;/p&gt;

&lt;p&gt;State machines are underused by developers, and we'd like to encourage you to look for opportunities to apply them.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you're familiar with Javascript, my personal recommendation will be to go through &lt;a href="https://github.com/davidkpiano/frontend-masters-xstate-workshop"&gt;this workshop&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  b. The Observer Pattern
&lt;/h3&gt;

&lt;p&gt;In the observer pattern, we have a source of events, called the &lt;em&gt;observable&lt;/em&gt; and a list of clients, the &lt;em&gt;observers&lt;/em&gt;. who are interested in those events.&lt;/p&gt;

&lt;p&gt;The observer/observable pattern has been used for decades, and it has served us well. But the observer pattern has a problem: because each of the observers has to register with the observable, it introduces coupling. This is solved by the next strategy, Publish/Subscribe&lt;/p&gt;

&lt;h3&gt;
  
  
  c. Publish/Subscribe (Pubsub)
&lt;/h3&gt;

&lt;p&gt;Pubsub generalizes the observer pattern, at the same time solving the problems of coupling and performance.&lt;/p&gt;

&lt;p&gt;The downside is that it can be hard to see what is going on in a system that uses pubsub heavily since you can't look at a publisher and immediately see which subscribers are involved with a particular message.&lt;/p&gt;

&lt;h3&gt;
  
  
  d. Reactive Programming, Streams, and Events
&lt;/h3&gt;

&lt;p&gt;It's clear that events can also be used to trigger reactions in code, but it isn't necessarily easy to plumb them in. That's where &lt;em&gt;streams&lt;/em&gt; come in.&lt;/p&gt;

&lt;p&gt;The current &lt;em&gt;de facto&lt;/em&gt; baseline for reactive event handling is defined on the site &lt;a href="http://reactivex.io/"&gt;http://reactivex.io&lt;/a&gt;, which defines a language-agnostic set of principles and documents some common implementations. In Javascript, we can use &lt;a href="https://www.learnrxjs.io/"&gt;RxJs&lt;/a&gt; library.&lt;/p&gt;

&lt;p&gt;Event streams unify synchronous and asynchronous processing behind a common, convenient API.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Transforming Programming
&lt;/h2&gt;

&lt;p&gt;We need to get back to thinking of programs as being something that transforms inputs into outputs.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Programming is about code, but Programs are about Data&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Sometimes the easiest way to find the transformations is to start with the requirement and determine its inputs and outputs.&lt;/p&gt;

&lt;p&gt;If you're familiar with &lt;em&gt;|&amp;gt;&lt;/em&gt; operator which sometimes called a forward pipe or just a &lt;a href="https://rescript-lang.org/docs/manual/v8.0.0/pipe"&gt;pipe&lt;/a&gt;, then using it means that you're automatically thinking in terms of transforming data.&lt;/p&gt;

&lt;p&gt;If your background is OOP, then your reflexes demand that you hide data, encapsulating it inside objects. This introduces a lot of coupling, and it's a big reason that OO systems can be hard to change.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Don't Hoard State; Pass It Around&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To manage Error Handling during the state transformations, there are two basic ways of writing the code: you can handle checking for errors inside your transformations or outside them.&lt;/p&gt;

&lt;p&gt;Thinking of code as a series of (nested) transformations can be a liberating approach to programming.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Inheritance Tax
&lt;/h2&gt;

&lt;p&gt;If you use inheritance, stop! It probably isn't what you want to do.&lt;/p&gt;

&lt;p&gt;Inheritance is coupling. Not only the child class coupled to the parent, the parent's parent, and so on, but the code that uses the child is also coupled to all the ancestors.&lt;/p&gt;

&lt;p&gt;Here are the suggestions that can help you to never need to use inheritance again:&lt;/p&gt;

&lt;h3&gt;
  
  
  a. Interfaces and Protocols
&lt;/h3&gt;

&lt;p&gt;Using interfaces create no code since it simply says that any class that implements the interface must implement the methods defined, but doesn't dictate how the code should be written.&lt;/p&gt;

&lt;p&gt;Interfaces and protocols give us polymorphism without inheritance.&lt;/p&gt;

&lt;h3&gt;
  
  
  b. Delegation
&lt;/h3&gt;

&lt;p&gt;By using delegation, we can split the class methods to not rely on the parent class API. We can either write the method directly in the class or take a step further to create another class that delegates the method that is related to the class.&lt;/p&gt;

&lt;h3&gt;
  
  
  c. Mixins, Traits, Categories, Protocol Extensions
&lt;/h3&gt;

&lt;p&gt;The basic idea of mixins is simple: we want to be able to extend classes and objects with new functionality without using inheritance.&lt;/p&gt;

&lt;p&gt;The implementation of &lt;em&gt;mixins&lt;/em&gt; can vary between languages. The important thing is the capability that all these implementations have: merging functionality between &lt;em&gt;existing things&lt;/em&gt; and &lt;em&gt;new things&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Configuration
&lt;/h2&gt;

&lt;p&gt;When code relies on values that may change after the application has gone live, keep those values external to the app.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Parameterize Your App using External Configuration&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Some things you will probably want to put in configuration data:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Credentials for external services&lt;/li&gt;
&lt;li&gt;Port, IP address, machine, cluster names&lt;/li&gt;
&lt;li&gt;Environment specific validation parameters&lt;/li&gt;
&lt;li&gt;License keys&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To access the configuration, the book suggests wrapping the configuration information behind a (thin) API. This decouples your code from the details of the representation of the configuration.&lt;/p&gt;

&lt;p&gt;The benefits of storing configuration behind a service API:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multiple applications can share configuration information&lt;/li&gt;
&lt;li&gt;Configuration changes can be made globally&lt;/li&gt;
&lt;li&gt;Configuration data can be maintained via a specialized UI&lt;/li&gt;
&lt;li&gt;Configuration data becomes dynamic&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That last point is critical as we move toward highly available applications. The idea that we should have to stop and restart an application to change a single parameter is hopelessly out of touch with modern realities. When configuration values change, there's no need to rebuild the code.&lt;/p&gt;




&lt;p&gt;What's your experience to keep your code easy to change, and configurable? Please share your journey and I'll be happy to respond. Thank you!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>books</category>
      <category>productivity</category>
      <category>writing</category>
    </item>
    <item>
      <title>The Pragmatic Programmer highlighted points: Ch4. Pragmatic Paranoia</title>
      <dc:creator>Albert Mulia Shintra</dc:creator>
      <pubDate>Mon, 30 Nov 2020 16:31:22 +0000</pubDate>
      <link>https://dev.to/chenxeed/the-pragmatic-programmer-highlighted-points-ch4-pragmatic-paranoia-24p4</link>
      <guid>https://dev.to/chenxeed/the-pragmatic-programmer-highlighted-points-ch4-pragmatic-paranoia-24p4</guid>
      <description>&lt;h1&gt;
  
  
  Disclaimer
&lt;/h1&gt;

&lt;p&gt;This article is just highlighting the tip of the iceberg from the original book. Please read the book for better detail and examples given by the author.&lt;/p&gt;

&lt;h1&gt;
  
  
  Intro
&lt;/h1&gt;

&lt;p&gt;Before moving to the section, this is the first tip to be given:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You Can't Write Perfect Software&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Knowing that no one writes perfect code, including themselves, Pragmatic Programmers build in defenses against their own mistakes.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Design By Contract (DBC)
&lt;/h2&gt;

&lt;p&gt;What is the correct program? One that does no more and no less than it claims to be.&lt;/p&gt;

&lt;p&gt;The function shall have the expectation, as described by Meyer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Preconditions&lt;/strong&gt;: The routine requirements.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Postconditions&lt;/strong&gt;: The guaranteed statement when the routine is done.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Class invariants&lt;/strong&gt;: The condition is always true from the perspective of a caller.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The contract between a routine and any potential caller can be read as:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If all the routine's preconditions are met by the caller, the routine shall guarantee that all postconditions and invariants will be true when it completes.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Note: I still don't really get the meaning of the class invariants. I assume it was about the variable we passed on to the function, should not be mutated to a different type or interface. CMIIW&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In any programming language, whether it's functional, object-oriented, or procedural, DBC forces you to &lt;em&gt;think&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;By not implementing DBC in your function, you are back to &lt;em&gt;programming by coincidence&lt;/em&gt; which is where many projects start, finish, and fail.&lt;/p&gt;

&lt;p&gt;You can implement the contract in your code by writing an Assertion. By having it, you can crash early and report more accurate information about the problem.&lt;/p&gt;

&lt;p&gt;You can use &lt;em&gt;semantic invariants&lt;/em&gt; to express inviolate requirements, a kind of "philosophical contract*.&lt;/p&gt;

&lt;p&gt;The example given is about validating the same payment transaction should not happened twice. The contract would be, no matter what failure it might happen, the error should be on the side of &lt;em&gt;not processing&lt;/em&gt; a transaction, rather than processing a duplicate transaction.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Dead Program Tell No Lies
&lt;/h2&gt;

&lt;p&gt;It's easy to fall into the "it can't happen" mentality. When that really happens, Pragmatic Programmers tell themselves that something very, very bad has happened.&lt;/p&gt;

&lt;p&gt;That's one reason why each and every case/switch statement needs to have a default clause, to detect when the "impossible" has happened.&lt;/p&gt;

&lt;p&gt;When it happens, Read the Damn Error Message.&lt;/p&gt;

&lt;p&gt;If you try to use a lot of &lt;code&gt;try...catch&lt;/code&gt; to run a function, the Pragmatic Programmer would prefer to just run it directly. Two reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The application code isn't eclipsed by the error handling&lt;/li&gt;
&lt;li&gt;The code is less coupled&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One of the benefits of detecting problems as soon s you can is that you can crash earlier, and crashing is often the best thing you can do.&lt;/p&gt;

&lt;p&gt;When your code discovers that something that was supposed to be impossible just happened, your program is no longer viable.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Assertive Programming
&lt;/h2&gt;

&lt;p&gt;"The &lt;code&gt;count&lt;/code&gt; can't be negative"&lt;br&gt;
"Logging can't fail"&lt;br&gt;
"&lt;em&gt;This&lt;/em&gt; can never happen...*&lt;/p&gt;

&lt;p&gt;Let's not practice this kind of self-deception, particularly when coding. Instead, add code to check it. The easiest way to do this is with assertions.&lt;/p&gt;

&lt;p&gt;The assertion is to check a condition that should never happen. Make sure the assertion method itself doesn't do any side effects that might create new errors.&lt;/p&gt;

&lt;p&gt;A common misconception about assertions:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Assertions add some overhead to the code. If the code has been tested and shipped, the assertion shouldn't be needed because they check for things that should never happen.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The assumption above is wrong because.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Testing might not find all the bugs.&lt;/li&gt;
&lt;li&gt;The production environment could be different from the testing environment&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you do have performance issues from your assertion, turn off only those assertions that really hit you.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. How to Balance Resources
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Finish What You Start&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It simply means that the function or object that allocates a resource should be responsible for deallocating it.&lt;/p&gt;

&lt;p&gt;An example of this case could be: When you run a function that uses and rewrite a global variable, make sure that you are aware of it and update or reset the value back to what it supposed to be.&lt;/p&gt;

&lt;p&gt;The more surefire way to handle it is to avoid global or shared variable across functions, and "Act Locally" by scoping the variable and pass it around as a function parameter.&lt;/p&gt;

&lt;p&gt;Because Pragmatic Programmers trust no one including themselves, it is always a good idea to build code that actually checks that resources are indeed freed appropriately.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Don't Outrun Your Headlights
&lt;/h2&gt;

&lt;p&gt;We can't see too far ahead into the future, and the further off-axis you look, the darker it gets. So Pragmatic Programmers have a firm rule:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Take Small Steps. Always&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What is a task that's too big? Any task that requires "fortune-telling". An example of fortune-telling:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Estimate completion dates months in the future&lt;/li&gt;
&lt;li&gt;Plan a design for future maintenance or extendability&lt;/li&gt;
&lt;li&gt;Guess the user's future needs&lt;/li&gt;
&lt;li&gt;Guess future tech availability&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When we're supposed to design for future maintenance, only to the point as far ahead as you can see.&lt;/p&gt;

&lt;p&gt;The more you have to predict the future, the more risk that you'll be wrong. Instead, make it easy to throw out your code and replace it with something better suited.&lt;/p&gt;




&lt;p&gt;What are your paranoia during development? Have you been in a situation where your code breaks unexpectedly? Have you been trying to predict the future of your software and how far it can go? Please share your experience and feedback, and I would like to learn from you as well. Thanks for reading!&lt;/p&gt;

</description>
      <category>books</category>
      <category>productivity</category>
      <category>programming</category>
      <category>writing</category>
    </item>
    <item>
      <title>The Pragmatic Programmer highlighted points: Ch 3, The Basic Tools</title>
      <dc:creator>Albert Mulia Shintra</dc:creator>
      <pubDate>Sun, 22 Nov 2020 17:23:17 +0000</pubDate>
      <link>https://dev.to/chenxeed/the-pragmatic-programmer-highlighted-points-ch-3-the-basic-tools-8nh</link>
      <guid>https://dev.to/chenxeed/the-pragmatic-programmer-highlighted-points-ch-3-the-basic-tools-8nh</guid>
      <description>&lt;h1&gt;
  
  
  Disclaimer
&lt;/h1&gt;

&lt;p&gt;This article is just highlighting the tip of the iceberg from the original book. Please &lt;a href="https://pragprog.com/titles/tpp20/the-pragmatic-programmer-20th-anniversary-edition/" rel="noopener noreferrer"&gt;read the book&lt;/a&gt; for better detail and examples given by the author.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. The Power of Plain Text
&lt;/h2&gt;

&lt;p&gt;As Pragmatic Programmers, our base material is knowledge. The best format to store knowledge persistently is &lt;em&gt;plain text&lt;/em&gt;, so that we can manipulate it with different tools. Example of plain text: "milk" (my favourite drink 😉).&lt;/p&gt;

&lt;p&gt;Plain text doesn't mean it's unstructured: HTML, JSON, YAML and so are all plain text.&lt;/p&gt;

&lt;p&gt;Human-readable forms of data, and self-describing data, will outlive all other forms of data and the applications that created them.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Shell Games
&lt;/h2&gt;

&lt;p&gt;For a programmer manipulating files of text, that workbench is the command shell. Yes, it's the tool that you see a lot in the blockbuster movies when someone's hacking the enemy base.&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%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2F1%2F10%2FDir_command_in_Windows_Command_Prompt.png%2F370px-Dir_command_in_Windows_Command_Prompt.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%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2F1%2F10%2FDir_command_in_Windows_Command_Prompt.png%2F370px-Dir_command_in_Windows_Command_Prompt.png" alt="commandshell"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For programmers raised on GUI interfaces and IDEs (like me!), this might seem an extreme move. After all, can't you do everything equally well by pointing and clicking?&lt;/p&gt;

&lt;p&gt;The simple answer is "no". A benefit of GUI is "What You See Is What You Get", and the disadvantage is "What You See Is &lt;em&gt;All&lt;/em&gt; You Get".&lt;/p&gt;

&lt;p&gt;Gain familiarity with the command shell, and you'll find your productivity soaring. Once you do, you should customize your shell to suit your needs. Some common customization:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Setting color themes&lt;/li&gt;
&lt;li&gt;Configuring a prompt&lt;/li&gt;
&lt;li&gt;Aliases and shell functions&lt;/li&gt;
&lt;li&gt;Command completion&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. Power Editing
&lt;/h2&gt;

&lt;p&gt;Choose and use as many editors as you want, and Achieve Editor Fluency. You may gain more time to spend if you improve your code editing process.&lt;/p&gt;

&lt;p&gt;What does "Fluent" mean? Find your way if you can code without using a mouse/trackpad because you can keep your hands on the keyboard with all the shortcuts you know to navigate around.&lt;/p&gt;

&lt;p&gt;To move towards fluency, check if you are doing something repetitive, get into the habit of thinking "there must be a better way". Then find it, and practice it to become your muscle memory.&lt;/p&gt;

&lt;p&gt;Grow your editor by digging into the extension that may help you. If not, try to write it yourself.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Version Control
&lt;/h2&gt;

&lt;p&gt;Version control systems keep track of every change you make in your source code and documentation.&lt;/p&gt;

&lt;p&gt;It does far more than undoing mistakes. It can also track changes in detail, such as the code author, the time, the files, the lines of code. They are invaluable for bug-tracking, audit, performance, and quality purposes.&lt;/p&gt;

&lt;p&gt;One of the most powerful and useful features is &lt;em&gt;the branch&lt;/em&gt;, to isolates your work until it's ready to be merged into the main code.&lt;/p&gt;

&lt;p&gt;With version control, you can always roll it back if something happens. Learn them now, not when the disaster strikes and you are under pressure.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Debugging
&lt;/h2&gt;

&lt;p&gt;No one writes perfect software, so it's a given that debugging will take up a major portion of your day.&lt;/p&gt;

&lt;p&gt;Embrace the fact that debugging is just problem solving, and attack it as such. It doesn't really matter whether the bug is your fault or someone else's. Fix the Problem, Not the Blame.&lt;/p&gt;

&lt;p&gt;The first rule of debugging: Don't Panic.&lt;/p&gt;

&lt;p&gt;Always try to discover the root cause of a problem, not just the particular appearance of it.&lt;/p&gt;

&lt;p&gt;To improve the accuracy of the bug report, you may actually need to watch the user who reported the bug in action to get more detail.&lt;/p&gt;

&lt;p&gt;To discover the bug better, you must brutally test your application with a realistic end-user usage pattern. The best way to start fixing bugs is to make it reproducible.&lt;/p&gt;

&lt;p&gt;The most important rule of debugging: Have the failing test before fixing code.&lt;/p&gt;

&lt;p&gt;To isolate the bug, look at the problem. Read the Damn Error Message (yes, the book highlights it clearly).&lt;/p&gt;

&lt;p&gt;You can eliminate the culprit faster by doing the "binary chop". Choose a stack frame somewhere in the middle and see if the error is manifest there.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(personal tips: With &lt;code&gt;git&lt;/code&gt;, you can use &lt;a href="https://git-scm.com/docs/git-bisect" rel="noopener noreferrer"&gt;bisect&lt;/a&gt; to do the "binary chop")&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Diagnosis your code with Logging and/or Tracing, by adding custom messages around the suspected codebase to print the valuable information in your screen or development console.&lt;/p&gt;

&lt;p&gt;A very simple but particularly useful technique for finding the cause of a problem is simply to explain it to someone else. By explaining, you need to think step by step and you may gain new insights into the problem. If you're alone, use &lt;a href="https://rubberduckdebugging.com/" rel="noopener noreferrer"&gt;the rubber duck&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;When faced with a "surprising" failure, you must accept that one or more of your assumptions is wrong. Don't just assume the cause, but prove it.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Text Manipulation
&lt;/h2&gt;

&lt;p&gt;Learn a Text Manipulation Language. These languages are important enabling technologies.&lt;/p&gt;

&lt;p&gt;Using them, you can quickly hack up utilities and prototype ideas, which may take five or ten times longer with conventional languages.&lt;/p&gt;

&lt;p&gt;Some examples of Text Manipulation Language are Perl, Python, and Ruby.&lt;/p&gt;

&lt;p&gt;One of the challenges: Write a program that converts YAML to JSON format.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Engineering Daybooks
&lt;/h2&gt;

&lt;p&gt;Pragmatic Programmers use daybooks to take notes in meetings, to jot down what we're working on, note debugging process, leave reminders, wild ideas, or just doodle.&lt;/p&gt;

&lt;p&gt;Three main benefits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It is more reliable than memory.&lt;/li&gt;
&lt;li&gt;Store ideas that may not immediately relevant.&lt;/li&gt;
&lt;li&gt;It acts as a kind of rubber duck.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Use paper, not a file or a wiki. There's something special about the act of writing compared to typing. Try a month and see if you're getting any benefits.&lt;/p&gt;




&lt;p&gt;What do you think of your current programming tools? Please share your thoughts in the comment and I'll be happy to respond!&lt;/p&gt;

</description>
      <category>books</category>
      <category>productivity</category>
      <category>programming</category>
      <category>writing</category>
    </item>
    <item>
      <title>The Pragmatic Programmer highlighted points: Ch 2, A Pragmatic Approach</title>
      <dc:creator>Albert Mulia Shintra</dc:creator>
      <pubDate>Sun, 15 Nov 2020 18:06:56 +0000</pubDate>
      <link>https://dev.to/chenxeed/the-pragmatic-programmer-highlighted-points-ch-2-a-pragmatic-approach-505l</link>
      <guid>https://dev.to/chenxeed/the-pragmatic-programmer-highlighted-points-ch-2-a-pragmatic-approach-505l</guid>
      <description>&lt;h1&gt;
  
  
  Disclaimer
&lt;/h1&gt;

&lt;p&gt;This article is just highlighting the tip of the iceberg from the original book. Please &lt;a href="https://pragprog.com/titles/tpp20/the-pragmatic-programmer-20th-anniversary-edition/"&gt;read the book&lt;/a&gt; for better detail and examples given by the author.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. The Essence of Good Design
&lt;/h2&gt;

&lt;p&gt;Good Design is easier to change than Bad Design. Thus, we believe in the ETC principle: "Easier To Change".&lt;/p&gt;

&lt;p&gt;ETC is a Value, not a Rule. It is a guide to help you make decisions. It requires some initial conscious reinforcement, by keep asking yourself during development, "Is it easier or harder to change?"&lt;/p&gt;

&lt;p&gt;Two way to validate if your code is ETC:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Try to make what you write replaceable, to make sure it won't become a roadblock in the future&lt;/li&gt;
&lt;li&gt;Note the situation and choice you made, to reflect back on when you need to revisit the code&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. DRY - The Evils of Duplication
&lt;/h2&gt;

&lt;p&gt;Programmers are constantly in maintenance mode. Whatever the reason, maintenance is not a discrete activity, but a routine part of the entire development process.&lt;/p&gt;

&lt;p&gt;To develop reliable software, easier to understand and maintain, is to follow the DRY principle:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;These are the problems of duplication along with the general suggestion:&lt;/p&gt;

&lt;h3&gt;
  
  
  DRY is More Than Code
&lt;/h3&gt;

&lt;p&gt;DRY is not just to avoid copy-pasting code, but it is about the duplication of knowledge or intent. When a part of the code logic needs to change, and the changes apply to different places, it means your code is not DRY.&lt;/p&gt;

&lt;h3&gt;
  
  
  Duplication in Code
&lt;/h3&gt;

&lt;p&gt;Not all code duplication is knowledge duplication. If there are two functions that do similar things but each has a different intent or purpose, then they are still DRY.&lt;/p&gt;

&lt;h3&gt;
  
  
  Duplication in Documentation
&lt;/h3&gt;

&lt;p&gt;When you comment on a function with the details of how the function works, then the comment is a duplication of the code. The name of the function should already say what it does, and the details are laid out in the source code.&lt;/p&gt;

&lt;h3&gt;
  
  
  DRY Violations in Data
&lt;/h3&gt;

&lt;p&gt;A data structure represents knowledge and it may have duplicated data. Where possible, always use accessor functions to read/write the attributes of objects to decouple the data structure from the implementation module.&lt;/p&gt;

&lt;h3&gt;
  
  
  Representational Duplication
&lt;/h3&gt;

&lt;p&gt;Your code interfaces to the outside world. If the interface change, the other code that uses it may break. This duplication is inevitable but can be mitigated. Find tools that help you standardize and document your API to be shared across teams.&lt;/p&gt;

&lt;h3&gt;
  
  
  Interdeveloper Duplication
&lt;/h3&gt;

&lt;p&gt;Perhaps the hardest type of duplication to detect and handle. We feel that the best way is to encourage active and frequent communication between developers. Make the code easy to reuse. If it isn't, people won't do it, thus risk duplicating knowledge.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Orthogonality
&lt;/h2&gt;

&lt;p&gt;In computing, the term has come to signify a kind of independence or decoupling. Two or more things are orthogonal if changes in one do not affect any of the others.&lt;/p&gt;

&lt;p&gt;These are the benefits of Orthonogality:&lt;br&gt;
&lt;strong&gt;- Gain Productivity&lt;/strong&gt;: Reduce development and testing time since changes are localized, promote reusability, and composable.&lt;br&gt;
&lt;strong&gt;- Reduce Risk&lt;/strong&gt;: The code is isolated. Make any changes and it only affects that area if there is a bug.&lt;/p&gt;

&lt;p&gt;Several techniques to maintain orthogonality:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Keep your code decoupled&lt;/li&gt;
&lt;li&gt;Avoid global data&lt;/li&gt;
&lt;li&gt;Avoid similar functions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An orthogonally designed and implemented system is easier to test. Writing unit test is itself an interesting test of orthogonality, to validate what does it takes to build and run the module.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Reversibility
&lt;/h2&gt;

&lt;p&gt;We don't always make the best decisions the first time around. The mistake lies in assuming that any decision is final, and not preparing the uncertainties that might arise.&lt;/p&gt;

&lt;p&gt;Make your code easy to change. Build abstraction layers. Break your code into components.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Tracer Bullets
&lt;/h2&gt;

&lt;p&gt;The principle applies to projects when you're building something that hasn't been built before, to build a software that can be tested for immediate feedback while moving towards a goal.&lt;/p&gt;

&lt;p&gt;Look for the areas where you have doubts and the biggest risk, and prioritize it during development.&lt;/p&gt;

&lt;p&gt;Tracer code is not disposable, you write it for anticipating the production code.&lt;/p&gt;

&lt;p&gt;Advantages of the tracer code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Users get to see something working early&lt;/li&gt;
&lt;li&gt;Developers build a structure to work in&lt;/li&gt;
&lt;li&gt;You have an integration platform&lt;/li&gt;
&lt;li&gt;You have something to demonstrate&lt;/li&gt;
&lt;li&gt;You have a better feel for progress&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Tracer code may not always hit their target. Make sure your tracer code is easy to change, to be validated based on feedback, and keep improving to generate a more accurate version of the code quickly.&lt;/p&gt;

&lt;p&gt;Tracer code is not Prototyping. Prototyping generates disposable code. Tracer code is lean but complete and forms part of the skeleton of the final system.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Prototypes and Post-It Notes
&lt;/h2&gt;

&lt;p&gt;Prototyping is much cheaper than full-scale production. It is meant to analyze and expose risk, and make corrections at a greatly reduced cost.&lt;/p&gt;

&lt;p&gt;We tend to think of prototypes as code-based, but it does not always have to be.&lt;/p&gt;

&lt;p&gt;Prototyping is a learning experience. Its value lies not in the code produced, but in the lessons learned.&lt;/p&gt;

&lt;p&gt;Details to ignore in the prototyping:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Correctness&lt;/li&gt;
&lt;li&gt;Completeness&lt;/li&gt;
&lt;li&gt;Robustness&lt;/li&gt;
&lt;li&gt;Style&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You must make it very clear that the prototype code is disposable, incomplete, and unable to be completed. This avoid the stakeholder to think that the prototype is ready for production use.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Domain Languages
&lt;/h2&gt;

&lt;p&gt;In some cases, Pragmatic Programmers can go to the next level and actually program using the vocabulary, syntax, and semantics of the domain.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Estimating
&lt;/h2&gt;

&lt;p&gt;Estimate to avoid surprises. By learning to estimate, you will be able to show a magical ability to determine the feasibility of the task/project.&lt;/p&gt;

&lt;p&gt;All estimates are based on models of the problem. A basic estimating tricks that always gives good answers: ask someone who's already done it.&lt;/p&gt;

&lt;p&gt;You may able to give estimation in two way:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Give the optimistic, most likely, and the pessimistic estimate&lt;/li&gt;
&lt;li&gt;Give initial estimated time, and update the schedule on every development iteration&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When asked, you don't have to give your estimation directly. Spend some time going through the mental model for better accuracy.&lt;/p&gt;




&lt;p&gt;I would love to read your sharing of your approach as well. Please leave a comment and I'll be happy to respond. Thank you!&lt;/p&gt;

</description>
      <category>books</category>
      <category>productivity</category>
      <category>programming</category>
      <category>writing</category>
    </item>
    <item>
      <title>The Pragmatic Programmer highlighted points: Ch 1, A Pragmatic Philosophy</title>
      <dc:creator>Albert Mulia Shintra</dc:creator>
      <pubDate>Sun, 08 Nov 2020 15:47:05 +0000</pubDate>
      <link>https://dev.to/chenxeed/the-pragmatic-programmer-highlighted-points-ch-1-a-pragmatic-philosophy-2n2i</link>
      <guid>https://dev.to/chenxeed/the-pragmatic-programmer-highlighted-points-ch-1-a-pragmatic-philosophy-2n2i</guid>
      <description>&lt;h1&gt;
  
  
  Disclaimer
&lt;/h1&gt;

&lt;p&gt;This article is just highlighting the tip of the iceberg from the original book. Please &lt;a href="https://pragprog.com/titles/tpp20/the-pragmatic-programmer-20th-anniversary-edition/"&gt;read the book&lt;/a&gt; for better detail and examples given by the author.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. It's Your Life
&lt;/h2&gt;

&lt;p&gt;Software development appears close to the top list of careers where you have control. If you're frustrated with your current environment, try to fix it.&lt;/p&gt;

&lt;p&gt;This industry gives you a remarkable set of opportunities. Be proactive, and take them.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. The Cat Ate My Source Code
&lt;/h2&gt;

&lt;p&gt;Pragmatic Programmers take charge of their own careers and aren't afraid to admit ignorance or error. We can be proud of our abilities, but we must own up to our shortcomings as well.&lt;/p&gt;

&lt;h3&gt;
  
  
  Team Trust
&lt;/h3&gt;

&lt;p&gt;Trust in a team is absolutely essential for creativity and collaboration.&lt;/p&gt;

&lt;h3&gt;
  
  
  Take Responsibility
&lt;/h3&gt;

&lt;p&gt;When you accept the responsibility for an outcome, expect to be held accountable for it. When you make a mistake, admit it honestly. Provide options, instead of blaming or making excuses.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Software Entropy
&lt;/h2&gt;

&lt;p&gt;Entropy is a term from physics that refers to the amount of "disorder" in a system. Disorder in software is usually caused by "technical debt". Don't leave them unrepaired.&lt;/p&gt;

&lt;p&gt;If you find yourself on a project where the code is cleanly written and well-designed, you'll likely take extra care to not mess it up.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Stone Soup and Boiled Frogs
&lt;/h2&gt;

&lt;p&gt;If you are in a situation where you know what needs to be done and how to do it, you may get "start-up fatigue" when you need to wait for permission, budget, or resources to do it.&lt;/p&gt;

&lt;p&gt;Instead, start with what you can already do and develop it. Once you've got it, show it to others and let them be excited and foster the idea further.&lt;/p&gt;

&lt;p&gt;On the other side, keep an eye on the big picture. Constantly review what's happening around you, not just what you personally are doing.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Good Enough Software
&lt;/h2&gt;

&lt;p&gt;As Ed Yourdon described in an article in IEEE Software, "When good enough software is the best, you can discipline yourself to write software that's good enough. Good enough for your users, future maintainers, and your own peace of mind".&lt;/p&gt;

&lt;h3&gt;
  
  
  Involve Your Users in the Trade-Off
&lt;/h3&gt;

&lt;p&gt;Understand the requirement from different constraints. Marketing deadlines, delivery schedule, key features should be discussed as part of the development process, among the software quality to ensure everyone's expectations are met.&lt;/p&gt;

&lt;p&gt;Often times, trade-offs are involved. Focus on the minimum deliverables that your user can play with to get their early feedback.&lt;/p&gt;

&lt;h3&gt;
  
  
  Know When To Stop
&lt;/h3&gt;

&lt;p&gt;Don't spoil your good program by overengineering and overrefinement. &lt;/p&gt;

&lt;h2&gt;
  
  
  6. Your Knowledge Portofolio
&lt;/h2&gt;

&lt;p&gt;Investment in knowledge always pays the best interest, but they're expiring assets, as it may be out of date.&lt;/p&gt;

&lt;p&gt;Build your portfolio by these guidelines:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Invest Regularly&lt;/strong&gt;: You must invest your knowledge portfolio regularly, even if it's just a small amount.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Diversify&lt;/strong&gt;: The more different things you know, the more valuable you are.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Manage Risk&lt;/strong&gt;: Don't put all your technical eggs in one basket.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Buy Low, Sell High&lt;/strong&gt;: Learn an emerging technology before it becomes popular.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Review and Rebalance&lt;/strong&gt;: You may revisit the technology you investigated before.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is the suggestion for you to build your portfolio:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Read a technical book each month&lt;/li&gt;
&lt;li&gt;Read non-technical books, too&lt;/li&gt;
&lt;li&gt;Take classes&lt;/li&gt;
&lt;li&gt;Participate in the community around you&lt;/li&gt;
&lt;li&gt;Experiment with different environment&lt;/li&gt;
&lt;li&gt;Stay up to date with the current trend&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Opportunities for Learning
&lt;/h3&gt;

&lt;p&gt;If you're asked a question that you don't know the answer to, don't stop there. Take it as a personal challenge to find the answer. Search the answer, or ask someone who can help you.&lt;/p&gt;

&lt;h3&gt;
  
  
  Critical Thinking
&lt;/h3&gt;

&lt;p&gt;The last important point is to think critically about what you read and hear. Few question you can ask and think about:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Why, why, why, why, and why? (The Five Whys)&lt;/li&gt;
&lt;li&gt;Who does this benefit?&lt;/li&gt;
&lt;li&gt;What's the context?&lt;/li&gt;
&lt;li&gt;When or Where would this work?&lt;/li&gt;
&lt;li&gt;Why is this a problem?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  7. Communicate!
&lt;/h2&gt;

&lt;p&gt;As a developer, we have to communicate on many levels. Here is a list of ideas for useful communication:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Know Your Audience&lt;/strong&gt;: Understand the needs, interests, and capabilities of your audience. Gather their feedback as well.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Know What You Want To Say&lt;/strong&gt;: Plan what you want to say. Plan an outline, review it yourself, and refine it until you believe it fits your audience.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Choose Your Moment&lt;/strong&gt;: Sometimes all it takes is the simple question, "Is this a good time to talk about ...?"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Choose A Style&lt;/strong&gt;: Adjust your style to suit your audience, be it formal, just the facts, or long discussion.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Make It Look Good&lt;/strong&gt;: Use styles, visual, and spelling-check to convey your audience better.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Involve Your Audience&lt;/strong&gt;: If possible, give your drafts to your audience and get their feedback. Build a relationship with them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Be a Listener&lt;/strong&gt;: If you want people to listen to you, &lt;em&gt;listen to them&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Get Back to People&lt;/strong&gt;: Keep your audience informed makes them feel that you haven't forgotten them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Documentation&lt;/strong&gt;: Pragmatic Programmers embrace documentation as an integral part of the overall development process.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Do you find this article helpful for you? Any feedback to make it better? Please leave a comment and I'll be happy to respond. Thank you!&lt;/p&gt;

</description>
      <category>books</category>
      <category>productivity</category>
      <category>programming</category>
      <category>writing</category>
    </item>
    <item>
      <title>The Pragmatic Programmer highlighted points: Intro and Preface</title>
      <dc:creator>Albert Mulia Shintra</dc:creator>
      <pubDate>Sun, 08 Nov 2020 15:46:44 +0000</pubDate>
      <link>https://dev.to/chenxeed/the-pragmatic-programmer-highlighted-points-intro-and-preface-al4</link>
      <guid>https://dev.to/chenxeed/the-pragmatic-programmer-highlighted-points-intro-and-preface-al4</guid>
      <description>&lt;h1&gt;
  
  
  Intro
&lt;/h1&gt;

&lt;p&gt;Heya!&lt;/p&gt;

&lt;p&gt;I have been wanting to do this for quite some time and I finally take the courage to start typing this. It begins when my current company, &lt;a href="https://piktochart.com"&gt;Piktochart&lt;/a&gt; bought &lt;strong&gt;The Pragmatic Programmer 20th Edition&lt;/strong&gt; for the developers. I am not a reader by nature, but this book has been recommended and stays relevant to the developer community for 20 years, so I'm tempted to read it myself.&lt;/p&gt;

&lt;p&gt;While reading, I found there are a lot of points that I find very true and useful. Thus, I believe that highlighting those points may help me to remember more!&lt;/p&gt;

&lt;p&gt;And this is the reason behind me for writing this post, to share the highlighted points and hopefully, it's useful for you. (And thanks for reading my intro in the first place!)&lt;/p&gt;

&lt;p&gt;ps: &lt;em&gt;The post will be separated by the chapters, from 1 to 9. I will post a new chapter every week, so stay tuned!&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Disclaimer
&lt;/h1&gt;

&lt;p&gt;The highlighted points that I wrote here are based on "The Pragmatic Programmer 20th Anniversary Edition" Version: P1.0, September 13, 2019. The points that I picked are based on my subjectivity and experiences.&lt;/p&gt;

&lt;p&gt;Some people also have written the summary as well out there, for example: &lt;a href="https://github.com/HugoMatilla/The-Pragmatic-Programmer"&gt;https://github.com/HugoMatilla/The-Pragmatic-Programmer&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The summary here is just &lt;em&gt;the tip of the iceberg&lt;/em&gt; from the original book as there are more details and examples not mentioned. &lt;strong&gt;Please support and &lt;a href="https://pragprog.com/titles/tpp20/the-pragmatic-programmer-20th-anniversary-edition/"&gt;buy the original book&lt;/a&gt; for the full overview&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If the publisher finds this inappropriate, please email me at &lt;a href="mailto:chenxeed@gmail.com"&gt;chenxeed@gmail.com&lt;/a&gt; and I will unpublish this article at will.&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Preface
&lt;/h1&gt;

&lt;p&gt;This book will help you to become a better programmer. It isn't theoretical, but more on practical topics. This book is about doing.&lt;/p&gt;

&lt;p&gt;Programming is a craft. There is no best solution. Don't just stick to any particular technology but have a broad enough background and experience base to allow you to have more options in particular situations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Who should read this book?
&lt;/h2&gt;

&lt;p&gt;This book is aimed at those who want to become more effective and productive programmers.&lt;/p&gt;

&lt;h2&gt;
  
  
  What makes a pragmatic programmer?
&lt;/h2&gt;

&lt;p&gt;These are the characteristics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Early Adopter&lt;/strong&gt;: You love trying things out, and able to grasp it quickly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Inquisitive&lt;/strong&gt;: You tend to ask questions to clarify the facts&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Critical Thinker&lt;/strong&gt;: You rarely take things without first getting the facts and challenges it&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Realistic&lt;/strong&gt;: You understand the nature of the problem and how much effort it will take&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Jack of all trades&lt;/strong&gt;: You're familiar with a broad range of technologies and environments&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Apart from the characteristic above, these are the most basic:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Care about your craft&lt;/strong&gt;: Make sure you are doing it well&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Think! About your work&lt;/strong&gt;: Always think what you are doing&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Individual Pragmatists, Large Teams
&lt;/h2&gt;

&lt;p&gt;Within the overall structure of a project, there is always room for individuality and craftsmanship.&lt;/p&gt;

&lt;h2&gt;
  
  
  It's a Continuous Process
&lt;/h2&gt;

&lt;p&gt;Every day, work to refine the skills you have and to add new tools to your projects.&lt;/p&gt;




&lt;p&gt;Thanks for reading, I would like to know your feedback if you find this post interesting, or if you have any suggestion to make it better. See you in the next post!&lt;/p&gt;

</description>
      <category>books</category>
      <category>productivity</category>
      <category>programming</category>
      <category>writing</category>
    </item>
    <item>
      <title>Consider Vue Composition API to improve the code quality</title>
      <dc:creator>Albert Mulia Shintra</dc:creator>
      <pubDate>Mon, 27 Jul 2020 14:58:31 +0000</pubDate>
      <link>https://dev.to/chenxeed/consider-vue-composition-api-to-improve-the-code-quality-31ne</link>
      <guid>https://dev.to/chenxeed/consider-vue-composition-api-to-improve-the-code-quality-31ne</guid>
      <description>&lt;p&gt;Heya!&lt;/p&gt;

&lt;p&gt;I have been working and prototyping with Vue Composition API for a while since the Vue 3 beta released on March. I would like to share some good experience I have while using it, for your reference if you're planning to use the new Vue 3, or migrate from Vue 2. Here we go!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;note: the code example are based on the new Vue 3 convention&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  1. No more &lt;code&gt;this&lt;/code&gt; instance
&lt;/h1&gt;

&lt;p&gt;As a Javascript Developer, we may have to deal with &lt;code&gt;this&lt;/code&gt; variable for quite some scenario due to the JS common behavior of inheriting the object or class instance. One of the common quirks you will face is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"In most cases, the value of this is determined by how a function is called (runtime binding). &lt;strong&gt;It can't be set by assignment during execution, and it may be different each time the function is called.&lt;/strong&gt;" - &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this"&gt;MDN&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You may face the similar situation while writing &lt;strong&gt;Vue with the object based properties&lt;/strong&gt;, since the &lt;code&gt;this&lt;/code&gt; instance is very tight to the concept of Vue inheriting its object properties, and the root prototypes. This is the example of a component called &lt;code&gt;my-counter&lt;/code&gt;, that should increment the count value by clicking the "Add" button, or press the key &lt;code&gt;+&lt;/code&gt; from your keyboard.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight vue"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;Count: &lt;span class="si"&gt;{{&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="si"&gt;}}&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="na"&gt;click=&lt;/span&gt;&lt;span class="s"&gt;"incrementCount"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Add&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;/&lt;/span&gt;&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;script&lt;/span&gt;&lt;span class="nt"&gt;&amp;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="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my-counter&lt;/span&gt;&lt;span class="dl"&gt;'&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="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;count&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="nx"&gt;mounted&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// register keyboard event to listen to the `+` key press&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;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;keydown&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&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;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;keyCode&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;187&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// 187 is keyCode for `+`&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;incrementCount&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;span class="na"&gt;methods&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;incrementCount&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;count&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;span class="nt"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="k"&gt;script&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It looks fine and simple. Notice that the &lt;code&gt;this&lt;/code&gt; in the method, it contains the &lt;code&gt;.count&lt;/code&gt; value from the &lt;code&gt;data&lt;/code&gt; we defined before. But also, &lt;code&gt;this&lt;/code&gt; contains more than that. It also contains the Vue root instance, the plugin installed (vuex, router, etc), &lt;code&gt;$attrs&lt;/code&gt;, slots, and more.&lt;/p&gt;

&lt;p&gt;Did you see there's a bug in the code above? If yes, good eye! There is an error on pressing the &lt;code&gt;+&lt;/code&gt; key from your keyboard, saying that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Uncaught TypeError: this.incrementCount is not a function
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is because the callback function of the event listener is bound to the instance of the &lt;code&gt;document&lt;/code&gt;, not the &lt;code&gt;Vue&lt;/code&gt; component. This can be easily solved by changing the function method to &lt;code&gt;arrow based function&lt;/code&gt;, but beginner developer may not realize it earlier, and they have to understand the inheritance concept of JS to get used to this.&lt;/p&gt;

&lt;p&gt;Okay, sorry for the long post 🥔 to explain the basic quirk of &lt;code&gt;this&lt;/code&gt;, now let's jump into Composition API!&lt;/p&gt;

&lt;p&gt;In the Composition API, it has no reliance to the &lt;code&gt;this&lt;/code&gt; instance. Everything is done in the &lt;code&gt;setup&lt;/code&gt; phase, which consist of creating the data and methods of your component. Here's the example of Composition API based on the &lt;code&gt;my-counter&lt;/code&gt; component above:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight vue"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;Count: &lt;span class="si"&gt;{{&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="si"&gt;}}&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="na"&gt;click=&lt;/span&gt;&lt;span class="s"&gt;"incrementCount"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Add&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;/&lt;/span&gt;&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;script&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&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;reactive&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;toRefs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;onMounted&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;vue&lt;/span&gt;&lt;span class="dl"&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="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my-counter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;setup&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;reactive&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;count&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;incrementCount&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="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;

    &lt;span class="nx"&gt;onMounted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="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;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;keydown&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&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;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;keyCode&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;187&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// 187 is keyCode for '+'&lt;/span&gt;
          &lt;span class="nx"&gt;incrementCount&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;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;toRefs&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;incrementCount&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="nt"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="k"&gt;script&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's compare the difference. Before, you rely on the object property &lt;code&gt;data&lt;/code&gt; to register the state &lt;code&gt;count&lt;/code&gt;, and &lt;code&gt;methods&lt;/code&gt; to register the function to increment the &lt;code&gt;count&lt;/code&gt;. The &lt;code&gt;methods&lt;/code&gt; rely on &lt;code&gt;this&lt;/code&gt; instance to access the &lt;code&gt;count&lt;/code&gt; value.&lt;/p&gt;

&lt;p&gt;After refactored into the Composition API, the functionality are all wrapped under &lt;code&gt;setup&lt;/code&gt; to initiate the data, create a function to mutate the count, and also attach keyboard event listener. No more quirks on &lt;code&gt;this&lt;/code&gt; value, so either normal or arrow function is not a problem anymore!&lt;/p&gt;

&lt;h1&gt;
  
  
  2. Better code splitting management
&lt;/h1&gt;

&lt;p&gt;With the Composition API example above, we can see that now we don't have to follow the Vue convention to write the component functionality to separated properties (&lt;code&gt;lifecycle hooks, data, methods, computed, watch&lt;/code&gt;), as everything can be composed as one function in the &lt;code&gt;setup&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It opens the chance for us to split our code if we want to organize the code better, especially when the component functionality is complicated. We can write all the functionality under the &lt;code&gt;setup&lt;/code&gt;, or we can also create a JS file to scope specific functionality to other file.&lt;/p&gt;

&lt;p&gt;Let's take the example from the &lt;code&gt;my-counter&lt;/code&gt; component. What if we want to split the functionality to attach the keyboard event separately?&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;// keyboard-event.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;onMounted&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;vue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;usePlusKey&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callbackFn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;onMounted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="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;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;keydown&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&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;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;keyCode&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;187&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// 187 is keyCode for '+'&lt;/span&gt;
        &lt;span class="nx"&gt;callbackFn&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="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;Now, we can import and use this function to the &lt;code&gt;setup&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;reactive&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;toRefs&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;vue&lt;/span&gt;&lt;span class="dl"&gt;'&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;usePlusKey&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;./keyboard-event&lt;/span&gt;&lt;span class="dl"&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="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my-counter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;setup&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;reactive&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;count&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;incrementCount&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="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;

    &lt;span class="nx"&gt;usePlusKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;incrementCount&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;toRefs&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;incrementCount&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;You may argue if it's important or not to split the keyboard listener function above, but I hope you get the idea that it's up to you to manage your code and the Composition API give you easier way to handle it. Another advantage that you see above, is that the lifecycle hook of the component can be defined separately!&lt;/p&gt;

&lt;p&gt;If you need to handle multiple scenario on mounted, now you can split them. 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;// my-component.vue&lt;/span&gt;
&lt;span class="nx"&gt;mounted&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;initPayment&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;initTracking&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="nx"&gt;methods&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;initPayment&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* init payment */&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="nx"&gt;initTracking&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* init tracking */&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 the Composition API:&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;// my-component/payment.js&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;initPayment&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;onMounted&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="cm"&gt;/* init payment */&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// my-component/tracking.js&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;initTracking&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;onMounted&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="cm"&gt;/* init tracking */&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// my-component.vue&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;initPayment&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;./payment&lt;/span&gt;&lt;span class="dl"&gt;'&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;initTracking&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;./tracking&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; 

&lt;span class="nx"&gt;setup&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;initPayment&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nx"&gt;initTracking&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;h1&gt;
  
  
  3. Function Reusability
&lt;/h1&gt;

&lt;p&gt;With the example above, we can see the potential that the function is not only meant for one component only, but can also be used for others!&lt;/p&gt;

&lt;p&gt;The reusability concept is similar to &lt;a href="https://vuejs.org/v2/guide/mixins.html"&gt;mixins&lt;/a&gt;. However there's a drawback of mixins, which is explained &lt;a href="https://css-tricks.com/how-the-vue-composition-api-replaces-vue-mixins/#mixins-are-considered-harmful"&gt;here&lt;/a&gt;. In short, naming collision and implicit dependencies are a "hidden bug" that can bite you when you're using it carelessly.&lt;/p&gt;

&lt;p&gt;With the Composition API, these two concern are &lt;del&gt;gone&lt;/del&gt; less likely to happen since the composition API function need to explicitly define the value it needs as a function parameter, and the variable name of the return value.&lt;/p&gt;

&lt;p&gt;Let's see the example of a mixin of counter functionality:&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;// mixin/counter.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mixinCounter&lt;/span&gt; &lt;span class="o"&gt;=&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="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;counter&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="na"&gt;methods&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;increment&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;counter&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using this mixin, we have to be considerate that it may overwrite the existing &lt;code&gt;counter&lt;/code&gt; data and &lt;code&gt;increment&lt;/code&gt; methods in the component it installed. This is what it means by "implicit dependencies".&lt;/p&gt;

&lt;p&gt;If we convert it to the Composition API:&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;// compose/counter.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;ref&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;vue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;useCounter&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;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ref&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;increment&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="nx"&gt;counter&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="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;increment&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;Using this function, it explicitly return &lt;code&gt;counter&lt;/code&gt; and &lt;code&gt;increment&lt;/code&gt; and let the component &lt;code&gt;setup&lt;/code&gt; to decide what to do with it. If by chance the name &lt;code&gt;counter/increment&lt;/code&gt; is already used or you need to use it multiple times, then we can still fix it by rename the variable 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;// use default counter and increment name&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;counter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;increment&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useCounter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;// since counter and increment already exist,&lt;/span&gt;
&lt;span class="c1"&gt;// rename it to countQty and incrementQty&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;countQty&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;incrementQty&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useCounter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cool! Perhaps one consideration here is, you need some extra time to bike shedding on deciding the new name of the variable 😅.&lt;/p&gt;

&lt;h1&gt;
  
  
  4. More control of the Typescript interface
&lt;/h1&gt;

&lt;p&gt;Are you using typescript to type your component interface properly? If yes, great!&lt;/p&gt;

&lt;p&gt;From the &lt;a href="https://vuejs.org/v2/guide/typescript.html#Basic-Usage"&gt;official docs&lt;/a&gt;, Vue has provided basic typescript support with &lt;code&gt;Vue.extend&lt;/code&gt;, or using &lt;a href="https://github.com/vuejs/vue-class-component"&gt;vue-class-component&lt;/a&gt; to write the Vue component as a class, leveraging the &lt;code&gt;this&lt;/code&gt; instance to type the data and methods properly.&lt;/p&gt;

&lt;p&gt;Refer back to the 1st point if we want to escape the &lt;code&gt;this&lt;/code&gt; quirks and still have strong typing interface, then the Composition API is a good choice.&lt;/p&gt;

&lt;p&gt;First, &lt;code&gt;setup&lt;/code&gt; is a pure function that takes the input parameter to replace the needs of using &lt;code&gt;this&lt;/code&gt; to access the component &lt;code&gt;props&lt;/code&gt; and the context &lt;code&gt;attrs&lt;/code&gt;, &lt;code&gt;slots&lt;/code&gt;, and &lt;code&gt;emit&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Then, all the data and function you wrote in the &lt;code&gt;setup&lt;/code&gt;, is up to you to type it 😍! You can write and type your code without having to abide to the Vue way of defining things like &lt;code&gt;data&lt;/code&gt;, &lt;code&gt;methods&lt;/code&gt;, &lt;code&gt;refs&lt;/code&gt;, &lt;code&gt;computed&lt;/code&gt; and &lt;code&gt;watch&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here's the example of a typed Vue component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// we use Vue.extend in vue v2.x&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;Vue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;extend&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="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;count&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="na"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;multiplyCount&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;count&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="na"&gt;methods&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;increment&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;count&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="na"&gt;watch&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// `val` type is `any` :(&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;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we rely on the &lt;code&gt;Vue.extend&lt;/code&gt; to automatically type the component interface. The &lt;code&gt;this.count&lt;/code&gt; on the computed &lt;code&gt;multiplyCount&lt;/code&gt; and method &lt;code&gt;increment&lt;/code&gt; will have the proper typing from the &lt;code&gt;data&lt;/code&gt;, but the watcher &lt;code&gt;count&lt;/code&gt; will not be typed 😕.&lt;/p&gt;

&lt;p&gt;Let's see how it's written in the Composition API:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// in vue 3.x, we use defineComponent&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;defineComponent&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="nx"&gt;setup&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;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ref&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;// typed to number&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;multiplyCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;computed&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;count&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="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;increment&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="nx"&gt;count&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;watch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// `val` is typed to number&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;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;multiplyCount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;increment&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The typing here is more explicit and predictable. You can customize the typing too if you need too, means that you are in control over the interface!&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;And that's all of my insight for you to consider using the Vue Composition API!&lt;/p&gt;

&lt;p&gt;I believe there's a lot more potential in the Composition API, so please share your feedback about your experience or what do you think about it! Any tips to improve will be appreciated too 😍&lt;/p&gt;

&lt;p&gt;I would like to highlight as well that the Composition API is not a silver bullet and &lt;strong&gt;you don't have to refactor your component to the Composition API&lt;/strong&gt; if you don't see a benefit of it, or your component is pretty simple.&lt;/p&gt;

&lt;p&gt;Thank you and have a great day!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>vue</category>
      <category>typescript</category>
      <category>codequality</category>
    </item>
    <item>
      <title>Be prepared to migrate your Vue app to Vue 3</title>
      <dc:creator>Albert Mulia Shintra</dc:creator>
      <pubDate>Sun, 12 Jul 2020 16:04:48 +0000</pubDate>
      <link>https://dev.to/chenxeed/be-prepared-to-migrate-your-vue-app-to-vue-3-eom</link>
      <guid>https://dev.to/chenxeed/be-prepared-to-migrate-your-vue-app-to-vue-3-eom</guid>
      <description>&lt;p&gt;Heya!&lt;/p&gt;

&lt;p&gt;In this post, I would like to share my recent experience of trying out Vue 3 beta version, specifically regarding some caveats you might want to note if you are planning to migrate your existing Vue 2 application to the upgraded version, Vue 3!&lt;/p&gt;

&lt;p&gt;The list below are going to help you to move towards Vue 3 best practices, and also to avoid some use-case in Vue 2 that you might going to have trouble with, if you're migrating to Vue 3.&lt;/p&gt;

&lt;p&gt;Let's get it on!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ukld0-PW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://pics.me.me/lets-get-it-on-memegenerator-net-lets-get-it-on-53147989.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ukld0-PW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://pics.me.me/lets-get-it-on-memegenerator-net-lets-get-it-on-53147989.png" alt="lets get it on" width="500" height="491"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Avoid using Vue Event Bus
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Short summary: Don't use &lt;code&gt;$on&lt;/code&gt; / &lt;code&gt;$once&lt;/code&gt; / &lt;code&gt;$off&lt;/code&gt; API since it'll be deprecated in Vue 3.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you have heard of Event Bus, it's a common term used in the Vue development whenever you're facing a situation where you need to make a shortcut to emit event from a child to parent, or vice versa. You can simply search "vue event bus" in your browser, and you'll find a lot of article explaining it.&lt;/p&gt;

&lt;p&gt;One thing to note is that, &lt;strong&gt;it's not the official method recommended by Vue&lt;/strong&gt; 🤯. Why I'm saying this, it's because you'll likely never see Event Bus mentioned in the official Vue docs. The closest reference is from &lt;a href="https://vuejs.org/v2/guide/migration.html#dispatch-and-broadcast-replaced"&gt;the migration guide from Vue 1.x&lt;/a&gt;, in which it's called "eventHub" there, and it recommends you to use Vuex instead.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;This pattern can serve as a replacement for $dispatch and $broadcast in simple scenarios, but for more complex cases, it’s recommended to use a dedicated state management layer such as Vuex.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can check the &lt;a href="https://github.com/vuejs/rfcs/blob/master/active-rfcs/0020-events-api-change.md"&gt;RFC docs&lt;/a&gt; as well to see why they don't recommend it.&lt;/p&gt;

&lt;p&gt;Since Event Bus concept is practically a Publish-Subscribe pattern which is a common method in programming, you can actually still use the concept, but with different libraries like &lt;a href="https://github.com/developit/mitt"&gt;mitt&lt;/a&gt;. 😉&lt;/p&gt;

&lt;p&gt;This is the example of Event Bus, and how to refactor it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Vue 2 example of event bus&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Vue&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;vue&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;eventBus&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Vue&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// subscribe&lt;/span&gt;
&lt;span class="nx"&gt;eventBus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;$on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sandwich-made&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sandwich made!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="c1"&gt;// publish&lt;/span&gt;
&lt;span class="nx"&gt;eventBus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;$emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sandwich-made&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Refactor to use 3rd party library like mitt&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;mitt&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;mitt&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;eventBus&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;mitt&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// subscribe&lt;/span&gt;
&lt;span class="nx"&gt;eventBus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sandwich-made&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sandwich made!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="c1"&gt;// publish&lt;/span&gt;
&lt;span class="nx"&gt;eventBus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sandwich-made&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Refactor your Filter functions to Method
&lt;/h1&gt;

&lt;p&gt;Based on the &lt;a href="https://github.com/vuejs/rfcs/blob/master/active-rfcs/0015-remove-filters.md"&gt;RFC docs&lt;/a&gt;, filter are going to be removed.&lt;/p&gt;

&lt;p&gt;Filter was there to help you "format" your value, for ex: to uppercase, add currency, short url, etc. Perhaps it's also inspired by &lt;a href="https://docs.angularjs.org/api/ng/filter/filter"&gt;Angular Filter&lt;/a&gt;. It looks nice since you can implement it in your template syntax. For example, here's a filter &lt;code&gt;toCurrency&lt;/code&gt; to add currency format to the price integer value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight vue"&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;"currency"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;{{ price | toCurrency }}&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;&lt;em&gt;note: price value is 25, then filtered by &lt;code&gt;toCurrency&lt;/code&gt; to $25.00&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Although it looks nice, keep in mind that it's practically a "syntax sugar" since in the runtime, it will always run &lt;code&gt;toCurrency&lt;/code&gt; to format the price whenever it's updated.&lt;/p&gt;

&lt;p&gt;If you refactor &lt;code&gt;toCurrency&lt;/code&gt; as a &lt;code&gt;method&lt;/code&gt; instead, it'll be written like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight vue"&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;"currency"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;{{ toCurrency(price) }}&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;The refactor in your Vue script is simply to move the function from &lt;code&gt;filter&lt;/code&gt; to &lt;code&gt;method&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;// before&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;toCurrency&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="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&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="nx"&gt;toFixed&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="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="c1"&gt;// after&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;methods&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;toCurrency&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="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&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="nx"&gt;toFixed&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="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;Cool! But, what if the &lt;code&gt;filter&lt;/code&gt; is registered as a global filter?&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;Vue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;toCurrency&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&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;`$&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="nx"&gt;toFixed&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="s2"&gt;`&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, I will recommend you to delete the global filter code above, and move your filter function to become a pure helper function first that can be shared. 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;// helper/filter.js&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;toCurrency&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="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&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="nx"&gt;toFixed&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="s2"&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 then, you can import the helper function to whenever component that needs to use it. 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;// price-component.vue&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;toCurrency&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;./helper/filter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="c1"&gt;// your vue object&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;methods&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;toCurrency&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;em&gt;note: just &lt;code&gt;toCurrency&lt;/code&gt; will work ;) thanks to &lt;a href="https://alligator.io/js/object-property-shorthand-es6/"&gt;ES6 object property shorthand&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Refactor your component &lt;code&gt;model&lt;/code&gt; into &lt;code&gt;.sync&lt;/code&gt;
&lt;/h1&gt;

&lt;p&gt;Based on the &lt;a href="https://github.com/vuejs/rfcs/blob/master/active-rfcs/0011-v-model-api-change.md"&gt;RFC docs&lt;/a&gt;, Vue 3 will deprecate the &lt;code&gt;model&lt;/code&gt; option in the Vue component, and it will replace &lt;a href="https://vuejs.org/v2/guide/components-custom-events.html#sync-Modifier"&gt;&lt;code&gt;sync&lt;/code&gt;&lt;/a&gt; to become multiple &lt;code&gt;model&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you have used &lt;code&gt;model&lt;/code&gt; option in your component to set two-way data binding, you can refactor it to become &lt;code&gt;.sync&lt;/code&gt; instead. Here's the example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight vue"&gt;&lt;code&gt;// BEFORE

// parent component
&lt;span class="nt"&gt;&amp;lt;child-component&lt;/span&gt; &lt;span class="na"&gt;v-model=&lt;/span&gt;&lt;span class="s"&gt;"visible"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

// the model option in the child component's Vue object
&lt;span class="nt"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;script&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;prop&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;visible&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;change&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="nt"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="k"&gt;script&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Refactor it to use &lt;code&gt;.sync&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight vue"&gt;&lt;code&gt;// AFTER

// parent component
&lt;span class="nt"&gt;&amp;lt;child-component&lt;/span&gt; &lt;span class="na"&gt;v-bind:visible.sync=&lt;/span&gt;&lt;span class="s"&gt;"visible"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

// delete the model option in the child component's Vue object
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the time comes for you to upgrade to Vue 3, you can simply rename the &lt;code&gt;.sync&lt;/code&gt; to &lt;code&gt;v-model&lt;/code&gt; instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight vue"&gt;&lt;code&gt;// Vue 3

// parent component
&lt;span class="nt"&gt;&amp;lt;child-component&lt;/span&gt; &lt;span class="na"&gt;v-model:visible=&lt;/span&gt;&lt;span class="s"&gt;"visible"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Easy peasy lemon squeezy! 😋&lt;/p&gt;

&lt;h1&gt;
  
  
  Be wary of using 3rd party plugins
&lt;/h1&gt;

&lt;p&gt;The beauty of Vue framework like other framework is, it provides API for community to create their own plugin.&lt;/p&gt;

&lt;p&gt;But in Vue 3, &lt;strong&gt;there will be a breaking changes that will make some plugins not compatible anymore&lt;/strong&gt;. One major change is, the Plugin installation and app initialization will be immutable from the original Vue instance. 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;// BEFORE, in Vue 2&lt;/span&gt;
&lt;span class="nx"&gt;Vue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myPlugin&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Vue&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="cm"&gt;/* your vue initialization */&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// AFTER, in Vue 3&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;createApp&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// new method to initialize Vue&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myPlugin&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's most likely that you will not able to refactor your code to use the plugins in the Vue 3, until the author of the plugin upgrades it. So this means, you should be considerate of using the 3rd party plugins if you're planning to migrate, as this will be the blocker.&lt;/p&gt;

&lt;p&gt;Check the issue or roadmap of the plugins you're using, to see if they're planning to upgrade to support Vue 3. This is the example of the plugins that'll support Vue 3:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/bootstrap-vue/bootstrap-vue/issues/5196"&gt;Bootstrap Vue&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/shentao/vue-multiselect/issues/1291"&gt;Vue Multiselect&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://vuetifyjs.com/en/introduction/roadmap/"&gt;Vuetify&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If your used plugins doesn't have the plan yet to upgrade to Vue 3, you can help to contribute by asking the author in the issue to support Vue 3, or even take part to help them upgrade. 🤗&lt;/p&gt;

&lt;h1&gt;
  
  
  Use &lt;code&gt;@vue/composition-api&lt;/code&gt; to write your component
&lt;/h1&gt;

&lt;p&gt;I really appreciate the Vue community for providing the &lt;code&gt;@vue/composition-api&lt;/code&gt; 🥰. Not only it can be used for the developer to hands-on using the &lt;a href="https://composition-api.vuejs.org/"&gt;Composition API&lt;/a&gt;, but it also provide API that'll become the core method in Vue 3.&lt;/p&gt;

&lt;p&gt;Take for example, &lt;code&gt;defineComponent&lt;/code&gt;. It will become the new standard of writing a Vue component in Vue 3, and you can already use it in your Vue 2 app!&lt;/p&gt;

&lt;p&gt;To use it, install &lt;code&gt;@vue/composition-api&lt;/code&gt; and replace your Vue object component initiation. 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;// BEFORE&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Vue&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;vue&lt;/span&gt;&lt;span class="dl"&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;Vue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;extend&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my-component&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="cm"&gt;/* your component props, data, methods, etc. */&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Simply replace it to use &lt;code&gt;defineComponent&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;// AFTER&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;defineComponent&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;@vue/composition-api&lt;/span&gt;&lt;span class="dl"&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;defineComponent&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my-component&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="cm"&gt;/* your component props, data, methods, etc. */&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 difference, you ask? Practically no difference, and that's the beauty of it! Your component shall work the same as usual, and added with "bonus" that now you can already refactor your component to use Composition API if you want to:&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;// Refactor to use setup()&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;defineComponent&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;@vue/composition-api&lt;/span&gt;&lt;span class="dl"&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;defineComponent&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my-component&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;setup&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/* your component props, data, methods, etc. */&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;em&gt;note: if you love Typescript, I'm pretty sure you will love the composition API because it'll help to &lt;a href="https://dev.to/chenxeed/consider-vue-composition-api-to-improve-the-code-quality-31ne"&gt;improve your component typing&lt;/a&gt;. ;)&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  What's More
&lt;/h1&gt;

&lt;p&gt;There will be another breaking changes such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/vuejs/rfcs/blob/master/active-rfcs/0008-render-function-api-change.md"&gt;Render function API change&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/vuejs/rfcs/blob/master/active-rfcs/0006-slots-unification.md"&gt;Unify scoped slots to just slots&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/vuejs/rfcs/blob/master/active-rfcs/0014-drop-keycode-support.md"&gt;keyCode modifier to be removed&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/vuejs/rfcs/blob/master/active-rfcs/0016-remove-inline-templates.md"&gt;inline-template to be removed&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But if you're not using it much, and you believe it can be refactored easily, then it's up to you to decide if it shall be changed soon or later.&lt;/p&gt;

&lt;h1&gt;
  
  
  Summary
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NQnByzGj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://image.shutterstock.com/image-vector/illustration-phew-sound-kid-boy-260nw-1438891808.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NQnByzGj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://image.shutterstock.com/image-vector/illustration-phew-sound-kid-boy-260nw-1438891808.jpg" alt="phew" width="252" height="280"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Phew! I hope this article helps you to be prepared when the time comes to upgrade to the Vue 3. As a Vue developer, I know that I am excited to see it coming, since it uses better API to handle reactivity, more Typescript support, and &lt;a href="https://dev.to/chenxeed/awesome-breaking-changes-in-vue-3-if-you-migrate-from-vue-2-3b98"&gt;better practices&lt;/a&gt; in the development.&lt;/p&gt;

&lt;p&gt;If I missed any API or notes to be defined, please let me know and I appreciate your feedback. Thank you and have a great day!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>vue</category>
      <category>tutorial</category>
      <category>changelog</category>
    </item>
    <item>
      <title>Use a Mediator in your Javascript Project to improve productivity</title>
      <dc:creator>Albert Mulia Shintra</dc:creator>
      <pubDate>Wed, 24 Jun 2020 14:18:28 +0000</pubDate>
      <link>https://dev.to/chenxeed/build-a-mediator-in-your-javascript-project-to-improve-productivity-3325</link>
      <guid>https://dev.to/chenxeed/build-a-mediator-in-your-javascript-project-to-improve-productivity-3325</guid>
      <description>&lt;p&gt;Heya! 👋&lt;/p&gt;

&lt;p&gt;In this post, I would like to share a design pattern to help organize your application code structure, especially when you are working as a team and the code needs to be developed collectively.&lt;/p&gt;

&lt;p&gt;Let's imagine a real use case to help us go through the tips.&lt;/p&gt;

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

&lt;p&gt;You need to build a payment form and a module to handle the payment. Let's say there's two developer to work on it, &lt;strong&gt;you and Albert&lt;/strong&gt;. (Yes you, let's work together! 😘)  On the discussion, you discussed with Albert that you're going to split the task: &lt;strong&gt;Albert will be working on the payment form&lt;/strong&gt;, and &lt;strong&gt;you'll handle the payment module&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The scope is quite specific, since Albert is mostly only working on the client side interface while you will be working on the API. It usually means, Albert should be working on the HTML code and the form submission, while you are working on the method to send the payment data to the API and check the response.&lt;/p&gt;

&lt;p&gt;If this app is handled in a single file, it might look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;onsubmit=&lt;/span&gt;&lt;span class="s"&gt;"return submitForm(event)"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;Credit Card: &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"credit-card"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;CVV: &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"cvv"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"Submit"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// pagePayment.js&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;submitForm&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;creditCard&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="mi"&gt;0&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cvv&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="mi"&gt;1&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;success&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;sendPayment&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="nx"&gt;creditCard&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;cvv&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;success&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;payment success!&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;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;sendPayment&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="c1"&gt;// let's just pretend this API exist ;)&lt;/span&gt;
  &lt;span class="k"&gt;return&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;/pay&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looking at the basic payment code above to receive credit card information and submit it to the API, it can (and definitely) still be improved based on the app specification. But in the current situation, you and Albert will have to modify the same file, which likely raise conflict on merging the code. 😰&lt;/p&gt;

&lt;p&gt;Imagine these use cases to add:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add validation to the form input&lt;/li&gt;
&lt;li&gt;Add multiple payment services&lt;/li&gt;
&lt;li&gt;Create different payment form for different payment services&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To avoid the conflict, the code should be separated by concern. We can simply modularize the payment module, but if the form submission code directly call the payment module, it creates "dependency" with the form and thus making the payment module hard to change.&lt;/p&gt;

&lt;p&gt;One solution to decouple the user interface code and the module code is, by having a "Mediator".&lt;/p&gt;

&lt;h1&gt;
  
  
  Mediator Pattern
&lt;/h1&gt;

&lt;p&gt;Let's see what it means:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Mediator is a behavioral design pattern that lets you reduce chaotic dependencies between objects. The pattern restricts direct communications between the objects and forces them to collaborate only via a mediator object." - &lt;a href="https://refactoring.guru/design-patterns/mediator"&gt;refactoring.guru&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;With this concept, now you and Albert can modify and improve your own codebase without having too worried about breaking each other. Let's see what kind of "Mediator" will serve them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// payment-mediator.js&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;paymentMethod&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;registerPaymentMethod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;method&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;module&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;paymentMethod&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;method&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="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Payment Method &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;method&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; is already exist!`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nx"&gt;paymentMethod&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;method&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;module&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;sendPayment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;method&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="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;paymentMethod&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;method&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="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Payment Method &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;method&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; is not exist!`&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;paymentMethod&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;method&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;send&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;Just like a traffic control, the mediator will keep the registered payment method and use it when someone needs to send the payment.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Pro tips: if you're a fan of Typescript, you can define the interface of payment module to make sure that the registered module have the expected interface.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now let's get back to our dear developers, you and Albert 😄&lt;/p&gt;

&lt;p&gt;For Albert, he can focus on improving his form submission script:&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;// pagePayment.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;sendPayment&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;./payment-mediator.js&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;submitForm&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;creditCard&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="mi"&gt;0&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cvv&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="mi"&gt;1&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;success&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;sendPayment&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&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;creditCard&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;cvv&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;success&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;payment success!&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;As for you, you can improve and add more payment method independently as well:&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;// payment-module.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;registerPaymentMethod&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;./payment-mediator.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;function&lt;/span&gt; &lt;span class="nx"&gt;payDefault&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="c1"&gt;// let's just pretend this API exist ;)&lt;/span&gt;
  &lt;span class="k"&gt;return&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;/pay&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="nx"&gt;registerPaymentMethod&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&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;payDefault&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's it for the basic mediator! Hopefully this makes you and Albert stay productive, able to work independently, and cheers for the collaboration 🍻&lt;/p&gt;

&lt;h1&gt;
  
  
  Consideration
&lt;/h1&gt;

&lt;p&gt;The question may raise here, who will develop and maintain the Mediator then? I believe that an intermediary module like mediator shall be develop and maintained by anyone in the team, to keep it running and updated based on the new requirement.&lt;/p&gt;

&lt;p&gt;Also, having a mediator is simply another module to maintain in the codebase, so it shall be a pattern that's agreed and embraced by your developer team to ensure certain concern are considered:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Is it overengineering?&lt;/li&gt;
&lt;li&gt;Is it too much abstraction?&lt;/li&gt;
&lt;li&gt;Is the codebase going to be improved for a long term?&lt;/li&gt;
&lt;li&gt;Is the team fine with the mediator abstraction?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let me know what do you think of the pattern and your concern as well.&lt;/p&gt;

&lt;p&gt;Thanks a lot for reading my post, hope it helps and have an awesome day!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>architecture</category>
      <category>codequality</category>
    </item>
  </channel>
</rss>
