Sunday, August 17, 2008

Relegated Brace Style

Have your gander at this JScript.NET:


 1 import System;
 2 import Accessibility; // ??
 3 import System.Windows.Forms;
 4 import System.Drawing;
 5
 6 var title = "top secret";
 7 var textfield = new TextBox();
 8
 9 function foreach( arr, fn ) {
10     forvar k in arr )
11         fn( arr[k] );
12 }
13
14 class main extends Form
15 {
16     function main() {
17         Text = title;
18         ClientSize = new System.Drawing.Size( 500, 380 );
19
20         textfield.Dock = DockStyle.Bottom;
21         textfield.Multiline = true;
22         textfield.Height = 106;
23         this.Controls.Add( textfield );
24     }
25
26     protected override function OnPaint( e: PaintEventArgs ) {
27         foreach( wordwise( textfield.Text ), function( word ) {
28             print( word + " in bed" );
29         } );
30     }
31 }
32
33 function wordwise( str ) {
34     return str.split( /[\s\n\t]+/ );
35 }
36
37 Application.Run( new main() );


Don't worry about what it does, no one cares. Instead notice how ugly the braces make it look. The C-style open-close brace syntax, along with not being able to have arbitrary items as keys in hashes, are the two things that are unfortunate about Javascript, and are two things that its cousin Lua doesn't have. In fact let's see how this code would look like in Lua.


 1 require "System";
 2 require "Accessibility"; -- ??
 3 require "System.Windows.Forms";
 4 require "System.Drawing";
 5
 6 local title = "top secret";
 7 local textfield = TextBox.new();
 8
 9 local main = {};
10
11 main.new = function()
12     local obj = {};
13
14     obj.Text = title;
15     obj.ClientSize = System.Drawing.Size.new( 500, 380 );
16
17     textfield.Dock = DockStyle.Bottom;
18     textfield.Multiline = true;
19     textfield.Height = 106;
20     obj.Controls.Add( textfield );
21
22     obj.OnPaint = function( e )
23         for word in wordwise( textfield.Text ) do
24             print( word + " in bed" );
25         end
26     end
27
28     setmetatable( obj, { __index = Form } );
29
30     return obj;
31 end
32
33 function wordwise( str )
34     return string.gmatch( str, "[^%s]+" );
35 end
36
37 Application.Run( main.new() );


Or something like that. What matters is that there is nary a brace to uglify things. Note: Semicolons are optional in both languages. Ah, but there is a way to save Javascript and all other braced languages. I call it the Relegated Brace Style. To do it, simply put all braces, both opening and closing, far to the right of the page:


 1 import System;
 2 import Accessibility; // ??
 3 import System.Windows.Forms;
 4 import System.Drawing;
 5
 6 var title = "top secret";
 7 var textfield = new TextBox();
 8
 9 function foreach( arr, fn )                                       {
10     forvar k in arr )
11         fn( arr[k] );                                             }
12
13 class main extends Form                                           {
14     function main()                                               {
15         Text = title;
16         ClientSize = new System.Drawing.Size( 500, 380 );
17
18         textfield.Dock = DockStyle.Bottom;
19         textfield.Multiline = true;
20         textfield.Height = 106;
21         this.Controls.Add( textfield );                           }
22
23     protected override function OnPaint( e: PaintEventArgs )      {
24         foreach( wordwise( textfield.Text ), function( word )     {
25                print( word + " in bed" )                          }
26         );                                                        }}
27
28 function wordwise( str )                                          {
29        return str.split( /[\s\n\t]+/ );                           }
30
31 Application.Run( new main() );


Slick eh? It's even better because Vim has its horizontal scroll bar disabled by default, so you can readily muster enough pretension to make the braces disappear completely, or at least be dead to you.

No comments: