Medieval Poetry

June 15, 2009 on 10:06 am | In Miscellaneous | No Comments

I read up on Wagner’s Ring Cycle this weekend, so I guess it’s not surprising that I dreamt of epic poetry. Normally, I don’t remember anything I read in my dreams, but in the dream, my wife asked me to explain the first line, and I woke up immediately afterwards, so I was able to reconstruct it from the explanation:

As proceeded the lancing of night’s knees,

My interpretation is that, since lancing the knees would cripple rather than kill, this must refer to the pre-dawn.

Struts for PHP

March 17, 2009 on 7:50 pm | In Programming | No Comments

It’s been about two years since I switched from PHP to JSP/Java. While certainly not the only game in town, Struts2 is pretty nice. For a Java solution, that is :)

What I can’t understand is why so many people want to port such frameworks to PHP. (Rasmus always rails against frameworks — no pun intended.) In Java, the basic functionality is, Write a Servlet which serves a page using println(). Compared to that, configuring Actions and JSP’s via XML is a pleasure. But in PHP, MVC is as simple as putting the PHP code at the top of the file and the HTML below it. I keep them together since they are tightly coupled. If you don’t like to mix them, put the HTML in an .inc file and include it at the end of the PHP code. If you need to serve different pages based on state, e.g. a workflow, a switch statement at the end of the PHP code can load the appropriate .inc file.

The only concept from Struts that isn’t directly available in PHP is the interceptor stack. Luckily, this is easily implemented. First, set up a mod_rewrite rule:

RewriteRule ^(.+)\.php /interceptor.php?target=$0 [QSA]

Then build interceptor.php to execute your interceptor stack before including the target page. If you want to configure which interceptors to execute based on the target, use an XML config file. Bonus points for parsing it once and storing the results in APC to speed up execution!

Update: A friend pointed out the auto_prepend_file directive. Combine this with the information in $_SERVER, and you may not need to use mod_rewrite!

Google

February 21, 2009 on 9:07 pm | In Computers, Crazy ideas | No Comments

My simmering indigestion from the articles about Google by Coding Horror and Whimsley caused me to burp up the thought that Google might actually be adjusting their search results to optimize revenue. I posted a quick comment on Coding Horror and wasn’t planning to mull it over any further, but then I realized that Google must be doing this. The only question is: To what extent?

If a site is displayed in the paid section, it is actually quite reasonable that Google does not show that site in the unpaid section. But what if moving a particular unpaid result to page 2 increases the number of clicks on the paid results? Almost nobody bothers to view page 2 of the results, so the result effectively ceases to exist! Even moving a result to the bottom of page 1 would have a huge impact, since many people probably don’t even bother to scroll down.

It would be easy for Google to do this kind of optimization. They have so much traffic that they could bucket test small fractions to look for significant increases in CTR without anybody noticing. So from the outside, this seems impossible to prove. Google is a black box, so who can say whether a particular result belongs on page 1 or page 2? But the thought that a particular site might effectively cease to exist in order to satisfy Google’s hunger for profit is pretty scary…

Apple Rocks

February 21, 2009 on 8:00 pm | In Miscellaneous | No Comments

My wife admitted a long time ago that iPhoto really good at making photo books. Recently, we were discussing typing in Chinese, and she didn’t believe me when I said that Apple included it as part of the OS, since on Windows, she has to purchase special software. About 5 minutes later, I had it working on my Mac, and she had to admit that Apple really was good. A couple of days ago, she actually uninstalled both Internet Explorer and Firefox from her Windows XP laptop and loaded Safari instead. And she loves it!

Improved Error Reporting in JSP’s

February 4, 2009 on 10:03 am | In Programming | No Comments

JSP’s are a pain because all errors go to the server log instead of the screen. This is not conducive to rapid iteration. In my quest to fix this, I first added a catch (ServletException ex) block to the first filter in the chain defined in my web.xml.

Then I discovered that Tiles swallows errors in included JSP’s. This blog confirmed my conclusions from researching into the Tiles source code. You need to implement a custom JspTilesRequestContext. I prefer to configure via web.xml rather than Spring. It works either way. Note that for Tiles 2.0.x, the online documentation is wrong! The key is org.apache.tiles.context.ChainTilesContextFactory.FACTORY_CLASS_NAMES, not org.apache.tiles.context.ChainedTilesContextFactory.FACTORY_CLASS_NAMES. The latter only works for Tiles 2.1.x.

