Archive for the ‘Uncategorized’ Category

The case against snippets

Monday, August 18th, 2008

Back when TextMate was relatively new and that famous video of ruby on rails was making the rounds. I watched in amazement with the rest of the nerds as code flew around the screen. I had caught Snippet Fever.

After using TextMate for a while I became addicted to snippets. The basic idea of a snippet is that you can write a few characters and they expand to whatever you want. For example, you can type: ‘fl’, hit Tab and TextMate will write:

  for(int i = 0; i < ... ; i++) {
    ...		     
  }

Where ... is where the insertion point will be each time you hit tab. You can do the same with Emacs in many different ways: Emacs skeletons, the snippet.el plugging, hand-coded elisp, etc. The point of this post, however, is not to show all the different ways that you can get snippets. The point is to ask whether it’s worth using snippets at all.

Snippets eliminate the need to type recurrent idioms. If you have to write accessors, it’s very pleasant to type ’set’, hit tab and watch the screen fill with 20 lines of code that just wrap a field in a class with a setter function. But this is fixing the problem in the wrong place.

The fundamental problem is that the language is too damn verbose.

Imagine English had no pronouns so if I wanted to tell you a story about my friend Tim and me, I had to write:

Tim had called Oscar to ask Oscar if Oscar could lend Tim the book that Oscar had bought the day before. Oscar told Tim that Oscar was still reading it, but that Oscar would be happy to lend Tim Oscar’s book when Oscar was done with the book so that Tim could read it.

The above paragraph is too verbose. It’s hard to write. Snippets would help me write it by automatically filling all the appropriate places with “Tim” and “Oscar”.

If the language you are using is so verbose that you need snippets, how about fixing the language?

Even on languages that don’t allow fixing the syntax you can still do something.

For example, in C, if you find yourself typing a for loop to access a list over and over again:

  for (int i = 0; i < length(list); i++) {
 
  }

Make a small pre-processor macro that abstracts that:

  #define EACH(list) for (int i = 0; i < length(list); i++)

And now you can write:

  EACH(list) printf("%s\n", list[i]);

And since code is usually read many more times than it is written, you have helped make the source base a little better.

Hidden Gems in C99 (1)

Friday, April 11th, 2008

After some late night reading of the C99 spec, I’ve found quite a few hidden gems. I’m going to start posting some of these. Since it’s late, I’ll just post a teaser.

On page 64 of the C99 standard it says:

In all aspects of the language, the six tokens

<: :> <% %> %: %:%:

behave,respectively,the same as the six tokens

[ ] { } # ##

except for their spelling.

Really? Then let’s try this program:

%:include <stdio .h>
int
main(int argc, char *argv<::>)
<%
	printf("hello world\n");
%>

Compile it an run it:

dirac src $ gcc main.c
dirac src $ ./a.out
hello world

Whaddaya know… I know, I know… useless. Wait for the next post then.

Haskell

Friday, April 11th, 2008

At the intersection between Haskell Programmers and VS.NET Users

Lazy-loading emacs functionality

Wednesday, January 2nd, 2008

Quick, how big is your .emacs file? How long does it take emacs to load? A few days ago I found that my .emacs file had slowly grown to the point where it was taking emacs a significant amount of time to load. Something needed to be done.

A quick glance at the file told me I was loading a lot of modules that I seldom use. For instance, I occasionally write some code in Common Lisp or Haskell, so naturally I was loading slime and the haskell environment. I commented those out.

However, after commenting them out, I quickly realized that not loading them was problematic. When I wanted to use them, I had to open my .emacs file, uncomment the relevant portion, and M-x eval-region the code.

Then it occurred to me that I had a clear entry point for some of these modules. When I write Common Lisp, I usually start by loading SLIME’s REPL by running M-x slime. I came up with the following code:

