<?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 Brett Cannon on Medium]]></title>
        <description><![CDATA[Stories by Brett Cannon on Medium]]></description>
        <link>https://medium.com/@brettcannon?source=rss-50c656a92c81------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*W-6IhjTvBPogx7rpvxC70Q.jpeg</url>
            <title>Stories by Brett Cannon on Medium</title>
            <link>https://medium.com/@brettcannon?source=rss-50c656a92c81------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 13:51:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@brettcannon/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[I have moved over to Svbtle]]></title>
            <link>https://medium.com/coder-who-says-py/i-have-moved-over-to-svbtle-564d52028a65?source=rss-50c656a92c81------2</link>
            <guid isPermaLink="false">https://medium.com/p/564d52028a65</guid>
            <dc:creator><![CDATA[Brett Cannon]]></dc:creator>
            <pubDate>Sat, 22 Feb 2014 23:57:17 GMT</pubDate>
            <atom:updated>2014-02-22T23:57:17.166Z</atom:updated>
            <content:encoded><![CDATA[<h4>http://nothingbutsnark.svbtle.com/</h4><p>While Medium’s WYSIWYG editor is rather nice, it just isn’t designed for code samples (e.g. there is no way to do &lt;code&gt; tags inline, only block-level &lt;pre&gt; tags). And for someone like me who writes code-heavy blog posts on occasion, it killed it for me.</p><p>Not to say Svbtle is perfect. While the Markdown support is great and even supports some GitHub Extensions (e.g. ```…``` code blocks), Svbtle doesn’t auto-save and the Google+ support isn’t as good so as to tie into Google+authorship. But the ability to simply use Markdown is enough to make me switch.</p><p>So the new blog is at <a href="http://nothingbutsnark.svbtle.com/">http://nothingbutsnark.svbtle.com/</a> . I have emailed the Planet Python folks to update my feed, but I know they get quite busy so it might be a week before it gets updated. And for those of you who <a href="https://plus.google.com/+BrettCannon">follow me on Google+</a> or <a href="https://twitter.com/brettsky">Twitter</a>, you should know that I have moved my ghost.io posts over to Svbtle as well.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=564d52028a65" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coder-who-says-py/i-have-moved-over-to-svbtle-564d52028a65">I have moved over to Svbtle</a> was originally published in <a href="https://medium.com/coder-who-says-py">Coder Who Says Py</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The key challenges in designing a programming language]]></title>
            <link>https://medium.com/coder-who-says-py/the-key-challenges-in-designing-a-programming-language-af0a418b6302?source=rss-50c656a92c81------2</link>
            <guid isPermaLink="false">https://medium.com/p/af0a418b6302</guid>
            <dc:creator><![CDATA[Brett Cannon]]></dc:creator>
            <pubDate>Sat, 23 Nov 2013 01:01:07 GMT</pubDate>
            <atom:updated>2013-11-23T01:01:07.328Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/581/1*SLdj6VSSkqRUHX9EBrkynQ.jpeg" /><figcaption>© OpenDocs</figcaption></figure><h4>or “Why there is no (and never will be) a perfect programming language”</h4><p>Designing a programming language is hard. I have been involved with <a href="http://docs.python.org/devguide/">Python’s development process</a> as an official core developer of the language for a decade over Python’s 24 years in existence and we are still trying to get it right. I have learned a bunch of programming languages to varying levels of expertise to learn from them both as a programmer and a contributor to <a href="http://python.org/">Python</a>: C, C++, <a href="http://clojure.org/">Clojure</a>, <a href="https://www.dartlang.org/">Dart</a>, Forth, <a href="http://golang.org/">Go</a>, <a href="http://www.haskell.org/">Haskell</a>, <a href="http://www.cs.arizona.edu/icon/">Icon</a>, <a href="http://java.com/">Java</a>, <a href="http://www.ecmascript.org/">JavaScript</a>, <a href="http://www.lua.org/">Lua</a>, MIPS assembly, <a href="http://ocaml.org/">OCaml</a>, <a href="http://php.net/">PHP</a>, <a href="https://www.ruby-lang.org/">Ruby</a>, <a href="http://www.scala-lang.org/">Scala</a>, <a href="http://www.schemers.org/">Scheme</a>, <a href="http://www.smlnj.org/">Standard ML</a>, and <a href="http://tcl.tk/">Tcl</a>. None of these langauges are perfect.</p><p>Why is it so bloody hard to design programming language? I think it boils down to two overarching topics and the difficulty of finding the balance between them which will never please everyone all the time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/280/1*bN8SMrH3BVc3FuHDczp7vA.png" /></figure><h3>Maintainability</h3><p>You are either writing code, or you are fixing it. That’s (roughly) the life of a programmer.</p><p>Now this encompasses several bits. One is simply the ease of writing new code. If your language provides an easy way to write good, solid code the first time then you hopefully minimize the amount of time spent maintaining it. This is typically a balance of verbosity; type systems requiring more code and effort for the tradeoff of maintainability of having the compiler do some low-level sanity checks for you.</p><p>After that comes ease of debugging. This typically relates to what your compiler doesn’t check for you statically. How easy is it to work backwards from an exception or crash? How does the language facilitate you verifying what things are correct? How hard is it to write tests (which I’m assuming you write for all your code)?</p><p>And speaking of ease of writing tests, what the language facilitates by providing a standard library also comes into play, at least initially. If you have to write a map-like data structure from scratch or an HTTP header parser in order to make HTTP requests, your productivity is going to be low. This won’t necessarily last, however, as the community around a programming language matures and third-party projects being to flourish and evolve faster than the programming language. But the activeness of the community along with the standard library of the language to help get you going play a role in maintainability (both getting your code written and not having to maintain that code which you use).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/200/1*UQzTXhc_cx8TsVN4_uLlFA.png" /></figure><h3>Performance</h3><p>But no matter how easy a programming language makes it to write new code and later debug it, none of it is worth anything if the language is too slow. This is when flexibility is sacrificed, which can often mean sacrificing maintainability in terms of how much extra work is required to accomplish the same thing in a slower language. You can statically declare everything in your language as much as you want, but if all you can do is integer math and array usage (e.g. a Turing machine), you are not exactly going to be productive.</p><p>How you balance maintenance against performance decides what shape your programming language takes. And since you can never make the perfect programming language it will influence what programmers are attracted to your programing language.</p><p>Take C as an example. Tight, well-written C code is hard to beat short of writing assembly when it comes to performance. But I think any programmer would find it hard to argue that C code is easy to maintain compared to other options. Getting your memory management right can be tricky. Segfaults are not the easiest things to debug. The list goes on. C exemplifies maintainability sacrificed for speed.</p><p>Towards the other end of the spectrum is Python. Writing code in Python is fast and highly productive. The language is a good balance of high-level constructs along with a small core language so that an experienced programmer can keep the whole language (sans black magic) in their head. And the black magic is there to help when needed (e.g. frame access for debugging). You shouldn’t be able to segfault your program from straight Python code that doesn’t do anything segfault-y (e.g. ctypes). And the debug cycle is extremely fast thanks to the lack of a expensive compilation step. But Python’s performance is not that of C. While you can get great performance with <a href="http://pypy.org/">PyPy</a>, Python’s “good enough” speed is not always good enough for everyone in all situations.</p><p>Then you start looking at languages that try to strike a balance between high maintainability and great speed; do you pay for your program with man-hours or CPU cycles? Go seems to be the newest favourite amongst people trying to find more of a middle ground. While typed, it heavily relies on type inference and uses interface typing over inheritance typing. It has fast compilation time to have a faster iteration cycle. Memory management is handled for you. And it’s fast. But it does have the issue (for <a href="http://alexgaynor.net/">some</a>) of very explicit error handling (which if you remember Go’s systems programming pedigree makes it not surprising). Lack of generics and access to the low-level guts of the language can lead to more work to accomplish something than it would in Python.</p><p>So what’s the point of this rant? Mainly to point out that programming languages are hard and thus there is no perfect language, now or ever. When you choose a language for a project you need to weigh performance against maintainability of what you will be producing; I would say you should figure what kind of performance needs you have and what computing resources are at your disposal and then find the most productive language that meets those performance needs (e.g. why the Python community has always said to prototype in Python before worrying if you actually need to go faster than what Python + C or PyPy can provide). Also be willing to learn a new language to see if it fits your style of coding better than your current favourite language since people will never stop trying to strive for that impossible goal of the perfect programming language.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=af0a418b6302" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coder-who-says-py/the-key-challenges-in-designing-a-programming-language-af0a418b6302">The key challenges in designing a programming language</a> was originally published in <a href="https://medium.com/coder-who-says-py">Coder Who Says Py</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[My Thoughts on Go (again)]]></title>
            <link>https://medium.com/coder-who-says-py/my-thoughts-on-go-again-3228b624c946?source=rss-50c656a92c81------2</link>
            <guid isPermaLink="false">https://medium.com/p/3228b624c946</guid>
            <dc:creator><![CDATA[Brett Cannon]]></dc:creator>
            <pubDate>Sat, 02 Nov 2013 21:02:32 GMT</pubDate>
            <atom:updated>2013-11-02T21:02:32.349Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*7kbd-tVk3co-9RiilFN1TA.png" /></figure><p>A little over three years ago I blogged <a href="http://sayspy.blogspot.ca/2010/10/my-thoughts-on-go.html">my thoughts on the Go programming language</a>. At the time I said <a href="http://www.python.org/">Python</a> &gt; <a href="http://www.scala-lang.org/">Scala</a> &gt; <a href="http://golang.org/">Go</a>. I came to that conclusion by reading all of the <a href="http://golang.org/doc/">various docs available on the Go website</a> and then doing <a href="https://github.com/brettcannon/find-duplicate-files">a couple of toy examples</a>. But since I did that evaluation I have shifted from a breadth-based evaluation of various programming languages to a more depth-based one (mainly due to a lack of time and thus having to be picky about what programming languages I learn). I’m also evaluating languages for a website that I have been planning to build for years which will have both a front-end and back-end component. That means choosing two languages for the project. For the moment I’m focusing on the back-end language.</p><p>Since I’m going to be doing this project with <a href="http://www.karlschmidt.net/">a good friend of mine</a> I’m not instantly choosing Python as the back-end language. While Python would let me be the most productive, a personal programming project should be fun, useful in a utilitarian sense, and a learning experience. While doing Python web back-end stuff would of course lead to some learning, it wouldn’t be nearly as much of a learning experience if I did the project in a different language. Plus using Python might not be as useful professionally for my friend as other languages.</p><p>But what language we choose can’t suck. =) While being an admitted programming language polyglot/whore, I am also willing to admit I&#39;m an opinionated/biased polyglot/whore. That means I&#39;m not going to use just any language since I would like to think I know good language design is and thus can’t stand poorly designed languages (just ask my co-workers).</p><p>And so I am evaluating Go and Scala as potential back-end languages for this project. Both are somewhat niche compared to e.g. Java, but they are gaining users consistently. Both are being used at real world companies which scale to large user bases. Both languages are high level and so I don&#39;t have to care about things like memory management or pointer fiddling. Both languages are stable and passed their 1.0 releases. But most importantly, both languages don’t drive me bat-shit crazy.</p><p>For Scala I have been taking the “<a href="https://class.coursera.org/progfun-003/class/index">Functional Programming Principles in Scala</a>” course at Coursera to refresh my functional programming chops as well as Scala knowledge (plus another friend of mine said she would learn Python if I took the course, so there’s that =). But for Go I didn’t have an equivalent (I took <a href="http://tour.golang.org/">A Tour of Go</a> previously, but it doesn’t teach me a programming paradigm I rarely get to use).</p><p>Luckily a little coding project popped up for me that Go seemed suited for: I needed to find out what files were duplicates of each other. While not complicated or requiring any novel programming solution, that’s actually not a bad thing when evaluating a language. It means that for the mundane stuff you can see if the language functions in a way that makes sense to you and allows you to be productive. And productivity is important. For one this is a personal project so I don’t want or need it to scale to a Google service so I don’t need to consider sacrificing my time in the name of efficiency (e.g. I ain’t going to use C++ to try and eek out every cycle of performance). I also have a limited amount of free time so I don’t want to sacrifice more time than I have to (within reason, obviously, else I would just use Python).</p><p>Anyway, I ended up <a href="https://github.com/brettcannon/find-duplicate-files">coding a solution</a> over a couple of nights when I had a few spare minutes that totals a little over 200 lines. That includes both a synchronous and partially concurrent solution. There is slightly less than that number of lines of testing code as well. So nothing <em>that</em> complicated, but it did involve all the basics and allowed me to get more of a feel of the language and a refresh of what I already knew from three years ago.</p><p>What conclusions about Go did I draw? You can tell the language was designed by systems programmers who wanted to be more productive (which makes sense if you know Go’s history). From the support of passing objects by pointer to the lack of exceptions, it’s obvious Go team members come from a history steeped in C++ coding (at Google especially) where they grew tired of manually managing memory and such but didn&#39;t want to entirely want to toss out all of the practices found in C/C++. And that’s totally understandable since you don’t necessarily want to fully diverge from common practices of your target audience, else you risk not gaining users.</p><p>And yet, somewhat ironically, it isn’t the C/C++ programmers that have taken to Go but the Python/Ruby programmers. Why is that? Well, when I have watched presentations on Go and there is a die-hard C++ programmer in the audience who asks a question, it typically revolves around how to gain back some control that Go has taken away, e.g. turning off garbage collection or more pointer control. In other words the typical programmer who loves C++ doesn&#39;t want to give up any control, while Go’s entire point is to take some of that control away to try and make you more productive.</p><p>Dynamic programming language lovers, though, are used to giving up plenty of low-level control for productivity gains. And so when a language like Go comes along that is small enough to fit in your head and gives you great performance characteristics you don&#39;t mind that you are giving up some high-level constructs for low-level speed boosts. That’s why some Python/Ruby programmers are switching. It does paint Go in a different light if you take it as Python but with typing and certain performance-hurting aspects removed compared to C++ but with abilities removed to make your life simpler.</p><p>Personally, I don&#39;t buy into the trade-off wholesale. I would have to measure performance against using <a href="http://www.pypy.org/">PyPy</a> in a project before I started tossing Python aside entirely and losing it’s flexibility in the name of speed. I do see the appeal though. Nothing really jumped out at me as horribly designed or a something that would totally drive me bonkers. Probably the biggest stumbling block was coming up with some idiomatic way to have a pool of goroutines to handle the hashing of files. After that it was realizing `defer` calls are executing at function exit and not code block exit (RAII in C++ and context managers in Python spoil you). Otherwise nothing leapt out at me to cause me to want to drop the project.</p><p>In the end, Go is still a viable language to use. Yes, it requires a bit more work than Python to reach the same semantic conclusion, but it isn&#39;t enough to drive me up the wall. And the trade-offs it asks of me to write maintainable, fast code do not feel systematically unreasonable. If someone wouldn&#39;t let me use Python for a project I would present Go as an alternative.</p><p>Now I just need to see how more Scala stacks up with some other test project to see which language I prefer behind Python.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3228b624c946" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coder-who-says-py/my-thoughts-on-go-again-3228b624c946">My Thoughts on Go (again)</a> was originally published in <a href="https://medium.com/coder-who-says-py">Coder Who Says Py</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Taking the Time to Re-evaluate Assumptions]]></title>
            <link>https://medium.com/@brettcannon/taking-the-time-to-re-evaluate-assumptions-46f13da6a4ec?source=rss-50c656a92c81------2</link>
            <guid isPermaLink="false">https://medium.com/p/46f13da6a4ec</guid>
            <dc:creator><![CDATA[Brett Cannon]]></dc:creator>
            <pubDate>Mon, 09 Sep 2013 22:42:54 GMT</pubDate>
            <atom:updated>2013-09-09T22:42:54.185Z</atom:updated>
            <content:encoded><![CDATA[<h4>Or “Hey, I have enough storage to use FLAC files!”</h4><p>Up until today I never really thought about using <a href="http://en.wikipedia.org/wiki/FLAC">FLAC</a> for my personal audio files.</p><p>Yes, I still own and buy digital music (Andrea is the one with the <a href="http://www.rdio.com/">Rdio</a> account). I like supporting artists I like. Plus I’m one of those people who will create a playlist and loop on it all day, every day, for a week so I don’t need a music subscription to mix things up (plus Andrea lets me know about new stuff). So for me, I listen to music through <a href="https://play.google.com/music/listen">Google Play Music</a> or my <a href="http://www.sonos.com/">Sonos</a> through local files.</p><p>Don’t ask me why, but I realized today that my storage situation for my music files had reached a point to make it feasible to use FLAC/<a href="http://en.wikipedia.org/wiki/Apple_Lossless">ALAC</a> versions of music. Maybe it was reading an article about <a href="http://www.theverge.com/2013/8/28/4667966/nin-audiophile-mastered-version-hesitation-marks">NIN’s “audiophile master” version of <em>Hesitation Marks</em></a> on <a href="http://www.theverge.com/">The Verge</a> and the mention of Neil Young’s forthcoming music service and player which focus on uncompressed music on that made me think about it. But I realized that my old habit of always using high bitrate mp3s wasn’t really necessary and I might as well just use the lossless file formats. It’s not like storage space is an issue anymore (I use a 64 GB USB 3 flash drive to serve my music from my router), so why bother losing any fidelity? Yes, I realize that my Sonos player might not really take advantage, but what’s the point of risking fidelity loss when space is no longer an issue?</p><p>While I wait to hear back from <a href="http://store.nin.com/">store.nin.com</a> tech support on my account so I can buy the latest album, I realized that if anyone was going to have given me lossless copies of files it would be Trent Reznor . And lo and behold, I had FLAC copies of <a href="http://ghosts.nin.com/">Ghosts I-IV</a> sitting amongst my music files. I checked that <a href="https://sonos.custhelp.com/app/answers/detail/a_id/80/~/supported-audio-formats">Sonos</a> and <a href="https://support.google.com/googleplay/answer/1100462?hl=en">Play Music</a> supported FLAC (they do), and so I went ahead and started using the FLAC versions and tar.xz’ed the mp3s for cold storage.</p><p>Obviously switching to FLAC over mps3 isn’t a revelation that is going to take the internet by storm. But it does make a good story that it does pay to stop on occasion and challenge one’s assumptions about what makes sense, especially in technology. I grew up during a time when encoding an mp3 took <strong>hours</strong>. Now it encodes faster than listening to the music. I remember my <a href="http://en.wikipedia.org/wiki/Ipod#Models_and_features">second-generation iPod which was 10 GB</a> was considered big (1/6 the size of the little flash drive protruding out of the back of my router with my entire music collection). Over these years my assumptions have just continued such that I never stopped to think that my aversion to using FLAC because of its file size compared to high bitrate mp3s and the lack of high-end music components negated the need to use FLAC. But now, thinking about it, why not use FLAC? Why risk not being able to gain even a sliver of clarity at the cost of some storage space?</p><p>So stop and think about what drives a decision on occasion? Especially with technology it quite possibly has been altered or entirely nullified such that you need to re-evaluate your decision.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=46f13da6a4ec" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Offline JavaScript Testing]]></title>
            <link>https://medium.com/coder-who-says-py/offline-javascript-testing-4a535ed1e5df?source=rss-50c656a92c81------2</link>
            <guid isPermaLink="false">https://medium.com/p/4a535ed1e5df</guid>
            <dc:creator><![CDATA[Brett Cannon]]></dc:creator>
            <pubDate>Mon, 02 Sep 2013 18:35:22 GMT</pubDate>
            <atom:updated>2013-09-02T18:41:10.437Z</atom:updated>
            <content:encoded><![CDATA[<h4>Or “How I tested my file://localhost web app”</h4><p>As the creator of <a href="https://oplop.appspot.com/">Oplop</a> and maintainer of its <a href="https://github.com/brettcannon/oplop/tree/master/HTML">primary implementation in HTML + JavaScript</a>, I feel an obligation to not break anything. If I screw up then someone ends up with an incorrect account password or can’t them when they need to. Me screwing up is a bad thing.</p><p>And so to help prevent that I figured I should actually test Oplop’s web app code. Now when it comes to testing web apps there are roughly two parts to a complete testing plan: unit tests and UX/end-to-end/integration/call-it-whatever-you-want-when-you-click-buttons tests. This post is purely about the former as I am lucky enough to have a web app whose UX is simple and small enough for me to test manually (although I hope to automate even those tests some day as well).</p><p>So for these previously mythical unit tests I needed a solution which would work offline (i.e. file://localhost is where all files served from) as Oplop uses the <a href="https://developer.mozilla.org/en/docs/HTML/Using_the_application_cache">HTML application cache</a> and so fancy XHR loaders and such are not an option. I also wanted a solution that could be used in some constant integration (aka CI) solution so that I would never accidentally break anything. This led to some digging online to figure how to hell to make this happen. It essentially broke down into three parts: the unit testing library, a way to drive those tests, and a CI solution to make sure whenever a commit occurred the tests still passed.</p><p>There are a lot of JavaScript unit testing frameworks out there. You could use <a href="http://qunitjs.com/">QUnit</a> from the <a href="https://jquery.org/">jQuery Foundation</a> or go more fancy and use a project like <a href="http://visionmedia.github.io/mocha/">Mocha</a> which lets you choose your own assertion library and then provides the extra stuff to write the unit tests. In the end I went with <a href="http://pivotal.github.io/jasmine/">Jasmine</a> as it just made the most sense to my brain at first reading and didn’t try to go overboard with features.</p><p>I also tossed in <a href="https://github.com/velesin/jasmine-jquery">jasmine-jquery</a> for good measure. That gave me some assertion functions that help with jQuery code. It also gave support for fixtures. Now this was the one place where working from file://localhost to guarantee compatibility with the application cache was annoying. Normally jasmine-jquery uses XHR to load fixtures so that you can keep them in separate files. Unfortunately XHR doesn’t like file://localhost as it views <strong>every</strong> file as its own domain from XHR’s perspective. So I had to inline string constants to get the fixtures I wanted. Not horrible, but a little annoying.</p><p>With Jasmine + jasmine-jquery I was able to write my tests and use Jasmine’s test runner to make sure my tests all executed properly. But that only gets me an HTML page in a single browser to verify the tests worked. What about running in multiple browsers at once? Or how about from the command-line for CI usage?</p><p>This is where a full-feature test runner comes into play. <a href="https://code.google.com/p/js-test-driver/">JsTestDriver</a> is the old-school test driver/runner which would execute your tests on multiple browsers simultaneously and then report back the results in the terminal. I actually once used JsTestDriver with QUnit on another project so I knew I wanted something like it, but I decided to look for something newer.</p><p>I ended up finding <a href="http://karma-runner.github.io/">Karma</a> from the <a href="http://www.angularjs.org/">AngularJS</a> team. It’s the spiritual successor to JsTestDriver and works well. It uses <a href="http://nodejs.org/">node.js</a> for installation and supports all the major browsers along with file monitoring so it automatically re-runs itself upon file change. It even launches the browsers for you and shuts them down when you quit. It also includes a Jasmine interface so that in my case setup didn’t require some other package to work with the unit test framework.</p><p>With Karma I’m able to test with <a href="http://www.chromium.org/blink">Blink</a>, <a href="https://developer.mozilla.org/en-US/docs/Mozilla/Gecko">Gecko</a>, and <a href="http://www.webkit.org/">WebKit</a> thanks to <a href="https://www.google.com/intl/en/chrome/browser/">Chrome</a>, <a href="http://www.mozilla.org/en-US/firefox/new/">Firefox</a>, and <a href="http://phantomjs.org/">PhantomJS</a> (+ <a href="http://www.apple.com/ca/safari/">Safari</a> on my laptop). And all of this can be done from the command-line so that it can be used from a CI solution.</p><p>I should also mention <a href="http://casperjs.org/">CasperJS</a> which is a PhantomJS/SlimerJS-specific test runner that I considered. Unfortunately that leaves out Blink and so coverage isn’t quite as wide with what’s possible with Karma.</p><p>With a unit testing framework to write tests in and a test runner to run the tests in as many browsers as possible, that just left a CI solution. Lucikily I had already discovered <a href="https://drone.io/">drone.io</a> for Oplop previously for use with its Python implementation. Some people really like <a href="https://travis-ci.org/">Travis</a> for CI, but it’s restricted to GitHub while drone.io works with GitHub, Bitbucket, and Google Code which I appreciate since I still have projects on Google Code.</p><p>The setup was extremely simple. You can see the setup at <a href="https://drone.io/github.com/brettcannon/oplop">Oplop’s drone.io page</a>, but it’s so short I can just paste the relevant bit here:</p><pre>npm install karma<br>sudo start xvfb<br>./node_modules/.bin/karma start karma.js --single-run</pre><p>Basically you just install Karma, activate the X11 screen buffer implementation, and then run your tests once. That will run in Chrome, Firefox, and PhantomJS (including automatically downloading and using the newest version of PhantomJS!) and make sure the tests pass in all browsers.</p><p>Overall I’m happy with the outcome. I’ve already used the tests to make sure I didn’t botch anything when I moved a namespace-based solution for modularizing the JavaScript code. And by refactoring my JavaScript to minimize the UI assumptions it made I can test most of the UX without having to drive the browser itself, leaving me with minimal manual testing (still plan to look into getting Selenium running so I don’t even need to do that).</p><p>The biggest pain was simply figuring all of this out. Seems everyone has their own opinion and way to do this sort of thing so there’s no authoritative solution to go off of. Plus you have to realize that a unit test framework that includes an HTML page for a test runner won’t get you simple multi-browser testing nor a way to integrate into a CI solution. I’m glad I put the time into researching all of this, but geez the world doesn’t make it straight-forward if you don’t know what you are doing.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4a535ed1e5df" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coder-who-says-py/offline-javascript-testing-4a535ed1e5df">Offline JavaScript Testing</a> was originally published in <a href="https://medium.com/coder-who-says-py">Coder Who Says Py</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What Would it Take to Stay in the 1% of Earners Perpetually?]]></title>
            <link>https://medium.com/@brettcannon/what-would-it-take-to-stay-in-the-1-of-earners-perpetually-dc7e8e8597e6?source=rss-50c656a92c81------2</link>
            <guid isPermaLink="false">https://medium.com/p/dc7e8e8597e6</guid>
            <dc:creator><![CDATA[Brett Cannon]]></dc:creator>
            <pubDate>Mon, 19 Aug 2013 21:24:33 GMT</pubDate>
            <atom:updated>2013-08-19T21:24:33.781Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*3z8wP56--EgH2O6p.jpeg" /></figure><h4>Or “How much Do I Need to Become a Trust Fund Baby?”</h4><p>In Canada, <a href="http://www.statcan.gc.ca/daily-quotidien/130128/dq130128a-eng.htm">the top 1% of earners in 2010 made $201,400</a>. Coming from America with the biased belief that financial systems are structured to keep the rich financially well off, I wondered exactly how much (or little depending on your viewpoint) money it would take to perpetually make the equivalent of $201,400 off of investments.</p><p>In other words, how much do you need invested to become a Trust Fund Baby? You would think you could live rather comfortably if you just eked your way into the top 1% of earners in Canada, so it seems like something that would be nice to reach. It would also suggest that if you have more than the amount required you could consider doing what Warren Buffett does and donate more to charity.</p><p>First thing is to calculate an actual net income from that gross income since earned income is taxed at a different rate than capital gains. Canada has both <a href="http://www.cra-arc.gc.ca/tx/ndvdls/fq/txrts-eng.html">federal and provincial income tax rates</a>, so it can vary from province to province. Using a <a href="http://www.ey.com/CA/en/Services/Tax/Tax-Calculators-2013-Personal-Tax">tax calculator from Ernest &amp; Young</a>, I’m going to focus on British Columbia since</p><ol><li>I would be happy living in Vancouver</li><li>It’s the top line in the calculator so it’s easier to follow</li></ol><p>That means someone who made $201,400 entirely from earned income would bring home $133,315. Yes, this is a gross simplification as there could be stock, bonuses, and various other things not taxed at the same rate as earned income, but this is a blog post I’m writing for fun so I want quick and simple.</p><p>Now that we know our goal is to receive $133,315 annually from our investments, we need to know what has to be earned in gross income from our investments. Once again, to keep things simple I&#39;m going to ignore dividends and such and just assume all money is made from selling stock and thus capital gains. According to the Ernst &amp; Young tax calculator we used earlier, the tax rate on capital gains is 21.85% in BC (this is because in Canada <a href="http://en.wikipedia.org/wiki/Capital_gains_tax#Canada">the tax rate is applied to 50% of the total capital gains at the highest tax bracket you reached</a>). This means our investments must earn $177,754 to give us $133,315 after taxes.</p><p>How much should we expect from our investments? I&#39;ve heard the rule of thumb that about a <a href="http://petetheplanner.com/site/2013/01/18/what-rate-of-return-should-you-expect-on-your-investments/">7% return is reasonable</a> to expect. That would mean we need to invest $2,539,343 and make 7% forever. Now obviously the market fluctuates so some years will be good and others will be bad, but amortized over your lifetime it should (hopefully) work out. If you want to be conservative and only assume 5% then you need to invest $3,555,080 which is an increase of $1,015,737 over the 7% goal/hope.</p><p>OK, so let’s go with the lower number of $2,539,343 with a 7% rate of return to at least make this silly goal seem easier to achieve. How the heck could you end up with that amount invested, starting from scratch? Assuming that 7% is consistent, how much would you need to put away each year at some time frame to reach this lofty goal of never having to work again while still being in the 1%?</p><p>Playing with <a href="http://www.thecalculatorsite.com/finance/calculators/compoundinterestcalculator.php">a compound interest calculator</a> it seems you could reach that amount of money in 30 years (which is about how long I have until I should expect to retire at the moment) with a monthly deposit of $2,200 with 7% interest compounded annually. If you push the horizon out to 40 years (more along my wife’s timeframe) it drops the monthly investment requirement to $1050/month. Even if $1050/month made up 10% of your monthly net income you would need to bring home $10,500/month net which is $180,000/year gross income.</p><p>In other words to make this in any way achievable for the average person to reach in their lifetime they need to be making $180,000/year starting at age 25 in order to simply retire in the top 1%. Obviously most people don&#39;t reach that kind of income that early in their lives nor have the discipline to save 10% of their net income every month for 40 years. Obviously your savings rate can grow as you earn more, but still, we are talking amounts of money that most people never reach.</p><p>This is why becoming rich enough to live off of your investments is tough. The amounts required, the discipline needed, and over such long time scales are not easy to meet in the slightest. This is why it typically take a bit of luck to reach this level of wealth in the world. Or rich parents.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dc7e8e8597e6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Trying to choose a soundbar]]></title>
            <link>https://medium.com/@brettcannon/trying-to-choose-a-soundbar-224dd32709a5?source=rss-50c656a92c81------2</link>
            <guid isPermaLink="false">https://medium.com/p/224dd32709a5</guid>
            <dc:creator><![CDATA[Brett Cannon]]></dc:creator>
            <pubDate>Wed, 24 Jul 2013 22:48:06 GMT</pubDate>
            <atom:updated>2013-12-11T15:23:45.898Z</atom:updated>
            <content:encoded><![CDATA[<h4>Or “Why can’t the simplest way ever be the best way for tech, and why can’t it be cheaper and more available in Canada?”</h4><p><strong>Update/spoiler alert</strong>: I bought the Sonos Playbar and am very happy with it. A co-worker has bought the Sony soundbar and is happy. Lastly, someone on Twitter followed my advice and bought the Player and is also happy.</p><p>My wife and I watch a lot of movies and television. Now we don&#39;t have cable or an antenna, so everything we watch is either rented digitally, on Netflix, or some Blu-ray we own; yes we are actual cord-cutters since I cut in 2000 and Andrea cut in 2009. But this also means we are constantly picking up quality shows and movies which are the kind of thing that grab us enough to watch an episode or two a night or a full-blown movie. What this means is even though programming is my passion but not for Andrea, a love of a good story told through moving pictures <em>is</em> shared between us and something we can do together any day of the week without issue.</p><p>What this means is that we spend a good amount of time in front of our TV relaxing. With me being a tech nerd it also means I try to have good A/V equipment. We have a 47&quot; Vizio TV which is at <a href="https://plus.google.com/115362263245161504841/posts/UcCcGUciVSr">an optimal distance from the sofa</a> (yes, I calculated it, measured it, and pulled the sofa forward to meet the measurement). We have a ps3 partially because it’s the best Blu-ray player available. We actually <strong>buy</strong> Blu-rays because of the video quality. Our TV has passive/theater 3D which we actually use and buy videos for (it’s actually quite good; better than in the theater by a decent margin). In other words I try to keep our A/V setup up to snuff based on the space the TV is in to maximize our average nightly activity without totally blowing out the budget.</p><p>But the one place where I have felt we are currently skimping on our TV setup is in sound. Before I met Andrea I had a Sony 5.1 sound system. It wasn&#39;t much since I lived in an apartment and I was a student, but I appreciated the surround sound for video games. Unfortunately it didn&#39;t work for Blu-rays (damn you, HDCP), but I had a taste of the difference a little bit of money can buy you aurally.</p><p>So I’m interested in upgrading our sound setup for the TV. There are some restrictions, though. Being one who likes to keep things relatively picked up, the idea of having cables snaking around does not please me. Being a renter means I can’t easily rip up the carpet or anything to run cables. And lastly, Andrea doesn’t like the idea of any visible cables either (our router, modem, and NAS are in two nice-looking storage boxes to hide the wires). That means no wires running to anything past the TV stand.</p><p>Andrea also doesn’t want our A/V system dominating the room it’s in. Me being me, I disagree when it comes to the TV but I understand not wanting a big, honking piece of black glass taking up the majority of your wall. But this aesthetic extends to the speakers as well, which I <em>do</em> totally understand since speakers are not usually the most attractive things in the world. That means not having tower speakers or anything overtly large and obnoxious.</p><p>With those restraints in place, I have come to the conclusion that a decent soundbar will fit the aural bill. I have spousal approval thanks to soundbars not taking up too much space or being ugly. I like them as they are a good bang-for-the-buck solution when your TV is in a spare bedroom — and will be for the foreseeable future for us — and not in some large den or open concept living room. Soundbars make everyone happy.</p><p>If you look out there for reasonable soundbars that sound good for the price and match our constraints, there seem to be four options at the time of writing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/410/0*Wcl3MiC6Ar3N2Cy8.jpeg" /><figcaption><a href="http://store.vizio.com/home-theater/s4221wc4.html">Vizio S4221W-C4</a></figcaption></figure><p>On the low end there is the <a href="http://store.vizio.com/home-theater/s4221wc4.html">Vizio S4221W-C4 2.1 soundbar</a>. It’s US$250 and just your basic 2.1 soundbar. Nothing crazy, but affordable and sounds good for the price.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/644/0*II9U5FmG3ForZlzx.jpeg" /><figcaption><a href="http://store.sony.com/webapp/wcs/stores/servlet/ProductDisplay?catalogId=10551&amp;storeId=10151&amp;langId=-1&amp;productId=8198552921666482262">Sony HT-CT260</a></figcaption></figure><p>Next up the price chain is the <a href="http://store.sony.com/webapp/wcs/stores/servlet/ProductDisplay?catalogId=10551&amp;storeId=10151&amp;langId=-1&amp;productId=8198552921666482262">Sony HT-CT260 soundbar</a>. According to <a href="http://thewirecutter.com/">The Wirecutter</a> it’s the <a href="http://thewirecutter.com/reviews/best-budget-soundbar/">best budget soundbar</a> (their <a href="http://thewirecutter.com/reviews/best-soundbar/">not-budget soundbar</a> is US$1,500 so that category is being ignored outright). It’s US$300 and simply higher quality in terms of sound as compared to the Vizio 2.1 soundbar.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/410/0*IVeTeDoHA9y62Rtz.jpeg" /><figcaption><a href="http://store.vizio.com/home-theater/s4251wb4.html">Vizio S4251W-B4 5.1</a></figcaption></figure><p>Going one step above that is the <a href="http://store.vizio.com/home-theater/s4251wb4.html">Vizio S4251W-B4 5.1 soundbar</a>. Yes, it’s a 5.1 system for USD 330. The subwoofer is wireless and the rear speakers just plug into the sub (no separate power cables) so getting to hide the speaker under the sofa passes the wife + renting wireless test. It’s also the step-up pick by The Wirecutter in their budget soundbar category.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*rfkaO2kYu8Bklg9O.jpeg" /><figcaption><a href="http://www.sonos.com/shop/products/playbar?lang=en-us&amp;region=us">Sonos Playbar</a></figcaption></figure><p>Lastly, there is the <a href="http://www.sonos.com/shop/products/playbar?lang=en-us&amp;region=us">Sonos Playbar 3.0 soundbar</a>. It’s USD 700, making it the most expensive pick in the bunch (which is why it’s listed as an alternative on The Wirecutter as “<a href="http://thewirecutter.com/reviews/best-soundbar/">Best Soundbar</a>” for music lovers pick). It is only on this list for three reasons:</p><ol><li>We already have a <a href="http://www.sonos.com/shop/products/play3">Sonos Play:3 speaker</a> and love it enough that we plan to expand the system with more speakers so we are already invested in the ecosystem and plan to invest even more</li><li>It’s expandable to 5.1 (albeit in a very expensive way and with other caveats)</li><li>The sound quality is supposed to be fantastic</li></ol><p>A lot of reviews knock the Playbar for its lack of subwoofer/bass response; you need to get the <a href="http://www.sonos.com/shop/products/sub">Sonos Sub</a> at USD 700 for that. If you toss in two Play:3 speakers — for another 2 x USD 300 = USD 600 — you now have <a href="http://www.hometheater.com/content/sonos-playbar-soundbar-page-2">a 5.1 soundbar system with great sound quality</a> … after spending USD 700 + USD 700 + USD 600 = USD 2,000. It’s definitely a way to slowly build your way up to a high quality 5.1 system, especially if you want to replace Play:3 speakers around your house with <a href="http://www.sonos.com/shop/products/play5">Play:5 speakers</a> over time, but rather pricey in the long run (although Home Theater claims it’s worth the price).</p><p>But one of the real knocks for this idea of eventual expansion — there’s another I will get to in a bit that is more general to soundbars — is the Playbar cannot decode DTS which a lot of Blu-rays have as their primary English audio track encoded in. That makes the idea of upgrading to 5.1 very unappealing and in no way a goal beyond perhaps adding the subwoofer.</p><p>Which brings us to a fundamental issue with soundbars: they pretty much universally expect you to take the sound output from your TV (typically optical audio out). This is a problem as many TVs don’t pass through audio from other inputs into the TV in anything but 2.0 audio. Now in our case our TV will pass through Dolby Digital or PCM which isn&#39;t perfect as Blu-rays typically use DTS which means it will be downsampled to 2.0 audio. While the Playbar will be fine with that, it does mean the other 3 soundbars would lose out on the subwoofer channel if provided. This is obviously minor, though, since the other soundbars will simply try to deduce what the subwoofer should output based on sound levels.</p><p>The only way to really work around this issue is to either manually swap the audio cable between your multiple devices or only have a single device for all of your A/V needs. As of right now we only use the ps3 so we could directly pipe the optical audio from the ps3 to the soundbar and thus skip any bad downsampling to stereo PCM or Dolby Digital 2.0 when that would make a difference. This is only something to care about if your soundbar is 5.1 (e.g. the high-end Vizio soundbar) or could be in the future (e.g. the Playbar).</p><p>Home Theater has a <a href="http://www.hometheater.com/content/tvs-and-soundbars-need-make-music-together">short article</a> on this issue in case I did a bad job explaining it. But in reality this only affects soundbars in a 5.1 setup since taking a stereo signal for a 2.1 system to 2.0 is not a big deal.</p><p>The other issue with soundbars is that I live in Canada. That means the prices for me (without a trip over the border) are actually higher:</p><ul><li>The Vizio soundbars are <strong>not even available</strong>, only older models</li><li>The Sony goes from USD 300 to CAD <a href="http://store.sony.ca/webapp/wcs/stores/servlet/ProductDisplay?catalogId=100803&amp;storeId=20153&amp;langId=200&amp;productId=8198552921666482262#overview">350</a></li><li>The Sonos goes from USD 700 to CAD <a href="http://www.sonos.com/shop/products/playbar?lang=en-us&amp;region=ca">750</a></li></ul><p>So I can either go across the border and hope that the border agent on the way back in doesn’t tax me, buy one on the way back from a a two night stay in the States, or I pay the slightly higher prices and give up on Vizio. Typically I pay the higher price when possible to support companies that put in the effort to actually be here in Canada and to have an actual warranty.</p><p>Any Americans reading this, realize that not everything you have is available up north and it is annoying.</p><p>Where does all of this leave me and my wife in terms of making a decision in our specific case? Basically it’s either Sony or Sonos, and it’s up to Sonos to beat the Sony in some price/quality comparison. Thanks to the lack of DTS and TVs typically not passing 5.1 sound through their audio out, the Sonos is competing on its own/potential CAD 1,500 3.1 soundbar system, not as a potential CAD 2,160 5.1 system (and that potential part is key since I won&#39;t be spending that kind of cash at once, otherwise I would consider shelling out all of the cash upfront and get the <a href="http://www.futureshop.ca/en-CA/product/martin-logan-martinlogan-motion-vision-sound-bar-vision/10208267.aspx">CAD 1,650</a> <a href="http://www.martinlogan.com/motionSeries/models/soundbar.php">MartinLogan Motion Vision</a> as recommended by <a href="http://thewirecutter.com/reviews/best-soundbar/">The Wirecutter</a> and <a href="http://www.wired.com/reviews/2012/07/reviews_soundbars/?pid=2528&amp;viewall=true">Wired</a>).</p><p>I don&#39;t think the quality of sound is really in dispute. Outside of <a href="http://reviews.cnet.com/surround-speaker-systems/sonos-playbar/4505-7868_7-35609166.html">C|Net</a>, everyone thought the sound was great: <a href="http://www.engadget.com/2013/03/25/sonos-playbar-review/">Engadget</a>, <a href="http://www.trustedreviews.com/sonos-playbar_Surround-Sound-System_review">TrustedReviews</a>, <a href="http://www.hometheater.com/content/sonos-playbar-soundbar">Home Theater</a>, <a href="http://www.theverge.com/2013/3/5/4067116/sonos-playbar-review-wireless-audio-invades-your-living-room">The Verge</a>, <a href="http://www.pcmag.com/article2/0,2817,2416616,00.asp">PC Magazine</a>. They all agree that adding the subwoofer takes the sound up a large notch to really great quality, but on its own it’s still very good. They also all pointed out the optical audio out issue with televisions, but since I&#39;m ignoring the 5.1 surround option that’s not an issue for me.</p><p>What it really comes down to is price. The Playbar is CAD 400 more than the Sony. That seems like a lot, but the key thing here is that our household has already bought into the Sonos ecosystem, which means if we decided we wanted a Sonos system in the TV room, that would be CAD 330 for a Play:3. That makes the price difference about CAD 70 between the Sonos and the Sony which is roughly a tax on having one less device in the TV room.</p><p>And so there you have it. For at least our household, it’s either the Sonos Playbar if my wife and I decide we would like a Sonos device in the TV room, else it’s the Sony. Time to have a talk with my wife to see how much she truly loves her music around the house. =)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=224dd32709a5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why I will never fit in amongst C++ programmers]]></title>
            <link>https://medium.com/coder-who-says-py/why-i-will-never-fit-in-amongst-c-programmers-3616b9e389c9?source=rss-50c656a92c81------2</link>
            <guid isPermaLink="false">https://medium.com/p/3616b9e389c9</guid>
            <dc:creator><![CDATA[Brett Cannon]]></dc:creator>
            <pubDate>Sun, 21 Jul 2013 17:59:11 GMT</pubDate>
            <atom:updated>2013-07-21T17:59:11.816Z</atom:updated>
            <content:encoded><![CDATA[<h4>Or “Why I don’t give a crap about your types and neither should you”</h4><p>I have been actively programming in <a href="http://www.python.org/">Python</a> for nearly 13 years, ten of which have been spent as a core developer of the language itself. You could say I have an opinion when it comes to programming languages. That I have taken sides. That I am a programming language bigot.</p><p>And you would kind of be right. Obviously I have chosen as my programming language of choice when I can use it. But I’m also not going to sit here and claim that Python is the best solution to every problem, nor that people have to like Python. All I ever ask of people who prefer to use other programming languages is to have a justification as to why and it be rational.</p><p>But when I run into people who can’t justify why they prefer to do something in a programming language a certain way without being able to give me what I view as a rational explanation, it drives me up the absolute wall. It’s baseless ignorance that has typically led to people bashing Python or dynamically typed programming languages back when Python wasn’t known by at least name to every programmer out there. I have a bachelors in philosophy and a Ph.D. in computer science: do not give me crap logic in general, and especially not when it comes to programming.</p><p>And yet this happens regularly. For some ironic reason,programmers are curmudgeons like everyone else when something new is presented to them. Programmers work on a daily basis with technology that has revolutionized the world, with tools that they can update on a whim, and yet when provided an opportunity to try something new which could improve their programming lives or just simply be different, they push back. “I like things just as they are.” Yes, my non-programming friends, programmers can often fear change (or at best be indifferent to the point of inaction).</p><p>The latest example I came across involves <a href="https://en.wikipedia.org/wiki/C%2B%2B11">C++11</a> and its new <a href="https://en.wikipedia.org/wiki/C%2B%2B11#Type_inference">‘auto’ keyword</a> after a conversation I had with some long-term C++ programmers (I have only been using the language part-time for a year so I typically defer to others for coding best practices). For those of who you don’t know, the ‘auto’ keyword does basic type inference for local variables where the type can be inferred by the return type of some expression (e.g. a function call’s return value). For those used to languages that provide type inference it brings C++ in line with languages where you only have to care at API boundaries where types act as documentation/contract (which I’m totally fine with, as are the language designers of <a href="http://www.scala-lang.org/">Scala</a>, <a href="http://golang.org/">Go</a>, <a href="http://www.dartlang.org/">Dart</a>, etc. just to name some newer examples). And for people used to dynamically typed languages this is just one less place to care about types which you have already learned how to live without (hopefully happily).</p><p>But when I suggested using ‘auto’ in a situation like this:</p><pre>void UppercaseSomething(SomeStruct&amp; thingy) {<br>  // …<br>  auto uppercase_something = thingy.get_something();<br>  UppercaseString(&amp;uppercase_something);<br>  thingy.set_something(uppercase_something);<br>  // …<br>}</pre><p>I had a bunch of C++ programmers tell me they disagreed with my usage of ‘auto’ for the ‘string’ type. I was given two reasons why this disagreement existed:</p><ol><li>The ‘string’ type is too “simple” to bother inferring</li><li>The ‘string’ type is short enough to not need the use of ‘auto’</li></ol><p>Up to this point using ‘auto’ seemed reasonable to everyone for complicated types and types whose names were a pain to type out. But to someone like me who prefers to live in a world where the only types I ever care about are at the API boundary, being told ‘string’ is too ‘simple’ and/or short for ‘auto’ just flabbergasted me.</p><p>When I was told the type was too “simple”, in actuality it was meant to mean that it’s so fundamental to the language that everyone knows it and so knowing it’s a string means everyone knows exactly what methods are available, what it’s memory model and semantics are, etc. By forcing people to write ‘string’ it lets people know they are working with something for which they can <em>probably</em> skip reading the docs for.</p><p>To this I said “seriously?!?” and then tried to properly articulate myself. To start, if you think ‘string’ is so fundamental, what about ‘iterator’? Everyone seemed fine with using ‘auto’ for the ‘iterator’ type from the STL, and yet that API is rather fundamental in C++. Same goes for ‘vector’. This judgment call over what is or is not fundamental to C++ is just that: a judgment call. Every other language that I know which provides type inference in no way encourages people to bother specifying the type for “simple” things like strings. In other words this classification of ‘string’ being so fundamental seemed borderline arbitrary and not to have proven itself as critical in any other programming language out there that provided type inference.</p><p>But this also assumes you can’t infer the ‘string’ type from either the expression setting the variable or its use around it. In the example above I think ‘UppercaseString()’ gives the type away. But if not knowing what type a variable is by strictly reading it truly bothered you then you probably shouldn’t be using ‘auto’ anywhere to begin with (this is where Java programmers might be chuckling to themselves).</p><p>I would also argue that using ‘auto’ makes the key points about variable access of the type more clear. If you had to read ‘const string&amp;’ and ‘const auto&amp;’ I would argue they are at worst equally readable to deduce that the variable holds a const reference, and at best the auto usage is easier as you don’t have to mentally ignore ‘string’ to understand that it doesn’t use pointer access.</p><p>And addressing the length argument for typing also seems arbitrary. If I already typed out ‘auto’ then who cares about the length? It’s still shorter anyway. But this argument was more for something like ‘int’. Okay, save a character, but is that really all that important? What about when that ‘int’ turns into a ‘long’, or a ‘long long’? Now you have no savings or actually typed more.</p><p>And even more important, what about refactoring? At least using ‘auto’ you have a chance of cutting back on refactoring errors as you don’t have to care about local variables, only non-compatible API boundary types no longer working.</p><p>In other words, it felt like I was back in a world where people made claims about a change they were introduced to without having evidence to back up those claims. The only argument I could possibly buy into is the whole “everyone knows what a string is, so when you know that’s the type you should just go ahead and type it”, but even then, switching back and forth between ‘auto’ and types deemed too simple seems like it would be mentally annoying at best and taxing at worst as a judgment call I would have to think about every time I touched/wrote a variable declaration. In other words not worth it in my book to make the distinction, especially as no other language with type inference that I have come across has suggested that kind of selective type declarations for local variables.</p><p>If people have suggestions or alternative explanation as to why everyone else was right I’m quite ready to listen and try to understand because I would like to understand in order to be a better C++ programmer and to understand the mindset.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3616b9e389c9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coder-who-says-py/why-i-will-never-fit-in-amongst-c-programmers-3616b9e389c9">Why I will never fit in amongst C++ programmers</a> was originally published in <a href="https://medium.com/coder-who-says-py">Coder Who Says Py</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Writing a Zip File Importer: The Loader]]></title>
            <link>https://medium.com/implementing-a-zip-file-importer-in-python/writing-a-zip-file-importer-the-loader-c84fc90f7f5d?source=rss-50c656a92c81------2</link>
            <guid isPermaLink="false">https://medium.com/p/c84fc90f7f5d</guid>
            <dc:creator><![CDATA[Brett Cannon]]></dc:creator>
            <pubDate>Sat, 22 Jun 2013 00:16:52 GMT</pubDate>
            <atom:updated>2013-06-22T00:16:52.498Z</atom:updated>
            <content:encoded><![CDATA[<h4>Part 3</h4><p>For a zip file-based <a href="http://docs.python.org/3/glossary.html#term-loader">loader</a>, luckily there is not much heavy lifting. Working with the finder led to the realization that zip files use <strong>/</strong> while users use whatever the hell they want for a path separator. With that path-managing code already dealt with, that leaves just implementing the <a href="http://docs.python.org/3/library/importlib.html#importlib.abc.SourceLoader">importlib.abc.SourceLoader</a> ABC. That requires implementing the following methods:</p><ul><li>get_filename(fullname)</li><li>get_data(path)</li><li>(optionally) path_stats(path)</li></ul><p>Not much, eh? This is the kind of thing that I view as the crowning achievement of importlib (that and making import maintainable in pure Python code). By staring at the interfaces originally defined in <a href="http://python.org/dev/peps/pep-0302/">PEP 302</a> for literally years, I have slowly been able to refine the algorithms involved with import (and especially loading) to the bare essentials that are absolutely required at any one point based on what may vary between uses (e.g. storage mechanism, variance of the language, etc.).</p><p>In the case of zip files, the three methods to be implemented are rather easy. The get_filename() method just returns what was given to the constructor. The get_data() method needs to translate paths which use OS-specific path separators and possibly starting with the zip file path as a prefix, but otherwise it’s just reading from a zip file and returning the bytes. The path_stats() method is actually quite easy thanks to <a href="http://docs.python.org/3/library/zipfile.html#zipinfo-objects">zipfile.ZipInfo</a> objects. Implement those three methods and you have source loading along with optional bytecode loading (toss in set_data() and you have bytecode writing as well, but for overhead reasons I didn’t implement it).</p><p>And that’s it! If you look back through the collection of posts on this topic you will find posts on the path hook, finder, and this loader post which cover (roughly) everything needed to make a zip file importer in pure Python come about. Issue <a href="http://bugs.python.org/issue17630">#17630</a> is tracking the issue of whether this code should end up in the stdlib (it’s a little sticky with zipimport there for backwards-compatibility and bootstrapping reasons, so a pure Python importer is duplicate functionality, albeit with easier maintainability and some niceties).</p><p>One thing that I should mention which is not critical to understanding how I went about writing a loader for zip files is a quirk of trying to work with bytecode files in zip files. It turns out the zip files only keep a modification time at a <strong>two</strong> second granularity. Yes, you read that right: two seconds. That means only even numbered seconds are possible. Unfortunately that’s troublesome as importlib as of this writing does just a straight numeric comparison of the timestamp found in the bytecode file and what path_stats() returns for the source file. In order to properly support bytecode files in order to minimize ignoring them I will probably have to extend the API for loaders to add a compare_mtime(source, bytecode) in order to support a one second fuzziness factor which can be overridden for this special case.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c84fc90f7f5d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/implementing-a-zip-file-importer-in-python/writing-a-zip-file-importer-the-loader-c84fc90f7f5d">Writing a Zip File Importer: The Loader</a> was originally published in <a href="https://medium.com/implementing-a-zip-file-importer-in-python">Implementing a Zip File Importer in Python</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Writing a Zip File Importer: The Finder]]></title>
            <link>https://medium.com/implementing-a-zip-file-importer-in-python/writing-a-zip-file-importer-the-finder-183a8410bd27?source=rss-50c656a92c81------2</link>
            <guid isPermaLink="false">https://medium.com/p/183a8410bd27</guid>
            <dc:creator><![CDATA[Brett Cannon]]></dc:creator>
            <pubDate>Sat, 22 Jun 2013 00:14:50 GMT</pubDate>
            <atom:updated>2013-06-22T00:14:50.704Z</atom:updated>
            <content:encoded><![CDATA[<h4>Part 2</h4><p>When writing a <a href="http://docs.python.org/3/glossary.html#term-finder">finder</a> like <a href="http://docs.python.org/3/library/importlib.html#importlib.machinery.FileFinder">importlib.machinery.FileFinder</a> it’s all about working with the storage mechanism you are trying to support. In this instance it’s making sure that the paths the finder is meant to work with are serviceable and being able to check if certain files exist.</p><p>In the case of zip files, it’s tricky because the paths use <strong>/</strong> as a path separator. If someone on Windows adds a path on sys.path using <strong>\</strong> as the path separator, you need to support it. That means constantly translating between OS-specific paths and zip file paths.</p><p>Once you have that down, then you need to implement your finder so that when you are asked if it can provide a loader for a module, it can search within the zip file in the directory the finder is meant to be in charge of to see if a source file can be found. This is the core of a finder and it takes a few steps:</p><ol><li>See if a directory matches the tail end of the module being search for, and if it does record that fact</li><li>If a directory exists then check for an __init__.py file for it to be a real package</li><li>If no real package is found, check for a .py file</li><li>If none of that works but a directory was found then return it as a portion of a possible future namespace package</li></ol><p>All of this roughly translates into the following code:</p><pre>def find_module(self, fullname):<br>  portions = []<br>  module_name = fullname.rpartition(‘.’)[-1]<br>  directory = self.make_directory_path(module_name)<br>  if self.has_directory(directory):<br>    portions.append(directory)<br>    package_base = self.join(directory, ‘__init__’)<br>    for suffix in SOURCE_SUFFIXES:<br>      package = package_base + suffix<br>      if self.has_file(package):<br>        return self.loader(fullname, package), portions<br>  module_base = self.make_file_path(module_name)<br>  for suffix in SOURCE_SUFFIXES:<br>    module = module_base + suffix<br>    if self.has_file(module):<br>      return self.loader(fullname, module), portions<br>  return None, portions</pre><p>The tricky bit that came out of implementing this general algorithm for zip files is that there are the paths you are using to look in the zip file (i.e. paths with only <strong>/</strong> as the path separator) and then the paths that you are shuttling around to support people passing in OS-specific paths. In other words the paths coming in and going out of that method need to use the path separators that the user chose to work with while everything internal needs to work with <strong>/</strong> only (and on top of that have directories all end in <strong>/</strong>). And it’s that weird dichotomy of directories that prevents importlib from having just a generic algorithm with some abstract API for which finder subclasses can simply override.</p><p>Luckily people don’t typically muck with the storage mechanisms of module code. I’m willing to bet something like 95% of all code is backed by files on a filesystem, 4% are zip files, and the rest is some exotic, custom finder (e.g. SQL database). So for most people writing custom importers it’s going to come down to the loader and not the finder.</p><p>Anyway, that’s the key points for handling a zip file finder: managing paths and re-implementing the basic algorithm for determining if a package, module, or namespace exists for the requested module.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=183a8410bd27" width="1" height="1" alt=""><hr><p><a href="https://medium.com/implementing-a-zip-file-importer-in-python/writing-a-zip-file-importer-the-finder-183a8410bd27">Writing a Zip File Importer: The Finder</a> was originally published in <a href="https://medium.com/implementing-a-zip-file-importer-in-python">Implementing a Zip File Importer in Python</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>