<?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[Engineering @ BuildZoom - Medium]]></title>
        <description><![CDATA[We don’t know much and have much to learn. While building this amazing business, we’ve learned a few interesting bits along the way and share them here in an attempt to strengthen our own understanding and contribute back to the community. - Medium]]></description>
        <link>https://medium.com/engineering-buildzoom?source=rss----6c77490a4b63---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Engineering @ BuildZoom - Medium</title>
            <link>https://medium.com/engineering-buildzoom?source=rss----6c77490a4b63---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 08:49:06 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/engineering-buildzoom" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Why can’t I fill this queue?]]></title>
            <link>https://medium.com/engineering-buildzoom/why-cant-i-fill-this-queue-9f58a9016cec?source=rss----6c77490a4b63---4</link>
            <guid isPermaLink="false">https://medium.com/p/9f58a9016cec</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[bugs]]></category>
            <category><![CDATA[multiprocessing]]></category>
            <dc:creator><![CDATA[Evan Paul]]></dc:creator>
            <pubDate>Wed, 16 Sep 2020 03:31:08 GMT</pubDate>
            <atom:updated>2020-09-16T03:31:08.621Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*Atus8Xi1kcAa69VZyyB2UA.png" /></figure><h4>A brief foray into the hidden complexity of cross-platform programming that rarely surfaces in scripted languages</h4><p>While working through some refactoring of one of our data pipelines, I encountered code hanging in the following block of our multiprocessing code. This block of code is responsible for distributing work to be processed (usually represented as file names) to subprocesses via a multiprocessing.Queue.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2bd92da4f7575ff8e3031c8590c7f95d/href">https://medium.com/media/2bd92da4f7575ff8e3031c8590c7f95d/href</a></iframe><p>That’s strange, I was under the impression that there wasn’t an upper limit to queue size (or at least not on the order of something I should ever encounter). Taking a look through Python’s multiprocessing Queue docs:</p><blockquote><em>maxsize is an integer that sets the upper bound limit on the number of items that can be placed in the queue. Insertion will block once this size has been reached, until queue items are consumed. </em><strong><em>If maxsize is less than or equal to zero, the queue size is infinite.</em></strong></blockquote><blockquote><em>put(obj[, block[, timeout]]) Put obj into the queue… If timeout is a positive number, it blocks at most timeout seconds and raises the </em><strong><em>queue.Full exception if no free slot was available within that time</em></strong><em>…</em></blockquote><p>An unspecified queue size will lead to an ‘infinite’ queue, but this error implies we’ve reached infinity. So… what’s infinity then?</p><h3>To infinity, and hopefully beyond</h3><p>Obviously infinity is a useful mathematical concept, not a concrete number. There is a finite amount of storage on any computer and thus limits need to be set to avoid errors. This raises a few questions:</p><blockquote>1) What is the implicit/undocumented queue size limit?</blockquote><blockquote>2) What’s determining this limit?</blockquote><p>Let’s try to initialize some Queues with different maxsize parameters:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1fb39a12b245c7a67a87baee8ba1ad17/href">https://medium.com/media/1fb39a12b245c7a67a87baee8ba1ad17/href</a></iframe><p>Interestingly, it appears the maximum representable number of a 16-bit signed integer (n-bit signed integers range: [-2^(n - 1), 2^(n - 1) - 1]) is limiting our queue size. Checking on one of our workhorses confirmed my suspicion that this was platform specific. So, now we&#39;ve answered the first question: 32,767 items is our maximum queue size on macOS. But, why?</p><p>As an aside, a new set of related questions arose here that I never fully answered:</p><blockquote>Why is a maximum queue size being represented as a signed integer? What would a negative value mean?</blockquote><p>The good news is that macOS is the development platform and Ubuntu is the target platform, so there isn’t much of an issue here (since we won’t enqueue anywhere near Ubuntu’s limit of 2³¹-1 items.</p><p>At this point I realized that the reason I hadn’t encountered this before is because I had never tested putting 32,768 items in the queue on macOS. Any time I had wanted to test at that scale I switched over to one of our more powerful Ubuntu instances. I decided to ignore this bug and continue working, but eventually curiosity came calling.</p><h3>Down the rabbit hole</h3><p>Let’s start with the traceback of the OSErrorwhen we try to set a maxsize great than or equal to 2¹⁵ on macOS:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/86f3b06a897257d6fdee3032855d0182/href">https://medium.com/media/86f3b06a897257d6fdee3032855d0182/href</a></iframe><p>After some brief reading through the documentation I expected the issue would probably center around pipes or semaphores since both are the main primitives that power python’s multiprocessing Queues. This traceback points to semaphores being the issue. I poked around some of the files to get a sense of what’s was going on, but then dove into synchronize.py as I assumed that is where the answer would be:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/378f183edf488f8419061e7defadd7ea/href">https://medium.com/media/378f183edf488f8419061e7defadd7ea/href</a></iframe><p>Okay so we’ve found a semaphore’s maximum value is the likely culprit and sort of answered the “what’s determining this limit” question, but why is this the value? What’s going on here?</p><p>As an aside, all I really remember about counting semaphores from my OS class I took ~5 years ago is that they are a way of tracking available resources to know when a process/thread needs to block in order to await resources (in this case the resource being managed is the queue size). I think that’s enough context to know why semaphores factor in here and why they would even have a maximum value associated with them, but I could use a refresher lesson in this domain regardless.</p><p>In Python, underscores prepended to variable names typically denote some sort of private nature (by convention). It has the same meaning at the module level, and in cpython’s case this is typically used to denote a C/C++ extension module.</p><p>For easier navigation I decided to download the cpython source and see what I could find.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/427f621db373970abd6381aa4b1d60e2/href">https://medium.com/media/427f621db373970abd6381aa4b1d60e2/href</a></iframe><p>This partially answers why a queue size can be negative, but I’m still unsure as to why we are using it as an integer here rather than an unsigned integer.</p><h3>Eureka, maybe?</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eee9c4fb52a166684f571565dc95c5ea/href">https://medium.com/media/eee9c4fb52a166684f571565dc95c5ea/href</a></iframe><p>There aren’t any specific references to macOS / OS X / Darwin as far as I can tell, but this macro seems to be the likely suspect.</p><p>Eventually I searched for _POSIX_SEM_VALUE_MAX and found a POSIX 2004 standards limits.h definition:</p><blockquote><em>_POSIX_SEM_VALUE_MAX: The maximum value a semaphore may have. Value: 32767 </em>(source: <a href="https://pubs.opengroup.org/onlinepubs/009695399/basedefs/limits.h.html">https://pubs.opengroup.org/onlinepubs/009695399/basedefs/limits.h.html</a>)</blockquote><p>Another more carefully worded Google search yielded an even stronger piece of evidence: an open source Apple XNU implementation of semaphore.h has SEM_VALUE_MAX defined conforming to the POSIX standard:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e5417d894ef5ded7cb98521531a1aaba/href">https://medium.com/media/e5417d894ef5ded7cb98521531a1aaba/href</a></iframe><p>This makes sense as macOS has been POSIX-complaint for some time now. More exhaustive testing could be done to see if this is actually what’s setting the limit, but I’m convinced and my curiosity at this point was satiated. There are still some unanswered questions, but I think I found enough to explain what’s going on.</p><h3>Addendum</h3><p>Our usage of the Queue may be slightly off. We’re running into this issue because we aren’t consuming the queue as we’re producing. Which makes sense since the contents of the queue are known from the start, and we currently we don’t add to it once the subprocesses have spawned (which could change). Using a multiprocessing.Pool, a more abstract multiprocessing class, might sidestep this issue and simplify our implementation. But for now, the Queue works just fine on Ubuntu and I can rest easy knowing this problem only affects large samples on macOS.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9f58a9016cec" width="1" height="1" alt=""><hr><p><a href="https://medium.com/engineering-buildzoom/why-cant-i-fill-this-queue-9f58a9016cec">Why can’t I fill this queue?</a> was originally published in <a href="https://medium.com/engineering-buildzoom">Engineering @ BuildZoom</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Speeding up our Listing Pages by 50%]]></title>
            <link>https://medium.com/engineering-buildzoom/speeding-up-our-listing-pages-by-50-41d7c3378106?source=rss----6c77490a4b63---4</link>
            <guid isPermaLink="false">https://medium.com/p/41d7c3378106</guid>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[performance]]></category>
            <dc:creator><![CDATA[Syed Mohsin]]></dc:creator>
            <pubDate>Mon, 25 Feb 2019 18:37:17 GMT</pubDate>
            <atom:updated>2019-02-25T18:37:17.408Z</atom:updated>
            <content:encoded><![CDATA[<p>On BuildZoom, <a href="https://www.buildzoom.com/san-francisco-ca/general-contractors">contractor listing pages</a> attract the majority of page views across our site from eager homeowners searching for builders in their area. Filled with rich data including reviews, aggregated project details, and high-quality images, these pages can be very taxing on our backend infrastructure, especially for listings in popular cities.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iE9g7wlmyiCRxe1CUhX3pw.png" /><figcaption>Top contractor results in San Francisco</figcaption></figure><p>In this post, we focus on techniques to identify and patch inefficiencies that plagued some of our slowest listing pages.</p><p>TL;DR: We discovered that often &gt;50% of page load time was spent in slow application code and performed garbage collection analysis to diagnose and refactor the inefficient logic.</p><h4><strong>Why it matters</strong></h4><p>Waiting longer than 1 second nowadays for a page to load will test the patience of many users. Besides a poor user experience, slow pages can cause search engine crawlers to complain, negatively impacting SEO. If your servers live in the cloud and a component of your bill is CPU utilization, slow pages can also have a direct quantifiable cost.</p><p>To combat these issues, we use a caching system called <a href="https://varnish-cache.org/intro/">Varnish</a> which shields our servers from repeatedly running intensive queries and computations. However, it is important not to develop an over-reliance on Varnish or expensive queries and non-performant logic can build up to an unmanageable state.</p><h4><strong>Discovery</strong></h4><p>Recently, our listing pages ranked in our top 5 “Most time-consuming” pages with listings for high-traffic locations like Los Angeles and San Francisco taking around <strong>10 seconds to load</strong>. It was clearly time to jump into action. We discovered this issue using NewRelic, our go-to analytics software that gives us valuable insights into the performance of our Ruby on Rails app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*eCbradhY3Q0XQJVz" /><figcaption>NewRelic: some of our slowest load times</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nliHCoXQtbf9UNTczp9eWA.png" /><figcaption>NewRelic: Listing page speeds over 1 week</figcaption></figure><p>Alarmingly, New Relic was reporting that a large portion of time was spent in application runtime (the brown section: SearchContractorsController#index) which meant unnecessary CPU burn, higher AWS bills, and potential disruptions to other parts of our application.</p><p>Generally, slow pages are caused by functionality that request data over the network, such as communicating with a database or a third party API. However, here we see the reverse with more time spent in application code than loading data.</p><p>While there are certainly database operations we can improve such as speeding up queries or removing <a href="https://guides.rubyonrails.org/active_record_querying.html#eager-loading-associations">N+1 database calls</a>, we will focus on fixing the slow application logic that will provide the greatest ROI.</p><pre>I, [2019–01–19T01:14:51.261468 #24840] INFO — : <strong>Completed 200 OK in 4572ms (Views: 523.0ms | ActiveRecord: 355.5ms | Elasticsearch: 210.5ms)</strong></pre><p>Hopping onto my development environment and grep-ing the logs, I confirmed that the majority of load time, about 3.5 seconds in this case, was spent in the Rails runtime as opposed to time spent hitting the MySQL database, calling external services, or rendering views.</p><p>This was about where NewRelic stopped being useful — it was great at finding slow queries but not so great at identifying bad logic.</p><h4><strong>Locating the offending code</strong></h4><p>In a typical Rails environment, slow application logic is often associated with inefficient processing of ActiveRecord objects. ActiveRecord is the Rails ORM which features a clean, object-oriented way to interact with a relational database and manipulate records. Unfortunately, the tradeoff is that ActiveRecord may require extra resources to provide that convenient functionality, which means a performance hit when processing large volumes of data.</p><p>Since a problem like this can be hard to track down, and I wanted to be confident that I’ve actually found the issue(s), I benchmarked every method and service that was called in the controller:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ebc6b6dade2b6589d70734e713d5a3f1/href">https://medium.com/media/ebc6b6dade2b6589d70734e713d5a3f1/href</a></iframe><p>Though this simple logging approach can be tedious and time-consuming, it is exhaustive and effectively drills down to the target code. With enough patience and log-parsing, I found the slow code existed in a single method called <strong>best_photos_for_contractors</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c1ae68155f50eddac497226d64ea556e/href">https://medium.com/media/c1ae68155f50eddac497226d64ea556e/href</a></iframe><p>This method is responsible for batch-loading and indexing high-quality contractor photos on our listing pages. The first thing we do here is query our database for all relevant images based on the input <strong>contractor_ids</strong>. Then, we iterate through each image, adding the image to a hash of <strong>keyed_images</strong> if it belongs to a certain contractor. Not the prettiest code, but not terribly complicated.</p><p><strong>best_photos_for_contractors</strong> was called by 3 different services and took a whopping <strong>1–1.5 seconds with each execution</strong>. Although the query to load relevant images was fast, the application logic for building the <strong>keyed_images </strong>hash single-handedly accounted for ~75% of load time for some of our slowest listing pages.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ce740e496acce38b98e6d590d77abaa5/href">https://medium.com/media/ce740e496acce38b98e6d590d77abaa5/href</a></iframe><p>Why is this line so slow? Our <strong>Image</strong> model is generic, so image records do not have <strong>contractor_id </strong>as a<strong> </strong>foreign key that we can easily reference. Instead, we derive the <strong>contractor_id</strong> via an association called <strong>contractor_galleries</strong>. With each iteration of the loop, this line is instantiating EVERY <strong>contractor_gallery</strong> associated with each <strong>image</strong> just to grab the <strong>contractor_id</strong> off the first gallery. As we cycle through potentially hundreds of images, the constant allocation and deallocation of objects heavily taxed Ruby’s garbage collection system.</p><h4><strong>Analyzing Garbage Collection</strong></h4><p>For a deeper dive into Ruby’s garbage collection, check out this <a href="https://www.speedshop.co/2017/03/09/a-guide-to-gc-stat.html">great post</a>.</p><p>High-level languages like Ruby perform garbage collection, allowing developers to rapidly produce code without being burdened by manual memory management. The tradeoff is that garbage collection systems can consume additional resources and slow down or halt an application when overexerted.</p><p>To test our hypothesis that the garbage collection system was being overwhelmed in this block of code, I ran Rail’s garbage collection profiler:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2625ee1e5143d7294c59fff7884e180a/href">https://medium.com/media/2625ee1e5143d7294c59fff7884e180a/href</a></iframe><p>The results were as expected:</p><pre>I, [2019–01–23T22:34:38.641000 #17935] INFO — :<strong> GC::Profiler.total_time: 1.1358461729998859 seconds</strong></pre><p>Across multiple tests, this code block accounted for 1–1.5 seconds spent performing garbage collection as it rapidly instantiates and discards ActiveRecord objects. In processor time, this is an eternity.</p><h4><strong>The Solution</strong></h4><p>The goal of <strong>best_photos_for_contractors </strong>is to build an index of images by <strong>contractor_id</strong>. A simpler way to build this mapping is to bypass ActiveRecord and directly write a SQL query for the (<strong>contractor_id</strong>, <strong>image)</strong> mapping:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4fd9ee8ad0dcb78c11b8f680564bedc2/href">https://medium.com/media/4fd9ee8ad0dcb78c11b8f680564bedc2/href</a></iframe><p>After some formatting, this raw query returns an array of tuples <strong>(contractor_id, image_id)</strong> that when looped over, is not memory intensive and does not instantiate ActiveRecord objects. Another set of tests running GC::Profiler indicated that this solution was successful in giving Ruby’s garbage collection system a break. The refactored method spends a fraction of the time in garbage collection:</p><pre>I, [2019–01–23T23:02:02.512583 #21905] INFO — : <strong>GC::Profiler.total_time: 0.01238478140000085 seconds</strong></pre><p>After deploying this fix on January 29th, our production application showed great results with time spent in application code flat-lining and becoming negligible.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aH15obJ_OFwYlKrmd-yFIQ.png" /><figcaption>NewRelic: 50% faster load time after deployment on Jan 29</figcaption></figure><h4><strong>In Conclusion</strong></h4><p>As our Rails application and business grow, we will certainly come across more instances where convenient high-level logic can come at the price of performance. Hopefully, this can serve as a cautionary tale to always have an understanding of what’s going on under the hood.</p><p>In addition, we looked at the power of performing basic benchmarking, parsing logs to analyze results, and running garbage collection analysis as valuable tools to locating and refactoring inefficient code. I hope this knowledge can serve you as you optimize your applications!</p><p>If you made it this far and enjoy hunting for performance optimizations, consider our team at BuildZoom — <a href="https://jobs.lever.co/buildzoom?utm_campaign=po_referrals&amp;utm_source=medium_engineering_blog&amp;utm_medium=web">we are hiring</a>!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=41d7c3378106" width="1" height="1" alt=""><hr><p><a href="https://medium.com/engineering-buildzoom/speeding-up-our-listing-pages-by-50-41d7c3378106">Speeding up our Listing Pages by 50%</a> was originally published in <a href="https://medium.com/engineering-buildzoom">Engineering @ BuildZoom</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Simplifying React form state management]]></title>
            <link>https://medium.com/engineering-buildzoom/simplifying-react-form-state-management-82cbd376ad40?source=rss----6c77490a4b63---4</link>
            <guid isPermaLink="false">https://medium.com/p/82cbd376ad40</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Connor Stauffer]]></dc:creator>
            <pubDate>Fri, 05 Oct 2018 16:58:25 GMT</pubDate>
            <atom:updated>2018-10-05T16:58:24.701Z</atom:updated>
            <content:encoded><![CDATA[<h4>Managing form state in modern JavaScript applications is usually verbose and error prone, but it doesn’t have to be.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*QreBUE30cI62xTB3ab8MAA.png" /></figure><p><strong>We’re going to demonstrate how a simple HTML form written in React can require a daunting amount of code and, worse yet, how that code can easily end up duplicated across a code base.</strong></p><p>Then, once we’re sufficiently frustrated, we’ll do it the same way over and over again for every form imaginable until we all rage quit or go into management.</p><p>Not really. Once we’re sufficiently frustrated, we’ll identify the parts of our component that can be abstracted away. Fortunately, we’ll see that the most verbose and complex parts are the easiest ones to extract. <strong>What we’ll end up with is a useful abstraction for managing UI-agnostic form state, and a simple form component responsible for little more than its markup.</strong></p><h4>Let’s get started!</h4><p>Let’s say we’re building a form to handle user sign ups. Here’s a component to handle the basic input state.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e1482250b6e23f2d3e9fe64a8a6135fc/href">https://medium.com/media/e1482250b6e23f2d3e9fe64a8a6135fc/href</a></iframe><p>Our form has text inputs for email and password. We’re using component state to keep track of the values. We have onChange handlers to update state whenever the input changes.</p><h4><strong>Let’s add some submission logic.</strong></h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bf8e31e80dbb7238e2acc3ffd3582a75/href">https://medium.com/media/bf8e31e80dbb7238e2acc3ffd3582a75/href</a></iframe><p>When the form is submitted via a click to the submit button or an enter key press from one of the fields, we’re calling thecreateUser method passed as a prop. (The details of that method aren’t relevant to this post. Assume it’s some async server call.)</p><p>This is about as far as the <a href="https://reactjs.org/docs/forms.html">React docs</a> will take us when it comes to building forms. But we’re still far from production-level behavior. We can’t just be firing off form submissions silently! User’s expect feedback when they interact with the page.</p><h4>So let’s add submission and confirmation state.</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e81da99c3a81acdcdf23c08153dc2454/href">https://medium.com/media/e81da99c3a81acdcdf23c08153dc2454/href</a></iframe><p>In addition to keeping track of the email and password fields, we’re persisting whether the form is in a submission or confirmation state. Immediately before this.props.createUser is called, we set submitting to true, which results in our button text changing from “Submit” to “…”. Once the createUser logic is complete, we set submitting to false and confirming to true, which changes our button text to “Success!”.</p><p>Big step up in UX there. But the next addition is where our task gets painful. Currently, we’re not imposing any restrictions on what form data can be submitted.</p><h4><strong>Time for validations.</strong></h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7294f340a2d025b2d8fbcd7cdf20b1b4/href">https://medium.com/media/7294f340a2d025b2d8fbcd7cdf20b1b4/href</a></iframe><p>What happened? It’s chaos! Let’s step through the major changes.</p><p>First off, our state object has changed slightly. We’ve moved the email and password values into a fields key and are tracking a list of errors and a dirty Boolean in addition to value. dirty signifies whether the field has been validated since it’s last change in value.</p><p>validatePassword adds an error to our password field state if the current input is less than six character long. validateEmail does the same for our email field state if the input isn’t a valid email or if this.props.getExistingUser resolves to a user object.</p><p>Each of these methods is called when its respective input field fires a blur event. Both are called when the form is submitted, before the createUser request is made. We avoid duplicate validations by skipping them if the field hasn’t changed since the last validation.</p><p>All the behavior in this component is reasonable. For a one-off form, I wouldn’t change much. But what about the next form we add to our code base?</p><h4>Let’s imagine we need to build another form.</h4><p>This new form has it’s own submission logic, fields, validations, and UI. Where do we start? We could start in the same place we did for our sign up form.</p><p>We’d create a new component, give it state to keep track of the field values, errors, submission state and confirmation. We’d add methods to validate each field and update the UI state on submission. We’d end up with something very similar to our registration form.</p><p>Each of our forms would be fine React components, but together, in the same code base, the logic duplicated between them would beg to be refactored.</p><h4>Almost all forms behave the same way.</h4><p>They’re made up of a set of fields, each having a default value, initialization logic, errors, and dirtiness. A form component cares about the state of each individual field and the state of the form as a whole. It shouldn’t validate unless a field has changed since the last validation. It shouldn’t submit unless all fields are known to be valid. It should handle any combination of sync and async field validations.</p><p><strong>Notice that none of these requirements have anything to do with the type of fields in or the appearance of the form.</strong></p><p>They are shared requirements of generic form state. Form state, not UI, will be the source of our abstraction. Let’s jump ahead to what we’ll be left with after our refactoring is complete.</p><p>Here’s our registration form with it’s state managed by a higher order component called withForm.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3ffe852c60586d695a27cd67000a347c/href">https://medium.com/media/3ffe852c60586d695a27cd67000a347c/href</a></iframe><p>Nothing but markup and configuration. Our component is wrapped in a HOC that takes a submit function, an optional confirmationDuration , and a map of fields each with a getErrors method. We’re defining only the things that vary significantly between different forms.</p><p>All the verbose state management still has to go somewhere. We haven’t gotten rid of it, but we’ve done the next best thing. We’ve isolated it in our withForm HOC.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/24a4557615697a056785d3973fbb271e/href">https://medium.com/media/24a4557615697a056785d3973fbb271e/href</a></iframe><p>It looks quite a bit like our original component. The state shape is identical. We’ve just generalized our validation methods to handle an arbitrary number of fields.</p><p>On each render, we build up a single form state object and pass it to the child component. The child component has all the information and control it had when managing its own state, but less clutter and potential for error.</p><h4>We’ve come a long way.</h4><p>We’ve gone from a verbose, naive implementation that doesn’t scale, to a reusable solution that will save us time, effort, and errors down the road.</p><p>This isn’t a new approach to building forms in React. I highly recommend looking into the open source library <a href="https://github.com/jaredpalmer/formik">Formik</a>. Its <a href="https://jaredpalmer.com/formik/docs/api/withFormik">withFormik</a> HOC is a more complete version of what we built in this post.</p><p>If, however, you’re trying to limit dependencies and think a minimal approach like withForm could work for your project, you can find a <a href="https://codesandbox.io/s/m5jxqmmx0x">working example to play with in this sandbox</a>.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2Fm5jxqmmx0x&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2Fm5jxqmmx0x&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2Fm5jxqmmx0x%2Fscreenshot.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codesandbox" width="1000" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/a62e62b8534c4aa8dd8fcf8b0e0aa6fd/href">https://medium.com/media/a62e62b8534c4aa8dd8fcf8b0e0aa6fd/href</a></iframe><p>Also, please remember to review and update any code included in this post or the sandbox to ensure it’s ready for your production needs.</p><p>Happy form building!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=82cbd376ad40" width="1" height="1" alt=""><hr><p><a href="https://medium.com/engineering-buildzoom/simplifying-react-form-state-management-82cbd376ad40">Simplifying React form state management</a> was originally published in <a href="https://medium.com/engineering-buildzoom">Engineering @ BuildZoom</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How I Redesigned Contractor Search at BuildZoom]]></title>
            <link>https://medium.com/engineering-buildzoom/working-with-elasticsearch-at-buildzoom-9f7d964b42a7?source=rss----6c77490a4b63---4</link>
            <guid isPermaLink="false">https://medium.com/p/9f7d964b42a7</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[elasticsearch]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[big-data]]></category>
            <dc:creator><![CDATA[Silas Tsui]]></dc:creator>
            <pubDate>Fri, 24 Aug 2018 17:59:53 GMT</pubDate>
            <atom:updated>2018-08-24T18:06:40.062Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Image result for elasticsearch logo" src="https://cdn-images-1.medium.com/proxy/1*4thJErMA9UpuP1jEBLRWFQ.png" /></figure><p>Over the past four months, I’ve been working at <a href="https://www.buildzoom.com/">BuildZoom</a>, a construction marketplace that leverages building permit and license data to help connect homeowners with the best contractors for their project.</p><p>I had an amazing experience as a software engineering intern here. One of the best things about working at BuildZoom is how much they focus on mentorship and collaboration. I got to work on meaningful, impactful projects across the entire stack, ranging from data engineering and performance optimizations to communications infrastructure and full-stack product features. In this article, I’m going to talk about how I redesigned contractor search using Elasticsearch during my internship.</p><h3>Introduction</h3><p>ElasticSearch (ES) is a distributed, full-text search engine wrapped in an HTTP web interface. It’s an awesome tool for full-text search, <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/inverted-index.html">relevancy ranking</a>, and aggregations. At BuildZoom, we use it for things like contractor listing pages and project distribution.</p><p>If you haven’t used ES before, I would recommend checking out their <a href="https://www.elastic.co/guide/index.html">documentation</a>. It’s very comprehensive and a great starting point.</p><h3>Redesigning Contractor Search</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/996/1*XeZ_DLNYy1G73oDlDRM7Qw.png" /><figcaption>Listing Page for General Contractors in San Francisco, CA</figcaption></figure><h4>Motivation</h4><p>When users search for contractors on BuildZoom, we can roughly categorize them into one of two categories: exploration and search.</p><p><strong>Exploration<br></strong>Homeowners often come to BuildZoom looking for services (e.g. bathroom remodel, roofing, etc.) or types of contractors (e.g. electrician, general contractors, etc.). When a user is just browsing, we want to show results with contractors that are most qualified for their project.</p><p><strong>Search<br></strong>Contractors often search for their own profiles and homeowners often search for the contractors that BuildZoom matched them with . When a user is looking for a specific contractor, we want to show results with the contractor they’re looking for.</p><p>The search algorithm we want to use on the listing page varies depending on the use case, so we want to infer what type of search a user wants to perform based on their search terms. The most straightforward way to do this is to match their search terms against a list of contractor_types and project_types. If their search term matches, the user is probably making an exploratory search; otherwise, they’re probably making a full-text search.</p><pre># matches contractor type -&gt; exploratory<br>search_term = &#39;electrician&#39;</pre><pre># matches project type -&gt; exploratory<br>search_term = &#39;bathroom remodel&#39;  </pre><pre># doesn&#39;t match contractor/project type -&gt; fulltext<br>search_term = &#39;oaktown builders&#39;  </pre><h4>Architecture</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/321/1*nLOIvMho4Pn-KV4uL8L_Cw.png" /></figure><p>Our architecture for Elasticsearch uses a service to convert front-end params from a controller to a standardized SearchConditions struct with all the available search configuration. The SearchConditions are then passed to ContractorElasticsearch, a module that translates attributes from the struct to Elasticsearch clauses. The resulting clauses are then combined together to construct our query.</p><p>This intermediate layer makes it easy for multiple controllers to interact with ES while keeping the code clean and maintainable. It also allows developers to construct ES queries without diving deep into Elasticsearch’s domain specific language.</p><h4>Algorithm Design</h4><p>I started by defining what attributes we wanted to consider in our search queries.</p><p>For exploration, we wanted to show off the best contractors on BuildZoom, so I prioritized attributes like BuildZoom score, photo quality, and relevant experience.</p><p>For full-text search, we wanted to display contractors with business names that closely matched the search query. It was important to create a string matching algorithm that worked really well.</p><h3>Implementation</h3><p>Creating the search algorithm was somewhat of an empirical process because I had to find a good balance between all the different attributes. I didn’t want any one attribute to dominate, but I still wanted some attributes to have more weight than others.</p><h4>Bool Query</h4><p>I used Elasticsearch’s <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html">bool quer</a>y for this. It matches boolean combinations of other queries, making it perfect for ranking the relevance of different contractors. It takes attributes like buildzoom_score and string_match_scoreinto account to produce a single search_score.</p><pre># starting point for our bool query</pre><pre>search_definition = {<br>    query: {<br>        bool: {<br>            should: [],<br>            must: [],<br>            filter: [],<br>            must_not: []<br>        }<br>    }<br>}</pre><p>The filter and must_not clauses filter contractors without affecting thesearch_score. We can use this to include contractors in a specific geofence or exclude contractors with an alert.</p><pre># returns contractors within 50 miles of SF without an alert</pre><pre>filter: [<br>    { term: { has_alert: false } },<br>    { geo_distance: { <br>        distance: 50, <br>        unit: &#39;miles&#39;, <br>        distance_type: &#39;arc&#39;, <br>        location: {lat: 37.77, lon: 122.41}<br>    } }<br>]</pre><p>The must clause filters out contractors <em>and</em> affects the search_score. This is very useful for string matching. We can return contractors sorted by how closely they match the query string and filter out contractors that don’t match the search term all in one clause.</p><pre># returns contractors ranked by how closely they match the query terms </pre><pre>must: [<br>    { match: {<br>        business_name: {<br>            query: &#39;oaktown builders&#39;, <br>            fuzziness: :auto<br>        }<br>    } }<br>]</pre><p>The should clause purely affects the search_score. We use this for things like photos and reviews, which provide a boost to the contractor’s ranking.</p><pre># provides a 2.5 point boost for contractors with photos</pre><pre>should: [<br>    { constant_score: { <br>        filter: { term: { has_photos: true } },<br>        boost: 2.5 <br>    } }<br>]</pre><h4>Fuzziness</h4><p>String matching is heavily based on fuzziness. The fuzziness clause is the acceptable <a href="https://en.wikipedia.org/wiki/Levenshtein_distance">Levenshtein distance</a> between the search term and the search results. If fuzziness is set to 1, searching for Silas could return results like Sylas or Silus.</p><h4>Function Score</h4><p>I primarily used the function score query to optimize string matching. The function score query has two components: queryand functions. I used query to return contractors that were somewhat similar to the search term, then used functions to rank the contractors based on how closely they matched.</p><p>Suppose a user was searching for “my way construction”. The function score query returns a list of contractors that fuzzy matched at least one of [&#39;my&#39;, &#39;way&#39;, &#39;construction&#39;]. Then, functions categorizes the business name into one of four buckets:</p><ul><li>exact business name match:my way construction</li><li>exact word match: my way general construction</li><li>fuzzy business name match: my bay construction</li><li>fuzzy word match: my bay electricians</li></ul><p>Contractor search scores are multiplied by a factor depending on how closely they match. The closer the match, the higher the resulting search_score.</p><h4>Analyzers</h4><p>Analyzers are used to specify how we want to index data for different fields. When we index a contractor’s business name into ES, we remove redundant words, clean up symbols, and tokenize strings.</p><p>For example, if a contractor’s business name is “Dave &amp; Silas Electrical Contractors”, it would get indexed as [“dave and silas electrical contractors”] and [&quot;dave&quot;, &quot;silas&quot;, &quot;electrical&quot;].</p><h3>Results</h3><h4>Exploratory Queries</h4><p>Exploratory queries still return contractors with relevant experience and high BuildZoom scores on a clean, aesthetically-pleasing page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MYWTpv3J3Bo77VEvttZqKw.png" /><figcaption>Before and After for “kitchen remodelers”</figcaption></figure><h4>Full-text Queries</h4><p>Full-text queries now show contractors that match the search terms much more closely.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f06Y0E_ZN1yPNWujkv3zGA.png" /><figcaption>Before and After for “my way construction”</figcaption></figure><h3>Conclusion</h3><p>Thank you for reading! I hope you were able to gain some insight into how you can leverage Elasticsearch for full-text search from this article. Elasticsearch is incredibly powerful and it definitely feels like I only scratched the surface of its full capabilities with this project.</p><p>If you’re interested in learning more or have any questions, feel free to <a href="https://www.linkedin.com/in/silastsui">contact me</a>.</p><p><em>If you want to work on projects like this, BuildZoom is </em><a href="https://jobs.lever.co/buildzoom"><em>hiring</em></a><em> for both internships and full-time positions!</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9f7d964b42a7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/engineering-buildzoom/working-with-elasticsearch-at-buildzoom-9f7d964b42a7">How I Redesigned Contractor Search at BuildZoom</a> was originally published in <a href="https://medium.com/engineering-buildzoom">Engineering @ BuildZoom</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[4 differences between interning at a start-up and corporation]]></title>
            <link>https://medium.com/engineering-buildzoom/4-differences-between-interning-at-a-start-up-and-corporation-37fcc97aaddb?source=rss----6c77490a4b63---4</link>
            <guid isPermaLink="false">https://medium.com/p/37fcc97aaddb</guid>
            <category><![CDATA[career-advice]]></category>
            <category><![CDATA[startup]]></category>
            <dc:creator><![CDATA[Yuliana Havryshchuk]]></dc:creator>
            <pubDate>Thu, 23 Aug 2018 22:11:47 GMT</pubDate>
            <atom:updated>2018-08-23T22:11:47.145Z</atom:updated>
            <content:encoded><![CDATA[<p>As a computer science student at the University of Waterloo, I’ve had the opportunity to do 6 internships at 5 different companies since 2013. I’ve learned a lot about programming, product development, and maybe most importantly, the type of company that I want to work for when I graduate.</p><p>This summer I completed a 4 month internship at <a href="http://buildzoom.com/">BuildZoom</a> — a company with a simple purpose: to help you find the perfect contractor for any remodeling or construction job.</p><p>BuildZoom was my first start-up experience. I previously worked almost exclusively at corporations with thousands of employees. This post will describe 4 of the biggest differences I noticed between working as an engineer at a large company, and working as an engineer at a start-up.</p><p><em>Disclaimer: Culture and processes vary a lot not only between companies, but even between teams. These observations are based only on my experiences!</em></p><h4><strong>#1 Involvement in the product development process</strong></h4><p>There are a few core stages involved in bringing a feature to market: defining a goal, performing data analysis, scoping out the idea, technical implementation, and finally, evaluation after it goes live. What makes a big difference to you as an engineer is <em>when</em> you are brought into the process.</p><p>At a large company, by the time that a task gets assigned to you, it’s finalized and ready to be implemented, having been hashed out by relevant teams (business, product, design). This is nice and straightforward, which lets you get right down to business, but what if you have a contribution that can make the feature even better? Or an idea for a small tweak to the design that could cut the development time in half? At a large company, there is often no room for this feedback because the tasks between teams are very clearly defined , and the task of an engineer is simply to implement. Not giving engineers the opportunity to contribute to the ideation process makes it less likely that the optimal solution will be found, and also makes individual engineers less invested in the products. It’s not as fun when you have no say in the features that you’re working on.</p><p>As an engineer at BuildZoom, I found that not only do you have a say in the product development process, you’re encouraged to speak up when you have concerns or ideas for improvements. The best solutions are found when all the stakeholders are involved in the process, leading to a good balance of company goals, user experience, and technical feasibility. I’ve had a lot of fun collaborating with different teams to iterate on a product idea before moving forward with the implementation. It helps you stay engaged in the company vision, and makes you feel empowered to make an impact.</p><h4><strong>#2 Depth vs. breadth</strong></h4><p>There is plenty of opportunity in the tech industry for both specialists and “jack of all trades” programmers. However, it’s not always possible to find both types of opportunities within one company.</p><p>At a large company with thousands of people, there’s a team for everything. At the minimum there’s a team for each product, and it’s not unheard of for a single feature to be split between multiple teams. This means you can get really deep knowledge of what you’re working on — which is great if you’re solving a really interesting problem, but let’s just say I wasn&#39;t too thrilled with spending 4 months working on a word count box.</p><p>Small companies simply do not have the bandwidth for every engineer to be a specialist in a small area of the code. Most of the engineers at BuildZoom are full-stack. Because the engineering team is small, I had the chance to contribute to a variety of cool features in different areas of the company. A few of the things I got to work on were: extending BuildZoom’s public API, automating data exchange processes with data licensing partners, and bringing new features to the website from the back-end to the front-end. It can be challenging to be exposed to many new technologies in a short period of time, but it leads to very rewarding growth in your skills as a programmer.</p><h4><strong>#3 When processes don’t work</strong></h4><p>Repetitive interactions in groups inevitably lead to processes, known as “the way things are done.” Processes are great! Without them, any organization would be chaotic. The difference is how companies deal with processes that become out-of-date or ineffective.</p><p>At a large company, when a process doesn’t work, the patterns of activities have usually become so entrenched that they are difficult to change. First you have to get everyone to agree that the old process doesn’t work. Then you have to getting everyone to agree on a new process! If hundreds of people are involved, it’s inevitable for this change to take months. At one large company I worked at, we had quarterly releases with certain tasks assigned to each release. This list of tasks was inflexible. If a team had completed all their tasks early and wanted to fit in some extra bug fixes into the release, they had to use a sneaky workaround to tie the bug fix to a task, and hope no one would notice. If you have to wade through bureaucracy to be <em>allowed</em> to get work done, there’s definitely a problem.</p><p>At a start-up, it is very clear when there’s an unnecessary process or a meeting that doesn’t make sense. And because of the small number of people involved, it’s very easy to make a change. At BuildZoom, new features and updates generally go live every Tuesday and Thursday which provides stability to the product development cycle. So what happens if something needs to be released on a Wednesday? All you have to do is write a slack message in our web development channel, requesting a deploy. There’s no red tape or paperwork to complete, which leaves a lot more time for your actual work.</p><h4><strong>#4 Mentorship</strong></h4><p>Mentorship is one of the most important parts of an internship. Strong internship programs are focused on proactively helping interns level up and contribute successfully to the company.</p><p>My experiences with large companies have been that interns are hired to do work that is left after the full-time engineers get assigned their tasks. This can include “blue-sky ideas” — ideas that are cool to explore and demo, but not nearly important enough to dedicate engineering time to. This usually means that your 4 months of work will never see the light of day. Another alternative is grunt work — work that’s mundane and repetitive and just needs to be done. My coworkers at large companies were really great, and tried to provide me with all the guidance I needed. But when they’re facing deadlines for their <em>real</em> tasks, and you’re trying to figure out the best architecture for your demo app (which no one will ever open after you leave), can you guess which one of these things will take priority?</p><p>I’ve wanted to work at a start-up for a long time, but the fear of failure was holding me back. From what I heard about startups, they were lean, moved quickly, and placed a lot of responsibility on each individual. That sounded really exciting! But what if I couldn’t handle it? During the interview process with BuildZoom, I asked one of the senior engineers “what is the mentorship at the company like? Will I be thrown into the deep end and told to swim?” He assured me that everyone on the team is invested in helping each other succeed. After 4 months, I can’t overstate how true that is. At the start of the internship, each small task that you’re assigned pairs you with another engineer, all of whom are extremely smart and very helpful. This is a great strategy to help you get familiar with different areas of the codebase and get to know other engineers. The team helps you build up your skillset and confidence, giving you larger and larger projects as you’re ready to take them on. And because the resources at a start-up are much more scarce, there are no ‘throwaway’ projects and everything you do as an intern matters.</p><h4>Conclusion</h4><p>Having read the article, can you guess which type of company I prefer working at? I have to admit that I might be biased, since working at BuildZoom was <em>really</em> awesome. I got to work on meaningful projects with really smart people who were excited to help me make an impact.</p><p>If the only reason you haven’t joined a start-up yet is because of the intimidation factor, I encourage you to take the plunge! Surrounded by the right people, your skills and confidence will build and you’ll surprise yourself with what you can achieve.</p><p><em>BuildZoom is </em><a href="https://jobs.lever.co/buildzoom"><em>hiring</em></a><em> for both internships and full-time positions!</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=37fcc97aaddb" width="1" height="1" alt=""><hr><p><a href="https://medium.com/engineering-buildzoom/4-differences-between-interning-at-a-start-up-and-corporation-37fcc97aaddb">4 differences between interning at a start-up and corporation</a> was originally published in <a href="https://medium.com/engineering-buildzoom">Engineering @ BuildZoom</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Three things (I thought) they taught me in school]]></title>
            <link>https://medium.com/engineering-buildzoom/three-things-i-thought-they-taught-me-in-school-88e4a88a2aa1?source=rss----6c77490a4b63---4</link>
            <guid isPermaLink="false">https://medium.com/p/88e4a88a2aa1</guid>
            <category><![CDATA[debugging]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[learning]]></category>
            <category><![CDATA[careers]]></category>
            <dc:creator><![CDATA[Morgan Connolly]]></dc:creator>
            <pubDate>Fri, 01 Jun 2018 17:49:26 GMT</pubDate>
            <atom:updated>2018-06-01T17:49:25.893Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to debug software</h3><p>My first experience programing was during freshman year of college, in CS10, <a href="http://cs10.org/sp18/"><em>The Beauty and Joy of Computing</em></a><em>.</em> CS10 is taught in<em> </em><a href="https://snap.berkeley.edu/"><em>Snap</em></a><em>.</em> <em>Snap</em> involves very little typing; to build programs you click and drag pre-defined blocks of “code” together to construct a program and press a “Play” button to run it.</p><p>When I started coding in <em>Snap</em> I had no intentions to be a professional developer. Or, even to take another computer science class after finishing CS10. But, sometime into debugging one of my first projects (a hangman game) I found myself thinking that if I could just make this code work I would feel like a wizard.</p><p>For me, debugging in <em>Snap</em> typically involved building a fat stack of blocks, then pressing play, and finally staring intently at my Sprites trying to figure out if they were doing exactly what I intended them to do. You might wonder what Sprites are. They are cartoon-like representations of objects, used to make OOP more accessible to beginners.</p><p>As you might imagine, this debugging strategy was time consuming and not particularly effective. When the following semester I encountered Python print statements, the insight they provided into the precise inner workings of my code felt like magic. When I met my first command line debugger, jdb, my first thought was “nah man this is harder than writing code”. But the following semester an <a href="https://cs162.eecs.berkeley.edu/">Operating Systems</a> course in C showed me the light and glory of line-by-line debugging with gdb.</p><p>June 2017, when I started working at <em>BuildZoom</em>, I grabbed the two relevant line-by-line debuggers for our stack, “byebug” for the ruby back-end and “debugger” for the AngularJS frontend and cheerfully prepared to annihilate all bugs found in testing quickly and easily ship perfect, bug free code all day every day. Then, reality kicked in.</p><p>As you might imagine, my first year working on a living code base within a team of developers has not been bug free. And, I’ve had to build upon the debugging skills I learned in college. Many times I’ve been assigned bugs to fix that are in parts of the code base several BART stops away from anywhere I’ve been before. And sometimes these bugs are reproducible only in a production environment — a situation that, I thought, must exist to frustrate me personally the first time I encountered it. My new favorite debugging techniques are unit tests and grepping logs. And, when I’ve tracked down insidious production bugs or produce working code in record time, I feel distinctly magical. Like that time back in college.</p><p>To summarize, I’ve learned that when debugging one should</p><ul><li>analyze symptoms — this step involves mustering all the information you can gather about the bug. How often has it happened? When did it start? What kind of users does it happen for? This is a good time to leverage logs and any kind of bug reporting system your team uses.</li><li>understand impact — this usually falls out of analyzing symptoms and is a a critical early step. Because, understanding the impact of a bug allows you to prioritize it.</li><li>pinpoint the source of the bug — this can be the most time consuming step. Finding the exact code/environment/input tuple that produces a bug is difficult. This is usually the time to break out the line-by-line debugger.</li><li>Fix the bug, and make a pull request — If you’ve completed the previous three steps this may very well be the easiest and quickest part of the process.</li></ul><h3>Asking questions</h3><p>I’ve encountered two big difference between asking questions at school and asking questions at work.</p><h4>Scope of the questions</h4><p>Problem sets and projects assigned in courses are, without fail, within the scope of the class. In other words, if you learn all the material presented in the class you will be able to complete the projects and problem sets well. In school, if you’re asking a question, there was probably a way for you to answer the question yourself in approximately the same time as it will take someone to explain the answer to you. For this reason, I avoided asking questions — and the accompanying risk of appearing stupid — by putting hours into a problem before I brought it to an instructor to ask for help. My personal algorithm for seeking help in school was roughly linear and looked something like this: try -&gt; google -&gt; cry -&gt; GSI (graduate student instructor).</p><p>In contrast to the limited and well defined scope of a college course, the space of problems you might encounter working in a production code base is vast. Even if a computer science education focused entirely on software development skills (which it most certainly does not) this hypothetical education would not be able to cover the set of problems you might encounter at work. For this reason, there are some problems you should not stare at for several hours before asking for help. For example, when learning a new code base there will probably be conventions you are unfamiliar with. And, someone who is familiar with these conventions can explain them to you far faster than you would be able to figure them out yourself.</p><h4>Who’s answering the questions</h4><p>The second big difference between asking questions at school and at work is at school there are people whose job it is to answer your questions. So, if you ask them something you could have googled, then they will probably just be happy that your question wasn’t harder to answer. And, if you ask them a long, rambling question that involves notes spread over several sheets of scratch paper they will probably just be happy you tried to solve the problem on your own before coming to office hours. In contrast, at work every question you ask is taking your coworker’s time away from their own projects. So there’s a much greater opportunity cost to answering questions than during school. If it’s a googlable question, you’re wasting their time. The same is true if the question is not presented concisely.</p><p>As any good adventuring party should, the engineering team at BuildZoom has specialists and domain experts. My current help seeking algorithm is significantly more dynamic. And, it benefits dramatically from the wealth of knowledge available a few steps in any direction from my desk.</p><h3>Building accurate estimates</h3><p>Making accurate time estimates in school is usually purely for your own benefit. For me, the process of building time estimates for projects and problem sets in school often went something like this</p><ol><li>Take number of days between the date the assignment comes out and the date it is due. Divide this number of days by two. Assume you’re working on the assignment for 1.5 hours per day. Call the result course_estimate.</li><li>Ask everyone I know who has taken the course to estimate how long the project took them.</li><li>Adjust the time estimates from number 2 based on which of the two possible sets of people the estimator comes from: people who code slower than me and liars (joke). Call this list of estimates friend_estimates.</li><li>Take the mean of the elements of friend_estimates and course_estimate; the result is approximately the number of hours the assignment should take to complete.</li></ol><p>At work, each project is something new. And, fielding reliable estimates necessitates diving deep into the technical nitty-gritty of the assignment. However, this means that if one is making time estimates one is forced to lay out a detailed plan before starting the project. This can be difficult. But, I’ve learned that one shouldn’t start a project until one has a relatively detailed time estimate. If I find that I can’t make a detailed estimate, this is usually a glaring indication that I should not start writing code until I have a better idea of how I’m going to complete the project requirements.</p><p>Of course, I sometimes find myself throwing out my careful, broken-down-into-30-or-15-minute-segments time estimate. When this happens, I find myself mourning the lost half an hour taken to craft the estimate. Of course, the time it would have taken to build and test the code would have been far more dear.</p><p>To create an estimate</p><ul><li>break the project down into major parts, e.g. front-end work, back-end work and try to figure out what existing files you’ll be touching. At this point, don’t even try to make time estimates yet. Just figure out what the major parts of the project are.</li><li>break each major part down into smaller parts, e.g. building a new API call, styling a new button. Try to make time estimates for each of these parts,</li><li>break any of your estimates that are larger than an hour down into smaller parts.</li><li>add up your current estimate and budget about 50% of it for QA and unexpected delays.</li></ul><h3>People would do that? Go on the internet and state their opinions as facts?</h3><p>My computer science education did not consist of learning anything as an opinion. So, when I set out to learn my first technologies out of school (<em>Ruby</em> and R<em>ails)</em> I was at a bit of a loss to shrewedly consume technical material. <em>RailsGuides</em> taught me about ActiveRecord callbacks (cool!) and later I encountered <em>Destroy All Software</em> screencast. DAS is not a fan of ActiveRecord callbacks. I was skeptical of this opinion. Surely an entire feature of the rails framework couldn’t just be *wrong*. But, eventually I did encounter a nasty callback bug and realized that perhaps the problem with callbacks is that they end up being used where something with easier-to-predict behavior would suffice. Anyone who has spent time learning software development will know that there are bitter and strongly-worded disagreements about the best way to write code. However, I’ve realized that you can take someone’s advice without subscribing to their dogma. And, I think I now understand better why developers state their opinions so strongly. I actually think this tactic is used for two reasons.</p><p>1. to say something strongly enough that people remember it and (more importantly)</p><p>2. to say something strongly enough to invite disagreement and spark discussion.</p><h4>tldr;</h4><p>I have learned much in the past year; I’ve probably learned more interesting, useful, and difficult stuff than I have in any previous year (except perhaps the year I learned how to read). And, I’ve certainly picked up quite a few nice new technical stats.</p><p>But, most of the learnings I’ve discussed here are not facts, techniques, technologies or concepts. The learnings I’ve discussed have been more abstract — and correspondingly more difficult to acquire. If I were to purpose these thoughts as advice they would more like a self-help book than documentation. I think this may be because school actually succeeded at teaching me how to to program. I do not claim that school made me a good programmer, you’d have to ask my manager that, but it taught me to be comfortable with coding.</p><p>However, school did not teach me how to be a software developer. That is, how to develop and debug in a large production code base, learn from your team, be reliable for the rest of the company and ultimately ship code. And, the first hints of magic I felt when dragging <em>Snap</em> blocks around in CS10 did not prepare me for the camaraderie and satisfaction of shipping code with an awesome team. But, when you think about it, that’s the real magic.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=88e4a88a2aa1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/engineering-buildzoom/three-things-i-thought-they-taught-me-in-school-88e4a88a2aa1">Three things (I thought) they taught me in school</a> was originally published in <a href="https://medium.com/engineering-buildzoom">Engineering @ BuildZoom</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Management Philosophy]]></title>
            <link>https://medium.com/engineering-buildzoom/controversial-management-philosophy-part-1-d2e7d2966327?source=rss----6c77490a4b63---4</link>
            <guid isPermaLink="false">https://medium.com/p/d2e7d2966327</guid>
            <category><![CDATA[engineering-leadership]]></category>
            <category><![CDATA[management]]></category>
            <category><![CDATA[organizational-psychology]]></category>
            <category><![CDATA[engineering-mangement]]></category>
            <dc:creator><![CDATA[Alex Litvak]]></dc:creator>
            <pubDate>Fri, 09 Mar 2018 19:50:49 GMT</pubDate>
            <atom:updated>2018-07-19T23:53:54.581Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/820/1*VxueD7QBrCyyQTNK1a0MTg.png" /></figure><p>This post touches on some of my more controversial and debatable ideas on management and leadership. They stem from 8 years of experience leading engineering teams ranging from 2 to 25. While my experience has been limited to software engineering, I’m fairly certain these ideas apply to just about every trade. My explanations barely scratch the surface and every one of them deserves several paragraphs. They’re meant to provoke thought and precipitate debate.</p><p>Please share your thoughts and disagreements below. Which of these should I expand on with examples and caveats? What controversial management ideas do you have?</p><h3>Lead from behind</h3><p>Giving credit when projects succeed and taking the blame when they fail is management 101. However, you’re just paying lip-service if you’re giving credit for a project that was entirely your initiative. Start early by providing business context but letting your engineers frame the problem, propose solutions, design the architecture, etc. Mentor your team to develop a framework that helps them arrive at good decisions. Monitor their decisions to provide guidance and avoid disasters, but think of yourself as the safety net, not the lead performer. This approach results in an engaged team that has skin in the game and feels empowered to make an impact. It also prepares your organization to scale by fostering creative thinking and problem-solving at all levels of the organization.</p><h3>Who’s your boss?</h3><p>The idea that your direct reports work for <strong>you</strong> is a common misconception. If you’re a good manager, you’re working for <strong>them </strong>more often than the other way around. Once you’ve explained business context and provided general direction (20%), your primary responsibilities include clearing their obstacles, keeping them motivated, and helping them grow professionally (80%).</p><h3>Be Human</h3><p>Develop warm personal relationships with your colleagues. Let them see your humanity, your shortcomings, your fears, your irrational interests. Learn theirs. Empathize with them and give them a chance to reciprocate. Admit your mistakes. Be human. This helps develop trust and predictability (more on that later). It increases the likelihood that your colleagues will recognize that you don’t have all the right answers and will speak up when they disagree. If your job calls for split-second life-and-death decisions with no time for debate (e.g., navy seals), this advice is only slightly less relevant.</p><h3>Make Yourself Redundant</h3><p>If you cannot go on vacation without having your team take a huge productivity hit, you are doing something wrong. Proactively prepare your colleagues to perform most of your activities better than you do. Once you succeed, they’re ready for a promotion and so are you. Promotions are obviously good for everyone involved, but this is especially good for the company because hiring at the bottom is much easier than hiring at the top.</p><h3>Political Capital</h3><p>Political capital refers to the professional trust you’ve built up and the influence that grants you. It’s a finite resource. When you take risks on behalf of the company, you invest your political capital. If the bet pays off, you receive a healthy return and pave the way to make bigger bets. As you run out of capital, your ability to build consensus and lead a team deteriorates quickly.</p><h3>Interpersonal Capital</h3><p>Interpersonal capital refers to the personal trust, respect, and goodwill that your colleagues and friends have for you. When you go beyond the call of duty to help a colleague achieve their goals, you earn it. When you ask a colleague to work overtime, you expend it. As you get deeper into debt, your colleagues begin to avoid you. If those colleagues report to you, avoiding you means looking for an exit.</p><h3>Soft Power</h3><p>Understand the difference between soft and hard power. Wield soft-power frequently and hard-power rarely. Soft-power is both more effective and more sustainable.</p><h3>Micromanagement</h3><p>Micromanagement is sometimes appropriate. Your recent hires and fresh grads will learn your codebase and build confidence much faster if you give them detailed direction and set clear expectations. Review their code and provide feedback on everything ranging from structure to style and naming. Build people up before you throw them in the deep end.</p><h3>Be Predictable</h3><p>Predictability is a weakness in the context of competition. It’s a strength in the context of collaboration. Being predictable expands your influence by allowing your reports and peers to interact with the virtual you without consuming your time. If people can predict your reaction and understand your mental model, they will have the confidence to make autonomous decisions and operate more quickly.</p><h3>Break The Rules</h3><p>There are an infinite number of possible circumstances that will affect your specific situation at any point in time. Knowing commonly accepted best practices and having a mental framework will help you make decisions quickly most of the time. However, remember to maintain sufficient awareness to disengage autopilot and deviate from the rules where appropriate.</p><p>If you’re interested to develop as an engineering leader and want to work with some amazing people, take a look at BuildZoom!</p><p><a href="https://jobs.lever.co/buildzoom/5d98e8d6-b8be-4871-a1de-109af3cb204d">https://jobs.lever.co/buildzoom/5d98e8d6-b8be-4871-a1de-109af3cb204d</a></p><p><em>Credit for the image: </em><a href="http://www.twin9.co.jp/company-profile/management-philosophy"><em>http://www.twin9.co.jp/company-profile/management-philosophy</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d2e7d2966327" width="1" height="1" alt=""><hr><p><a href="https://medium.com/engineering-buildzoom/controversial-management-philosophy-part-1-d2e7d2966327">Management Philosophy</a> was originally published in <a href="https://medium.com/engineering-buildzoom">Engineering @ BuildZoom</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Tracking Email Reply CTAs]]></title>
            <link>https://medium.com/engineering-buildzoom/tracking-email-reply-ctas-a85589a2077f?source=rss----6c77490a4b63---4</link>
            <guid isPermaLink="false">https://medium.com/p/a85589a2077f</guid>
            <category><![CDATA[email-marketing]]></category>
            <category><![CDATA[email]]></category>
            <category><![CDATA[call-to-action]]></category>
            <dc:creator><![CDATA[Justin Paulin]]></dc:creator>
            <pubDate>Wed, 20 Dec 2017 00:28:20 GMT</pubDate>
            <atom:updated>2017-12-20T00:28:20.281Z</atom:updated>
            <content:encoded><![CDATA[<p>Here at <a href="https://www.buildzoom.com/">BuildZoom</a>, we occasionally send out emails asking for information in a reply. All replies are forwarded to our customer service system, where they’re handled by a real person. Limitations in this system, however, make it difficult to track which specific email is successful in soliciting replies when we send multiple emails to a single user.</p><p>In this post, I’ll be going over how we’re planning on solving this problem.</p><h3>Tracking Emails</h3><p>In order for us to track outbound emails, we need to have some way of identifying the email that can survive a reply (which can mutate the subject line, leave out the body, or come from another email address if the user has email forwarding setup).</p><p>When choosing where to include your unique identifier, you have a few different options, each with their advantages and disadvantages.</p><h3>Custom Headers</h3><p>Using custom headers as a unique identifier will involve setting a unique Message-ID header in each of your outbound emails. This will be invisible to the user (unless they look at their email headers), however, may not be as reliable as other methods, as some email clients will strip out headers when an email is replied to or forwarded.</p><h3>Custom Reply-To Email Address</h3><p>A custom reply-to email address can act as an effective unique identifier, as it will be easy to parse, and is the most reliable method, as it will not be stripped out in a reply, and will be respected by all major email clients.</p><p>An example of a custom reply-to email address would be person+YOURUNIQUEID@buildzoom.com. Everything between the + and the @ can be easily parsed and tracked.</p><p>A custom reply-to email address may, however, reveal the fact that your emails are automated, as the reply-to email will be visible to the user.</p><h3>Recipient</h3><p>You can also use the recipient as a unique identifier, but you won’t be able to attribute replies when you’ve sent multiple emails (ie. a follow-up) to a single email address. This method is much easier to implement, however.</p><h3>Hidden ID in Email Text</h3><p>Finally, you can use a hidden identifier (ie. text in white font on a white background) in the body of the email to uniquely identify emails.</p><p>This method will make it easy to attribute replies manually, however, the ID may be stripped out if the user replies to the email and doesn’t send the body of the original email in their reply. Additionally, it may look a bit suspicious if the user highlights the identifier, making it visible.</p><h3>Accessing Emails</h3><p>Once you’ve started sending out emails with your unique identifiers, you’ll need some way to read and process your replies. In this post, we’re going to assume that you’re using either an email box as a support ticket system, or are using helpdesk software (like Zendesk or HelpScout) where email replies are turned into support tickets; so we’ll go over a few ways you can silently record reply statistics, while maintaining your existing support workflow.</p><h3>POP3 Message Processing</h3><p>POP3 is a commonly used email protocol that lets you easily retrieve email from a server. We can use POP3 by forwarding inbound emails to a second email address, and enabling POP3 on that second mailbox.</p><p>We can then use a library like <a href="https://docs.python.org/2/library/poplib.html">poplib</a> or <a href="http://starship.python.net/crew/pson/pop.html">pop</a> to pull all messages from this mailbox, process them (by reading our unique identifier and saving the details), and delete them.</p><p>This method requires you to run the message checker periodically to poll the email box through a cron job or something similar; as the server maintains the message queue — We just need to process the latest messages whenever we have time.</p><p>This method will be slower in returning results (it’ll depend on your run interval), however, it’ll perform a lot better; especially when there are large numbers of emails being received.</p><h3>Piped Email</h3><p>An alternative to POP3 involves piping inbound emails into a script. When email piping is set up, when any inbound emails are received, a script will be run with the email headers and body as an input. The script can then perform various tasks using the email data.</p><p>This can be done by setting up a pipe recipe in procmail, or by using smtpd to copy all inbound emails to a specific address to a script.</p><p>You can then use the email headers and body passed into the script to determine the unique identifier and record the reply.</p><p>This method will result in much quicker results, as the script will run whenever a new email is received. On the other hand, if a lot of email is received, this method can result in poor performance.</p><h3>Conclusion</h3><p>In conclusion, the best choice for us will use custom reply-to email addresses, and POP3 message processing. This is because custom reply-to addresses are the most durable and customizable, and because POP3 message processing will allow us to easily scale and handle large volumes of email without requiring any refactoring.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a85589a2077f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/engineering-buildzoom/tracking-email-reply-ctas-a85589a2077f">Tracking Email Reply CTAs</a> was originally published in <a href="https://medium.com/engineering-buildzoom">Engineering @ BuildZoom</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Life at BuildZoom]]></title>
            <link>https://medium.com/engineering-buildzoom/life-at-buildzoom-c5447fefba09?source=rss----6c77490a4b63---4</link>
            <guid isPermaLink="false">https://medium.com/p/c5447fefba09</guid>
            <category><![CDATA[summer-internships]]></category>
            <category><![CDATA[engineering]]></category>
            <category><![CDATA[internships]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[intern]]></category>
            <dc:creator><![CDATA[Hannah Pedersen]]></dc:creator>
            <pubDate>Thu, 03 Aug 2017 22:41:03 GMT</pubDate>
            <atom:updated>2017-08-03T22:41:02.848Z</atom:updated>
            <content:encoded><![CDATA[<p>My time with BuildZoom started in the spring, as I was struggling to survive the dark months of technical interview preparation, applications, and endless interviews for software engineering internships. Unlike the impersonal interviews with so many larger companies, interviewing with BuildZoom was a stark contrast and it remained that way throughout my 12 week internship. Working for Buildzoom meant people being excited to welcome me, knowing my name, thanking me for my contributions, and inviting me whole heartedly to the team.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Fivpx11tJW64WDdU." /><figcaption>The view from BuildZoom’s 14th floor office in downtown San Francisco</figcaption></figure><p>Unlike most internships where “You get to touch real code!” (one company actually told me this) or spend the entire summer working on one large project, I got to work on many different projects, large and small, across the front and back end. Like many startups, the number of high priority projects outweighs the number of engineers, so the work that I and the other interns received were projects that BuildZoom’s engineers had in their backlog. With every production release, I had code being released to the website, allowing me to experience the joy of seeing the features I had worked on and the horrors of introducing bugs and errors. I’m thankful for being able to feel both of these extremes many times over the summer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B71cEAX7aBSw-ok6Bv0EUg.jpeg" /><figcaption>Post lunch comradeship</figcaption></figure><p>Despite the never ending list of work to do, no one hesitated to put it on hold to help me whenever I had a question, even if it was something that required hours of help. I had never worked with Ruby on Rails, AngularJS, or MySQL previously, but thanks to the never ending patience of the BuildZoom engineers, I became comfortable working in all of these realms.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AmQnoNLQatP4grq0CGsbQw.jpeg" /></figure><p>My final project, which was designing and building the backend for the communications preferences, built upon skills that I had acquired from smaller projects all summer. It involved creating a new table, querying for data to populate it, building controllers, models, and completing a large refactor of around 1,000 lines of code in the codebase. This project was challenging, thought provoking, and enjoyable, but what made it most memorable were the meetings with the product, marketing, and design teams where I was able to make suggestions and give feedback. I got to closely see the process of planning, tweaking, developing, and re-tweaking that goes into every large project, something that most internships don’t give you.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dU3PlEUeC8j-egSMHYPRKg.png" /><figcaption>Working with my mentor on the new communications preferences platform</figcaption></figure><p>I titled this “Life at BuildZoom” instead of “Life as an Intern at BuildZoom” or “Life as an Engineer at BuildZoom”, because I never felt like I was only an engineer or only an intern this summer. Life at BuildZoom meant amazing catered lunches, going clubbing with co-workers, being thrown into the deep end of writing lots of real code (and learning to swim!), getting a project that a second year engineer wanted, and learning at least 20 new acronyms about contractors and homeowners.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/952/1*1AP99n-NoKS-nevswxi5QQ.png" /></figure><p>During one of the first on-boarding meetings held for the interns, one of the employees said “If you don’t learn a ton, make friends, and have an incredible summer, we’ve failed at our job,” so let it be known- BuildZoom, you did not fail at your job. Thank you for the exceptional internship!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UTOA_4ZVKQT8nfolL06WTw.png" /><figcaption>Intern and new hire outing to an art gallery and tea tasting</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c5447fefba09" width="1" height="1" alt=""><hr><p><a href="https://medium.com/engineering-buildzoom/life-at-buildzoom-c5447fefba09">Life at BuildZoom</a> was originally published in <a href="https://medium.com/engineering-buildzoom">Engineering @ BuildZoom</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[On-demand loading of AngularJS 1.x modules]]></title>
            <link>https://medium.com/engineering-buildzoom/on-demand-loading-of-angularjs-1-x-modules-d70ee6c9bf86?source=rss----6c77490a4b63---4</link>
            <guid isPermaLink="false">https://medium.com/p/d70ee6c9bf86</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[angularjs]]></category>
            <dc:creator><![CDATA[Artem Kholodenko]]></dc:creator>
            <pubDate>Thu, 12 Jan 2017 23:35:18 GMT</pubDate>
            <atom:updated>2017-01-12T23:50:12.960Z</atom:updated>
            <content:encoded><![CDATA[<p>Single page client-side apps can quickly become bulky and full of niche dependencies. Services and components needed by non-core features start to bloat the script size and increase load times. Below is a guide based on an experiment I performed for on-demand loading AngularJS 1.x modules and leveraging the functionality of a service of the module.</p><h4>Create module and service</h4><p>The service you want to load on demand should be declared in it’s own module. In the simple example below, I declare an alerter module and as part of the module an alerterService in a file named alerter_module.js.</p><pre>angular.module(&#39;alerter&#39;, []);</pre><pre>angular.module(&#39;alerter&#39;).service(&#39;alerterService&#39;, [function () {<br>    return {<br>        showAlert: function (message) { <br>            alert(message);<br>        }<br>    };<br>}]);</pre><h4><strong>Load module via AJAX</strong></h4><p>For a simple example assume you have a user module with a userButtons component that needs to show an alert when a button is clicked.</p><pre>angular.module(&#39;user&#39;, []);</pre><pre>angular.module(&#39;user&#39;).component(&#39;userButtons&#39;, {<br>    template: &#39;<br>         &lt;button ng-click=&quot;$ctrl.onAlert(\&#39;Good!\&#39;)&quot;&gt;Good&lt;/button&gt;<br>         &lt;button ng-click=&quot;$ctrl.onAlert(\&#39;Bad!\&#39;)&quot;&gt;Bad&lt;/button&gt;&#39;,<br>    controller: [function () {<br>        this.$onInit = function () {<br>            this.alerterService = null;<br>        };<br>        <br>        this.onAlert = function (message) {<br>            if(this.alerterService) {<br>                this.alerterService.showAlert(message);<br>            }<br>            else {<br>                this.loadAlerter(message);<br>            }<br>        };<br>        <br><strong>        this.loadAlerter = function (message) {<br>           // ... details below<br>        };    </strong><br>    }];<br>});</pre><p>The first time a button is clicked this.alertService is null. This leads to this.loadAlerter(message) being called.</p><p>Inside this.loadAlerter(), load the JS file containing the alerter module and related code. I’m using jQuery’s $.getScript() shortcut method in this example. The script tag creation can also be done manually using vanilla JS. This action pulls the code remotely and makes available on the page.</p><pre>$.getScript(&#39;alerter_module.js&#39;, function () {              <br>    // ...<br>});</pre><p>Now, add the alerter module to the array of modules injected into the module where you want to use alerter functionality.</p><pre>$.getScript(&#39;alerter_module.js&#39;, function () {              <br>    // adding &#39;alerter&#39; to array of module dependencies<br>    // injected into &#39;user&#39; module<br><strong>    angular.module(&#39;user&#39;).requires.push(&#39;alerter&#39;);</strong><br>});</pre><p>Then, get a reference to the alerter module:</p><pre>$.getScript(&#39;alerter_module.js&#39;, function () {              <br>    angular.module(&#39;user&#39;).requires.push(&#39;alerter&#39;);</pre><pre>    // get reference to &#39;alerter&#39; module to <br>    // get access to its services<br><strong>    var injector = angular.injector([&#39;ng&#39;,&#39;alerter&#39;]);</strong><br>});</pre><p>Then, get a reference to the alerterService that you want to leverage and call a method of the service. Remember to bind the scope of the component to the callback, to access this.alerterService.</p><pre>$.getScript(&#39;alerter_module.js&#39;, function () {              <br>    angular.module(&#39;user&#39;).requires.push(&#39;alerter&#39;);<br>    var injector = angular.injector([&#39;ng&#39;,&#39;alerter&#39;]);<br>    <br>    // get reference to service of module<br><strong>    this.alerterService = injector.get(&#39;alerterService&#39;);</strong><br>    <br>    // call method of service<br><strong>    this.alerterService.showAlert(message);</strong><br>}<strong>.bind(this)</strong>);</pre><p>Putting it all together:</p><pre>angular.module(&#39;user&#39;, []);</pre><pre>angular.module(&#39;user&#39;).component(&#39;userButtons&#39;, {<br>    template: &#39;<br>         &lt;button ng-click=&quot;$ctrl.onAlert(\&#39;Good!\&#39;)&quot;&gt;Good&lt;/button&gt;<br>         &lt;button ng-click=&quot;$ctrl.onAlert(\&#39;Bad!\&#39;)&quot;&gt;Bad&lt;/button&gt;&#39;,<br>    controller: [function () {<br>        this.$onInit = function () {<br>            this.alerterService = null;<br>        };<br>        <br>        this.onAlert = function (message) {<br>            if(this.alerterService) {<br>                this.alerterService.showAlert(message);<br>            }<br>            else {<br>                this.loadAlerter(message);<br>            }<br>        };<br>        <br>        this.loadAlerter = function (message) {<br>           <strong>$.getScript(&#39;alerter_module.js&#39;, function () {                  <br>               angular.module(&#39;user&#39;).requires.push(&#39;alerter&#39;);<br>               var injector = angular.injector([&#39;ng&#39;,&#39;alerter&#39;]);<br>               this.alerterService = injector.get(&#39;alerterService&#39;);<br><br>               this.alerterService.showAlert(message);<br>            }.bind(this));</strong><br>        };    <br>    }];<br>});</pre><h4>Conclusion</h4><p>The above example demonstrates the ability to load and inject angular modules on demand. This minimizes the code loaded as part of the initial page load. The approach also forces the separation of non-critical code into modules for better decoupling and maintenance.</p><p><em>Note: In addition to using services, it’s possible to $compile and add to DOM components and any other pieces of a loaded module.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d70ee6c9bf86" width="1" height="1" alt=""><hr><p><a href="https://medium.com/engineering-buildzoom/on-demand-loading-of-angularjs-1-x-modules-d70ee6c9bf86">On-demand loading of AngularJS 1.x modules</a> was originally published in <a href="https://medium.com/engineering-buildzoom">Engineering @ BuildZoom</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>