(defun slime ()
  (interactive)
  (add-to-list 'load-path "~/emacs/slime")
  (setq inferior-lisp-program "/usr/local/bin/sbcl")
  (require 'slime)
  (slime-setup)
  (slime))

Which basically sets up my slime environment and then loads slime. Note that the slime function itself is redefined as part of the evaluation of (require 'slime), so although this looks like a recursive call, it’s not.

I’ve used this trick in several other cases and I think it’s kind of neat. It helps me keep Emacs loading fast, but I still have all the bells and whistles available.

Leave a door open (for Peace)

Saturday, December 8th, 2007

John Lennon

Today is John Lennon’s death anniversary. He was shot to death on December 8th, 1980. I always commemorate this day by listening to John Lennon’s music, smoking unfiltered Gitanes, playing guitar, and letting my hair grow long… for a day… ok, not really, but I do try to listen to Imagine, at least once.

At any rate, there is a website now to commemorate John. It has a letter from Yoko Ono and a video from John Lennon.

The goal of an artist is to elicit some kind of emotion from the audience through his art. You cannot see the video and feel nothing. I left it running in the background while doing other things as I’ve seen Lennon interviews thousands of times and can usually remember how they go, but the imagery at the end, after John stops talking, pulled my eyes and engaged me like nothing has for a long while. I literally couldn’t stop watching.

At any rate, here’s to you John Lennon. You were the Walrus.

Norton Fighter

Wednesday, December 5th, 2007

And on a lighter note… I found this clip hilarious.

Part 1:

and part 2:

If I used Windows, I’d buy norton antivirus just because of this ad.

Timeo Facebook et dona ferentes

Wednesday, December 5th, 2007

Which can be loosely translated as “I fear Facebook even if it brings gifts”. My friends have noticed that for quite some time I’ve stopped updating my Facebook status, or even interacting with the site altogether. The reason is simple. I don’t trust them.

A short time ago, Facebook introduced a new feature called “Beacon” that lets websites send status to your mini-feed. It immediately drew a lot of criticism because of its invasion to privacy, to the point that many people figured out ways to block it. Facebook’s CEO apologized for it today and made beacon opt-out.

Now, I was going to write a detailed article about why this bothers me and how insincere the apology sounded, but our good old friend Fake Steve Jobs did a much better job in this blog post. Money quote:

These guys are like Google, only their slogan isn’t “Don’t be evil” — it’s “Don’t get caught.”

And later:

The smarmy fake apology is not at all reassuring and seems to have been written by PR people who were trying to imitate a 23-year-old kid who’s speaking from the heart and trying to sound all sheepish and aw-shucks — except the flacks can’t do it because they’re as insincere and stage-managed as as the Facebook guys. Plus, let’s face it, the flacks are getting paid in Facebook equity, right?

I couldn’t have said it better myself. You should go read the whole post.

The Apple Experience

Monday, October 1st, 2007

Last week I was once again sucked into the argument of whether Apple is a hardware company or a software company. My take on it: it’s neither.

Apple is an “experience” company. They do hardware because it’s the only way to have the software under control. They do software because without good software, the hardware is useless. What they ultimately worry about is the user experience.

They partnered with AT&T because they needed to add the cellular network to the experience and they couldn’t build their own (they would have loved to). If they look like control freaks from the outside it’s because, well, they are.

On that note, in this piece, Saul Hansell argues that AT&T allows applications on other phones, except the iPhone. Saul claims that Mark Bercow, senior vice president of business development for Palm, encourages developers to do pretty much as they please with the Palm Treo:

Indeed, some developers have made video streaming applications, a particularly big bandwidth hog. (Of course, the cellular companies are free to charge whatever they wish for bandwidth use.)

Now go to the AT&T website and try to find their data rates. I could find a $24.99 data rate that was limited to 10MB, and an unlimited plan (with some fine print about per-kilobyte charges) for $69.99. That is in addition to the cost of the plan. The iPhone unlimited data plan is $20 per month. So while developers can create apps for any other AT&T phone, customers get charged per kilobyte. Now consider again what Steve Jobs said about third-party apps:

You need it to work when you need it to work. Cingular doesn’t want to see their West Coast network go down because some application messed up.

My take on that is that AT&T doesn’t want to see their network swamped with data transfers without being able to charge for them. However, a per-kilobyte service plan would have totally ruined the iPhone experience for the users. Apple wanted users to experience having a device that is always connected to the Internet. That experience is ruined if you’re worrying about how much you’re accessing the net because your service provider charges you per-kilobyte.

The reason Apple is so successful is because using their products is a pleasurable experience. That’s their trick, they think about the “experience”.

Meet the new Blog, same as the old Blog

Friday, July 27th, 2007

As you can see, I finally bit the bullet and moved my blog to my own domain. I plan to do the same with the website (it’s still being hosted at Galileo University). The website needs a lot of work. It’ still so… twentieth century!

The blog is now running on WordPress and I have to admit it was trivial to install. The admin interface is also pretty decent.

My one complaint is that the WYSIWYG editor that they ship seems to mangle my HTML pretty bad, to the point that I have to use Emacs to fix it, so I won’t use it.

One big improvement is that the code snippets now have syntax highlighting. Awww, look at those beautiful colors. I’ll be posting more code snippets now that looking at them doesn’t hurt my eyes.

Well, that’s all for today. Don’t forget to subscribe to the new RSS Feed and to cancel the old one. I won’t be posting to blogger anymore.

Code Tells You How
Revision History Tells You Why

Wednesday, December 20th, 2006

I had a little rant on commenting style and it seems that Jeff Atwood has beat me to it. Well, here’s my rant anyway.

There are three different components I care about when reading code: “The How”, “The Why”, and “The What”.

“The How” is the code. The very essence of code is to tell the computer how to do something. It’s not ‘what’ to do, because that is open to interpretation, you might be trying to do something (what) and telling the computer to do something else (how). I don’t need comments for the how, I can read the code. An example of a bad “how” comment is:

/* assign 0 to all flags */
for (i = 0; i < length; i++) things[i]->flag = 0;

I can see you’re setting all flags to zero. Thanks for the non-information.

“The What”. This is the interpretation of “The How”. Basically, it’s what that code is trying to accomplish. For instance:

/* clear all flags */
for (i = 0; i < length; i++) things[i]->flags = 0;

At least now I know what zero means. But it still doesn’t tell me a lot. As a matter of fact, it’s rare when I need to know “The What”. If I need comments to know “The What”, the code is probably poorly written. I should be able to infer “The What” from “The How”.

“The Why”. This is what I really care about. Why is this piece of code like this? Why not some other way? What was in the programmer’s mind when he wrote that piece of code? What alternatives were considered and discarded? Why were they discarded?

/* We're on a deadline and I don't understand why the flags
 * array is getting corrupted. Clearing all flags seems to
 * fix it, but there is a deeper issue here that needs
 * investigating
 */
for (i = 0; i < length; i++) things[i]->flags = 0;

Ah, much better now. I’ve gained some insight into the programmer’s mind, so later when I’m working on the code I can make sense of this line. Basically, I can answer the question: “Why was this added?”

But wait, there are problems with this approach.

First of all, you’re using up 5 lines of comments for 1 line of code. That’s verbose, but not the worst of the problems.

What’s worse is that if I’m just skimming the code because I’m trying to solve some other problem for which I don’t really care about this particular line, you’re going to make me stumble. I’m going to read that comment, remember that I once knew of a way in which the flags could get corrupted and spend the next 1/2 day chasing an issue that has a workaround. Your nice comments are on my face when I don’t need them.

Even worse, this style also leads to comments needing maintenance. The code will change, and the comment won’t be updated with the change. You’ll get

/* We're on a deadline and I don't understand why the flags
 * array is getting corrupted. Clearing all flags seems to
 * fix it, but there is a deeper issue here that needs
 * investigating
 */
for (i = 0; i < length; i++) {
        if (things[i]->flags & FLAG_INIT) things[i]->flags = 0;
}

Whoa there buddy! You’re saying that flags were getting corrupted and now you’re using them? How can you trust them? Either the comment is wrong or the code is wrong.

What probably happened was that some programmer didn’t read the comment (or read it but forgot to change it) and found a convenient place for putting his change. Now understanding this piece of code has been made harder than it should be.

And all of this is just looking at code statically. Code is organic, it grows, it changes, it evolves.

There aren’t a lot of comments in the BitKeeper source base because we rely heavily on the Source Control system to answer the “Why” questions. So you’ll see the above code as:

for (i = 0; i < length; i++) things[i]->flags = 0;

No comments whatsoever. However, if you need to understand what was going on, you can get the annotated version:

ob 1.123.21: for (i = 0; i < length; i++) things[i]->flags = 0;

and see the comments for the ChangeSet 1.123.21:

ChangeSet@1.123.21, 2006-07-21 10:45:34, ob +1 -0
  Fix bug 2004-10-21: app crashes when restarting
  src/main.c@1.23, 2006-06-29 19:40:46, ob +1 -0
    We're on a deadline and I don't understand why the flags
    array is getting corrupted. Clearing all flags seems to
    fix it, but there is a deeper issue here that needs
    investigating

Now the information is there, but it’s not in my face unless I need it.

Note that when looking at code this way, the question “What changed?” is trivially answered by the diffs and therefore doesn’t need to be answered by the comments. E.g. the following are really bad comments:

src/main.c@1.23, 2006-06-29 19:40:46, ob +1 -0
  Add code to clear the flags

Well, duh! I can see that from the diffs!

@@ -274,6 +278,7 @@
   things = getThings(x, y, z);
   length = getLenght(things);
+  for (i = 0; i < length; i++) things[i]->flags = 0;
   doStuff(things);
   }

I can see that you added code to clear the flags, why did you do it? That’s the interesting bit.

Technorati Tags: ,