<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Duy Nguyen on Medium]]></title>
        <description><![CDATA[Stories by Duy Nguyen on Medium]]></description>
        <link>https://medium.com/@duydnguyen07?source=rss-3a9caaa116bb------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*RpIaaDoyJhAkad0YZ9mXyQ.png</url>
            <title>Stories by Duy Nguyen on Medium</title>
            <link>https://medium.com/@duydnguyen07?source=rss-3a9caaa116bb------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 11:37:43 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@duydnguyen07/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Can you tell me a story with your code?]]></title>
            <link>https://medium.com/@duydnguyen07/can-you-tell-me-a-story-with-your-code-2b35bd528819?source=rss-3a9caaa116bb------2</link>
            <guid isPermaLink="false">https://medium.com/p/2b35bd528819</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Duy Nguyen]]></dc:creator>
            <pubDate>Mon, 06 May 2024 07:51:44 GMT</pubDate>
            <atom:updated>2024-05-06T07:51:44.390Z</atom:updated>
            <content:encoded><![CDATA[<p>When I first started learning how to code, technical skills were the main subjects of education. Things like, how to use a for-loop, what is a while loop, how to do recursions, etc. Most of the time, I learned about what the concepts are and why they are used, but that’s about it.</p><blockquote>After years of programming, I realized how little my formal education actually taught me about writing code, period.</blockquote><p>The point of most coding programs is to solve a specific problem — getting things to work so that it serves a purpose. This is the basis for everyone who ever touches code. But little do we care about the human who will see that code, including the author him/herself. What exactly do I mean?</p><p>When we write code, our minds are fixated on the problem itself. We think to ourselves, <em>how can I get this to work? </em>or <em>how can I make it more efficient? </em>After a while, we manage to solve the problem and are happy with the solution, then we move on. A while later, some human, us included, will look at this code and go, <em>what the heck is going on here? Why is this written this way?</em></p><p>If you have ever had this experience, you know exactly the feeling. It’s frustrating, confusing, annoying, and just plain displeasure. After feeling this way numerous times, I hope you get the urge to do something about it, either to improve your skills or to tell others that they should improve theirs. Either way, I have a simple tip for you.</p><blockquote>Try to tell a story with your code</blockquote><p>Sounds simple right? It’s easier said than done because inherently, making something easy to read is significantly harder than getting something to work. One must put in a lot of effort in crafting and devising a flow path that the readers, code reviewer, or your future self, can follow.</p><h3>Here are some tangible tips</h3><h4>Give labels to your conditions</h4><p>In doing so, the reader does not need to figure out what the condition is for. In addition, they can highlight inconsistencies with the condition, in case the label does not match up with the logic.</p><pre>// Instead of doing<br>if(listOfAnimals.findIndex(animal =&gt; &#39;dog&#39; || &#39;cat&#39;) &gt; -1){ ... }<br><br>// Do<br>const isDogOrCatPresent = listOfAnimals.findIndex(animal =&gt; &#39;dog&#39; || &#39;cat&#39;) &gt; -1;<br>if(isDogOrCatPresent) {...}<br></pre><h4>Be expressive with your variable names</h4><p>Just like in the example above, a listOfAnimals could have been called array but that would not immediately tell the reader what is inside the array. It is easy to fall into the trap of blindly naming your variable because it feels laborious to write out a descriptive word. But these are just small investments that will pay off overtime as you do not need to think critically about what is contained inside a variable. This further reduces the cognitive load when writing.</p><p>One side note, if you notice that you need more than 10 words for your variable, it might be a sign that your data modeling is unnecessarily complex.</p><h4>Be under just enough pressure</h4><p>This is a less applicable tip but nonetheless important. When we are under pressure, there are often little room for little details. We often may think, who cares if this variable is called c instead of arrayOfItems and we proceed to just shorten the names so that we can get things to work faster. This is often a mental trap as we get more tired, we lose the ability to concentrate and remember what each variable mean. This further leads to slowdown and unnecessary bugs.</p><p>One thing to reduce stress is to take a 15 minutes break or so to cooldown and let your mind settle down before committing to more coding. Also, you will be surprised at how effective your mind is at solving problem while you are away from the computer. You may come up with a better solution while doing something else away from the problem.</p><p>In addition, speak to your stakeholders and express your current status. This may gain you more time and most importantly, <strong>empathy</strong>, which further lowers the stress, which will help with writing more readable code.</p><h4>Find the simplest solution for your problem</h4><p>A lot of time, we get caught up in the shiny new things or wanting to express our technical knowledge. This, often times, leads to overkilled solutions that is overly complex for you and the reader of your code. One simple solution for this is building in iteration. Start with the most straight forward solution that will get you to your goal. Once there, evaluate where things can be improved, and then make incremental changes.</p><p>When trying to tell a story, we don’t want to obscure the important information in convoluted unnecessary details. Same thing with coding, we don’t want to use an Ngrx state for a simple isolated component that has a few properties. Just use a plane old literal property! When there is a need for sharing state, then maybe we need to create a shared Service or maybe a Ngrx state.</p><h3>Conclusion</h3><p>In summary, writing code is very much about telling a story. It is clear and concise. It moves the reader along with descriptive contexts and clear flows. As a code author, it is only half the battle to solve a problem. It is equally as important to guide the reader through your solution, and make sure that they have the easiest time understanding what you are writing. I hope you find this useful and apply it to your work everyday. Happy storytelling!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2b35bd528819" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Will Your Angular App Survive Without Unit Tests?]]></title>
            <link>https://blog.stackademic.com/will-your-angular-app-survive-without-unit-tests-b05ba1150dfb?source=rss-3a9caaa116bb------2</link>
            <guid isPermaLink="false">https://medium.com/p/b05ba1150dfb</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[front-end-development]]></category>
            <dc:creator><![CDATA[Duy Nguyen]]></dc:creator>
            <pubDate>Tue, 03 Oct 2023 13:15:31 GMT</pubDate>
            <atom:updated>2023-12-04T01:12:15.435Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently, we deleted 95% of our unit test in our Angular codebase and I can tell you one thing, we have not regretted it yet.</p><h4>Seriously, is this even a good idea?</h4><p>If you have worked in any serious production-level codebase, you know that unit tests are always encouraged. And for my team, it was no different. We had a lead developer who was adamant about testing everything and while I thought this was too extreme, I agreed with it to some extent. However, this was becoming a tall order and it made me question whether it was still worth it at all.</p><blockquote><em>Just for reference, we used </em><strong><em>jest</em></strong><em> and </em><strong><em>nx cache</em></strong><em> to help speed things up.</em></blockquote><p>There were multiple reasons why I was getting very frustrated with unit testing in Angular. First of all, writing unit tests in Angular feels more like writing integration tests. Secondly, the whole codebase had so many tests that every build took forever. Thirdly, maintaining these tests was mundane.</p><p>My biggest frustration with writing Angular tests is how much overhead there is— especially when you just want to test a little small function. Firstly, you have to ensure that all imports were done correctly, and then you have to ensure that the mock data are properly injected so that the component will rerender when it gets updated. This is especially painful when the component uses observables as its data source. I often have to rearchitect my code so that I can write easier tests, e.g. moving code to a separate service or switching from using observable to plain old promises. All of this, just to test a function or a few use cases, feels like an overkill.</p><p>The next pain was the time it took to get a build to pass. Obviously, the more tests we have, the more time it will take. We have already taken steps to cache test results and play with jest settings to speed up the tests, but it still did not scale. As a result, the pipeline took somewhere between 30 minutes to upward of 3 hours just for the test alone. Of course, we can pay more money to increase the pipeline power, but this is not an option.</p><p>Lastly, maintaining Angular tests feels like meaningless work that has no additional value. One of the most frequent tasks that we have to do is to update the import to a component test. This happens because the component has received new services, pipes, other components, etc. and all we needed to do was to update these imports so that the test wouldn’t fail. This is meaningless. We added no new logic, yet we have to update the imports just to keep this test happy. Why do we need to spend time on this?</p><p>So obviously there are pains, but what can we do?</p><h4>Let’s delete them all!</h4><p>Yes, we did it. All tests were removed at first BUT we had to add back a few. It was clear from the beginning that the whole app did not require testing. But a few key areas were worth the time because they were business-critical!</p><h4>What does business critical mean?</h4><p>Typically, when we build applications, we are trying to solve real-world problems. Some are life-and-death related, e.g. conducting clinical trials, and some are just for quality of life, e.g. social media. We must understand how these apps are used in the real world and what negative impact they have on the user and the business if there is a bug.</p><p>When tests are removed, you no longer have a safety net to give you reassurance that the new code is working well with the existing code. As a result, the probability of releasing bugs into production increases.</p><blockquote>This is the key to survival with little unit testing!</blockquote><p>For example, there is an app that provides clinical trial information to the doctors at the hospital. It is not supposed to show these doctors whether the drug they are giving to the patient is a placebo tablet or a real drug. At the same time, it needs to provide which drug (placebo or real) the patient is taking to the trial conductor. In this case, the component that provides this information is business-critical because if this information is leaked due to a bug, the whole trial is invalidated and millions of dollars goes down the drain. Worst yet, if the patient took the wrong drug and had an adverse effect because the app showed the wrong information, it would be an even bigger disaster.</p><p>So the rule of thumb, at least how I see it, is that we can relate a feature to the monetary risk (or life risk if relevant) and figure out whether it can cause huge damage if there is a bug or not. Of course, this requires that we, as developers, understand the business we are in as well. This is often overlooked and I highly advocate for learning more about the business as developers.</p><h4>How does it feel now that we have deleted almost every test?</h4><p>Well, so far so good! The biggest issue that we have so far was that we needed to do more manual testing in the relevant areas of change, to ensure that we did not introduce regression bugs. Otherwise, we have been moving much faster than before. We still need to write some level of unit test but only in critical areas of course.</p><p>One thing that became very apparent was that Typescript typing became increasingly more important. We faced a few production issues where a data object was not valid and the whole page froze as a result. It was identified later that the types used had a conditional property that was supposed to be there but it wasn’t. Arguably, a unit test might have saved it, but what is for sure is that we cannot rely on a conditional property — and this is a typing issue more than a lack of unit test issue. But this is important to highlight.</p><blockquote>When you have less test, Typescript becomes your last line of defence. Make it more strict and adhere to the types as much as possible!</blockquote><h4>So to answer the question, will your Angular app survive without unit tests?</h4><p>Yes. If you pick and choose the right area of emphasis to test and be more strict with typing, then you can move much faster with the right amount of risk!</p><h4>Thank you!</h4><p>I hope you enjoy this article and please subscribe, it will help me a lot in my quest of writing more often. Cheers! =)</p><h3>Stackademic</h3><p><em>Thank you for reading until the end. Before you go:</em></p><ul><li><em>Please consider </em><strong><em>clapping</em></strong><em> and </em><strong><em>following</em></strong><em> the writer! 👏</em></li><li><em>Follow us on </em><a href="https://twitter.com/stackademichq"><strong><em>Twitter(X)</em></strong></a><em>, </em><a href="https://www.linkedin.com/company/stackademic"><strong><em>LinkedIn</em></strong></a><em>, and </em><a href="https://www.youtube.com/c/stackademic"><strong><em>YouTube</em></strong></a><strong><em>.</em></strong></li><li><em>Visit </em><a href="http://stackademic.com/"><strong><em>Stackademic.com</em></strong></a><em> to find out more about how we are democratizing free programming education around the world.</em></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b05ba1150dfb" width="1" height="1" alt=""><hr><p><a href="https://blog.stackademic.com/will-your-angular-app-survive-without-unit-tests-b05ba1150dfb">Will Your Angular App Survive Without Unit Tests?</a> was originally published in <a href="https://blog.stackademic.com/">Stackademic</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Use “git add” smarter with “--patch”]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://levelup.gitconnected.com/use-git-add-smarter-with-patch-cbe6bf5129e8?source=rss-3a9caaa116bb------2"><img src="https://cdn-images-1.medium.com/max/1920/1*dQUIQ2i-aN_sBD9b20vYLQ.jpeg" width="1920"></a></p><p class="medium-feed-snippet">Have you ever pushed a commit and later get a comment from a reviewer saying that you left a debugging message in your files? This used to&#x2026;</p><p class="medium-feed-link"><a href="https://levelup.gitconnected.com/use-git-add-smarter-with-patch-cbe6bf5129e8?source=rss-3a9caaa116bb------2">Continue reading on Level Up Coding »</a></p></div>]]></description>
            <link>https://levelup.gitconnected.com/use-git-add-smarter-with-patch-cbe6bf5129e8?source=rss-3a9caaa116bb------2</link>
            <guid isPermaLink="false">https://medium.com/p/cbe6bf5129e8</guid>
            <category><![CDATA[git-interactive-mode]]></category>
            <category><![CDATA[git]]></category>
            <category><![CDATA[how-to-use-github]]></category>
            <category><![CDATA[developer-workflow]]></category>
            <category><![CDATA[git-workflow]]></category>
            <dc:creator><![CDATA[Duy Nguyen]]></dc:creator>
            <pubDate>Tue, 05 May 2020 23:01:53 GMT</pubDate>
            <atom:updated>2022-07-30T09:16:54.081Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Unit testing private methods in Angular]]></title>
            <link>https://medium.com/@duydnguyen07/unit-testing-private-methods-in-angular-79ab5ff9cf27?source=rss-3a9caaa116bb------2</link>
            <guid isPermaLink="false">https://medium.com/p/79ab5ff9cf27</guid>
            <category><![CDATA[karma]]></category>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[private-methods]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Duy Nguyen]]></dc:creator>
            <pubDate>Sat, 25 Apr 2020 15:49:41 GMT</pubDate>
            <atom:updated>2024-09-25T13:04:53.958Z</atom:updated>
            <content:encoded><![CDATA[<p>Unit testing in Angular is challenging, especially when it comes to private method testing. There are times when it is so impossible that you don’t want to even test at all. In this article, I hope to provide some tips and tricks on how to do this more easily.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pkNtOYmUrylp7vqt2PGDRA.png" /></figure><p><strong>Disclaimer:</strong> This article only focuses on the “How” <em>instead</em> of the “Why” and the “Should/Should not” of unit testing private methods. Many people have raised concerns about the practice of testing private methods, but I am not here to debate that.</p><h4><strong>TLDR</strong>: Unit testing a private method can be done in a few ways:</h4><ol><li>Testing through another public method.</li><li>Converting the <strong>private</strong> method into a <strong>protected</strong> method, then create another class that extends and exposes this protected method in your unit test.</li><li>Re-exposing the method as <strong>public</strong> but with “<strong>_</strong>” prefix to signify that it is a private method. (I do not recommend this unless your team agrees with this convention)</li></ol><p>Inherently a private method is not accessible to the outside world, this is by design in the OOP world. However, in order to unit test just this method, one must be able to access it in an isolated fashion in order to perform the most isolated test possible — it seems like a contradiction between the concepts isn&#39;t it?</p><p>With that being said, there are ways to get around this restriction to achieve what we want. The first is the most obvious one.</p><h3>Testing through a public method</h3><p>Supposed you have the following class that has a <em>public</em> <em>init()</em> method that takes in two parameters and calls a few methods in itself. This method also calls the private method <em>setLanguage()</em> that you want to test. To test our <em>setLanguage() </em>method, we would run the test on <em>init</em>() while mocking/stubbing away the part that we don’t want to test in <em>init()</em> itself.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/74916c5d5ff698660383aa678acd85ce/href">https://medium.com/media/74916c5d5ff698660383aa678acd85ce/href</a></iframe><p>As you can see, I’m using <em>jasmine.createSpyObj()</em> here quite heavily to “weed” out the parts I do not care about. Since the <em>translationsMock</em> variable is an empty array, only the call to <em>setLanguage()</em> really has an effect. Thus, the test is effectively only testing <em>setLanguage()</em></p><blockquote>Also a side note, I’m purposely not using Angular’s <em>TestBed </em>because it is simply not necessary. There are many articles written about how simply cutting out Testbed, when you don’t need to render anything on the browser for your test, will increase test performance. Personally, I also find it much easier to write and maintain as well.</blockquote><p>This is a simple example where <em>init() </em>only<em> </em>call one other method, what if your method is much more complex and it is just too much effort to stub and mock everything? Well, there is another way.</p><h3>Testing using an extended class</h3><p>Since OOP provides us with another type of method, namely <strong>protected, </strong>we can leverage it to directly access the method that we want to test. This technique is great for maintaining encapsulation from public access but still allows us to run our unit test in an isolated manner.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/748e7331878c2621e39dd2b66288a1f2/href">https://medium.com/media/748e7331878c2621e39dd2b66288a1f2/href</a></iframe><p>Here, the <strong>private</strong> <em>setLanguage</em> is changed to <strong>protected</strong>, which enables us to create the <strong>I18nServiceExtended</strong> class in our spec file. This class exposes a method called setLanguage<em>Extended()</em>, which takes in the same parameters as the base <em>setLanguage() </em>method. It simply calls the <em>setLanguage() </em>without doing much else, thus allowing us to write tests “directly” against the base function — through calling <em>i18nServiceExtended.setLanguageExtended()</em>. Voila!</p><p>This technique has a really big advantage in that, it allows us to skip the extra stubbing and mocking for the <em>init()</em> method. In return, the tests are written specifically for the private method, and as a result, they are much simpler and direct tests, which improves maintainability and readability. Also, the extended class is not used anywhere else besides the spec file, so we can be sure that no one is suspiciously accessing our protected method.</p><p>While this is a good solution, there is still a small problem with it. It does not work if we don’t want to expose the <em>setLanguage() </em>method to the child classes. However, this is a really rare case that I seldom see in Angular apps. Mainly because Angular lifecycle components are not extended with traditional inheritance. Hence, inheritance only ever automatically works 100% with services, directives, and similar class-based objects. So there isn’t much value in using inheritance for components. This is good for us since we want to use it for testing a private method!</p><h3>Using “_” prefix to mark a private method instead</h3><p>The last technique is more or less a last resort kind of technique. I am personally not in favor of this technique unless your team is on board with it. Basically, instead of going through all the trouble of testing via another public method or creating a child class, we simply remove the <strong>private</strong> key and rename the method with an “<strong>_”</strong> (underscore) prefix. So in our example, <em>private setTemplate()</em> would simply become <em>_setTemplate(). </em>Then perform our test like we normally do with a public method.</p><p>This technique isn’t necessarily the worst thing in the world if your team follows and respects the pattern. This means that a method name starting with “_” should never be used outside of its class. Though this is achievable on an organization level, it definitely does not beat the compiler guard itself. When a new person is onboarded, they may not know/care enough to stop themselves from doing this. For that reason, I would advise against this. Of course, you may still do it if your team is disciplined enough.</p><p>There you have. Happy testing!</p><h3>Support my work</h3><p>I write articles for fun and make a small side income. I would appreciate any support for my work via a <a href="https://buymeacoffee.com/duydnguyen07">form of donation</a> or simply following me to help me contribute to the community more in the future! Cheers =)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=79ab5ff9cf27" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>