<?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 Juan Manuel Villegas on Medium]]></title>
        <description><![CDATA[Stories by Juan Manuel Villegas on Medium]]></description>
        <link>https://medium.com/@villegasjuan?source=rss-2480a8b6ff76------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*YogwTuLTAOlOhH0BIyLWDA.jpeg</url>
            <title>Stories by Juan Manuel Villegas on Medium</title>
            <link>https://medium.com/@villegasjuan?source=rss-2480a8b6ff76------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 12:16:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@villegasjuan/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[Implementing a scalable Search service (I)]]></title>
            <link>https://villegasjuan.medium.com/how-do-we-search-smylen-com-i-e8ae791ce87f?source=rss-2480a8b6ff76------2</link>
            <guid isPermaLink="false">https://medium.com/p/e8ae791ce87f</guid>
            <category><![CDATA[cache]]></category>
            <category><![CDATA[vue]]></category>
            <category><![CDATA[search]]></category>
            <category><![CDATA[inde]]></category>
            <dc:creator><![CDATA[Juan Manuel Villegas]]></dc:creator>
            <pubDate>Mon, 28 Jun 2021 14:01:07 GMT</pubDate>
            <atom:updated>2021-07-04T17:49:43.869Z</atom:updated>
            <content:encoded><![CDATA[<p>At <a href="https://smylen.com/">Smylen</a> we offer Patients the possibility to find a dentist in their local area from a curated database of the best dentists. At the heart of the product is our search interface. It allows patients to specify their criteria, such as the treatment needed and their preferred location, for which Smylen will generate a list of dentists. The patient can then select one of the results and book their treatment online.</p><p>In this series of articles I’ll go over our Search module, and explain the techniques and tools we use to produce fast results even for the most complex queries our interface can produce.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h4X6GavF5DcMjIRAjuvUig.png" /></figure><h4>Preliminary Comments</h4><p>Most of the problems the majority of developers solve in their daily jobs could be classified, when considered against the set of all possible problems to be solved in Software Development, as simple or mundane. Filtering a table, creating a predictive text input, making a UI a bit faster/more responsive. This is mostly because all these basic problems have already been solved and the tools to solve them are available to all of us just a command line instruction away (hello <em>npm install x</em>).</p><p>These problems and their solutions, however, become interesting when they are faced with two issues:</p><ul><li><strong>interoperability</strong>, which is combining them to produce more complex solutions,</li><li><strong>scale</strong>, which is testing the solution when the application faces an increasingly high demand.</li></ul><p>In this article we focus on <strong>Scale.</strong></p><p>Our search algorithm was extremely basic: we collected a few parameters from a UI and sent those in a GET request to our API. The API validated the request and forwarded it to a Service that handled the actual Search. The Service would filter and sort our MySQL table, decorate the results by collecting data from different tables and services, and produce a set of results for the User. There was nothing spectacular about this and the implementation wouldn’t impress anyone. However, it worked surprisingly well: our database didn’t have a substantial amount of records for a long time, and thus the Service was able to produce a set of results in under a second without relying in any complicated caching or indexing scheme.</p><p>However, Search began to degrade slowly as our database grew and the complexity of our business rules increased. We reached to more Doctors, we offered more pricing plans, we integrated with Booking systems, and thus the time our Search needed in order to produce results quickly bounced outside the limits established by the acceptance criteria. In particular, searches that spanned a big area or that included several filters were painfully slow: worst case scenarios reported execution times &gt; 30s, which is obviously not acceptable and falls more into the category of errors.</p><h3>Characteristics of a Search Service</h3><p>A Search service should usually expose the following characteristics:</p><ul><li><strong>Speed</strong>: exactly how fast depends on your particular application. I’d recommend looking into a competitor’s application to see how well they are doing. If you can, choose one you admire or have some respect to, or one that has already proved their value. In our case, and despite we don’t operate in the same market, we like to look at Airbnb for their search service. Airbnb performs searches in under 300ms, and the number of candidates they analyze are probably the same as ours, so it is realistic to aim for an execution speed of ~300–500ms. If your Application focuses more on searching log archives, that number will be more likely in the order of seconds, or maybe minutes.</li><li><strong>Deterministic</strong>: <em>search(x1, y1)</em> must return the same as <em>search(x1, y1)</em>. This is important because your Users will consider this a given. For example, if they are browsing your Map and want to share the current search with a friend, they may just copy the URL and share the link. Looking at the backend, if two same searches return different results, it would mean that at some point the implementation is taking unhealthy shortcuts.</li><li><strong>Efficiency</strong>: similar to speed, but different. Efficiency must be ensured for all Searches, no matter what the inputs are. It’s not acceptable if a User gets 200ms results while panning around New York, but if he then decides to zoom out to include New Jersey, he suddenly gets results in the order of seconds. And if he continues to zoom out, the site suddenly stops being responsive. Your average measurements may still be good and you may be tempted to hold to that, however if the requirements include supporting searches in the state or country level, then your averages won’t take you long.</li><li><strong>Best effort</strong>: when you move your algorithm from calculating search results on the spot to relying on caches and indexes you have to sacrifice one thing, which is the freshness of the data you are coming up with. And that’s ok: it’s a sacrifice you must be willing to make in order to search fast. For example, if a new Dentist registers in Smylen and completes his profile, he may not show up in Search results immediately even though he may be a perfectly valid candidate. He may not be there even an hour after the registration. But he will come up right after our indexer finishes it’s next scheduled Job. This may sound a bit frustrating, but that’s how all search services that operate at scale work: look at Google for an example, if you created a new website right now, it will probably take a few days to show up in the results. DNS services are another example: if you make a change to your records, you need to wait a few hours for them to propagate to the hundreds of intermediate caches that operate around the world. Having said that, we will look at some mechanisms for cache invalidation and reindexing that will tend to alleviate this issue.</li></ul><h3>But first, let’s speak UI</h3><p><em>The main focus of this series is the backend implementation. However, I feel like a good introduction to our Search UI is important so that the reader can have some additional context. Our UI is also a fine combination of different actors, and thus it is interesting to study as well.</em></p><p>For Smylen, our Search API design was strongly guided by the UI in our website, as it is mainly from there where most of the traffic is coming from. There are a few secondary use cases, but they are mostly adaptations of the main search UI.</p><p><strong>The Inputs: filtering and sorting</strong></p><p>The search works around three main inputs: the location, the treatment and an optional treatment variation. It also supports a set of secondary (“advanced”) filters and several sorting methods.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lqN9_93J7Hz3i9KOcYjo0g.png" /></figure><p>The area marked with a <strong>1</strong> shows the main filters. The User enters a ZIP code and the desired Treatment (which depending on the selection may open up a third input to choose the desired treatment variant). Clicking “Update Search” triggers a new Search, as the main filters are enough to generate the most basic of searches.</p><p>The area marked with a <strong>2</strong> shows the sorting methods that are currently supported. Clicking one of these updates the Search again. We will see later how these sorting strategies were implemented.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*HxdsjkbA3_Xqo-bbKv8DcA.png" /></figure><p>The <strong>“Advanced Search”</strong> button opens up a slideout with additional filters. Filters have different types, some of them allow a range of values (budget, distance), others are booleans (pay with affirm support) and others provide lists of elements that should be supported by the search result (for example, selecting several amenities produces a list of amenity ids that the search results must be associated with).</p><p>All these filters and sort types are stored in a global Object managed through a data repository. For our particular case, we used Vuex because we run our frontend in Nuxt. Vuex, and Vue in general, are fantastic as they offer a perfect balance of simplicity and power. Whenever a filter is changed, a Vue component commits it’s value to the repository. Because Vuex is reactive, if the filter is displayed somewhere else it is automatically updated. Also, because we dump the repository data to a Cookie, if the User refreshes the page they still see the same filters selected. Having our filters in the repository also makes it easy to start new searches: the search handler doesn’t have to look up the values one by one, but rather just asks the repository for the data. Finally, we also use the repository to create a URL representation of the search:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4yuBySdwHzt7MR9M_-Z7YA.png" /></figure><p>Whenever a filter changes, a new URL is generated and pushed to the browser’s history. This allows the User to navigate back and forth as if he was browsing regular pages. Also, if the User wanted to share the Search with a friend, he could just copy and paste the URL or generate a QR code of it. All this may sound rather standard, but it’s very important to provide the User with the experience they expect, as otherwise it may lead to frustration: browsers have gotten so popular that certain features are just expected to be there, such as refreshing a page and staying in the same place, navigating through history or even coming back to a page at a later time and find the selected preferences in place.</p><blockquote>It’s very important to provide the User with the experience they expect, as otherwise it may lead to frustration: browsers have gotten so popular that certain features are just expected to be there, such as refreshing a page and staying in the same place, navigating through history or even coming back to a page at a later time and find the selected preferences in place.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qgQ1QFsY5fPcs6Sie1Lb5g.gif" /></figure><h4>The Map</h4><p>Another important piece of UI is the Map. It was designed as a standalone component, so that it isn’t tightly coupled to the main Search. This means the map shows results here, but is also used in other areas of the application.</p><p>The Map component implements a Google Map, and provides a Facade to interface with it in a more generic way: that is, the Search component doesn’t know about the Google Maps API, but instead works with higher level methods and events. This small but important design decision allows the Map to concentrate all changes related to the Maps API updates: if we need to update the Maps API, we need only to look at the Map component for possible issues. It also hides away all the complexity related to drawing pins, displaying info windows, moving the map around, etc: you don’t want that to leak to higher level modules.</p><p>So as a summary, the Map has an interface that:</p><ul><li>receives a set of search results as input, and has the responsibility of displaying them in a Google Map. There is a lot of code for this, for example for implementing the Pins (if you noticed, they aren’t regular pins but a bunch of CSS positioning magic that allows a few moving parts to stay together) and redrawing the results when they come back from the API.</li><li>exposes a set of events that the Search can subscribe to in order to update the results. We mentioned that one of the main search inputs is the location. Whenever the User pans around the map or zooms in/out, the Map reports an event that includes the updated location (in Maps terminology, it’s <em>bounding box</em>). The search reacts to this event by generating a new API request to get updated results.</li></ul><h4>Search Results</h4><p>Finally we have the Search results, marked as 3 and 4 in the screenshot above. Here is the main search result again:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*adgakdZNUb72DTelFZGOLA.png" /></figure><p>Results are displayed both on the left hand side of the Search UI and also as pins on the map. When the User moves the mouse over the result, the map is moved to indicate where the Pin is located. This is useful because not all results may fit in the displayed map area. Pins are clickable, and clicking on them reveals a small overlay with a summary of the service. These overlays were implemented using <a href="https://developers.google.com/maps/documentation/javascript/infowindows">InfoWindows</a>. Pins are implemented using <a href="https://developers.google.com/maps/documentation/javascript/custom-markers">Custom Markers</a>, and a lot of CSS work was done to make them pretty unique.</p><p>The search results component displays the profile image, the price, the location, the treatment, the office’s reviews, a summary of supported amenities, as well as some metadata such as the savings percentage when compared to other services in the same area. And this is only part of it: we also calculate prices for more advanced use cases, such as when Users indicate that they have dental insurance, which requires additional processing.</p><p>There is a lot of information contained in that box, and this is one of the reasons why searching is so difficult. The information that we usually need to produce results isn’t available in one MySQL table, but instead it is distributed among several datasources, and these may include tables, databases and even external services. For example, the available slots that are displayed on the right hand side of the component are coming from a Booking service that Smylen operates but that is external to the main API. With all this information distributed among different sources, it is evident that generating results on the fly would not meet our speed requirements in the long run.</p><h4>Mobile</h4><p>Given that a majority of our users come from Mobile platforms we spend a lot of time working on our mobile UIs. In general, the vast majority of Smylen can be used perfectly well from a Mobile device. Search is no exception, and all the components find their spot when using a small screen to browse the site.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kIlBcvSfEjOoiadJa028CQ.png" /></figure><p>The source of truth for determining the device type is currently just the width of the page. <strong>Basing all decisions using only this parameter is risky: you aren’t considering the actual features of the device to make decisions. However, I believe that given the size of our team and our priorities, this achieves a good balance of usability for most users, while at the same time allows us to remain responsive to changes.</strong></p><p>As for implementation, we used a combination of Vue templates and components that render one way or the other depending on the device: in general, the components are wrapped around a parent component that is assigned the responsibility of analyzing the screen size and coming up with the best component for it. A sort of responsive factory, if you may.</p><p>When in mobile, the search will push the map to the background and feature only a list of results. To show the map, the user clicks on a link that takes him back and forth between map and list. The map works exactly the same as in desktop. The availability calendar is compacted at the top, displaying only one date at the time. The filters are moved to the Header, where they replace the Smylen logo and the User menu. Advanced Search becomes a modal because slideouts in mobile aren’t a very good idea.</p><p>So now that we went through how Search looks like in Smylen we can start looking into the actual problems that we found when our database grew past a certain threshold, and the steps we took to produce a Search service that responds in a few hundred milliseconds, even for the most complex of queries.</p><p>Check the next part of this series! (AVAILABLE SOON).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e8ae791ce87f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Pragmatic Approach to Committing Code]]></title>
            <link>https://levelup.gitconnected.com/the-pragmatic-approach-to-committing-code-cb77db2dbb8c?source=rss-2480a8b6ff76------2</link>
            <guid isPermaLink="false">https://medium.com/p/cb77db2dbb8c</guid>
            <category><![CDATA[github]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Juan Manuel Villegas]]></dc:creator>
            <pubDate>Thu, 10 Dec 2020 00:10:45 GMT</pubDate>
            <atom:updated>2020-12-10T14:30:29.465Z</atom:updated>
            <content:encoded><![CDATA[<p>One of the best books about developing quality software is probably <a href="https://en.wikipedia.org/wiki/The_Pragmatic_Programmer"><em>The Pragmatic Programmer</em></a>, written by A. Hunt and D. Thomas. It was originally published in 1999, but the same principles and recommendations are still valid (and even more important!) today. In the book, the author focuses on pragmatism applied to Software Development, which means basically being conscious about your whole development process with the ultimate goal of producing the highest quality code possible.</p><p>This article describes some of the techniques a developer should be applying alongside writing code in order to ensure the highest possible output and the most efficient collaboration with other teammates.</p><h3>Assumptions</h3><p>For simplicity we will assume a few things about your process. <strong>If you are not applying more than one or two of these, I’d suggest to revise your development process as soon as possible.</strong></p><ul><li>Your codebase lives in a repository and uses configuration files (env, json, whatever) to set up environment-specific settings. A modern repository would most likely be <em>git</em>-based, but some companies might still be doing <em>svn</em>.</li><li>You are developing in a so called <em>feature branch</em>, and you have at least a <em>master</em> branch holding your production code and a <em>staging</em> branch holding your pre-release code (actual names may differ).</li><li>You follow the <a href="https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow">Git Workflow</a> (or something similar).</li><li>You have a test suite covering at least partially your source code, and either a CI/CD service automatically generating the builds or alternatively a manual way of triggering it.</li><li>You use a modern IDE (such as WebStorm or VSCode), or a properly configured Vim/Atom/etc. For my examples I’m going to use <a href="https://www.jetbrains.com/webstorm/">WebStorm</a>.</li></ul><h3>Multiple Commits</h3><p>Sometimes you will have to work on big features that span multiple areas of the project, but you won’t be able to separate your work into multiple pull-requests (PRs) because they wouldn’t make much sense on their own. Even when this happens, it is still important that you separate related functionality into commits, with concise, clear and helpful commit messages. This will later on be of great help for your reviewer to get a general idea of what you meant to do with each of the commits, and even allow her to review them one by one.</p><p>You can use this approach even if you have worked in your whole feature at once: just create different commits at the end, by carefully inspecting each of the files and grouping them accordingly. This will also help you to go back into your changes and review them once more, potentially capturing syntax errors, code-style inconsistencies, or even spotting potential refactors.</p><h3>Code Styles</h3><p>There will also be an article specifically on this topic and why this is so important (and it’s certainly highlighted in the book I mentioned above), however, <strong>remember to strictly follow your team’s code styles</strong> (assuming you have agreed on those with your team, otherwise it would be important to do so as soon as possible). <br>With most modern tools you can specify the rules in a configuration file, and then have your IDE or Editor check them (and even fix most errors) automatically (hint: WebStorm, for instance, allows you to select any piece of code and apply the code styles by pressing Cmd + Option + L). <strong>So there is absolutely no reason not to use code styles in any modern project</strong>.</p><p>A side note must be made here: sometimes you will find yourself editing a file that wasn’t created following any code styles: incorrect indents, missing punctuation, etc. It’s not a good idea to fix the whole file on the same PR/Commit you are using to work on a feature. This is usually distracting, confusing for the reviewer, and leads to a PR bloated with files. Instead, switch to your staging branch, fix the styles in the file and commit that as an independent PR. In relation to this, remember that some IDEs automatically fix the code-styles when you open a file, so in general it’s a good idea to disable that feature if you are working in a team.</p><h3>Unit Tests</h3><p>The first thing you have to do right after your feature has been written is to go and write the unit tests that will exercise the code to ensure everything is working as expected (This is of course assuming you haven’t followed a test-driven development approach, in which case your tests would have been written before the feature).</p><p>Notice I said <em>feature has been written</em> instead of <em>feature has been completed</em>. This is important because a feature cannot be considered completed until it’s been fully integrated, which means a minimum set of unit tests has been added.</p><p>How much to test and what tests to write is a topic for yet another article, but the key takeaways here are:</p><ul><li>Your coverage level must be at least &gt; 90% for statements, 100% for functions and around &gt; 85% for branches. Anything below that and your reviewer can (and should) reject your pull request.</li><li>Your feature is not x% completed if you haven’t written any tests. <strong>It’s impossible to know how much of a feature is completed until the unit tests have been written, executed and passed.</strong></li><li>Be mindful about your tests: don’t write them just to fulfill the coverage requirements of your team or just to show that you added tests to your feature. Make it so that any errors or inconsistencies are revealed and fixed: <strong>remember that a successful test is not that one which passes correctly, but instead it is that which reveals at least one error.</strong> Testing is also a great opportunity to spot possible areas where a refactor would make the general structure of a solution better.</li></ul><blockquote>A successful test is the one that reveals at least one error, not the one that passes correctly.</blockquote><h3>Run your Tests (locally)</h3><p>Your new feature was probably not developed in a vacuum, and most certainly involves communication with already existing modules. Maybe it wasn’t a new feature but just some updates to an existing one. This means that not only the new tests are important, but also all those that already exist in the suite. If your implementation negatively affected another part of the project, and assuming you have a healthy test suite in place, the errors will be revealed by running your whole test suite locally before sealing your pull request.</p><p>Notice this step is about <strong>locally</strong> running the tests. Don’t commit and push and have your CI server do it for you. This will introduce unnecessary commits with bug fixes that could have been avoided at first place. This is assuming you can run the tests locally, and this will depend on the size of the project and nature of the environment (e.g.: some Java/.NET builds can take significant time and resources to execute a whole test suite, meanwhile a medium size Node project can run a whole unit-tests suite in 5 minutes).</p><p>Important note: Don’t ask your reviewer to look at your code before ensuring that the test suite has passed. If a reviewer opens up a pull-request that has one or more failing marks, she will immediately stop looking at it and bounce it back to you for adjustments, resulting in time wasted for both.</p><h3>Review your Changes</h3><p>Reviewing your changes one by one may seem like a lot of extra work, specially after you have verified that everything works through unit tests and manual testing. However, at this particular step we are interested in something unit tests won’t catch, because they can only inspect the correctness of semantics and syntax, but not the actual “style” of the code.</p><p>Giving you a few extra minutes yo go through your changes one by one allows you to detect areas where the solution can be further improved. This is specially important if the pull-request is quite large.</p><p>Using WebStorm with Git, this is particularly easy:</p><ul><li>Hit Cmd + 9 to open up the <strong>Git Panel</strong>, and click on the <strong>Local Changes</strong> tab.</li><li>This tab lists all the files that have changes in the current change list.</li><li>Select the first file and press Cmd + D. This triggers the Compare screen.</li><li>Toggle the side by side viewer (I believe it gives a much better panorama over the unified viewer).</li><li>Review your current change, and press F7 to go to the next change. Press Shift + Cmd + ] to jump to the next file, or Shift + Cmd + [ to go back to the previous file.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nq_IQU2n4q1ZfszCwCqNYA.png" /><figcaption>Side by side changes viewer in WebStorm</figcaption></figure><h4>Refactor</h4><p>While reviewing your changes, you may find yourself spotting multiple areas of improvement in simple but still important things: variable and method names, chunks of code that would be better in their own function, etc. Refactoring these is extremely easy, and by relying on your IDE you can do it without the fear of breaking anything in the process.</p><p>For example, to rename a variable in WebStorm, just place the cursor over the variable and press Shift + F6. Enter the new name and press Enter. This will scan your variable’s scope, replacing any usages with your new name. <strong>Relying on your editor for this is a far superior and safer technique than using find &amp; replace.</strong></p><p>The good thing is that if for any reason you use <em>find &amp; replace</em> and make a mistake, if your test coverage is 100%, your tests will catch the error.</p><h3>Push</h3><p><strong>A developer that has completed a feature and also followed the aforementioned techniques can now say that the Feature is indeed x% completed.</strong> Assuming the code was written following the specification, and that the tests were written consciously to test that specification, there is a lot less uncertainty in terms of what percentage of the task is done and what’s missing. On the contrary, if a developer claims a feature is 90% completed but hasn’t written any tests yet, then she is estimating a 10% effort to fix all the possible issues that the new feature could have introduced in the whole system. For any medium/large project, this is at least a bit unrealistic (in general, it is said that at least 50% of your time is used fixing bugs and testing…).</p><p>Pushing your commits consist of uploading the branch to your remote repository:</p><pre>git push --set-upstream origin &lt;your-branch-name&gt;</pre><p>That will create a new branch in your remote and upload your commits. If you are using Github or Gitlab (or any other GUI), you can go ahead and create a Pull Request through their user interface. These interfaces are really helpful, as they extend Git with a set of collaboration features that allow multiple developers to easily contribute on any piece of code that gets committed.</p><p>The <strong>Pull Request (PR)</strong>is basically an intention to merge your changes into your target branch, which should usually be your <em>staging</em> branch first. Your PR should include a medium-length description of what has been done: mention briefly what you did, what decisions were taken and why, what problems were fixed and what problems still need to be addressed, etc. Also focus on technical aspects: mention any required configuration items and how to create them, specify the steps that should be followed in order to test the feature and, for features that include a UI, provide a few screenshots so that the reviewer knows what to expect. Be mindful about this description: don’t just copy-paste the commit messages, basically because the reviewer can already see them in the history, but instead add relevant information.</p><pre><strong>feature/your-feature =&gt; staging =&gt; production</strong><br><em>-&gt; the very minimum stages your changes should go through. Some companies will have multiple staging branches (alpha, beta, release x, etc).</em></pre><p>When creating a PR, you need to select one or more reviewers, which are other developers in the team, to review the changes. Depending on the company’s approach, the reviewer’s role may be more or less important. Some companies do this very lightly, while others take it very seriously and the review of a single PR can span multiple days and even weeks.</p><p>Whatever your company’s policy is, the end goal here is to get another set of eyes on your changes. Getting another developer to look at your code provides an invaluable insight because they usually attack the changes from a different perspective: this will reveal things that you probably ignored, provide alternative solutions to problems, uncover bugs that you may have skipped because of the well known <strong>developer bias</strong>, and most importantly, <strong>gives you an opportunity to learn from the suggestions others will be giving you</strong>.<strong> </strong>Because of all this, the comments from the reviewer shouldn’t be interpreted as a critic: take your time to understand what has been corrected and why, open a discussion if after reasoning about the issue you still think your solution is correct, and reply on every comment acknowledging the issue and explaining how it was resolved.</p><p>After each review from the reviewer, you potentially face a new iteration of the cycle: if bugs are discovered, you will have to go and fix them, update the tests, review the code again, and commit the changes to the PR, requesting a new review and indicating what has been done to address the issues.</p><h3>Merging</h3><p>Once your reviewer and yourself are happy with the <em>pull-request</em>, the time has come to merge your feature branch into <em>staging</em>. Deploying to <em>staging </em>gives an opportunity to test your changes in an environment that is very similar to your production environment: a properly set up staging environment would use the same services, databases and APIs production does, and is accessible to stakeholders (through an URL, if it’s a web app, a demo APK, if it’s a mobile app, an installer if it’s a Desktop application, etc). This is the point where non technical users will jump into the testing group and provide their feedback on the feature by testing the working application. Most of the time, you can expect some changes to be asked for, mostly regarding UI/UX, wording, etc.</p><p><strong>Once your feature has landed on <em>staging</em> it is considered done</strong>. It now exists as part of a bigger change-set, and although you may still encounter integration issues, the bulk of the work has been done. You can expect someone to report an issue at a later time, but if the proper steps have been followed, your feature should be looking pretty solid by now, protected by a representative suite of tests and having passed thorough inspections by yourself, your reviewer(s) and your stakeholders. You can move on to your next task now!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cb77db2dbb8c" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/the-pragmatic-approach-to-committing-code-cb77db2dbb8c">The Pragmatic Approach to Committing Code</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How we created a real-time Leaderboard for a million Users]]></title>
            <link>https://levelup.gitconnected.com/how-we-created-a-real-time-leaderboard-for-a-million-users-555aaa3ccf7b?source=rss-2480a8b6ff76------2</link>
            <guid isPermaLink="false">https://medium.com/p/555aaa3ccf7b</guid>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[redis]]></category>
            <category><![CDATA[cache]]></category>
            <dc:creator><![CDATA[Juan Manuel Villegas]]></dc:creator>
            <pubDate>Tue, 31 Mar 2020 01:40:17 GMT</pubDate>
            <atom:updated>2020-03-31T23:40:48.931Z</atom:updated>
            <content:encoded><![CDATA[<p>A few years ago, while working for Rocket Languages, I was assigned the task of rewriting the Leaderboard and Points System of the application we were working on as part of a migration we were making from our legacy codebase to a modern PHP application powered by Laravel. What appeared to be a relatively simple assignment ended up revealing a series of challenges that had to be overcome to meet the functional and non-functional requirements of our Leaderboard.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZQl56KvSACL2zOUSFWW8hQ.jpeg" /></figure><p>A Leaderboard is a pretty straightforward system: users earn points for doing <em>things</em> in the Application, and these points are then added up and displayed in a Leaderboard. Different grades of complexity can be added depending on the requirements. For example, the underlying representation of the data will be different if there is a need to keep track of the individual actions the users perform, or if we are only interested in the total points they’ve earned. Complexity also grows depending on how the data is to be retrieved: are we only interested in the top 10 users, or do we want to be able to tell, for each user in the Application, what his position is in the Leaderboard? Are we doing this for a relatively small set of users, or are we thinking about scaling it to the millions?</p><h3>What we had</h3><p>Broadly speaking, the <a href="https://www.rocketlanguages.com/">Rocket Languages</a> application was an online language learning service. There were several courses, each of which comprised dozens of chapters, and for each chapter there was a set of exercises the students had to solve. The exercises were different in nature and interacting with them generated points. Also solving an exercise would give the students points, along with marking some stuff as read, answering questions in the forum, etc. In general, using the application generated a stream of points that were collected in the student’s profile and displayed in different areas: as a badge next to their name while using the application, in a dedicated Leaderboard area and also in the footer of every message students posted in the forum.</p><p>The Leaderboard area was especially interesting because it offered the possibility of filtering the points in different time ranges (daily, weekly, monthly), and also, of looking up a specific user, as well as showing the logged-in user position in the Leaderboard at all times.</p><p>The legacy implementation was very rudimentary and solved the problem by using intermediate MySQL tables that were updated by cron jobs every once in a while, a combination that is as error prone as it sounds, not to mention it also creates a tight coupling between the database and the application. Because the cron job was needed to maintain the Leaderboard up to date, the data displayed in the application wasn’t in real-time, but rather had a message indicating that it was only updated “regularly”.</p><h3>What we were asked to do</h3><p>The requirements were very simple. The legacy Leaderboard had to be adapted to fit into the new codebase, which was being built using Laravel. Functionality would remain the same, and no matter what schema changes we came up with, the old data had to be imported into the new database.</p><p>To spice it up a bit, and following what we had seen in similar applications in the industry, we decided to challenge ourselves by adding additional requirements that would bring more value into the Leaderboard and also make the whole task a bit more challenging for us. Thus, we decided that the new Leaderboard had to <strong>show data in real-time</strong>, providing immediate feedback to Users while they play with the application. Real-time data would be available regardless of how large the number of people playing with it and for all of the different classifications that we had (think of time-based Leaderboards and category-based Leaderboards).</p><h3>The problem</h3><p>Turns out that a Leaderboard, at its core, is extremely simple to represent in terms of data structures. There will be a <strong>Points</strong> table which is going to look like this:</p><pre>&lt;Int&gt; user_id, &lt;Int&gt; amount, &lt;Timestamp&gt; created_at</pre><p>Every time a User earns points for doing something, we would just add an entry to this table. Then, if we wanted to get the total points for a user for the current month, we would just execute something like this:</p><pre>select sum(amount) from Points where user_id = ? and created_at &gt;= &quot;2020-03-01&quot;</pre><p><em>(The above is just pseudo code sql style, I haven’t really tested it.)</em></p><p>Now, it gets interesting when you extrapolate this solution to a table with 100 million rows that gets 100 thousand new ones every day. It turns out, that SQL-type databases are great for maintaining consistency, storing information in well-designed schemas, generating reports and querying data. But, it’s not performant when it needs to deal with huge sets of information. The solution exposed above will quickly become useless as more and more users feed the tables with information, no matter how fast the server is or what indexes are added to the Schema.</p><p>So, the real challenges when developing a Leaderboard system are:</p><ul><li>Provide <strong>reports of the Leaderboard for a scalable dataset</strong> (speaking about millions of users).</li><li>Support for users constantly earning points, generating thousands of new points per day (in our case, it was around 150K). That is, <strong>writes to the data structure have to be very performant</strong>.</li><li>Reports have to be <strong>available in real-time. That is, reads from the data structure have to be very performant.</strong></li></ul><p>While working on this, we set up some additional challenges for ourselves:</p><ul><li>Possibility of knowing the position and score of any given user in the Leaderboard at any time.</li><li>Be able to fetch any range of the Leaderboard, not just the top X entries.</li><li>Fetching the leaderboard should take less than... a second? It’s hard to set a time based metric here, but the idea is that loading a Leaderboard should be as fast as loading an entry from a small table in a database.</li></ul><h3>The solution</h3><p>The following is going to be a walkthrough of the solution we implemented for our specific use case. However, I’ll leave out language specific stuff and make the examples as generic as possible. We used Redis and MySQL, but any Caching system in combination with any persistent storage would work, given that they provide the appropriate data structures.</p><h4>Persistent Storage</h4><p>To begin with, we are going to forget for a moment about the requirement to display the data in real-time, and focus in what is going to be our data storage. In general, this is the recommended approach for solving problems: first you write the simplest solution you can think of, and then you iterate on it to improve it.</p><p>The persistent storage is where we’ll store all the points the users earn. As this is going to be our data, and because data is valuable, we need to make sure we store it in a database that makes it easy to represent it, stores it in a consistent way, and has a proven mechanism for reads and writes. Either a SQL or a NoSQL database system would do it, but personally I have far more experience with SQL, so that’s what we chose for our solution.</p><p>The Schema for the table that will hold all the points the users earn looks like the following:</p><pre>CREATE TABLE `user_points` (<br> `id` int(10) unsigned NOT NULL,<br> `user_id` int(11) NOT NULL,<br> `amount` int(11) NOT NULL,<br> `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP<br>) ENGINE=MyISAM DEFAULT CHARSET=utf8;</pre><p>Depending on the characteristics of the project, the Schema above may also incorporate some indexes (for example on the <em>user_id</em> column and, maybe, another one on the <em>created_at</em> column). In our case, we had additional columns representing the source of each of the points.</p><p>Once we have the Points table, we can start feeding it by inserting data every time the users do something in the Application.</p><h3>Fast reads</h3><p>So now that we have our Points table and our users are feeding it, the question is how do we extract information from it and present it to our users? To solve this, we are going to need an additional data structure that will support the Points table. This data structure will contain a summary of the information in the Points table and will be organized in a different way. Our Points table will still be the source of truth, but the new data structure will be used to access the data.</p><h4>About Redis and its Sorted Sets and Hashes</h4><p>Redis is an in-memory database software that works primarily in the RAM memory of a the computer, making it really fast to read and write. It also provides the implementation for some very useful data structures. Some of these are pretty well known, such as Sets, Hashes and Maps. The simplest data structure in Redis is the key-value store, where the key and the value are of type String.</p><p>Their <a href="https://redis.io/">website</a> contains pretty solid documentation and provides an online CLI to interact with a cloud-based Redis server, so it’s really easy to immediately start practicing with the different commands while going through the documentation.</p><p>One of the least known data structures that Redis provides is the<strong> </strong><a href="http://Data types – Redis"><strong>Sorted Set</strong></a><strong>.</strong> This, in fact, is an incredibly useful data structure, but also one that wouldn’t be straightforward to implement by yourself. The Sorted Set is basically a mapping between a Key of type String and a Score of type Number. The Key is always unique, and the score can be any number. Most of the operations supported by the Sorted Set take logarithmic time O(log(n)), which means that its execution time even for huge data sets is going to be very low.</p><p>For our Leaderboard implementation, the Sorted Set is going to store the total Points each of the Users have. Because the user_id is a unique field, it makes a great candidate in becoming the Key of the sorted set. The number of points will be the Score.</p><p>As mentioned before, Hashes are also implemented by Redis. Hashes are excellent structures for accessing information. They store data using keys, which have an associated value. If you know the key, accessing the value stored in that key takes constant time O(1). Checking if a key exists will also take O(1) time, and so does inserting a new key into the Hash. The value associated to the key can be anything, although it is better to keep the amount of data stored at the minimum. Remember, these structures will all live in the RAM of the computer, which is a very limited resource.</p><p>So, now that we have introduced these two data structures, how are we going to use it to create our Leaderboard? Recall from previous paragraphs that fetching the information from MySQL is not possible as it is not efficient and, most importantly, not scalable. However, <strong>it is possible to do it just one time</strong>.</p><p>The general idea will be as follows:</p><ul><li>Extract all the information required to build the Leaderboard from your Points table in MySQL. What we will extract here is the total sum of points for each of the Users in the database. The result set will the IDs of the users together with the total Points they have earned.</li><li>Create a Sorted Set in Redis and feed it with the result set obtained in the previous step. As explained before, the ID of the User becomes the Key of the Sorted Set entry, and the total number of points becomes the Score. To add an entry to a sorted set you need to use the <a href="https://redis.io/commands/zadd"><em>ZADD </em>command</a>. <em>ZADD</em> takes three parameters: the name of the sorted set, the score and the key, and takes O(log(n)) time to execute.</li></ul><p>The pseudo code below shows the two steps described:</p><pre><strong>MYSQL<br></strong>select user_id, sum(amount) as score from user_points group by user_id order by score desc</pre><pre><strong>CODE<br></strong>// lets assume $rows contains the resultset from the query above<br>foreach ($row in $rows)<br>  storeInLeaderboard($row-&gt;user_id, $row-&gt;score);<br>endforeach</pre><pre>// the helper function goes like this<br>function storeInLeaderboard(userId, score) {<br>  $redisLibrary-&gt;zadd(&quot;leaderboard&quot;, score, userId)<br>}</pre><p>At this point, the Leaderboard is loaded in a Sorted Set. In Redis, to query the Sorted Set we have to use the <a href="https://redis.io/commands/zrevrange"><em>zrevrange</em></a> or <em>zrange</em> commands. Both commands are essentially the same, with the exception that <em>zrevrange</em> fetches the highest scores first, and that’s what we need for the Leaderboard:</p><pre>zrevrange leaderboard 0 9 withscores</pre><p><em>zrevrange (</em>as well as <em>zrange) </em>takes<em> </em>O(log(N)+M) to execute, where N is the total number of entries in the Set and M is the number of elements to be returned. For our Leaderboard, M is negligible as we are usually going to be interested in the top 10 entries, or the entries around a certain key, but never in a very big M (a very big M tends to make the execution time closer to linear). The output of the command looks something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/492/1*1XA2C-O8kS6TyvqVk2G0lA.png" /><figcaption>Our top 10 entries in the Leaderboard called “leaderboard”, together with their scores. The way you read this result is, for example, user 11 has 190 points, user 10 has 160, user 7 has 24 points, and so on. <strong>Note that the numbers on the far left doesn’t make sense and only represent the row number from the result set.</strong></figcaption></figure><p>The way the output above is to be interpreted is by assigning two lines per entry. For example, lines 1 and 2 contains the user_id (key) and the total points (score) respectively - this translates to <strong>user_id: 11 has 190 points in this Leaderboard</strong>.</p><p>Important to note is that in the example we used <em>zrevrange</em> to fetch the top 10 entries using 0 and 9 as parameters, but we can fetch any range of the Leaderboard with the same execution time.</p><p>Another useful command provided by the Sorted Set is <em>zrevrank</em> (and the converse <em>zrank</em>), which takes a key as parameter and returns the associated score. It takes O(log(n)) time to do so. <em>zrevrank</em> thus constitutes a key part of our Leaderboard: we can use it to fetch the position of any user in logarithmic time.</p><p>Now that we have the ability to fetch the scores of each of our Users in a very low execution time, we only need to pull additional data required to display information in the Leaderboard, such as the User’s name and other application-specific data that may be needed (for example, in our case we needed to show the Course the User is enrolled in). As the ID of the user is available in the Sorted Set, the additional information can be retrieved using standard MySQL queries (the Users table will be indexed with the User ID, and thus the READ operation will be fast enough).</p><h4>Caching the User’s information for faster access</h4><p>Even though accessing User information in MySQL is generally fast, for some Applications that require multiple Joins to get the necessary data it may be convenient to use another data structure to increase the speed of the queries. We introduced <strong>Hashes</strong> before, and they are a great solution to cache the extra data required for the Users.</p><p>The only drawback of Hashes is that they take a lot of memory, so we can’t just dump all the Users information at once. Depending on the specific requirements of the Leaderboard, different strategies could be adopted. For example, for our implementation, only the top 20 entries for the leaderboard are displayed by default. That’s the most common use case, and thus we wanted to have the information for those users readily available in the Hash. Eventually, the Leaderboard may change and new Users may make it to the Top 20. In that case, the new User’s information is added to the Hash.</p><h4>Accessing our Leaderboard in real-time</h4><p>The key to accessing data in a Leaderboard in real-time will be, once the initial setup of the Sorted Set is , to keep it updated every time a User earns points in the application. In the simplest implementation, the z<em>add</em> method would be called every time a new entry in the Points table is created. For Applications with a massive flow of data, it may be needed to leverage a buffer to reduce the writes to the Redis store (or maybe not, this is something to be tested. Our Application didn’t require to do so, and it managed a pretty big amount of data).</p><p>The diagram below shows a general overview of how all the pieces fit together to render a sample Leaderboard:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*EpPUn4umyjHISSTnzv_sNA.png" /><figcaption>The example is from an Age of Empires game. In these games, players earn points constantly during a potentially long period (40 mins on average for a single game). Note that N (the number of players) in this example is very small, so execution times are going to be extremely good. However, the amount of data inserted into the Points table will be huge, and recalculating the Leaderboard from MySQL would be impossible.</figcaption></figure><h3>Final Words</h3><p>It is very interesting to experience how problems that have very straightforward solutions can become a big challenge when they are scaled to huge amounts of data. When the flow of data is so big, additional care needs to be taken in every single step of the proposed solution. We must ensure that the data is available in reasonable execution times, and thus our storage must provide very fast READ operations. If the stream of data coming from the Application is big, the storage needs to support fast writes as well. At the same time, we must pay attention to data consistency, as no matter how fast we retrieve it, if it’s not consistent then it becomes worthless.</p><p>In this quick summary I showed you how to implement a shadow data structure using Sorted Sets to back up your persistent storage in order to provide incredible execution times for solving a very common problem in the gaming industry. The Sorted Set is the data structure that made everything possible, and in our case we used Redis as the method of implementation, although other libraries, or even a custom one, would provide the same benefits.</p><p><em>Stock Photo by </em><a href="https://unsplash.com/@crisovalle?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText"><em>Cris Ovalle</em></a><em> on </em><a href="https://unsplash.com/s/photos/speed?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText"><em>Unsplash</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=555aaa3ccf7b" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/how-we-created-a-real-time-leaderboard-for-a-million-users-555aaa3ccf7b">How we created a real-time Leaderboard for a million Users</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Frontend and the Single Responsibiity Principle]]></title>
            <link>https://medium.com/swlh/frontend-and-the-single-responsibiity-principle-90ef734e622e?source=rss-2480a8b6ff76------2</link>
            <guid isPermaLink="false">https://medium.com/p/90ef734e622e</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[software-design-patterns]]></category>
            <category><![CDATA[single-page-applications]]></category>
            <category><![CDATA[refactoring]]></category>
            <dc:creator><![CDATA[Juan Manuel Villegas]]></dc:creator>
            <pubDate>Thu, 26 Mar 2020 06:14:13 GMT</pubDate>
            <atom:updated>2020-03-28T07:21:36.500Z</atom:updated>
            <content:encoded><![CDATA[<h3>Frontend and the Single Responsibility Principle</h3><p>One thing I have come across multiple times when jumping into already existing Single Page Application (SPA) projects or when reviewing SPA code in general is the misunderstanding of the role the Frontend has in systems like this. Developers tend to confuse the boundaries of the Frontend’s responsibility by throwing too much stuff at it. This stuff is usually application logic that finds its way into State Management modules (such as Redux-style stores), mixins or components, making the frontend aware of things it shouldn’t, even sometimes duplicating functionality that already exists where it should: in the backend.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*32PP8kmeJlmS2AVexbISMA.jpeg" /></figure><h3>Whats the Frontend’s single responsibility?</h3><p><em>Note: before we begin, to fully understand the article it is a requirement to know what the </em><strong><em>Single Responsibility Principle</em></strong><em> is and why it is important. There is a great blog post by Robert C. Martin on the topic here: </em><a href="https://blog.cleancoder.com/uncle-bob/2014/05/08/SingleReponsibilityPrinciple.html">https://blog.cleancoder.com/uncle-bob/2014/05/08/SingleReponsibilityPrinciple.html</a>. <em>Wikipedia doesn’t have a very complete page describing this concept, but they do have one for an equivalent one here: </em><a href="https://en.wikipedia.org/wiki/Separation_of_concerns">https://en.wikipedia.org/wiki/Separation_of_concerns</a></p><p>In this post, we are going to be thinking of the frontend’s responsibility at a very high level of abstraction. Under this view, the responsibility becomes everything related to the rendering of our application: managing routes, displaying forms, executing remote calls to the API, handling the animation of page transitions, opening and closing a side menu, etc. You get the pattern; there is no knowledge about the application domain here. It’s all stuff related to the rendering of the application in a device, be it a browser, a phone or a terminal.</p><blockquote>The Frontend’s single responsibility is everything related to rendering an Application in a device, including Route management, animations and display of data.</blockquote><p>A good exercise in determining if a certain block of code belongs to the Frontend is to ask yourself the question: if I were to deploy this application on a different device, would I need to copy this code over? If so, then likely whatever the code is doing doesn’t belong to the Frontend and should be abstracted to a reusable library.</p><p>Another example of the above idea can be seen when writing the backend of an application. Imagine you are writing a function to handle a route for your API. In this function, there will be a part where you will validate that the request’s verb is the appropriate one(GET, POST, etc), then there will be some parameter validation, and potential data sanitation. After all that, you will start executing your domain code. If, for example, an error happens while validating your data, the function will probably do something like this (Express code):</p><pre>if (req.query.matchId === null) {<br>    res.status(422);<br>    res.send({ code: ‘invalid_data’ });<br>}</pre><p>This block is clearly only relevant in the context of an HTTP request, and thus doesn’t belong to your application domain.</p><h3>The Problem</h3><p>Suppose your application is selling some products, and for each product your application charges a fee for the transaction, except for two products for which you know the ID in advance. After the user chooses a product, you display a checkout form and you need to show the user whether they have to pay the fee or not. It’s as simple as:</p><pre>function hasFee(productId) {<br>    Return [2, 4].indexOf(productId) === -1 ? true : false;<br>}</pre><p>If you drop this function as a getter in your State Management module you can easily access it from anywhere in your application, and quickly solve the problem of displaying the fee to the user.</p><p>But, there is a problem with this approach, even within this simplified example. Several problems, actually.</p><p>The most obvious one is the exposure of database IDs. If your database IDs change or are different across environments, the function is not going to work anymore. Even if you declared two constants for those IDs in an <em>.env</em> file, you would still have a confusing intersection of domains: database concepts entangled in the frontend of your application. Not to mention that testing by creating dummy objects will be complicated, as IDs are generally not predictable.</p><p>The second problem is the worst though- that function is part of the application domain. As such, it doesn’t fit into the high level Single Responsibility we defined above for the Frontend. Let’s dive deeper.</p><h3>A dumb frontend</h3><p><strong>In terms of application domain, our frontend must be as dumb as possible. On the other hand, the backend feeding the application must be as smart as possible and concentrate all the knowledge.</strong> This type of architecture is called API-centric, and guarantees that at any time it’s possible to reach back to a central authority to resolve any conflicts and fetch relevant data.</p><blockquote>An API-centric Application is one where all the domain knowledge is represented in the API, and access to it is exposed through accessors to the different Clients.</blockquote><p>This means that our Single Page Application will turn to the API for whatever information it needs. Even for simple stuff like testing if a product has a fee or not.</p><p>Following this approach has several advantages, not only at the practical level but also at an architectural level.</p><ul><li><strong>Avoiding duplication</strong> is the primary reason. It’s highly likely that if you needed a function in the frontend to resolve a domain problem, you will also need that function in the backend. This leads, in the best case, to having two functions doing the same thing in two different places. In the worst case though, it leads to two functions trying to do the same thing but in actuality doing a different thing because a developer forgot about the version in the frontend and updated only one when the requirements changed. Even worse is, when you have a situation like the one described in the next bullet.</li><li><strong>Favoring a multi client design.</strong> When the Frontend is restricted to do only what it’s meant to do (display data, allow navigation, communicate errors, etc) it becomes really easy to spawn new clients in new devices. When all the complexity of your domain is properly packed in your API, deploying your application to a new device becomes just a matter of putting up the views, fetching the data, and rendering it.</li><li><strong>Testing becomes easier.</strong> Nothing beats a properly tested codebase- a tenet that is still presently not embraced by a sufficiently large number of developers (at least not at the practical level, meaning that they know and accept the benefits of testing, but still refuse to spend time doing it).</li></ul><p>Testing becomes easier basically because separating the domain related code into functions in your backend will ultimately force you to create small libraries, properly exported/imported for usage, rather than creating code that is entangled among other unrelated code. Your function’s signatures will improve because you will have a better understanding of what the function’s real responsibility is after it’s isolated. The benefits are many, and one or more posts will be dedicated to that particular topic. But for now, let’s just emphasize that identifying domain related code and separating it into modules will make your testing easier and better.</p><ul><li><strong>A dumb frontend is usually easier to scaffold</strong>. This is a common pattern: on monday morning your boss asked you to display all the products your store sells in a Slider. So you fired WebStorm, coded a nicely designed Slider component and rendered your Deals. Then, on Tuesday, your boss called you again saying that Sliders are not the hot thing anymore, and instead they’ll be using a ListView to display those products. If your component separated responsibilities, you can easily discard the Slider component, import all the domain code and write the ListView component.</li></ul><blockquote>When all the complexity of your domain is properly packed in your API, deploying your application to a new device becomes just a matter of putting up the views, fetching the data and rendering it.</blockquote><h3>Speed</h3><p>There is another reason to make your Frontend dumb which isn’t strictly related to an architectural concept but rather to the fact that the device in charge of displaying your data is usually going to be less powerful, sometimes much less powerful, than the machine, or potentially the grid of machines serving your backend code. For this reason alone, you don’t want code like this in your views:</p><pre>&lt;div class=”cart”&gt;<br>    &lt;div class=”cart__total”&gt;<br>        {{ (price + vatTax) * percentDiscount + someExtraCharge }<br>    &lt;/div&gt;<br>&lt;/div&gt;</pre><p>I know. It’s just a simply expression, any device can easily solve it right? It turns out that SPA code in general isn’t quite performant (although we’ve seen great improvements in the recent years), specially when your audience isn’t strictly constrained to modern devices. Plus, it already has a bunch of other stuff to attend: routing, caching, solving those meta tags for your SEO team, not to mention rendering HTML and maintaining the shadow DOM, and the list goes on. So, the more stuff you can take out from it, the better!</p><h3>Final Words</h3><p>In this post we’ve gone through an overview of what the Single Responsibility Principle is and how it impacts the frontend code of our application. We’ve seen code that works and fulfill functional requirements, but that violates architectural principles and isn’t scalable in the long term. We have also listed the benefits of having a properly organized domain-related code.</p><p>As a summary, the most important things to do are then to be able to identify what belongs to the frontend and what does not, and be able to factorize it out into library code, which will probably end up residing in your backend code. The benefits for this will be a single source of truth for the stuff that really matters in your project, which is the Domain Knowledge, and improvements in terms of scalability, readability and testing capacity.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=90ef734e622e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/frontend-and-the-single-responsibiity-principle-90ef734e622e">Frontend and the Single Responsibiity Principle</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>