I dislike “Startups” in the UK

February 16th, 2010

I’ve worked at some small companies as both employee and freelancer. I’ve also started my own business a number of times. In all these experiences, the company in question was never been termed a “startup”. It was always just “a business”.

To me, defining yourself as a startup is a mistake. It smacks of drinking the Silicon Valley Koolaid and the almost desperate business models that follow. On TechCrunch today there was a good example of this; the company described themselves as “a fast growing tech company with a deep product backlog, high growth on our core web and email offerings and an overwhelming desire to invest in our customer understanding and in our technology“. You know what I smell? BULLSHIT!

There is often bitching about how there isn’t a Valley culture anywhere in Europe, today I started to wonder if that isn’t such a bad thing. Perhaps there are just fewer rich idiots over here prepared to fund stupid ideas?

Calling yourself a “business” forces you to think about… BUSINESS! If you went around saying you ran a business, people would expect it to do something: to provide a service or product, but when you call yourself a Startup it’s like you have a license to ignore common sense.

In fact, I do now talk about one of my past businesses as a Startup: I spunked $50,000 up the wall on an unproven idea that crashed and burned with zero customers. Truly, a Startup ;-).

HPHP - First thoughts

February 2nd, 2010

Facebook have released HipHop, a piece of software that converts a PHP script into C++ and then compiles it into a static binary. That binary operates it own HTTP server.

About a year ago I wrote a couple of posts about my thoughts on Apache and how we don’t really use it much and actually why I think it’s still relevant. In one fell swoop Facebook has provided an answer/solution to my ruminations; it is now possible (I think) to run PHP as a standalone application server (kinda like Rails with Mongrel, or Python with CherryPy et al).

Most of the applications I write have a single entry point that bootstraps the framework and handles all the routing - my “index.php”. For this scenario, using HPHP sounds like a win: you compile your index.php and instead of using .htaccess to route a set of urls to it, you just configure Apache to reverse-proxy those requests to your backend HPHP instance/s (not sure yet if it will thread, fork or block yet). You could also still run regular php scripts on that Apache server, and just offload some requests to the HPHP daemon.

It sounds like it will really come into its own when you’re essentially running Apache as a container for a monolithic PHP application. Instead of running Apache+PHP on a backend server, you just run the HPHP application. Win! (This is going to rock for API/REST services.)

Some questions:-

  • How will it handle autoloading and dynamic classes? Presumably we’d have to give the compiler a list of directories of libraries to include in the compilation?
  • What about 5.3? I’ve personally switched, does HPHP support it? (Also, will HPHP and PHP releases be synced?)
  • How does the HTTP/server stuff work? Is it a single process that manages worker-pools, does it thread or what?
  • How will multiple applications work? I’m guessing each one will need a new HPHP daemon.

Social Botnet and RATM 4 Xmas

December 17th, 2009

I’ve been taking part in the campaign to get Rage Against The Machine’s track to Christmas #1 and for the first time have really been exposed to the power of social media. The Facebook group has 800k+ members and is a beast of an advertising machine. There has been so much activity on the group with wall posts and people refreshing the page that it has frequently caused technical issues at Facebook, which is kinda amazing - it’s like a natural DDOS attack.

At one point, the group founder (Jon Morter) disabled the wall and held his congregation ransom: “When this story has received 2000 Diggs I will open the wall again”. A few minutes later the article had 2000 diggs and it hit the Digg front page. Later someone posted links to a Guardian poll which is now 90%+ in favour of the campaign’s position. They inadvertently have created their own “social botnet”, with a traffic cannon of people ready to game polls and overwhelm sites/comments/and so on. This is I suppose, what the campaign is: a DDOS attack on the charts.

I love it when the internet affects “real life”.

My Development Environment

November 1st, 2009

The Old

Most developers I’ve encountered run a local LAMP stack (WAMP5, MAMP, etc), but I think they suck:  they abstract away the cool stuff, like compiling extensions, modules and other fun software that you might want to play with. How’s a newbie going to learn some Apache administration if all they do is press a button called “Start Apache”? Some, myself included, run a separate Linux server. The benefits of having your own linux box for development include having an environment closer to production (assuming you deploy on linux), more flexibility in the software that you compile on it, and somewhere to practice your linux-fu. It’s just better.

