Monthly Archives: April 2007

A Painful Trip Through WordPress

You may have noticed a bit of mess in the posts to my blog today.  I've just started a process where I'm going to either publish old draft posts, or delete them.  My first post was a post from a conversation I had with my wife, Memory Constipation (not memory leaks).  It was actually a bit exciting having this happen.

  1. I edited the article, and clicked publish, went to my blogs front page, and noticed no listing
  2. I looked at the posting, and noticed the date was "June 2006"… hmmm I guess I started writing the article then…
  3. I changed the date to now, went to the front page, and still no article…
  4. remembering an AJ rant at work about future posts, I thought that perhaps I was future posting.  I set the date to yesterday, and noticed the post. YAY!! I then did my refresh trick to try and see the post on JavaBlogs, but no joy….
  5. So I played with the date, and finally got it to about now on the post.
  6. I went back to JavaBlogs, and noticed that the post now had appeared twice d'oh… what was worse is that the first permalink (with yesterday's date) was broken NASTY.
  7. so I went and created a new post with the same title and yesterdays date.  Unfortunately WordPress forces the article names to be unique, so this didn't work.  People trying with the original posting to JavaBlogs would still get a 404 :(
  8. By now I was getting a bit annoyed with WordPress, but wasn't about to give up. I tried forcing the name of the post, but that didn't work, so had to head into hacking the permalink approach.  The rest of this post shows what I did (warning this was a hacky approach to PHP and updating wordpress).

So the problem was that when wordpress was looking for the post it was using the date of the post, and the unique name to look for the post.  Given that there was no post with the right name on the date, no post was found, so wordpress returned a 404.  My work around is to leave the query code as close to the original as possible, only adding a check at the end, that will remove date the date parameters if no posts were found.  The following code goes at the end of  the get_posts function in the WP_Query class.

// if there aren't any posts returned by the query, name and year parameters were specified, then
// we are probably dealing with a post that has moved.
// so lets go ahead and try a query without any date fields.
if (sizeof($this->posts)==0 && isset($q["name"]) && isset($q["year"])) {
   unset($this->query_vars['year']);
   unset($this->query_vars['monthnum']);
   unset($this->query_vars['day']);
   return $this->get_posts();
}

It's a nasty hack, but it does the job, now the following urls, all return my page.

Memory Constipation (not memory leaks)

This post is the first in a series of efforts to clean up my list of drafts.  This particular post is probably about a year old.  It is somewhat fitting that it is the first in my effort to clean out my drafts ;).  Before making each post in this series I'll edit the post to clean it up, and add new comments in italics, or footnotes).

I often spend time talking about different things with my wife, Suzanne. We both like learning, and sharing from our different experiences (in fact I'd almost call Suzanne a compulsive learner). 

I was recently struggling with an application with memory leaks1. When explaining this to Suzanne (who is currently working in a hospital setting treating a lot of kids with enuresis and encopresis)2, she pointed out that it is probably more of a case of memory constipation.

Especially in an environment with Garbage collection. The issue is that the rubbish ain’t getting out of the system.

1 - That was two years ago, in a Swing fat client.  Two years later at Ephox we getting OutOfMemoryExceptions in Java Applets

2 - actually now she is a full time Mum, and doing some Enjo demos on the side.

 

Three Months at Ephox

 I've been at Ephox for three months now, and the ride is continuing to be a good experience. I've had the pleasure of spending most of the past month in development. It was quite strange finishing up the support, with me actually being sad to leave.  I was really getting into the customer interaction, and being able to help people come to some good positive solutions.

Moving out from support to development was a happy transition to make.  I went as far as to move desks getting closer to poking Doug and AJ.  I had a blissful first week of development, getting into the swing of things before Andrew (the CEO) came to visit.  I was really getting into the swing of being a happy code monkey, and Just a programmer, when Andrew shook things up, and we heard of plans for a restructure introducing more product managers, with the goal of better working as a company.  I am now going to join AJ, and Damien, and be a product manager.  AJ and Andrew have already started talking about what this might involve, and I'm really looking forward to what the new role has to offer.  Given the fact that I came to Ephox to code, it is ironic that I will have spent more time in support than coding, and I'm going to be a manager.  That said, I am definitely looking forward to the new role, and to the opportunity to be the customer voice (or as Dan North used to put it, "The Truth").  I've always been keen on delivering software that meets peoples needs, and one of my key responsibilities in this role will be guiding the development of EditLive! to ensure that we continue to meet expectations.  There is plenty to learn, and I'm excited about the new opportunities to grow.  

The month of coding has been fun.  I've learnt some things about JavaScript, and we got to add some nice features to EditLive.  I also got to learn more than I ever wanted to know about IWWCM.  My respect for Jack (our resident IWWCM expert) has gone up immensely (not that I didn't respect him before).  It has been nice to loosely be a part of the work that has been put into improving the stability, startup experience, and performance of the Editor, and I look forward to continuing to be able to influence the direction of EditLive!