Another important issue is that displaying an error page only works if no output has been committed. If some output has already been written, you have to append to that. Through trial and error, I discovered that the following will close all possible traps, so the error output will always be visible:

""></script></style></head>

Before Tiles 2.0.6, JspTilesRequestContext throws a plain IOException. For JDK 1.5, this cannot contain a cause, so you must look at the logs for the original exception. Tiles 2.0.6 introduces TilesIOException which does include the cause. This cause is always a ServletException.

The exception that my code actually catches is usually an onion. To get at the root problem, you need to drill in until you find an exception with null cause. To make it more confusing, you must call getRootCause() on ServletException and getCause() on all other exceptions. In order to get a line number in the JSP, you must print all intermediate exceptions. One of them will usually include an excerpt from your JSP.

Semantic Markup

February 2, 2009 on 9:23 pm | In Programming | No Comments

JSP has warts. For example, tags built using Java can contain <&...> in the body, but tags built as .tag files cannot. But JSP is wonderful for building a semantic layer on top of raw HTML. Finally, we can get beyond the assembly code level. It’s really simple, too. All you need is a single Java-based tag which can echo from an arbitrary resource bundle. Pass the resource key (and the bundle name, if you want to be generic) as a parameter to the tag. Presto! As long as the name of the key is semantic, you can write <st:tag type="my-semantic-name">...</st:tag> instead of raw HTML. If you want to be sexier, you can group the tags and leverage the tag name: <st:my-tag-group type="my-semantic-type">...</st:tag>. The practical side of semantic markup now becomes obvious: You can change the raw HTML in one place and your entire application updates to the new markup structure.

Of course, you don’t have to stop there. How about a smart replacement for <head> that automatically includes your CSS and JavaScript? Even better, make it load either the debugging or minified versions based on a cookie (signed, of course) so you can serve minified to everybody else, but turn on debugging for yourself!

Dear Abby

January 27, 2009 on 8:59 am | In Miscellaneous | No Comments

I recently saw an advice column in a newspaper at a friend’s house where a person was asking about how to discuss budding bisexuality with a conservative, Christian family. The advice boiled down to, You are not alone. If you are afraid of abuse, get help. While true, it completely missed the point. A conservative, Christian family will expect that one practices abstinence until marriage, and that marriage is a lifelong, monogamous relationship. If the person intends to do that, then bisexuality would be irrelevant. If not, then that should be the primary topic for discussion. Having sex all willy nilly is a very different lifestyle and carries all sorts of risks, e.g., STD’s.

lolspeak

January 27, 2009 on 8:52 am | In Programming | No Comments

I finally have a project on github: lolspeak — it translates text into lolcats speak.

Scripting vs Coding

January 26, 2009 on 8:45 pm | In Programming | No Comments

Jeff Atwood’s latest blog, A Scripter at Heart, got me thinking. A colleague of mine recently expressed the same sentiments, namely that he preferred the instant gratification of a Basic interpreter. Looking back, I was the opposite. Yes, it was painful to switch from Applesoft Basic to FORTRAN (on a Mac, no less — Absoft had a very clever hack for integrating with MacOS), but the reason I never looked back was that by the time I switched in my senior year in high school, I was writing programs that were so large and complicated that they didn’t even fit in the available 48K on my Apple ][+. I was using chain to preserve the data whenever the program needed to load a different code segment. You can imagine how desperately I needed real subroutines instead of GOSUB! So when I could compile a complete program into one unit and get compile-time checking to make sure I was calling each subroutine correctly, I was in heaven. Besides, the pain of switching from a command input application model to an event driven application model so heavily outweighed any pain induced by the need for compilation and linking that I don’t think I actually noticed :)

And by the time I switched to Think C and its object model, I was again desperate, because the complexity had long since outgrown the procedural model…

Snow

January 26, 2009 on 11:34 am | In Family | No Comments

We drove up to Mount Wilson this afternoon to give our daughter her first taste of snow. It’s a rather rare commodity here in sunny Southern California. The biggest patch we found was about the size of two bathtubs.

It was especially interesting because the top of the mountain was inside a thick cloud. It felt like a completely different world as we built tiny snowmen and tossed snowballs inside our little bubble of visibility.

Hopefully, we’ll make time for a longer trip soon…

Next Page »

Powered by WordPress with Pool theme design by Borja Fernandez.
Entries and comments feeds. Valid XHTML and CSS. ^Top^