I use an online box for development (an Amazon EC2 instance specifically) which means it’s accessible anywhere I am, so long as I have a decent internet connection. Others have a machine on their local network, which removes the internet requirement, but introduces a physical constraint. Some crazy fools run linux as a desktop, which is probably not acceptable for most people. Recently the network connection at my office has been crappy and is beyond my control. This has interupted my coding  so I’ve taken the opportunity to freshen things up and switch to using a virtual machine, as well as finally dropping ZDE 5.5.

The New

My new environment is similar to the old: I still use my Mac for writing code and I still run a linux server; the difference is by running the linux server as a virtual machine (via VMWare) I eliminate the network connection. This is the best of both worlds: I write code locally (on my pretty Mac) but also have a linux environment.

VMWare Fusion can be configured to provde your instances with static IPs, which lets you make an entry in /etc/hosts for a domain like devserver.

There isn’t anything special about this setup but I’m very happy with the glue/configuration that holds everything together.

MacFuse: exposing the VM’s filesystem

MacFuse is a cracking piece of kit. It allows you to “mount” a remote filesystem using SSH (or whatever protocol you like). This means my Mac has a path /Volumes/devserver that points to the /home/richard path on devserver. My IDE can now open and edit files on my linux server as if they were local.

I also run a SSH server on devserver and shell into it from my Mac Terminal. The VMWare window is permanently minimized and I rarely interact with the GUI.

Netbeans 6.7 - win!

Finally. I can say farewall to my old friend Zend Studio 5.5 :D. It’s a bit of a shame Zend went down the Eclipse route imho, I liked the old ZDE, but it’s time to move on. I’m not sure entirely what changed since Netbeans 6.5, but whatever it is I like. It took a few hours of fighiting but I beat it into submission  (I really dislike their “Run Configuration” dialog when creating a new project). In Netbeans-speak, my projects are setup as “local server”, but with a “remote” hostname (devserver). The sources directory is set to something like /Volumes/devserver/projects/myproject.

The feature that made it possible for me to switch is the new “Path Mapping” capability for debugging (Project Properties -> Run Configuration -> Advanced…). It maps a remote path (/home/richard/projects/myproject/www/foo.php) into a local path, so that the IDE can open up a local file to display the debugging stuff (ie /Volumes/devserver/projects/myproject/www/foo.php). In my setup, the translated local path is actually the remote file, due to the MacFuse magic. This means Netbeans will display exactly the same file as the debugger on the remote server is working with. Neat trick! (I also had to create a mapping to my devserver’s PHP include_path, which was /usr/local/lib/php => /Volumes/devserver/.phplib, where the .phplib was a symlink to /usr/local/lib/php). (This allows me to debug any PHP libraries that I have installed.)

Apache Magic: mod_vhost_alias

The module mod_vhost_alias allows you to dynamically create new virtualhosts without editing any config files. I have it setup so that a domain in the format XXX.devserver points to the document root /home/richard/projects/XXX/www. So when I create a new project in my editor at /Volumes/devserver/projects/new-project it is immediately available to view, debug or profile at http://new-project.devserver.

Conclusion

I’m happy. This feels like a nice, self-contained environment.

  • Develop on a linux server.
  • Write code on my Mac (or Windows).
  • Not reliant on the network.
  • Can do “remote” debugging.
  • Netbeans is lightweight and pretty.
  • Bonus: Kcachegrind is available to me through the VMWare instance.

Advanced jQuery Plugins

August 6th, 2009

This is *so* cool: http://yehudakatz.com/2009/04/20/evented-programming-with-jquery. It’s a really elegant way of writing jquery plugins.

It basically splits the plugin into 2 parts: initialization of custom events and the code that handles those events. The code that handles the events is the “meat and potatoes” and is defined as a public property of the plugin; however you can also pass in a replacement object to the plugin function, with a new set of event handlers — and that’s the really clever bit.

If you want to extend the plugin, you just clone the base handler-object and either chain your own handlers or replace the existing ones. This is like the Decorator pattern but for javascript event handlers. So for example, the plugin might turn the text red when you click it. If you wanted it to also bold the text, you clone the base object and add a new “listener” for the “changeColor” event which will make the text bold.

