Archive

Archive for the ‘Ideas’ Category

Easy, Simple, Wrong

December 17th, 2009

<rant>
Hey everyone, you can’t simplify indefinitely so stop trying so square the circle!

I’m not saying stop trying to simplify things, just stop once there’s a minimal and acceptable level of complexity. If you dumb it down too much, it’s plain wrong.

Once you’ve reached that point, either you leave well enough alone, or add to it by organizing things. More often than not organizing will actually degrade runtime performance but will decrease development time. If you’re not happy with that, maybe development isn’t for you. You’re likely going to prefer messes and will spend too much time shifting crap around instead than dealing with it.
</rant>

Sorry about that. :)

General, Ideas, Programming, Rants ,

Client-Side JavaScript DSLs

December 14th, 2009

This blog post is about solving one of my JavaScript paint points; specifically its verbosity when defining inline functions. The way in which I solve it is interesting to me and hopefully to someone else too.

Here goes…

Why not do DSLs on the client side by compiling to JavaScript in a compiler written in JavaScript?

I realize that doing a “true compiler” is computationally expensive, but the leaps in JavaScript performance we’ve seen lately make it tractable.

To do the job with the respect it deserves, you need to build a full parse tree, but for my particular DSL, a replacement scheme that relies on regular expressions should do fine except in the pathological cases. Except for obfuscated code, I have yet to find a piece of code that uses commas to separate expressions for example.

So, what do I have in mind?

I love the JavaScript language, but its format for defining lambda expressions is nasty. You’re forced to insert the function keyword where (except for pathological cases) it could be inferred from its context. This wouldn’t bother me at all if I wasn’t required to define them so often while working with jQuery.

For those who are visual, this is the code I’d like to write:

<script type="text/x-javascript">
$({ 
  alert("Hello"); 
 
  $("a").click((e) { 
    alert("clicked"); 
  });
 
  $.post("testing.php", {a: 100, b:100}, (result) { 
    alert("Result is: " + result);
  });
});
</script>

And this is the code I have to write right now:

<script type="text/javascript">
$(function() { 
  alert("Hello"); 
 
  $("a").click(function (e) { 
    alert("clicked"); 
  });
 
  $.post("testing.php", {a: 100, b:100}, function(result) { 
    alert("Result is: " + result);
  });
});
</script>

Now, it’s not a big change, but it’s definitely worth it as your jQuery lambda heavy code starts to balloon.

This is how you could make it happen:

  1. Define a script section that uses an “invalid” mime type and place your code there. This will be ignored by the browser, so you won’t get any syntax errors.
  2. Include an external script that searches for scripts of a certain type, converts them, then evaluates.

If you want to see this idea in action take a look here: http://www.machete.ca/x-javascript.html

I can’t be the first person to have thought of this, but I thought I’d tell it on the mountain just incase.

Thoughts?

Ideas, JavaScript, Programming , , ,

Java Observer Pattern Woes

March 2nd, 2009

I have no problem with the Observer Pattern, only its default implementations. Most language libraries provide implementations that are way too abstract and result in good programmers writing buggy code.

As an intro for those who don’t know the back story, the Observer Pattern is a software design pattern that can be used to detach an object from its uses. There are always two players, the thing being Observed (the Observable) and the thing doing the Observing (the Observer). The typical usage scenario goes like this.

  1.  Instance of Observable gets created
  2.  Instance of Observer is created
  3.  Observer registers itself to the Observable for change notifications
  4.  Observable changes
  5.  Notification messages are sent to all objects registered as observers.

Taking a look at the observer as defined in the Java API (see below), you can see that the Observer makes few assumptions about the object emitting the updates, the idea being that it can be reused with different kinds of Observable objects.

public interface Observer {
    void update(Observable o, Object arg);
}

Unfortunately, this comes at a high cost. To make use of the update notification, the Observer typically needs to cast the Observable to a more useful type and interpret the argument appropriately.  If you were planning on reusing it with many kinds of Observable objects, you’d end up with an update method resembling a switch statement for each kind of Observable thing.

As a concrete, though convoluted, example, say I have a Directory class and a DirectoryWatcher that needs to correctly display the number of files in that directory.  The code might look like this:

class Directory extends Observable {
  public File[] getFiles() { ... }
 
  public void createFile(File file) {
    ...
    notifyObservers();
  }
 
  public void deleteFile(File file) { 
    ...
    notifyObservers();
  } 
}
 
 
class DirectoryWatcher implements Observer {
  public void update(Observable o, Object arg) {
    if (o instanceof Directory) {
      System.out.println(((Directory)o).getFiles().length);
    }    
  }
}

“if (o instanceof Directory) {” Yuk!

One improvement is to require that all Observers implement a custom interface and then invoke appropriate methods on them.

For example:

class Directory {
  private List<DirectoryObserver> observers = new LinkedList<DirectoryObserver>();
 
  interface DirectoryObserver{
    void fileDeleted(Directory directory, File file);
    void fileCreated(Directory directory, File file);
  }
 
  public void addObserver(DirectoryObserver o) {
    observers.add(o);
  }
 
  public void removeObserver(DirectoryObserver o) {
    observers.remove(o);
  }
 
  public File[] getFiles() { ... }  
 
  public void createFile(File file) {
    ...
    for (Observer o : observers) {
       o.fileCreated(file);
    }
  }
 
  public void deleteFile(File file) { 
    ...
    for (Observer o : observers) {
       o.fileDeleted(file);
    }
  }
}
 
class DirectoryWatcher implements Directory. DirectoryObserver {
 
  public void fileCreated(Directory directory, File file) {
     System.out.println(directory.getFiles().length);  
  }
 
 
  public void fileDeleted(Directory directory, File file) {
     System.out.println(directory.getFiles().length);  
  }
}

Now, I know that the code above has some flaws in the way it dispatches notifications (Exceptions being one) and that it is significantly longer that the more abstract code above, but this doesn’t need to be the case. By using a helper class ObserverPool (which I don’t define here, but I will eventually) this could be brought down significantly to the code below:

class Directory {
  public final ObserverPool<DirectoryObserver> observers 
                    = new ObserverPool<DirectoryObserver>(this);
 
  interface DirectoryObserver{
    void fileDeleted(Directory directory, File file);
    void fileCreated(Directory directory, File file);
  }
 
  public File[] getFiles() { ... }  
 
  public void createFile(File file) {
    ...
    observers.broadcast("fileCreated", file);
  }
 
  public void deleteFile(File file) { 
    ...
    observers.broadcast("fileDeleted", file);
  }
}
 
class DirectoryWatcher implements Directory.Observer {
 
  public void fileCreated(Directory directory, File file) {
     System.out.println(directory.getFiles().length);  
  }
 
  public void fileDeleted(Directory directory, File file) {
     System.out.println(directory.getFiles().length);  
  }
}

The benefits of the code above are that the observer may make use of the observable object in a type-safe way and each notification is implicitly more useful since it each kind of change may have its own method.

Problems with the above approach are that the observable object needs to call broadcast with an argument of the method on the observers that needs to be invoked. This is a magnet for bugs since a typo breaks everything at runtime. I believe this can be overcome by using Java’s Dynamic Proxy feature to create a proxy object that stands in for the observers.

Using this proxy approach the code could be:

class Directory {
  public final ObserverPool<DirectoryObserver> observers 
                    = new ObserverPool<DirectoryObserver>(DirectoryObserver.class);
 
  interface DirectoryObserver{
    void fileDeleted(Directory directory, File file);
    void fileCreated(Directory directory, File file);
  }
 
  public File[] getFiles() { ... }  
 
  public void createFile(File file) {
    ...
    observers.getProxy().fileCreated(this, file);
  }
 
  public void deleteFile(File file) { 
    ...
    observers.getProxy().fileDeleted(this, file);
  }
}
 
class DirectoryWatcher implements Directory.DirectoryObserver {
 
  public void fileCreated(Directory directory, File file) {
     System.out.println(directory.getFiles().length);  
  }
 
  public void fileDeleted(Directory directory, File file) {
     System.out.println(directory.getFiles().length);  
  }
}

Much better, if I say so myself.

To recap, the typical implementation of the Observer Pattern trades reuse for type safety. Because of this, programmers need to write terrible code to put the checks back in. It’s my opinion that writing more specific code that doesn’t require the loss of type safety is always a better approach, and that by making use of generics and some Dynamic Proxies your end product will be more robust and be easier to understand.

Ideas, Programming , ,

Tiled UIs for Business Apps

February 18th, 2009

I’ve been enamored with Zoomable User Interfaces since I read “The Humane Interface” by Jef Raskin but was unsure how to bring the ideas to the web.  

Just yesterday, I realized that you could use a Tiling system ala Google Maps with dynamically generated tiles and achieve a rough zooming interface general enough to display arbitrary scenes.

The data need not be simple maps, or even of a scene that rarely changes, it could be a full server side scene graph that represents a company’s assets.

For example, imagine a “wall of clients” where each client is displayed as a tiny rectangle.  By colour coding them according to your criteria you could, at a glance, see the health of your entire company.

Then for further detail you could drill down through the tiles to read about a particular client’s file.

I’m pretty sure this could be relatively easy to do.  Performance wouldn’t be great, but if the items in the scene were simple enough, the images should compress pretty well.

I can’t wait to try out these ideas.  I don’t know how well it’ll work out, but I do know that it’ll be different.

Ideas, User Interfaces

Social Network for Personal Development Idea

February 3rd, 2009

Just had an idea for a Social Network dedicated to self improvement through group improvement.

I’m imagining a site where members announce their goals publicly and the system matches them up with other members with the similar goals.

Also, depending on the goal, it displays appropriate graphs showing the user’s progression.

The reason I think this has merit: people tend to keep goals they share publicly.

Now, the system could make your declaration public (as in your co-workers could find them), or public in that only other members with similar goals can see them.  I’m not sure exactly how I want it to work yet.

Just some first thoughts…

Ideas, Social Networking ,