February, 2008


28
Feb 08

JavaScript Puzzlers

I have just attended a great course on JavaScript soft typing by Peter Thiemann. No doubt I’ll write more about it later, but today I want to show how fun and quirky can JavaScript be.

Note that all puzzles are given in succession and every next one includes all the code of the previous ones. Also note that I tested the puzzles in Mozilla Rhino, and can’t be held responsible for how standards non-compliant browsers (read Internet Explorer) will behave.

Puzzle 1

> var obj = {x : 1}

What are the values of

1) obj.y
2) print(obj.y)
3) obj.y.z

[Answer]

Puzzle 2

> var x = "x"
> obj.undefined = "gotcha"

What are the values of

1) obj[x]
2) obj[obj.y]

[Answer]

Puzzle 3

> var a = 17
> a.x = 42

What are the values of

1) a.x

[Answer]

Puzzle 4

> var flag = new Boolean(false);
> result = flag ? true : false;

What are the values of

1) result

[Answer]

Puzzle 5

> function f() {return this.x}

What are the values of

1) f()
2) obj.f = f; obj.f()
3) new f()

[Answer]

Puzzle 6

> function g1() {
>   u = 2;
>   print(u);
>   var u;
>   u = 3;
>   print(u);
> }
>
> function g2() {
>   u = 4;
>   print(u);
>   eval("var u");
>   u = 5;
>   print(u);
> }
> u = 1;
> g1();
> print(u);
> g2();
> print(u);

1) What values are printed out?

[Answer]


26
Feb 08

Worse-Is-Better Design or Sloppy Code?

Although most of you probably heard of this before, today Worse-Is-Better triggered a chain of thoughts in my head that goes way back.

When I entered the Java development community the hot stuff in my side of the aquarium were RUP, UML and Design Patterns. The approach to software engineering that I learned was to foresee as much as I can and put those ideas in the design. As many others I would often use a design pattern where it fitted, without giving thought if it was really necessary there.

As a result of that Aranea was born. From the point of view of software design Aranea was almost perfect. It was based on independent components with few interdependencies, it foresaw situations like AJAX and desktop deployment long before they were implemented.

By the time we started working on JavaRebel I got somewhat disillusioned in those practices. Therefore, although I originally wrote a throwaway prototype, we ended up evolving it without ever rewriting entirely. Although the code was not directly a mess, it definitely preferred simplicity over unnecessary abstraction and not a single design pattern was hurt while hacking at it.

Both projects were large and complex enough to serve as a case study. But like the original author of the “Worse-Is-Better” essay, I still can’t choose between the two. In fact, I’m not even sure that these are separate approaches, it may very well be just the amount of sloppiness the project can take before it collapses.

Now, sloppiness is a good negative word. It is also fitting, since it refers to the character of the development process, rather than its success.

A very good example of sloppiness in today’s Java ecosystem is Hibernate. It is designed with simplicity in mind and it’s code is a tangled mess of just enough design. Nevertheless it’s a hugely successful project which provides a clean enough API to make developers happy. It is designed evolutionary and at some point developers had to give up full backward compatibility for new features.

On the other side of the scale is Spring. It’s code is precise, well organized and object oriented to the point of nausea. It has a layered design with the simple abstract core and never had to sacrifice backwards-compatibility.

In their own way both these projects are about equally complex. And I don’t mean lines of code, but rather how hard is it for a newcomer to start hacking at their code. In the end it’s almost as hard to unravel the layers of abstraction as to jump through the sloppy code. The main difference is perhaps that Spring offers more extension points whereas Hibernate is somewhat harder to debug.

“Wait”, you say, “Does he really suggest that sloppy code can be good? Why just yesterday I had half a heart attack looking at it!”. The trick is that I’m comparing two highly successful open source projects made by some of the best people in the Java community. This makes a whole lot of difference.

In the end it’s unsurprising to come to the thesis propagated by the agile community: in the hands of the right person any approach will work. A talented leader will trade the right things for sloppiness and a clueless newbie will create an overcomplicated abstract mess. My experience tells me that in my next project I will use just as much sloppiness as the project will take without collapsing and make sure the people around me can handle it as well.

What do you think? Can discipline protect a project or will it create even more complications? And what should be the guiding star when designing it?


22
Feb 08

WAS ist das? RANT