The pattern makes is very easy to unobtrusively extend the plugin. Previously I’d have used callback hooks but with this style of plugin there’s no need. It’s even better than callback functions because you can completely overwrite the “meat and potatoes”.

Shit like this is why I like Javascript :-).

Usenet copyright violations and Wolverine

April 8th, 2009

My usenet provider has deleted all binary articles relating to the recently leaked Wolverine work print. This is interesting because it hasn’t deleted terabytes of other data that infringes on other copyrighted works. Why not?

Either the copyright holders or their agents can’t/are not pushing the same buttons as were pushed for the Wolverine film (FBI involvement?) or they don’t really care about it. I’d like to think it’s the later, and that gives me hope that that some people in these big organisations have a clue about the internet and online piracy.

The “theatrical box office” takings of a movie are much higher than DVD sales, so it makes sense for the distributors (20th Century Fox in this case) to go bat-shit-crazy when something happens that affects their primary income. The distributor will also get a much higher % of the box office than it will from DVD sales (it costs a lot more to produce and sell a DVD than licensing a print to a cinema) and whilst the box-office is 5x the DVD gross, it might be 20x the revenue they actually receive.

The way my provider has very quickly removed the content shows that copyright violations *can* be enforced if there is enough will (although I’m not too sure of the state of the P2P systems) but at the same time suggests that run-of-the-mill violations like TV episodes or DVDs are allowed to exist.

Sabbatical

April 4th, 2009

It’s been 18 months since I left my day job, and I’m happy to say it’s been a pretty good 18 months. In the same time period prior to going solo I went through 3 jobs and lived in 3 different cities. Some say that makes me a little flakey and that it reflects negatively on my “CV”; but to me it shows that I can identify when I’m not enjoying life and that I’m prepared to do something about it. I’m sure we’ve all met people who bitch and moan about their jobs but they never quit, or address the reasons why they’re unhappy. That’s definitely not me ;-).

I’ve reached another one of those moments where I feel like I have to change some things up. It’s a combination of work and personal reasons but right now I feel like I’m just bobbing along; I don’t have any big goals I’m working towards and whilst I’m not unhappy, I’m not particularly happy. I need some time to reflect on life and so I’ll be taking a sabbatical over the next few months.

I’m looking forward to it a lot. I’m planning on spend my programming-cycles finishing off the first release of APRIL and working on some other projects that always came 2nd best to “real work” (I need to put phpevents.com to good use). My eyes are also on some geeky conferences coming up. I’ve booked for Bamboo Juice and can’t wait to spend a week relaxing in Cornwall followed by a fix of geekiness. I’ll also certainly be jumping back on the train and making the next few PHPLondons. Generally it’s going to be some time where I can get some things done without the pressure/distraction of work.

I need to sort out my long term plans too. I’m tired of telling people the same old spiel about Pluggable.

Thanks for asking, things are going great! I’ve got loads of work on at the moment - more than I can manage. I’ve been thinking of expanding, perhaps taking on an employee or bringing in some other freelancers - perhaps a CO-OP thing? Not sure…

Everyone who has asked me “how’s business?” in the last year got that response. I need to shit or get off the pot, either I’m building a business or I’m a freelancer (either is fine, so long as I know wtf I’m doing!).

I’ve never been great at the long term. Ask me what I’m doing in 5 years and I don’t have an answer, the best I’ve got is a vague picture of where I’d like to be by 30 (+3 years). I am however starting to grasp that time does indeed march on, and that it’s worth doing the things that seem small and insignificant in the short term, but which have great value in the long term (and that are only achievable after a long period of time). Sucks, I feel like a grown-up!

I love you really Apache

April 4th, 2009

I kinda slagged off Apache’s roll in a lot of web applications. The gist of that post is that Apache’s traditional life-cycle has been replaced by functionality within the web app itself (eg the web app handles authentication, rather than the web server). What I totally missed though, and it seems patently obvious now, is that Apache’s ability to be used as an HTTP wrapper for PHP apps is a good thing.

If you take away the logging/resolving/auth/negotiation/etc stages you’re left with a daemon that speaks HTTP. At this “stripped away” stage it receives an HTTP request and passes it through to PHP, which does some processing and then Apache returns an HTTP response. That might not seem like much functionality but it’s an absolutely critical part of a web app. My last post suggested that using Apache in this way was “bloated overkill”:-

