Technology

  • npm, Inc. first hires announced

    Raquel Vélez (@rockbot) and CJ Silverio (@ceejbot) are the first hires of npm, Inc., joining @izs and @seldo.

    I’m excited for this team. Congratulations to all of you!

    If you’d like to join them, they’re hiring for a Senior Ops position.

  • Upstage is back

    Upstage is my library for building web presentations. Created way back in 2010, before deck.js even existed, it’s the only presentation software I know of built on YUI. Last year I used it with getUserMedia to show multiple live demos of various tablets and phones on the big screen.

    Since YUIConf and HackSI are right around the corner, and I’m speaking at both events, it’s about time Upstage got some love for 2013. I’ve switched Upstage from an Ant-based built system to Shifter, updated it to the latest YUI 3.13.0, and rewrote most of the README. It’s open-source under the BSD license. Happy presenting!

  • Stream Concat Anti-Pattern

    NodeConf 2013 was great. You should go. I learned a lot of new stuff. Yesterday, I applied things I learned there for the first time.

    I often write code that looks like something like this:

    var stdout = "",
        child = cp.spawn(process.execPath, args);
    
    child.stdout.setEncoding("utf8");
    child.stdout.on("data", function (chunk) {
        stdout += chunk;
    });
    child.on("close", function () {
        cb(null, stdout);
    });
    

    I want to buffer stdout data that comes from the child process until it exits. Typically, I do this by appending data as it comes in (chunk) to a string variable (stdout).

    This is an anti-pattern. There’s no need for the data I’m accumulating in stdout to be a string. When I convert Buffers into strings with setEncoding("utf8"), and then append them to create an even bigger string, I’m needlessly copying data into V8 when it’s more efficient to keep it outside.

    We want to keep as much data as we can inside of Buffers, which corresponds to memory outside of the V8 heap. Node makes this easy. Since I’m collecting a lot of data here, I could do this better by rewriting that code to the following:

    var stdout = [],
        child = cp.spawn(process.execPath, args);
    
    child.stdout.on("data", function (chunk) {
        stdout.push(chunk);
    });
    child.on("close", function () {
        if (Buffer.isBuffer(stdout[0])) {
            cb(null, Buffer.concat(stdout));
            return;
         }
         cb(null);
    });
    

    Instead of taking large strings and appending them to enlarge another larger string, I can simply gather all of the Buffer chunks in an array until I’m ready to concat them all into a single large Buffer.

    If I needed to read part of the output, perhaps the last few characters, I can use the Buffer’s slice method to convert a small part of the total results to a string for inspection.

    A smart way to do this easily is to use the concat-stream module, which does this all for you.

    If you liked this, you’ll love going on substack’s stream-adventure. You’re guaranteed to learn something, so give it a try.

  • When PHP Actually Is Your First Programming Language

    I have read a few articles about how PHP is a perfectly good first language and how it should never be a first. These articles are interesting because my first programming language was PHP and I have been using it extensively since 2004.

    One thing is true: it’s really easy to get started with PHP. My first real job was creating a system to log phone call metadata from a PBX. I was able to get a working version up quickly because 90% of it was in PHP. There’s a function for nearly everything, and if there isn’t, you’ll find sample code online quickly. I had the project done in a month with very little prior experience. However, because it was so easy, it was horrible. Procedural, PHP inline with HTML, calls directly to mysql library functions, really bad logic.

    When I was brought on to develop on their flagship web application, my coding practices improved from exposure to a better codebase. The biggest improvement was when I started working with PHP code developed by a new coworker that I would later learn was heavily influenced by Java. I went from looking at objects as namespaces to a representation of something. I learned to separate logic from presentation. I started to think about code more logically. I was baffled at problems caused by PHP’s lax nature (such as when (0 == 'hey') is true). However, I had knowledgeable coworkers to explain these problems when the language wouldn’t.

    The best thing about PHP is that it takes away the need to manage “stuff” and let’s you get started right away. For me, I was able to more quickly grasp object oriented programming because PHP was more forgiving. This later helped me embrace objects in JavaScript, understand polymorphism, and become a better web developer.

    That’s also why it’s so awful. If it weren’t for my exposure to good PHP code influenced by proper programming practices, I would have remained a contributor to the vast amount of bad PHP code out there. The unfortunate problem is that most new developers who use PHP stay in that place, because their stuff works. For most jobs, that will suffice. It’s only when they move to other languages that the frustration begins.

    Making PHP my first language made it harder to grasp pointers, strong types, and memory management in other languages. It initially made it more difficult to use OOP properly. However, if I didn’t make PHP my first language, I may have been so intimidated by the “stuff” that I may have given up.

    Whatever you choose to learn as your first language, the key is to learn from great code. You will make mistakes and your language, framework, or mentor should be there to guide you. If you don’t, you’re only going to become frustrated and confused later.

    Update: There’s a good discussion about this article on Hacker News.

    My first languages were actually Applesoft BASIC followed by VB6, but nothing serious came out of my experimentations with them except for a brief stint working on ignitionServer.

  • iPhone UI Anti-Examples

    Check out the screenshots of TripLog/1040, The Athlete’s Calculator, and Handy Randy, new iPhone applications from PalmOS developer Stevens Creek Software. Via Gruber, who points out that this really isn’t a joke.