<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Daniel Phan on Medium]]></title>
        <description><![CDATA[Stories by Daniel Phan on Medium]]></description>
        <link>https://medium.com/@danielphan?source=rss-fe81799f9a51------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*JRfHbCIMGVYpSS0l.jpg</url>
            <title>Stories by Daniel Phan on Medium</title>
            <link>https://medium.com/@danielphan?source=rss-fe81799f9a51------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 11:26:18 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@danielphan/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Using go doc for REST APIs]]></title>
            <link>https://medium.com/@danielphan/try-things-testing-part-1-a734215b25fa?source=rss-fe81799f9a51------2</link>
            <guid isPermaLink="false">https://medium.com/p/a734215b25fa</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Daniel Phan]]></dc:creator>
            <pubDate>Fri, 03 Jun 2016 11:19:46 GMT</pubDate>
            <atom:updated>2016-06-05T03:48:44.783Z</atom:updated>
            <content:encoded><![CDATA[<h4>Docs and tests are both communication</h4><p>Very early on while we were building the API for <a href="https://www.capsulecares.com/">Capsule</a>, we needed to both test and document the API for client developers.</p><p>Tools like <a href="http://swagger.io/">Swagger</a> were a great way to document the API, but made it possible for the docs to get out of date. It <em>was</em> possible to integrate Swagger in a way that guaranteed the docs’ correctness (e.g. generate our API from the Swagger schema), but we had written enough of API that we were reluctant to do a major overhaul just for that.</p><p>At the same time we wanted to add tests that exercised more of the API end-to-end than the unit tests we currently had. <a href="https://www.getpostman.com/">Postman</a> provided a nice way to test and share API requests and responses, but it wasn’t trivial to add it to our automated testing/continuous deployment system.</p><p>Finally, while each system would have worked fine independently, we had a feeling that these were actually the same problem, and could be solved quicker and simpler together. So we thought we’d try something a little different.</p><h4>Testing by example</h4><p><a href="https://blog.golang.org/examples">Examples</a> in Go are functions that are expected to write to <strong>stdout</strong>. They are executed as part of testing, and their output is compared to an “expected output” comment left by the developer at the bottom of the function. When running <strong>go test</strong>, if the output and comment match, the test passes. If the output and comment are different, the test fails. And, assuming the example passed, the function and its output show up nicely formatted when running <strong>go doc</strong>.</p><p>As a meta-example, this code:</p><pre><br>func ExampleReverse() {<br>    fmt.Println(stringutil.Reverse(&quot;hello&quot;))<br>    // Output: olleh<br>}</pre><p>generates these docs:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/540/0*oQO77F2MEkbbh7xx.png" /><figcaption>Two birds with one stone: the same code acting as both docs and tests</figcaption></figure><p>and this test output:</p><pre>$ go test -v<br>=== RUN TestReverse<br>--- PASS: TestReverse (0.00s)<br>=== RUN: ExampleReverse<br>--- PASS: ExampleReverse (0.00s)<br>PASS<br>ok      github.com/golang/example/stringutil    0.009s</pre><p>If we could use examples to test and document the API, then this seemed like the perfect solution:</p><ul><li>We wouldn’t need to rewrite any API code</li><li>We didn’t have to integrate 3rd-party libraries, just the standard Go tools</li><li>Our docs would always be up-to-date, because they were also tests</li><li>We wouldn’t have to introduce a separate “API schema” language</li><li>Our docs would be comprehensive, because even tests that were meant to check error conditions would show up by default</li><li>Our tests would be easy to write: they’re just comments</li></ul><h4>How exactly?</h4><p>For each request/expected response pair that we wanted to document/test, we created a new example function that makes the request to an <a href="https://golang.org/pkg/net/http/httptest/">httptest</a>.Server and prints the response. The expected output comment is then just the expected HTTP response (we actually printed the HTTP request as well, for clarity). The key was to write the example functions in such a way as to show up legibly for developers unfamiliar Go or <strong>go doc</strong>.</p><p>An example like:</p><pre>func ExampleDrugEndpoints_Get() {<br>    DrugEndpoints{}.Get() // Makes and prints the test HTTP request and response.</pre><pre>    // Output:<br>    // # Request<br>    // GET /v1/drugs/12–3456–789 HTTP/1.1<br>    // Capsule-Version: 1.2.3<br>    //<br>    // # Response<br>    // HTTP/1.1 200 OK<br>    // Access-Control-Allow-Origin: https://www.capsulerx.com<br>    // Content-Type: application/vnd.api+json<br>    //<br>    // {<br>    //   “data”: {<br>    //     “id”: “12–3456–789”,<br>    //     “type”: “drug”,<br>    //     “attributes”: {<br>    //       “brandName”: “Wolfsbane Potion”,<br>    //       “genericName”: “Aconite”<br>    //     }<br>    //   },<br>    //   “included”: []<br>    // }<br>}</pre><p>generates these docs:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*msOP0nooewuNqmYS0jImHA.png" /><figcaption>We made the examples methods of XxxEndpoint types to nicely group the request/responses in the generated go docs</figcaption></figure><p>It was pretty exciting once we got to this point, because we no longer felt like documentation or end-to-end testing was a major pain point. From then on, whenever we found a bug in the API, we could fix and document the behavior in one swoop. Also, this laid the foundation for tools that could, for example, automatically notify client developers when changes to the API were made, or prevent backwards-incompatible API changes altogether.</p><p>Quickly, the number of example tests we wrote exploded. In and of itself, this was a good indicator that the system was working, and it was usable. But, it also became tedious to manually edit each example comment when an API changed, especially as API responses became larger. Inspired by visual web app testing tools like <a href="https://github.com/Huddle/PhantomCSS">PhantomCSS</a>, we decided to automate most of the comment tweaking we encountered.</p><h4>Visual testing using git</h4><p>We noticed that the examples failed <strong>go test</strong> most often because we intentionally added, removed, or renamed fields in the API response. Also, whenever an example did legitimately fail because of a bug, the output from <strong>go test</strong> was too long to help us pinpoint exactly where the expected and actual responses differed.</p><p>Both of these problems were solved by writing a program, <strong>fixdocs</strong>, whose job was simple: whenever an example fails, replace its current “expected output” comment with whatever its actual output was. We would run <strong>go test</strong>, pipe its output through <strong>fixdocs</strong>, and instantly have updated each failing example. Of course, it seems circular to “fix” tests by replacing the expected output with the actual output, but a quick look at <strong>git diff</strong> would let us find exactly which examples changed and how they changed. If we spotted a bug, we could fix it and try diffing again. With <strong>fixdocs</strong>, it became much faster to figure out whether the API change had the intended effect, just by scanning the diffs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/798/1*B1dMF_4V11clSYDI1JTnKw.png" /><figcaption>git diff tells you exactly what API responses will change</figcaption></figure><h4>Deploying documentation and the API side-by-side</h4><p>As a finishing touch, we ran a <strong>go doc</strong> server alongside our API server which gave the API the ability to serve its own documentation. This was very useful, since the beta API server would serve the beta documentation, and the production API server would serve the production documentation, etc. By simply changing the URL from <a href="https://api.beta.capsulecares.com/">https://api.beta.capsulecares.com</a> to <a href="https://docs.beta.capsulecares.com,/">https://docs.beta.capsulecares.com,</a> you could see the guaranteed-to-be-correct docs for the API you were currently accessing, <em>because those docs were also the tests for that particular API binary</em>.</p><h4>API explorer</h4><p>The biggest opportunity we had in mind for this documentation/testing system was the ability to craft and send API requests while browsing the docs. This was also the biggest feature we were missing from Swagger.</p><p>It was easy enough to change the example functions to make arbitrary API requests if you were already editing the API code, but the experience was much less fluid than if you could do so directly in the browser.</p><p>We did find that <strong>go doc</strong> supports editing and running example code in the browser, using the <a href="https://play.golang.org/">Go Playground</a>, which seemed like it could work great as a basic API explorer. And for a while we tried to make this work for our code. Unfortunately, this feature is mostly unsupported. It only seems to work for examples in the Go standard library, and seems like it won’t get love any time soon, which is a shame.</p><h4>Mock data</h4><p>In the next post, I’ll discuss how we tried to side-step a lot of problems we usually see when defining test data (e.g. brittleness, duplication), and how we ended up with unexpected benefits, like the ability to use the same test data in unit tests, end-to-end tests, and when running the API locally.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a734215b25fa" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Do we really need to use package managers?]]></title>
            <link>https://medium.com/@danielphan/do-we-really-need-to-use-package-managers-5cab9790f27a?source=rss-fe81799f9a51------2</link>
            <guid isPermaLink="false">https://medium.com/p/5cab9790f27a</guid>
            <category><![CDATA[git]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Daniel Phan]]></dc:creator>
            <pubDate>Wed, 24 Jun 2015 07:39:56 GMT</pubDate>
            <atom:updated>2015-06-24T07:39:56.268Z</atom:updated>
            <content:encoded><![CDATA[<p>I’ve become pretty skeptical of using package managers within organizations. At Google and Foursquare, we had monolithic main repositories, and checked third-party code directly into the repository (apparently <a href="http://thread.gmane.org/gmane.comp.version-control.git/189776">Facebook</a> does this as well). This way, when you cloned, you always got all the code you needed to get up-and-running: no extra flags, no broken, half-cloned repositories. And when you made a change, you would know <em>exactly</em> which downstream dependencies you were about to break.</p><p>Asana has historically had the same practice, until recently. Our desire to have more open-source code led us to adopt more community-standard practices, such as using multiple inter-dependent repos, using semantic versioning, and letting NPM download our dependencies instead of checking them into git. And so far, with the exception of our code looking more like other open-source code, things seem worse.</p><p>There are more build breaks, and builds are less repeatable between different machines. Bad semantic version updates means that code that was working yesterday does not work today, because I ran <em>npm install</em>. Breaking changes can take days or even weeks to make it to downstream dependencies, creating huge, headache-inducing merges. And if the NPM registry is down, Travis can no longer build, since it can’t fetch dependencies, even if it can fetch the code from GitHub.</p><p>Similarly to how <a href="https://medium.com/@danielphan/git-submodules-as-single-points-of-failure-bab43e93e512">git submodules introduce single points of failure</a>, NPM packages do as well. In fact, downloading artifacts of any kind — be it git submodules, NPM packages, JARs from Maven or Ivy — introduces single points of failure into a clone or build process. If any of these systems is unavailable, your build will fail.</p><p>Checking these artifacts into your repository reduces your build’s vulnerable surface area, by reducing your dependencies on the Internet. And if you hate the idea of checking binaries into git, you can try <a href="https://git-lfs.github.com/">git lfs</a> or <a href="https://git-annex.branchable.com/">git annex</a>, etc. (a dependency on <em>GitHub and S3</em> seems strictly better than a dependency on <em>GitHub, NPM, Maven, Ivy, etc.</em>).</p><p>Fetching dependencies through a separate channel feels suspiciously similar to <a href="http://harmful.cat-v.org/software/dynamic-linking/">dynamic linking</a>—artificially loosening versioning constraints between pieces of code in ways that can affect its correctness. But it’s also unclear how to play nicely within the open-source ecosystem without using the same technologies as other open-source projects. Google had the (now seemingly unmaintained) <a href="https://github.com/google/moe-py">make-open-easy</a> tool, but it was still (ironically) complex to add to a workflow.</p><p>If you know of an organization that has a pain-free, open-source-friendly workflow, let me know.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5cab9790f27a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Git submodules as single points of failure]]></title>
            <link>https://medium.com/@danielphan/git-submodules-as-single-points-of-failure-bab43e93e512?source=rss-fe81799f9a51------2</link>
            <guid isPermaLink="false">https://medium.com/p/bab43e93e512</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[git]]></category>
            <dc:creator><![CDATA[Daniel Phan]]></dc:creator>
            <pubDate>Wed, 24 Jun 2015 06:55:58 GMT</pubDate>
            <atom:updated>2015-06-24T06:55:58.069Z</atom:updated>
            <content:encoded><![CDATA[<p>I tend to avoid git submodules, not because I have very strong feelings about them, but mostly because I’ve tried to use and understand them, but continue to have problems. When faced with the option between <em>learn more about git submodules and maybe it’ll work this time</em>, and <em>learn about a different way to incorporate git repositories</em>, it’s just more compelling to choose the latter. However, there is one detail about submodules that I do believe is a true flaw.</p><p>When cloning a git repo, each submodule must be cloned recursively. Not only is this not done by default (it must be specified with the <em>recursive</em> flag), but it introduces single points of failure into the cloning process.</p><p>Imagine that you are building a project using Travis, and you depend on two other projects using submodules. If either of those projects’ repositories are unavailable for whatever reason (e.g. either host is unavailable, bad SSH keys, etc.), <em>your build will fail</em>. The more submodules you introduce into your project, the more chances you have to fail.</p><p>Why introduce single points of failure, when you can just check the third-party code into your own repository? This way, only a failure to clone the main repository will fail the build, which is strictly better than before. I’ve had good experiences using <em>git subtree</em> and <em>git subrepo</em> to manage dependencies in this way. And in addition to ensuring a more repeatable clone step between different machines, you’ll allow developers to use just the standard git commands to get up-and-running.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bab43e93e512" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Multi-agent systems]]></title>
            <link>https://medium.com/@danielphan/multi-agent-systems-ed1b97e2aaaf?source=rss-fe81799f9a51------2</link>
            <guid isPermaLink="false">https://medium.com/p/ed1b97e2aaaf</guid>
            <category><![CDATA[management]]></category>
            <category><![CDATA[rationality]]></category>
            <dc:creator><![CDATA[Daniel Phan]]></dc:creator>
            <pubDate>Wed, 24 Jun 2015 06:42:13 GMT</pubDate>
            <atom:updated>2015-06-24T06:42:13.668Z</atom:updated>
            <content:encoded><![CDATA[<p>In January, I attended a workshop run by the <a href="http://rationality.org/">Center for Applied Rationality</a>. The stated goal of these workshops is to help individuals become more <em>rational.</em> In this sense, being more rational does not mean becoming Spock-like or emotionless, but simply getting better at <em>achieving your goals.</em></p><p>To get the most out of the workshop, one of CFAR’s recommendations was to <em>steal ideas</em>. Not (quite) literally intellectual property theft, but rather taking an idea, integrating it into your own network of ideas, retelling it in your own words — <em>making it yours</em>.</p><p>Something I stole from the workshop was a concept of modelling my decision-making processes as the interaction of different agents. <a href="https://en.wikipedia.org/wiki/Intelligent_agent">Agents</a>, in this sense, can be thought of as players in a game, each with their own (not necessarily competing) goals, motivations, and strategies. For example, when woken up by an alarm clock in the morning, there might be a few different agents:</p><ul><li>one that wants to sleep more</li><li>one that wants to go climbing</li><li>one that wants to be the kind of person that wakes up early</li><li>one that doesn’t want to be the kind of person that wakes up early using self-guilt</li><li>etc.</li></ul><p>So the night before, I might have listened to the <em>one that wants to go climbing</em>, and set the alarm for early, but when I wake up, I might listen to the <em>one that wants to sleep more</em>, and snooze away the morning. And this behavior, setting an alarm early just to snooze through it, is <em>irrational</em>, since it is ineffective at optimizing towards either goal: I didn’t go climbing and the alarm kept me from getting high-quality sleep.</p><p>My alternative explanation of what CFAR is trying to do through their rationality workshops is to provide tools and advice for how to align these different agents so that they work together, instead of against each other. One way might be to eliminate some agents altogether (e.g. convince myself that sleeping in is not important to me). Another way might be to create an environment where agents do not need to compete (e.g. plan to climb at the end of the day).</p><p>And of course, it’s not always obvious, even to ourselves, what truly motivates us. For example, I might tell myself that the only reason I’m trying to lose weight is to be healthy, but I would find it much harder to be on a diet if it didn’t make me more attractive as well. Tools for finding and delineating these agents are also something we covered during the workshop.</p><p>Finally, I’m left with the vague feeling that this agent-alignment model generalizes to even larger systems than just the ones encased in the skulls of individuals. <em>Individuals</em> might be formed from systems of motivations. <em>Teams</em> are formed from systems of individuals. O<em>rganizations</em> are formed from systems of teams<em>, economies</em> from organizations, and so on, creating huge fractals of interacting agents.</p><p>So you might call agent-alignment inside an individual the practice of <em>rationality</em>. In which case, it seems appropriate to call agent-alignment inside of a team or organization the practice of <em>management</em>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ed1b97e2aaaf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Investing in the process, not the outcome]]></title>
            <link>https://medium.com/@danielphan/investing-in-the-process-not-the-outcome-1edc1620f0fd?source=rss-fe81799f9a51------2</link>
            <guid isPermaLink="false">https://medium.com/p/1edc1620f0fd</guid>
            <dc:creator><![CDATA[Daniel Phan]]></dc:creator>
            <pubDate>Mon, 15 Jun 2015 05:23:54 GMT</pubDate>
            <atom:updated>2015-06-15T05:23:54.688Z</atom:updated>
            <content:encoded><![CDATA[<p>One of my favorite quotes comes from <a href="https://en.wikipedia.org/wiki/Jon_Finkel">Jon Finkel</a>, considered one of the greatest <a href="https://en.wikipedia.org/wiki/Magic:_The_Gathering">Magic: The Gathering</a> players of all time. When <a href="http://www.reddit.com/r/IAmA/comments/jz3u7/iama_jon_finkel_ask_me_anything/c2g9dxl">asked</a> about the similarities between Magic and poker:</p><blockquote>I think the biggest thing is the deep seeded [sic] emotional understanding that the right play is the right play regardless of outcomes. The ability to make a decision 5 straight times, lose 5 times because of it, and still make it the 6th time if it’s the right play.</blockquote><p>It was a hard choice to leave a job where the <a href="https://asana.com/jobs">perks</a> are so good that I wince when describing them to people. But when I asked myself “<em>how did I get here?”</em>, the answer wasn’t “<em>a deliberate and intentional process”</em>. The answer was “<em>I’m not sure, maybe I got lucky?”.</em></p><p>The second answer isn’t one I endorse, in the same way that I wouldn’t recommend other people play the lottery to make money, even if I had just won. So although I might have made the wrong play and done well for myself so far, I continue to have the deep-seated, emotional urge to make what I think is the right play.</p><p>And if the game is <em>to find the kind of life that I am most deeply satisfied with</em>, I think the right play is to take a page from <a href="https://en.wikipedia.org/?title=Simulated_annealing">search algorithms</a> (<em>the</em> study of finding things!) and start by looking wide.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1edc1620f0fd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A year full of trying things]]></title>
            <link>https://medium.com/@danielphan/a-year-full-of-trying-things-cf9551e36c73?source=rss-fe81799f9a51------2</link>
            <guid isPermaLink="false">https://medium.com/p/cf9551e36c73</guid>
            <dc:creator><![CDATA[Daniel Phan]]></dc:creator>
            <pubDate>Fri, 12 Jun 2015 02:01:46 GMT</pubDate>
            <atom:updated>2015-06-12T02:01:46.331Z</atom:updated>
            <content:encoded><![CDATA[<p>Up until last week, my wife and I were software engineers at <a href="http://asana.com/">Asana</a>. We’ve decided to take some time off from the conventional full-time startup lifestyle, and to find and design the kind of life we’d like to have, from first principles.</p><p>One of our goals is to document our experience over the next few months, and our goodbye announcement to the company seemed like an appropriate thing to share first.</p><blockquote>Sometimes Annie and I overwhelmed by how lucky we are.</blockquote><blockquote>- To have found someone we each love spending all our time with.</blockquote><blockquote>- To have found something we love doing.</blockquote><blockquote>- To have found such amazingly talented and constantly self-improving peers.</blockquote><blockquote>So it’s very difficult for us, but we’ve decided to do something scary: we’re leaving Asana for a year. We’ll be moving back to Canada (our home and native land), to take some time to learn, create, and experience. To make space and time (space-time?) to do things that we talk about but never get around to. To:</blockquote><blockquote>- Figure out what it means to be an aunt and uncle</blockquote><blockquote>- Write a compiler from scratch</blockquote><blockquote>- Bake ALL the bread!</blockquote><blockquote>- Make containers that have scales built in</blockquote><blockquote>- Create a smell-based dating site</blockquote><blockquote>- Try a hundred other different, terrible ideas</blockquote><blockquote>- Find and explore what opportunities exist, and choose among them intentionally</blockquote><blockquote>Asana is the very best place we’ve ever worked. And it’s hard to be leaving when things are so exciting. But timing is never perfect, and we don’t want to let perfection get in the way of taking action. That, and we totally expect that we’ll be back at Asana before the year is up.</blockquote><p>I’m still looking for ways to best articulate the feelings that compelled us to make this change. I think this is a good start: <em>if I had to program an AI that shared my exact values and life-goals, should I expect it to spend more than 1/3 of its waking hours working full-time?</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cf9551e36c73" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>