Delimiters

acme on 2008-04-07T08:58:51

It has just struck me that the following line of a Template Toolkit template has six different delimiter styles: <, ", [%, (, ', and {:

Add new size
Is this too many or just right?


Just right

Juerd on 2008-04-07T12:12:15

It may not be pretty, but it does make reading the code much easier.

}) %]"> enables the reader to extract information that the single-delimiter style ))))) cannot provide. I think that ))))) is prettier, but less practical.

} is the end of a hash
) is probably the end of a function call
%] is the end of a TT directive
" is the end of text; the following > tells us it's an SGML-ish attribute value
> is the end of an SGML-ish tag

Re:Just right

Aristotle on 2008-04-08T05:52:01

I think that ))))) is prettier, but less practical.

Funny you say that, ‘cause I was just thinking the same about sigils today. Yeah sure, they’re noisy and can gunk up the code – also, they’re extra red tape that needs to be typed over and over. But they make it so much easier to scan the code! Consider something like this:

10 * $bar * log( $foo + 2 + cos( $baz ) )

You can immediately tell apart the “slots” where variable values go, the static pieces, and the function calls, even if without consciously reading the code. Sure, it’s uglier than this:

10 * bar * log( foo + 2 + cos( baz ) )

But the sigil-laden version is definitely easier to scan. In fact, now that I’m laying them out side by side like this, the difference is striking.

I like sigils. Not pretty. But convenient.

Re:Just right

Juerd on 2008-04-08T09:54:34

I had that side-by-side experience when changing the javascript on http://speedtest.aoeu.nl/ to a more perl-like style. Another huge advantage of sigils is that you can use variable interpolation - that's the rare occasion where sigils are prettier than the alternative, lots of concatenated string literals.

Re:Just right

jdavidb on 2008-04-08T15:21:32

I happen to think convenience is pretty.

Maybe just right with a possible tweak

ferreira on 2008-04-07T12:48:48

It may be just right. Mainly because not having so many delimiters could be worse: requiring the need for hard-to-read escaping.

On another note, maybe one of the delimiters of the mentioned template piece could disappear, by writing something like:

a href=[% uri_for('add_size', {product_id => product.id}) | qq %]>Add new size</a>

where the qq filter would add the leading and trailing quotes and would take care of any necessary entity-escaping. It hardly contributes to the original intent of your note, but it improves on the correctness of producing HTML attribute values. (The assumption "this never has characters that need escaping" is an invitation for future pain.)

More ways to do it

Phred on 2008-04-07T18:26:28

There's more than five ways to do it :)

As an aside

grink on 2008-04-08T18:57:05

I like to use a helper function href() , which is a wrapper to c.uri_for, to generate that sort of thing (since it's so common)

<a [% href(...) %]>Lorem ipsum</a>

when I look at the stack...

grendelan on 2008-04-09T00:49:47

when I look at the J2EE stack, or the LAMP stack, or any of the other "web based" shite, I long for the elegance of native GUIs..... NeXTStep in particular. I wonder if us engineers will ever be able to steal things back from the marketing droids.

Quoting specifically, not just delimiting

Crag on 2008-04-09T20:45:26

Those are all quoting characters. Additional delimiters in that string are whitespace, comma, equals sign, => and a period for a total of eleven delimiters.

But quoting in particular is more interesting than delimiting in general. Hofstadter wrote a great book about it. Maybe you've heard of it. :)

Using different quoting syntax for different contexts makes perfect sense to me. Here's what your example would have looked like in a fake-lisp syntax I'm making up right now:

(a (href (uri_for "add_size" (product_id (product id)))) "Add new size")

I tried indenting but it didn't help:

(a
  (href
    (uri_for
     "add_size"
     (product_id (product id))
    ))
  "Add new size")