"programmingblog" entries

Javascript without the this

Using closures in a different way

One of JavaScript’s many wrinkles is the way that this works. It can be quite confusing, since the semantics are quite different from the purely lexical scoping rules which apply for regular variables in JavaScript. What this references can often be totally unrelated to the lexical scope of a function. To work around that we often see tricks like:

function blah(){
  var that = this;
  somethingThatRebindsThings( function(){
    that.whatever();
  });
}

Anyone who’s done much JavaScript development has felt this pain. Imagine if that was never needed. How could we get there? Well, one way would be to just never use this. Sounds crazy? Let’s see.

Read more…

The new PHP

PHP's experiencing a renaissance, with improvements and new standards

elephant-2
The programming language many love to hate is experiencing a renaissance. This is not your parents’ PHP. The new PHP is a more mature language with community standards, a growing affinity for interoperable components, and a passionate movement to improve performance. If you have bypassed PHP for alternative languages, or if you are a PHP veteran unaware of recent changes, you owe it to yourself to give PHP a second look.

Language Features

PHP 5.5 (the latest stable build as of this writing) has made major progress from earlier versions. Recent PHP releases contain powerful new features and helpful developer tools, such as a built-in web server, generators for simpler iteration, and namespaces. With PHP 5.4, traits were introduced (a la Scala or Perl) to allow code reuse in single inheritance languages, as well as closures, which allow you to code PHP in a functional style. Other important features include the built-in FastCGI process manager and phpdbg debugger, and a new password hashing API that makes it easy to hash and securely manage passwords in PHP.

Read more…

Implementing hypermedia clients: it’s not rocket science

Not ugly, not complicated

hypermedia

At Fluent 2013, O’Reilly’s Web Platform, JavaScript and HTML5 conference, Layer 7 Principal API Architect Mike Amundsen demonstrated how to build hypermedia clients, for situations with and without humans in the driver’s seat.

(If you’d like to know more about hypermedia in general, this interview provides more background.)

In his talk, Implementing Hypermedia Clients: It’s Not Rocket Science, Mike explored how hypermedia approaches drive conversation between clients and servers, and the application structures that result from those structures.

  • 1:44 – “The Semantic Gap: Hypermedia tells us what we can do, but it doesn’t say why.”
  • 6:04 – Hypermedia and application control information – links!
  • 8:09 – Control factors – “I accept RSS, can you give me RSS?”
  • 10:41 – Foundations of the class scheduling domain example
  • 16:30 – “What is a hypermedia client that a human would use?”
  • 19:24 – “Faithful Hypermedia Clients (FHCs) pass along whatever the server returns, and lets a human sort it out.”
  • 31:20 – “So what’s a Hypermedia for machine client?… Makes choices, not waiting for a human”
  • 33:25 – Working with Maze+XML
  • 37:10 – The power of generic types

If the Web Platform, JavaScript, and HTML5 interest you, consider checking out our growing collection of top-rated talks from Fluent 2013.

Delegation patterns in Ruby

Reducing object bloat begins with doing less

[inlinetweet]In almost every project there are those objects which seemingly get involved in every aspect of the application.[/inlinetweet] These are the so-called “god objects”: they can do everything (omnipotent), they know everything (omniscient), and they are everywhere in the application (omnipresent). Most often these are objects which are at the intersections of business logic: User or Account, Project, and Order are all usual suspects.

One of the core tenants of object-oriented programming is that large problems are made up of many smaller problems, and as such, can be solved by providing solutions to those smaller problems in the form of objects. [inlinetweet]God objects violate this core tenet by trying to be one solution for too many problems.[/inlinetweet]

[inlinetweet]A typical example of a god object is the `User` model of many Rails applications.[/inlinetweet] Here, `User` might be responsible for user specific information as well as knowing about phone numbers, emails, profile information, preferences, and handling authentication. It’s too much and it results in `User` being coupled to every aspect of the application.

Although there are many tactics a developer can employ to limit the influence of these “god objects”, one of the simplest is just reassigning some of their responsibility, and using a delegation pattern may be the simplest way to do just that.

[inlinetweet]Let’s look at four different ways we can use delegation in Ruby.[/inlinetweet]

Read more…

Debugging for beginners: a response

Practical strategies for debugging

This is a follow up to Brian MacDonald’s post on Debugging for Beginners. I read Brian’s post avidly, as I am always keen to take a look at different approaches to finding those elusive problems that plague all programmers (even those with decades of experience) from time to time.

Anyhow, I have to admit that I was a little bit…disappointed. You see, Brian writes in a wonderful, readable way, about topics that concern all programmers, whatever their background. But, I found that the general focus of his article was less on how to debug (even at a higher, theoretical level), and more about how to make fewer mistakes.

Read more…

Why polyfills matter

The changing landscape of web platform extensibility