Emergence

I picked up a copy of the book Emergence a month or so ago while browsing a local bookstore.  It's about emergent behavior, and complexity theory, not (as my wife seems to think every time I mention the title) about coming out of the closet.

I've found it to be quite interesting read, filling in many of my missing bits of understanding around the world of complex systems.  I've read about it before, spending a fair amount of time at QUT working with a guy who is doing alot of work in Security Risk Simulation and modelling in complex systems.

It's interesting because Computers on their own are becoming increasingly complex.  I'm not going to say that a web application is a complex system, but the interactions between browsers, operation systems and JVM's does seem to cause behavior which is at least approaching emergence.

Thinking in these terms can be useful when doing bug-fixing.  The bizarre side effects and bugs that can happen in different configurations can be thought of emergent behavior, removing some of the blaming that can happen.  So perhaps sometimes the bugs aren't Sun's/Microsoft's/Mozilla's fault, rather behavior that has emerged from the way in which the agents (browse/operating system/jvm) have interacted.

This then makes bug fixing  a case of preventing agents from getting into situations that cause the bad behavior to happen.

Some of the interesting examples are the ant colony studies, which have shown how the ants interact to create the behavior which is like the ants are all working under a centralized controller.  Long term studies of the ant colonies show the colony displaying properties which are greater than any one ant does. Basically the idea is that the collective behavior is not controlled centrally. This type of reasoning is often used to remove the need for a creator/God figure in control of things.

People who make these statements normally skip over the fact of the need for the initial behaviors to be initialized in the first place.  While these behaviors are relatively simple, each ant does still have some quite intricate (almost designed) rules that are being used.

As the book says of a computer simulation of emergent systems (specifically talking about modelling the behaviour of slime cells), "Of course, on the most fundamental level, StarLogo is itself a centralized system: it obeys rules laid down by a single authority — the programmer.  But the route from Resnick's code to those slime mold clusters is indirect.  You don't program the slime mold cells to form clusters; you program them to follow patterns in the rails left behind by their neighbors.  If you have enough cells, and if the trails last long enough, you'll get clusters, but they're not something you can control directly.  And predicting the number of clusters — or their longevity — is almost impossible without extensize trial-and-error experimentation with the system." and continues "Systems like StarLogo are not utter anarchies: they obey rules that we define in advance, but those rules only govern the micromotives.  The macrobehavior is another matter."

This matches my current view of the world.  I don't think that emergent behavior acts to prove or deny the existence of a creator.  It would be possible to match most (if not all) belief systems with Emergence, which for me means that I am happily able to be a Christian and someone who uses and applies the principles and lessons from emergent behavior throughout my life, especially while coding.

Minor Updates

After gentle feedback from Suzanne (OK so she laughed at me, not with me), I've made some minor updates to the wording above, to improve the readability.

Five things I learnt about Javascript this month

In the past month at Ephox, we have been doing some heavy JavaScript (in this article JavaScript == ECMAScript 3.0) work, the effects of which will be seen in the next release of EditLive! I’ve learnt a bunch of new things, and thanks to a nice internal Continuous Learning seminar by AJ, much of the background theory has gelled for me.

1. this is the current object at execution time (think python and self).

An almost obvious statement which doesn’t really suprise initially. Java guys will look at that and be thinking, so what. It gets more fun when you think about the dynamic nature of java script and that you can assign functions as variables and pass them around as a reference. Assigning variables to objects is possible, and allows the dyanmic definition of behaviour.

This all becomes important when you are assigning functions to variables for reuse. For example if you are to override a funtion, and keep the original for later use, you need to be careful not to loose the context.

So if you override the onsubmit handler of a form, but want to keep the original onsubmit handler around you might want to do seomthing like this:

 form.originalOnSubmit=form.onSubmit;
 form.onSubmit=newSubmissionFunctionThatCallsOriginalSubmissionFunction;

2. Function is a way of creating a function object—pass in a string, get a function out.

Pretty much as expected: 

myFunction = new Function(“arg1", "arg2" "{alert(arg1 +arg2);}”);

That will create a function with arguments arg1 and arg2, and will raise an alert with arg1+arg2.

For more details see here.

3. eval evaluates a string as javascript

yep it does

See here

4. prototype is the way that Objects are created in javascript.

JavaScript is a prototype based language. A  prototype function is specified by the following syntax:

MyObject.proptoype.myFunction = function(arg){
 //javascript function
}

see here for more info.

5. innerHTML rationalises the content being set

Browsers will parse the contents of the text being set via innerHTML, which will cause the contents to be changed in various minor ways. For example comments in the middle of attributes will be escaped.

There is much to learn about Javascript, and the above are a couple of the bits that I have learnt recently. 

Update:

As Tom has pointed out in the comments, be careful with the Python/JavaScript analogy.  Python binds the self early, so the analogy falls in a heap early on.

Ruby File Generation

Join me on a journey through using ruby to generate java, learning how to use the built in ruby templating tool erb, and some of the rubyish techniques that erb leverages.

 Our adventure starts with a need to generate a large number of very simple java classes in order to have a nice simple test case for the boys at sun.  The requirements for the classes are:

  1. they are some how different
  2. it is easy to tell when each class is loaded by the class loader
  3. it is easy to tell when each class is instantiated

These requirements would be painful to meet (when creating 1000+ classes) even in the most powerful java IDE.  An automated technique for generating code.  In steps Ruby and erb.

A basic ruby script which has a simple template to base the files on, and creates the java classes.

require 'erb' #we are using the erb library.

# create an erb template using the multiline string starting after EOF,
# and finishing at the EOF below.
template=ERB.new <<EOF
package
ephox; 

public class SimpleClass<%=class_number%> {
    static {
        System.out.println("static SimpleClass<%=class_number%>");
    }
    public SimpleClass<%=class_number%>() {
        super();
        System.out.println("init SimpleClass<%=class_number%>");
    }
}
 

EOF

5.times do |counter| # perform code in this loop five times, remembering the counter.
    class_number=counter+1
    File.open "src/ephox/SimpleClass#{class_number}.java" ,'w' do |file|

         # open the file for writing.
        file.puts template.result(binding)

                   #merge the template into the file, passing in the current binding.
    end
end

 

In looking back over the example, you can see some interesting rubyisms.  These are some of the spots where ruby is revealed. Note the following:

  • the use of closures (File.open, and 5.times),
  • everything really is an object (the method times being called on the object literal 5),
  • heredoc notation (<<EOF )
  • the ease of templating with erb (which keeps normal ruby syntax in the template),

The statement I made above about everything being an object is pushed to its limits when looking at the method call: template.result(binding).  The binding method is available as a part of the Kernel, a mixin(providing methods for use) that is a part of Object, the mighty superclass of everything in Ruby.  This method will return a Binding object, which erb uses to get data to merge into the template.

The File.open line opens a file in write mode, overriding the contents of the file.  For more examples of how to do this, look at the PLEAC site, which has a good set of ruby file examples, and quite a complete set of ruby examples.  This is an excellent resource for learning a language.

In all, Ruby does a good job of working with files, and templating.  Erb is a great tool for templating, and forms the backbone of many well known ruby programs.