Trevor Burnham

Sure, it works in practice…

Entries Tagged as 'coding'

Flicker-​​free Typekit

January 28th, 2010 2 Comments

I’m a fan of Typekit. Being able to go beyond the old Arial-​​Verdana-​​Times-​​Georgia paradigm, on any modern browser, without Flash (unlike sIFR), and with selec­table text (unlike Cufón), is a dream come true.

One problem, though: It takes a few mil­lisec­onds to load up and render those fonts. That’s not so bad in itself; what’s bad is that the browser renders every­thing in non-​​Typekit fonts first, creating an annoying flicker every time the page loads as the text gets replaced by fancier fonts.

For­tu­nately, there’s a fairly easy solution (though not an offi­cially sup­ported one—see below). Just add this to your site body:

1
2
3
4
5
6
7
8
9
10
11
12
13
<body id="domain-com">
  <script type="text/javascript" src="http://use.typekit.com/KIT_ID.js"></script>
  <script type="text/javascript">
    document.getElementById('domain-com').style.opacity = 0;
    setTimeout("document.getElementById('domain-com').style.opacity = 1", 1000);
    Typekit.load('KIT_ID', {
      afterLoad: function(data) {
      	setTimeout("document.getElementById('domain-com').style.opacity = 1", 1);
      }
    });
  </script>
  <!-- content goes here --> 
</body>

Then sub­sti­tute your actual domain for domain-com, and (impor­tant!) your site’s unique Typekit ID for KIT_ID. You can get this ID by logging in to Typekit, launch­ing the Kit Editor, and clicking the “Embed Code” link; it’s an 8-​​letter alphanu­meric combination.

Here’s a breakdown:

  1. The <body> ID on line 1 isn’t just a good selector; it’s good form. The most common con­ven­tion is to use your domain (with a dash instead of a dot), which makes things easier for the Grease­mon­key types.
  2. The script include on line 2 brings in the Typekit descrip­tion of your site’s kit. You can move this line up to your <head> section if you want.
  3. Line 4 makes your entire site dis­ap­pear. Because this is at the top of your body, nothing is going to get rendered. It’s impor­tant that this line be here, rather than in another file or even at the foot of your body.
  4. The call to Typekit.load does the actual loading of the fonts, and the afterLoad callback will be executed right after that happens. That’s when we want to make the body opaque again, right? Well, not quite. It seems that there’s a small delay between the callback and text refresh. For­tu­nately, using setTimeout to add a tiny delay (1ms) seems to elim­i­nate this, finally ridding us of the dreaded flicker.

Caveat: The afterLoad event is cur­rently con­sid­ered exper­i­men­tal. An official solution for avoiding flicker is likely to be added in the future.

Tags:      

The Most Important IDE Feature

January 24th, 2010 Comments Off

Say you’re working on a project with hundreds of files. Changes to one file neces­si­tate changes in others in an unfath­omably complex tree struc­ture. Using File -> Open each time you need to switch files is time-​​consuming, which is why simple text editors have been widely aban­doned by pro­gram­mers in favor of inte­grated devel­op­ment envi­ron­ments (IDEs) that offer tabs and a widget that shows the file struc­ture at all times. This, more than syntax high­light­ing, is the reason why few projects are edited in Notepad.

But that’s still not enough. Why navigate the depths of a file tree when you can simply type the name of the file you want? This is the most impor­tant feature an IDE can offer. In Eclipse, it’s called Open Resource. In TextMate, it’s called Go to File. In Coda and Apple’s own XCode, it’s aptly dubbed Open Quickly, and goes one step further by per­form­ing full-​​text search on the files in the current project using Spot­light. Per­son­ally, I prefer the instan­ta­neous of the Eclipse/​TextMate approach; but either way, it’s cer­tainly faster than using the mouse.

If you’re a devel­oper who isn’t using this feature, find it. It will change your life at least as much as I’m guessing Quick­sil­ver already has.

Tags:  

Smalltalk Lives!

January 5th, 2010 Comments Off