All the app servers and servlet containers I’ve seen these days the IBM’s Websphere Application Server tops them all. The most unexpected, complicated and difficult to understand by far.

Lets start by trying to find out what the server is doing at a given point. The startServer.sh will echo the name of the log file that you can tail. Great! At one point you understand that there’s a lot more going on in the background.

You do a quick find -mmin 3 and find out that do get an overview you have to tail about 5 logs: startServer.log, stopServer.log, SystemErr.log, SystemOut.log, server1_exception.log. Divide and conquer but how am I supposed to keep an eye on them? How many pages of documentation do I have to read to monitor the correct one?

I won’t even start on how to find the administrative console or what services are started on which ports. Once you find it and start deploying an application you understand that exploded format is a big no-no, no option for that. Ok, let’s copy or link the application to the installedApps folder. Nope, does not cut it. Nothing happens, none of the 5 logs show anything.

Just zip -r ../app.ear * and deploy the ear file. It will be unpacked to installedApps and off you go. Well actually you still have to zip even the WAR files inside the EAR for the deployment to be a success.

Now you configure your IDE to generate webapp’s classes to the deployed application. You see that the application starts reloading after every change. Probably somekind of auto loading. You go to the administrative console, find “Class loading and update detection”. This must be it. The checkbox is _un_ticked??? Why is it reloading on every update then? I don’t know, but just enable the damn thing and use a really large polling interval and it will all go away.

I can actually go on but I’m probably just wasting time. By the way, Glassfish’s profile/domain is 6.7 MB, my IBM’s profile/domain is 458MB. Go figure.


19
Feb 08

Aptana Jaxer or sliced bread?

Sometimes a technology appears that is just so damn cool you are amazed. More often than not the ideas behind it can be quite simple.

Aptana Jaxer is exactly such a technology. There is nothing new about having a server-side API. There is nothing new about building applications in HTML and JavaScript. The genius part is running Mozilla engine on the server-side and having access to full server resources from the browser via a controlled environment with no extra layers.

As far as I understood the communication between server and client is done by:

  • DOM updates are seamlessly propagated from the server to the client
  • Function calls can be proxied to call to the server. All marshalling is done automagically

This pretty much means that code runs seamlessly in a mixed, secure environment.

Although the “runat” attribute that controls whether code is run server-side or client-side can be a bit unlogical at first, the setup can allow to build powerful applications with only one (count it, one) technology — HTML and JavaScript. And this is the technology you have to use anyway to even deploy something on the web.

Of course “the one technology” suffers from being dynamically typed and generally known for its quirkiness, but with JavaScript 2.0 support on the way to Mozilla and good IDE support (which Aptana is in a good position to provide) this technology might yet give a fresh meaning to the word web application.

An interesting question I’d like to ask from the Aptana developers is if we can add a third environment to the mix — desktop applications? Having the same API to use on the server, in the browser and (with less restrictions) on the desktop could give Adobe AIR a run for their money.


14
Feb 08

Getting Files to Remote Places

Every now and then I need to share files with others. Either need to upload something to an accessible place or just give somebody a link to a set of files. Two handy scripts:

[perl]### BASH function, copy to ~/.bashrc ###
putfile(){
basename=${1##*/}
scp $1 user@host:/home/user/public_html/tmp/$basename
echo http://host/tmp/$basename
}[/perl]

Now this function is accessible from the terminal session. Just write putfile fileName and it will scp the file to the remote machine and output the URL that you can give to your friends. Be sure to have key based authentication enabled.

Do you want to quickly share a folder from your HDD without creating a quick link to your /var/www? Fire up a webserver with python.
[python]
python -c "from SimpleHTTPServer import test; test()"
[/python]

You can't remember you ip or don't want to type it every time to give the link? Echo the IP also to your console. Mind that this example uses the eth1 device, so if you have a different one be sure to change it.

[python]
### add it to ~/.bashrc
alias web='python -c "from SimpleHTTPServer import test;
import socket,fcntl,struct;
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM);
print \"http://\"+socket.inet_ntoa(fcntl.ioctl(
s.fileno(),0x8915, struct.pack(\"256s\", \"eth1\"))[20:24])+\":8000\";
test()"'
[/python]

Now you can share any folder by just typing web and you can copy the URL to your friend on LAN.