From its nascent days, the growth of the web has been marked by the waxing and waning of technologies, frameworks and ideas. Old ideas and technologies expire and fade away, and new ones arise in their place. Much as the cicada molts and leaves behind an old shell as it moves into adulthood, the web has seen countless ideas come and go as it has evolved.

Relics (and Catalysts) of the Web

Remember XHTML? More specifically, do you remember caring deeply about XHTML? You likely do. Do you still care about XHTML? Chances are, the answer is no. The same goes for Flash, DHTML, HTML Components and countless other buzzwords of the web that once felt so alive and important, and now feel like relics of another time.

Occasionally, however, we collectively stumble upon ideas and technologies that stand the test of time. These are ideas that don’t just evolve with the web–they are often a catalyst for the evolution of the web itself. Ideas like Cascading Style Sheets and XMLHTTPRequest, the vendor hack that spurred the AJAX revolution, are two examples among many.

Read more…

Prototype and adapt with the MEAN stack

Rapid web development with MongoDB, Express, AngularJS, and Node.js

Web development may seem like a bustling space where everything changes every 5 minutes, but, in reality, the fundamental high-level concepts of building a web application haven’t changed much since the introduction of Ajax. The libraries and concepts, like the MEAN stack, that people have built up around HTTP and browser-side JavaScript simply provide abstractions to help people build sophisticated browser-based tools more easily. However, the fundamental challenges of web development remain mostly unchanged, and the ultimate arbiter of the value of a web development framework is how easily it enables you to overcome these challenges. In this article, I’ll highlight what I believe to be the fundamental categories of web development problems, and how the MEAN stack, consisting of MongoDB, Express.js, AngularJS, and Node.js, helps you solve them.

Problem 1: Prototyping, or, how do I build the damn thing?

With the growing popularity of the lean startup model, the pressure to shorten product development cycles and churn out a prototype application quickly and cheaply has never been greater. And, as developers, we’re doing this better at an exponential rate. Projects that once required hundreds of millions of dollars of capital in the late ‘90s became projects that you could build in a month or two with a couple tens of thousands of capital at a startup accelerator around 2008. Now, these sorts of projects are being churned out at hackathons around the country in a matter of days. As great as this seems, we can do better.

Read more…

Bitcoin is an open network that exhibits resilience and anti-fragility

Over time, crypto-currency networks such as bitcoin will get stronger

If a crook gets access to the credit card or wire transfer networks, it’s a disaster. That’s because, as I explained in my recent article about security models, these traditional financial networks achieve trust by excluding bad actors through access control. Effective access control requires exclusivity and strict vetting, only a small carefully vetted group of “trusted actors” are granted control.

Bitcoin and other crypto-currencies based on the blockchain invention are different. Trust is based on computation, not access control. On the bitcoin network you trust math so everyone can have access. That also means that there will be bad actors, arguably just as there are on access control networks, and nuisance attacks. Fortunately, these types of attacks cannot affect the distributed asset ledger, the blockchain, because to achieve the level of trust to write into the ledger you must apply enormous distributed computation. The root of trust is in the majority of computing power, not individual actors or any central authority.

Read more…

Talking to Chromecast from iOS

Part One: Easily find Chromecast devices on your local network

Now that Google has opened up the Chromecast API for anyone to play with, it’s possibile to create iOS applications that can leverage the $35 device as a way to display to HDMI devices wirelessly. In this series of tutorials, we’ll go over the API, starting with configuring your project to use the framework, and finding devices out on your local network to play with.

Let’s assume you’ve set up a Chromecast device attached to an HDMI TV and have it configured for your local network. Now it’s time to get an App set up to use it. We’ll use the iPhone Simulator in these examples, since it can talk to Chromecast devices just like a physical device, as long as the Mac you are developing on is on the same LAN as the Chromecast dongle.

Begin by creating a project, as usual. For this example, I used a single-view Storyboarded app. I set up an UITableView inside the default UIView, hooking it’s datasource and delegate to the default view controller the wizard had created. Next, I went to the Google Google Cast API page and downloaded the iOS framework, then used the “Add Files…” project option to add the framework to the project, copying in the files.

Read more…

DIYbio and the hacking metaphor

Definitive answers require further testing

The following is from the second issue of BioCoder, the quarterly newsletter for synthetic biologists, DIY biologists, neurobiologists, and more. Download your free copy today.


Within DIYbio, one cannot escape the hacking metaphor. The metaphor is ubiquitous and, to a point, useful. The term connotes both productive play with an existing technology aimed at improvement and, at the same time, play with sinister undertones. In this sense, hacking captures the promise and pitfalls of the dual uses any mature technology might be put to, whether that technology is as dramatic as nuclear power/weapons or as mundane as a free/premium software license. But every metaphor has its limits. Pushed too far, metaphors break down, and instead of illuminating, they obscure. Which brings me to ask: how far can the hacking metaphor be pushed within DIYbio—at least the part of DIYbio falling in line with synthetic biology?

Read more…