We don’t really use Apache for much, so why do we use it at all? I think it boils down to being a nice host environment for PHP. There’s nothing really wrong with that, except PHP is tightly coupled to Apache. I think Apache rocks, but if we just end up using it as a host for PHP, then that’s bloated overkill? What about if we stripped out the features of Apache that we dont use until we’ve got a lightweight http wrapper for our PHP app to run in… isn’t that what Rubyists do with Mongrel?

I’m an idiot. Apache’s ability to be used as an HTTP wrapper isn’t a bad thing: it’s a good thing!

If you view Apache like this then you’ve got a rock solid foundation to create an “HTTP application server” (ie a standalone daemon communicating via HTTP). It’s one of those pieces of software that you can run on pretty much any environment and has been QA’ed over years. It’s solid, trusted and “fast enough”.

I remember talking to Rob Allen about potentially moving to Ruby or Python and one of the barriers for him was reliability. With PHP we can stick scripts in Apache’s docroot and forget about them - they’d probably survive a nuclear winter. Neither of us had the confidence that you could do the same with a Ruby/Pyhon stack (eg running Mongrel or CherryPy).

PHP is like a scripting language for Apache - and that’s a massive strength.

Going back to the old school

April 3rd, 2009

PHP stands for “PHP: Hypertext Preprocessor”.

In computer science, a preprocessor is a program that processes its input data to produce output that is used as input to another program

Hypertext in this context means HTML. The idea was you write PHP code which is then sent to the preprocessor to execute and it sends you back HTML. Instead of having a static HTML file on your website you would have a PHP file which would be interpreted and converted into HTML. It was common for every page on a site to have its own PHP file. It made sense: users were migrating from static HTML pages which had their own files, so they would replace them with a PHP file.

Copy & Paste code duplication quickly became a problem. Code would be refactored into functions and stored in a .inc file, to be included by the .php script.

I remember a lot of my .php files looked like this:-

<?php
include("inc/common_funcs.inc");

if($_POST['submit']){
  // form was submitted, do some logic
}
?>

<html>
  … webpage + form goes here (and the form’s action would be the same page as this
</html>

There were a few other styles I remember too. One was to have a php file for the webpage/form and another for the processing of the form (ie the form’s action); this was a throw-back from the Perl CGI days where the static HTML would reference the .cgi file. Another was to pass a “cmd” or “action” variable and then have a switch statement to control the application flow.

<?php
  if($_POST['submit']){
    switch($_POST['cmd']){
      case “save”:
        // do the logic for “save”
    }
  }
?>

If you showed code like that to any PHP developer today they’d probably rip you a new one and say it’s crappy old school. But that last example should ring some bells - it’s pretty much the precursor to today’s frameworks’ Controller/Action style and fundamentally is the same thing (and probably runs faster). Sometimes I think that not a lot has changed in the PHP world from ten years ago :-).

I’ve been thinking about frameworks for a while. For me I’ve never really found one that clicks with how I work, or how I grew up learning PHP. I like things really simple - like loading a .php file in the browser and my code being executed. No routing, no dispatching, no jumping through a dozen files before my code runs. When I use frameworks the magic stuff scares and frustrates me; sometimes I feel almost claustrophobic - I’m stuck inside its way of doing things and I can’t get out.

A couple of years ago I designed a framework for a company who needed their “old school” code refactored. At the end of the project I remember turning to a colleague and exclaiming that I thought “it totally frigging rocks!”. What (I thought) it did very well was to make it easy for their internal developers to write new pages for their site. It took care of some of the setup stuff (bootstrap, loading config data, db layer) and then got out of their way.

<?php
class SomeHandler{
  public function run($context){
    $db = $context->getDb();
    $request = $context->getRequest();

    // do some logic

    $person = $db->getPerson("Richard");

    $context->setViewVar("person", $person);
    $context->setTemplate('person.tpl');
}
?>

The developer writes their logic inside the run() method and has access to stuff like the DB or Request through the $context object. Their job is to write some logic and then set some variables for the view, that’s it. To give you some context, this class is instantiated by the “bootstrap” index.php file, based on some trivial routing, something like this:-

<?php
//bootstrap index.php

// setup context the context
$context = new Context();
$context->setDb($dbObject); // etc

$handler = "SomeHandler"; // replace this with some simple routing

$handler->run($context);

if($context->template){
  $templateEngine->render($context->template, $context->viewVariables);
}
?>

As I remember it, URLS mapped 1:1: to handler files. The routing component was a little involved because it tied into another system, but for prototyping we used a router that directly translated the path to a class. So “/path/to/foo” would be the handler “Handler_Path_To_Foo”. We’re back to writing one .php file to handle one url, back to the old school. The templating system was there to use if they wanted (by setting a template in the $context object) but if not then no worries - whatever they echo’ed from within run() would be output.

I can’t claim this is amazingly complicated or innovative, but I think that’s kinda my point: it was dirt simple but it was really effective. The learning curve was practically zero for new developers, the system just augmented the basic offering of PHP (and made some “company specific” tools available to them).

I’ve got an archive of the project and today I counted the number of lines for the “framework” and it came in at about 600, including comments and empty lines which is tiny, almost nothing. But looking back on the code I think it’s some of the best code I’ve ever designed/written.

Why blog this now? As a part of APRIL I’ve been looking at the “server” component recently. This is the part of the library that helps a developer create their own RESTful webservice. In essence that means it’s “A Framework”: it handles the routing/mvc part of things (”Oh no, not another framework!”). I’m using the same sort of design for this as I did for the framework I talked about. It’s ultra-lightweight, I’m a bit embaressed to even call it a framework, but from the work I’ve done on it so far I’m getting the same feeling as I did before - it’s simple, fast and “feels” like old school PHP, in a good way.

First impressions of CouchDB

March 29th, 2009

I’m spending my weekend playing with CouchDB. It’s a “document” database (storing JSON objects) which is pitching itself as more suitable for some web applications than traditional relational databases like MySQL. There are other resources that explain it well so I won’t go into its feature set, this is more a note of what I thought of it after a play.

The first thing you notice is the built-in UI is pretty slick. Think of a really slimmed down “PHPMyAdmin” that’s built into the database. This let me start playing around with the new concepts of CouchDB (documents, map/reduce) without getting bogged down with hooking it up to PHP.

I started creating some documents and it hits you pretty fast that this isn’t anything like MySQL. All the documents are stored within a database (you can have many documents inside a database, and many databases) but there’s no type-differentiation between them: it really is just a big bag full of JSON objects. The convention is to create a “type” property on your documents yourself and once I did that things started to click better.

So having put some documents in, how the heck do I query them? There’s no SQL; instead you need to create views which use two functions (map/reduce) to provide the logic for the query. It took me a good few hours to read up on this and understand the basics. The idea is that for a given view, all documents will be sequentially passed into your “map” function and it’s up to your logic (programmed in Javascript) to decide if any data from that document is going to be put on the output stack. A simple example of this is a view to extract documents of a certain “type” (assuming I’ve used the convention of giving my documents a type property).

function(doc){
  if(doc.type == "person"){
    emit(doc._id, doc);
  }
}

This map function will “emit” (put on the output stack) all my “person” documents, indexed by the _id property (a UUID that each document must have, like a primary key). This gives you a fine level of control of the outputted data (I think more than SQL would) because you’ve got a full programming language available to define the logic; although this worries me a little: with great power comes great responsibility! It took me a while to understand that there will be no query logic in my application any more. That’s a double-edged sword I suppose (good: no dodgy SQL written by juniors; bad: you lose the flexibility of writing arbitrary queries).

I tried to setup a sample database for a project I’m working on and got stuck really fast. Getting your head around “documents” when you’ve been in “relational database” mode for 10 years is difficult. I found a useful trick was to think about how i’d implement my project in the offline world, with paper-based forms and documents. It definitely feels “wrong” to be breaking the database rules and I think it’s going to take some more time to understand the best ways to setup my data in documents.

I do really like the general concept of a document/object database though and I think it’s a better fit for the majority of the web application work that I do. Currently I feel I’m doing a disproportionate amount of fighting with my relational database (MySQL), particularly on very active projects where I’m often touching the database to implement new functionality; I’m hoping that something like CouchDB will “go with the grain” better.

It’s definitely something I’ll be spending some more time getting to know.