blog


21
Dec 10

Don’t just write comments, tell a story

We all have our ways of writing comments in our code. Some prefer huge blocks of text at the beginning of a source file. Some prefer a paragraph before every chunk of code. Sometime we sprinkles a few one-liners here and there to explain a cryptic regular expression or two. But no matter how we write comments, they always get in our way of coding. That’s why we end up spending very little time documenting. It makes your code longer. Adding new code in between always messes up the comments and then you have to spend some more time realigning comments. Look at the following screenshot and see if any bright ideas come to mind.

Docco Screenshot

Dual-pane source/comment view

The HTML output was generated using a tool called Docco and provides a very clear separation of code from comment.. This little Javascript utility parses out comments from a source file and aligns them next to the syntax-highlighted code using plain old tables. But imagine, what if your IDE let you write your code like this. Imagine a two pane view, the left pane is the code view, and the right pane is the documentation view. For every chunk of code you write, you can move your cursor to the right pane and write documentation for it. Adding or removing lines from the code view automatically updates the position of the comments on the documentation view. So your documentation is always in sync with the code.
Most IDEs today have a way to run a program in “debug mode”. But what about running it in documentation mode ? In this mode, the IDE would automatically set a break-point at every block of comments. You step through the program’s execution and read your documentation as if it were a linear story. Once you have stepped through the entire program, the IDE would dump the entire thread of comments to the console in the order it was traversed. This is your program in pseudo-code. You know you have done a good job at documenting, when the pseudo-code tells a good uninterrupted story. This output is also your debugging log as the story would not make any sense if your logic went wayward.
I don’t know about you, but this would solve a lot of problems I regularly face at work. Every now and then, somebody decides to send me a huge code-base, asking me to add features to it or hunt down a cryptic bug. Naturally, as I am not the author of this code-base its hard for me to know where to start from. Most of the time I am not even sure what the program does. I can see comments sprinkled here and there. Only if I had some idea of what a class is doing, I would understand what the comments mean. The Documentation Mode would give you a temporal view of all the comments in the code. It would give you a context, it would tell you story. We humans are good at listening to stories. We are even better at making out when a story has holes in its plot. Thus this temporal view would be a useful indicator of which parts of the code need more documentation.
In my opinion, we need to move away from a pure writer-source centric view of the IDE to the more reader-documentation kind of view. It would probably require us to agree upon some kind of markup for source files. But hey, its almost 2011, its about time we move away from medieval ways of representing code in text files.

19
Dec 10

Uplifting Demise

I died as mineral and became a plant,
I died as plant and rose to animal.
I died as animal and I was human.
Why should I fear?  When was I less by dying?
Yet once more I shall die human,
To soar with angels blessed above.
And when I sacrifice my angel soul
I shall become what no mind ever conceived.
–Persian poet Rumi Jalaluddin

18
Dec 10

Managing states in web-applications

Nowadays as more of our websites are turning into web “applications” we are being challenged to find newer ways to express complicated UI interactions. Back in the old days we used to think of a site in terms of pages. Each page would correspond to a real file on the server. The navigation area would have links to all these static pages and this is how sites were organized. The introduction of dynamically generated pages didn’t change this model too much. We could still say we are on a certain page of a website and associate that location with a URL. But now-a-days all that has changed. Take any vanilla web application, for example Gmail. What page are you on when you are viewing your inbox, or one of your many tagged folders. The URL doesn’t change. All that changes is the internal state in the Javascript code. Before the web app era the code for managing states could have been segregated into multiple server side scripts. But now all of the state and transition logic is mashed up in one source file.

In this article we are going to look at a mechanism of managing states in an webapp using the Flex framework. Using this as a foundation we will invent our own syntactic sugar to sweeten the deal. First let’s look at MXML code snippet:

The code snippet defines two possible states: 1) stateUno and 2) stateDos. Inside the state tags, some UI update logic is defined declaratively. Moving to stateUno adds a new label to a component called mainBox and sets the label’s text to “State Eins” and font size to 18. Something similar also happens inside stateDos. Let’s see how one would invoke the logic define inside on these states.