Byte magazine’s classic Smalltalk coverI was under the impres­sion that these days, you’re more likely to find Smalltalk in the History depart­ment than the Computer Science depart­ment. That is, until:

  • I read a great inter­view with Dan Ingalls, the “mother of Smalltalk” (Alan Kay is the father) in the excel­lent Coders at Work, in which he dis­cusses all the neat, fun things he’s been doing with the language lately, like Lively Kernel, a browser-​​based pro­gram­ming learning envi­ron­ment, and Squeak, which powers the One Laptop Per Child laptops, among other things.
  • I learned that Smalltalk powers DabbleDB. DabbleDB is a cool site that hosts spread­sheets in a much more web-​​like way than services like Google Spread­sheets, which are just trying to ape Excel in the browser (just as Excel aped Lotus 1–2-3, and Lotus 1–2-3 aped VisiCalc…).
  • All the other geeks inter­viewed in Coders at Work and Mas­ter­minds of Pro­gram­ming seem to like Smalltalk. C and its descen­dants (C++, Java…) get a lot of bashing, but Smalltalk is typ­i­cally treated with unadul­ter­ated admi­ra­tion. If lan­guages were on Rotten Tomatoes, Smalltalk would be 92% fresh.

I don’t think Smalltalk is going to make a Big Comeback (newer lan­guages like Scala are just too cool), but it’s def­i­nitely going to stick around for a while. And unlike the sticky, mal­odor­ous remnants of COBOL, I have no objec­tion to the clean, classic scent of Smalltalk.

Tags:  

PHP Must Die

January 2nd, 2010 Comments Off

PHP > RubyI’ve been working on a mockup for The­o­ryville, and I wanted to throw together a server-​​side script to respond to some simple AJAX requests. My first thought was to do it in PHP, but then I remem­bered that PHP is the least elegant language in the world. So, I figured, I’ll do it in Ruby! I wrote what I needed, uploaded it to the Apache server, and I was done—except for the minor detail that Apache wasn’t running the code.

I figured that fixing the problem would be a simple matter of adding an AddHandler line to the Apache con­fig­u­ra­tion. At worst, installing some sort of mod_​ruby. No such luck. Turns out mod_​ruby was aban­doned many moons ago. The reason? Effi­ciency. Starting and shutting down an appli­ca­tion every time someone makes a query is a stupid, grossly inef­fi­cient way of doing things. But guess what? For many appli­ca­tions, folks don’t give a flying fork how many processes they spawn. They only care about being able to upload some­thing to their existing web space and have it just work.

Example: You could write a fan­tas­tic new blogging platform in Ruby, but would it have Wordpress’s Famous 5-​​Minute Install? No shell access required! Just upload it, go to the instantly avail­able web inter­face, and tell it how to find your database. You know, the one you set up using php­MyAd­min?

Look, the Ruby com­mu­nity is rightly proud of Rails. I’m not saying that large or high-​​traffic web appli­ca­tions should run on PHP (though Wikipedia and Facebook somehow manage). But if you’re a Ruby­van­ge­list, you need to fight on every front you can—including the just-​​upload-​​it-​​to-​​Apache front. It’s great that you’ve made installing and running a spe­cial­ized web server take just 4 shell commands. But for most folks, that’s 4 too many.

Rubyists, I urge you: you need to bring back mod_​ruby, for all our sakes. Anything PHP can do, Ruby can do better.

[Addendum: It’s worth men­tion­ing that there is a solid mod_​python, and it behooves all Apache admin­is­tra­tors to install it. Also, I was able to rescue my Ruby script the old-​​school way: CGI. I added #!/usr/bin/ruby as the first line of the file and changed the exten­sion to .cgi. I feel slightly dirty, but it works.]

[Update 1/​9/​10: While I thought the title was original (at least, to the extent that any hyper­bolic title is), it turns out there was an excel­lent post also called PHP Must Die back in the dark days of 2004 about how Python and even *shudder* Perl are better lan­guages than PHP. I took this fact as a given for this post. Via Hacker News.]

Tags:  

Always Be Exploring

December 29th, 2009 2 Comments

Anti-syphilis poster: Get tested!This post by math/​CS prof Peter Boothe caught my eye:

What I am claiming here is not just that if you code faster, you will get your solution written faster. I am claiming if you code faster, you will get your solution done MUCH faster. The speedup is greater than linear.

I agreed with the sen­ti­ment, but I wasn’t sure about his expla­na­tion. Then I read this comment from Matt Kangas:

IMO, the key point is: teach exploratory programming.

That’s the answer! Here’s my expe­ri­ence: I took exactly one CS class as an under­grad; it was so arduous, so time-​​consuming, that I vowed to myself: Never again. But I kept being drawn to various pro­gram­ming projects, so I learned to code com­pe­tently on my own time. At some point last summer, I think I hit the 10,000 hours of practice that Malcolm Gladwell posits as a heuris­tic pre­req­ui­site for being really good at some­thing. One thing I learned around hour #7,500 was the impor­tance of testing pieces of code inde­pen­dently as much as possible when building a larger project. Begin­ners always work at the appli­ca­tion level: Change some­thing, re-​​run the appli­ca­tion, see if it works. This is some­times nec­es­sary (if, say, you’re changing the UI), but should be avoided whenever possible. Experts tend to write code in small, unit-​​testable pieces. Many are test-​​driven.

Not every test has to be a formal unit test. Just trying out a line or two of code on its own and watching what happens is often useful. This is yet another reason why Java is dead to me as a language. Java is so slanted toward BDUF that when it sprang forth Athena-​​like from the head of James Gosling, it was blessed (?) with a formal standard for doc­u­men­ta­tion, but no command line interpreter—a feature offered by just about every other language, includ­ing Java’s spritely yet statically-​​typed spawn, Scala.

For those of you building anything big enough for Hofstadter’s Law to apply, my #1 piece of advice would be this: Get your code tested, today!

Tags:  

Bridging the Web

December 27th, 2009 Comments Off

Python is a much, much better language than JavaScriptI’ve been reading a lot about pro­gram­ming lan­guages lately. Many exciting new lan­guages have come out in recent years, and the use of multiple spe­cial­ized lan­guages within a single project has become much more common, thanks largely to the polyglot JVM and .NET plat­forms. It’s an exciting time to be a programmer.

Unless, of course, you’re pro­gram­ming user inter­faces for the web. Which everyone is.

As Jeff Atwood has said, “Every­thing that can be pro­grammed in JavaScript will be.” That’s because the browser is the most popular oper­at­ing system on the planet, and JavaScript is its lingua franca. But no one likes JavaScript. Or, more pre­cisely, no one falls in love with JavaScript the way that people fall in love with Ruby or Python or (my favorite of late) Scala.

I never fully under­stood why JavaScript was so kludgy and inel­e­gant until I read the (highly rec­om­mended) inter­view col­lec­tion Coders at Work. Long story short, one guy at Netscape (Brendan Eich) threw the language together in ten days, with the stated goals of creating some­thing seman­ti­cally Scheme–like but syn­tac­ti­cally Java-​​like. Good god!

So why are we still, 14-​​odd years later, still using the same language for all of our webapps? Because even if some backwards-​​incompatible NewScript were intro­duced, stan­dard­ized and embraced by Microsoft, Mozilla, Google and Apple tomorrow, most people would take years to update to a browser suf­fi­ciently new­fan­gled to run it. Heck, most people don’t even know what a browser is. Thus, new webapps have to be written in a half-​​baked language to be run through decade-​​old interpreters.

But there is a solution. And for all I know, Sergey Brin and Larry Page are already plotting its exe­cu­tion from their orbital bat­tlesta­tion. (Where did you think those Google Earth pics come from?) What we need is a new language for web browsers (or, better yet, a virtual machine standard that could support multiple lan­guages), along with a compiler that can generate JavaScript for the benefit of old-​​timers. The project would be insanely dif­fi­cult, but it’s half-​​done already. Look at GWT, which lets you write Java that compiles to JavaScript. Now suppose that a browser (say, Chrome 5) could run GWT’s Java bytecode directly, bypass­ing JavaScript alto­gether. Chrome 5 users would enjoy a much smoother and poten­tially richer web expe­ri­ence. Mean­while, older browsers would still present a per­fectly usable webapp. And pro­gram­mers every­where would rejoice.

That, I hope, is the future of the web. So please, Google, I urge you: Don’t let my children grow up in a world that runs on JavaScript.

Tags: