tags in Categorias
cine deportes Fútbol Fama hoteles informatica internet musica Paquetes Vacacionales TV viajes vuelosTop weblinks
It's not the languages, but their idioms that matter
Some things that you can say eloquently in one (human) language, may not sound
as nice when you translate to another language. Idioms add spice to conversations
in a language
(and when overused they turn into cliches). Idioms don't take the aggregated meaning
of the
words that make them. Their meaning's shaped by culture, tradition, and history.
Idioms make learning a language interesting, tricky, and fun.
One of the challenges of picking up a language is learning the idioms.
The same applies to computer languages as well. Their "idioms" make them fun to learn.
You may accomplish a task in a number of ways in a language. When you're new, you
may try out
some ways you're used to in other languages. Learning that "language way" of doing
things is, however,
exciting. We need to take the time to learn the idiomatic differences and there in
lies the fun.
Here is a little swing code in Java (I can reduce this to one method and use anonymous
inner classes
for the event handler, but Groovy would choke on that, so I decided to keep the code
like below for
the purpose of this example).
import javax.swing.*;
import java.awt.event.*;
import java.util.Date;
public class SwingIt implements ActionListener
{
JLabel myLabel;
public void actionPerformed(ActionEvent ae)
{
myLabel.setText(new Date().toString());
}
public void showExample()
{
JFrame frame = new JFrame();
frame.setTitle("Swing It");
frame.setLayout(new java.awt.FlowLayout());
frame.setSize(300, 200);
frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
myLabel = new JLabel("Test");
JButton myButton = new JButton("Click Me");
myButton.addActionListener(this);
frame.getContentPane().add(myLabel);
frame.getContentPane().add(myButton);
frame.setVisible(true);
}
public static void main(String[] args)
{
new SwingIt().showExample();
}
}
[
Like I said above, I can eliminate the actionPerformed method in SwingIt class and
replace
the event listener code as:
myButton.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
myLabel.setText(new Date().toString());
// This will require myLabel to be declared final in showExample
}
}
)
]
The above code pops up a frame with a label and button.
When you click on the button, it displays the current date and time in the label.
Almost all Java code is Groovy code. So you can literally copy the above file to
SwingIt.groovy and type groovy SwingIt.groovy to execute the code in Groovy.
But, that's no fun. We've not really seen the Groovy way of doing that.
So, let's start by stripping out code that's not needed, after all, Groovy is
a light weight language with high signal-to-noise ratio. Also, we will make some
slight changes to the way we will write the event handler addActionListener.
So, here is the modified code SwingIt2.groovy:
import javax.swing.*
import java.awt.event.*
JFrame frame = new JFrame()
frame.setTitle("Swing It")
frame.setLayout(new java.awt.FlowLayout())
frame.setSize(300, 200)
frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE)
myLabel = new JLabel("Test")
JButton myButton = new JButton("Click Me")
myButton.addActionListener( { myLabel.setText(new Date().toString()) } as ActionListener
)
frame.getContentPane().add(myLabel)
frame.getContentPane().add(myButton)
frame.setVisible(true)
The code that catches my eyes is
myButton.addActionListener( { myLabel.setText(new Date().toString()) } as ActionListener
)
Here the code block within {} is treated to implement the ActionListener interface.
Can we do better? Groovy provides markup builders and there's one for Swing (think
of this
as DSL for Swing in Groovy). Here's SwingIt3.groovy:
bldr = new groovy.swing.SwingBuilder()
frame = bldr.frame(
title: "Swing It",
layout: new java.awt.FlowLayout(),
size: [300, 200],
defaultCloseOperation: javax.swing.WindowConstants.EXIT_ON_CLOSE)
{
myLabel = label(text: 'Test')
myButton = button(text: 'Click Me', actionPerformed: { myLabel.setText(new
Date().toString()) } )
}
frame.setVisible(true)
I was chatting with Neal this
afternoon, and he mentioned Paul
Graham's discussion on Language Succinctness.
Groovy code is more succinct than Java code.
How would the code look like in JavaScript (With Java 6 and JSR 223, you can write
Java-platform code in one of
several different languages including JavaScript)? Let's take a look.
var swingpkg = new JavaImporter(javax.swing, java.awt, java.io, java.lang)
with(swingpkg)
{
var frame = new JFrame()
frame.title = "Swing It"
frame.setLayout(new FlowLayout())
frame.setSize(300, 200)
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)
myLabel = new JLabel("Test")
frame.getContentPane().add(myLabel)
myButton = new JButton("Click Me")
myButton.addActionListener(
function() { myLabel.text = new Date().toString()
}
)
frame.getContentPane().add(myButton)
frame.setVisible(true)
Thread.sleep(10000)
}
To run the above code, copy it into SwingIt.js and run jrunscript SwingIt.js (I'm
running Java 6 for this).
The code that caught I eyes here is:
myButton.addActionListener( function() { myLabel.text = new Date().toString()
} )
How about trying this same example in JRuby? Here is SwingIt.rb. To run it type jruby
SwingIt.rb:
require 'java'
module Java
include_package 'javax.swing'
include_package 'java.awt'
include_package 'java.awt.event'
include_package 'java.util'
end
my_label = Java::JLabel.new('Test')
my_button = Java::JButton.new('Click Me')
my_button.addActionListener(
Java::ActionListener.impl { |name, *args| my_label.text = Java::Date.new.toString()
}
)
frame = Java::JFrame.new
frame.setSize(300, 200)
frame.setLayout(Java::FlowLayout.new)
frame.getContentPane().add(my_label)
frame.getContentPane().add(my_button)
frame.setVisible(true)
Here the code that caught my eyes is:
my_button.addActionListener(
Java::ActionListener.impl { |name, *args| my_label.text = Java::Date.new.toString()
}
)
Let's take a look at the event handling code in these languages:
Java:
myButton.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
myLabel.setText(new Date().toString());
// This will require myLabel to be declared final in showExample
}
}
)
Groovy:
myButton.addActionListener( { myLabel.setText(new Date().toString()) } as ActionListener
)
or
myButton = button(text: 'Click Me', actionPerformed: { myLabel.setText(new Date().toString())
} )
JavaScript:
myButton.addActionListener( function() { myLabel.text = new Date().toString() } )
JRuby:
my_button.addActionListener( Java::ActionListener.impl { |name, *args| my_label.text
= Java::Date.new.toString() } )
All these code do the same thing. However, each of them is idiomatically different
(eloquent) in their own languages.
Writing Java code in Groovy (or JRuby) is like me trying to translate an idiom from
my mother tongue Tamil to English.
That generally sounds real awkward and loses the essence. To say it in English, I
would simply choose to use plain English
or find a suitable English idiom. Like wise, we need to figure out the idiomatic way
in languages we choose. And that is
what keeps me excited and I hope it does you as well.