The interesting bit of code is the last line. All the code before that is to read the states declared in the markup and store them in an array. The attribute dataProvider=”{possibleStates}” is what lets the button know where to find the list of states. The attribute itemClick=”{currentState = String(event.item);}” causes the UI to move to a specific state. At this point, the Flex runtime does all the hard-work of looking up the state in the array and applying the programmatic logic defined in the first code-snippet.

Now, I’m not a big fan of defining UI update logic to be defined in markup. That’s what scripting languages are for. But I do like the idea of declaratively defining styles and associating them to application states. We’ll now look at the closest analogy to implementing this idea using nothing but CSS pseudo-classes. Let’s look at this piece of code:

This is a typical piece of code for styling hyperlinks. The code causes the hyperlink to appear blue when the mouse is over it, and red if it has been visited. In CSS terminology :hover and :visited are called pseudo-classes. But if you think about it, they are just different states the hyperlink component can be in. Because hyperlinks are components that are baked directly into the browser, the state is updated automatically, and the correct styling for that state is applied from the stylesheet. Sadly browsers currently don’t recognize user-defined pseudo-classes and consequently there’s no way to programmatically set the state of a UI component. But just for a moment, imagine they were.

Add this point, let’s invent some custom HTML and CSS syntax to explore this idea of defining states in stylesheets. Let’s imagine a simple component that shows the strength of a password using a color-coded label.

To interactively update the color of the strength indicator, we would end up writing some jquery to update the class name after determining the complexity of the password. But what if we could write something like this instead:

This looks like a more readable code to me. Here I am making an assumption that states are applied at a widget level. But they don’t have to be. I could have a single call to update the state of an entire app. This would update the state-based styling for all the widgets in the app. Not only would this technique add more semantics to existing CSS code, but it would reduce a lot of the javascript code needed update styles every time there is a need to visually notify the user of a new state.


14
Dec 10

Sandstorms

You can build stadiums that cool themselves, you can attempt to recreate Venice… But you can never fool the sandstorms..

A camel being loaded at the port in the early nineteen-forties

They will keep coming back to remind you of your bedouin past…


4
Sep 10

Cumulonimbus

Cumulonimbus: Latin Cumulus (accumulated) and Nimbus (cloud)

It’s a file-browser for your cloud-data, it’s a better bookmarks manager, it’s Chrome OS without the OS. It’s the United Clouds of Information.

No matter what OS you use, there is always an equivalent of “My Documents”, “My Pictures”, “My Music”, “My Videos” in every file-manager. And there is a good reason for it. These are the most common types of file/media we work with every day. If you browse one-level below these folders, you will see a personalized hierarchy of sub-folders. And this organizational structure is different for everybody. For college students, the My Documents might have a college folder, under which there might be a folder for each course. Each course folder might hold a variety of documents (pdfs of readings, word files of papers, ppts of lectures, etc.) The same applies to My Pictures & My Videos, which might be organized by events, and My Music which is might be organized by Artists.

Fast forward to the Web 2.0 generation. You now have significant amounts of data in the “cloud”, in addition to what you have on your desktop. Even if you are a very busy person, there is some sanity to how the data on your desktop is organized. But the data in the “cloud” is in a complete mess. Because there isn’t really one cloud. But each web service has its own cloud. And your precious data is spread all over the place. Let’s think about this for a moment.

I’ll start with Picture first, because that’s the most common type of media on the web. Now think of where your Pictures are stored online. Some of them are on Facebook, some of them may be on Flickr, some on Dailybooth. Each of these services provide their own way of structuring your pictures. Facebook has its albums, Flickr has its sets, etc. Now wouldn’t it be nice if I could have a unified interface for all my pictures, independent of my source. Sort of like the “My Pictures” on my computer. And maybe using this UI I could organize my pictures into folders, regardless of where they actually reside. So finally my Florence trip pictures can be in the same folder, even though some of them are of Facebook, and some on Flickr. You can take this unified UI a step further by using it organize Pictures from the same source. For example, imagine you attended a party last weekend. You have some pictures of it in your album of Facebook. Your friends also have your pictures in their albums. With this new layer of abstraction provided by the UI, you can now put both these images in the same folder. Once you have all of this, you can start thinking of pictures the way you think of them when they are stored on you computer. Suddenly, Right Click > Edit with Picnik, seems a hell of a lot more intuitive.

Secondly, lets talk about “Documents”. Documents is an umbrella term for Word files, Power-Points, Spreadsheets, PDFs, Web-Pages etc.  Here its the same deal as it is with Pictures. The paper that you are writing for a class, may be in Google Docs. But the reading for that assignment is on Scribd. Even worse, it could just be a link to a PDF on the class-website. The websites you browse to do research for your paper are not in one place. Sure you could bookmark them. But experience tells me you are not going to bookmark all your readings, and your papers, and your citations and organize them in folders. Why? Well, it’s going to be all tucked away in the Bookmarks menu. And that’s not where all your documents are. It just doesn’t quite fit the mental model of the file system and the desktop.

I hope you get the picture (no pun intended). Cumulonimbus is a file system abstraction over all the data stored in the web. It lets you draw a line across all the individual clouds of the web and mark it as your own personal cloud. And the awesome part is, all your higher-order folders and their organizational scheme does not need to reside in the cloud. It could just live in your browser’s local storage. If you really want, you could configure Cumulonimbus to save the organizational data as bookmarks so that you can use any of the bookmark syncing services to carry around your organizational preferences.

To wrap up, Cumulonimbus is glorified bookmarks manager. It let’s you establish your own organizational scheme over your data that’s stored in the clouds. The difference is, besides manually bookmarking things, the application can pull data from different sources, bookmark them and have them neatly organized in folders. As a result, your web-life can be just as organized as your desktop life.


4
Sep 10

Adding A library to a GWT project

One of the annoying things about the GWT community is that 3rd party developers don’t always distribute their libraries as JAR files. Since you are required to include source files in the JAR anyways, developers just ask you to check-out the source from their repository. As a result of this, adding a new library to a GWT project is not as straight-forward as adding a JAR to your class-path. In this article I will show you how to check-out a library from a Google Code SVN repository and add it as a new module in your GWT project. For the purposes of this tutorial I’ll use the GWT Canvas library as an example.

  1. Use an SVN client to check-out the code to local directory. If you have a command-line client, you can copy the checkout command directly from Google Code, under the Source tab. If you are using a GUI client like Tortoise SVN, just copy the URL part of the check-out command and download the source.
  2. Browse through the directory hierarchy of the library, and open any source file. In my case, I opened up Canvas.java. Note down the fully qualified package name up to (but not including) the client package. For example, in my case it is gwt.canvas. This will be the package name for our new module.image
  3. Now browse back to the parent directory of client. There you will find the .gwt.xml file corresponding to the library. Copy the file name up to (but not including) the .gwt.xml part. This will become the module name.
  4. Within Eclipse, right click your project name and go to New >Module. Paste the package and module names from steps 2 & 3. In my case, the module name was gwt-canvas. However, Eclipse doesn’t seem to like module names with special characters, so I get rid of it.
  5. Go back to the client directory, copy all its contents. Come back to Eclipse, select the client package and paste. You now have the source files included in your project. We are almost done.
  6. In step 3, if you had to remove special characters from the module name, then read ahead. Otherwise skip this bullet. Open the original .gwt.xml file, copy its textual contents. Replace the contents of the .gwt.xml file of the newly added module, with the text you just copied. Caution: Don’t replace the contents of the .gwt.xml file of your entry point module.
  7. For the last step, open up the .gwt.xml file of your original project. Add a new inherits declaration with name being the package name concatenated with the module name. My package name was gwt.canvas and my module name was gwtcanvas. So this is how my inherits declaration looks like:image

So that’s it. I bet there is an easier way of doing this. Hope I figure it out someday.


  • Twitter
  • Tumblr
  • Facebook
  • LinkedIn
  • YouTube