Wednesday, October 19, 2011

Which Programming Language? CoffeeScript, Java, PHP


Today we are going deeper on the importance of feedback when choosing a programming language.

Did you try to spike a little with a new programming language? A very useful way to do it is through the measure of feedback you have from the language and its environment or ecosystem.
So if you tried to download, install, compile... (stopwatch at hand) good! If you didn’t, I’m going to free you from the effort through this and other next posts, so keep in touch.

Which Language?

Let’s consider two “old” languages: Java and PHP and a new one: CoffeeScript

Which Platform?

I’m working on a Mac, and so let’s suppose we want to deploy on it.
It’s important to define your platform because you can try something on a platform and deploy on another one, but of course if you change it on the way, you have an unknown.

Which Context?

I’m writing during the weekend and, unlike other working days, I usually take a nap after lunch, it’s something I’ve been carrying since I was young, it’s a break in the daytime and it lasts about 30 minutes or so, a sort of reversed pomodoro or, better, a pomodoro focused on sleeping ;-)
After that, I’m "energized" and ready for the afternoon. So what?
  1. Let’s consider a preliminary of N languages (only three today) some mainstream, some cutting edge. The goal is to hack a timer that wakes me up from my nap after 30 minutes.
  2. Note: some languages implement sound generation more smoothly; other are not really sound-friendly, so for this first attempt, a timer that prints “wake up” is enough.
  3. Finally for this specific experiment I’d add another requirement: let’s jump to light speed and go somewhere in the universe where 30 minutes are equal to 3 seconds (my nap is incredibly reduced ;-)
psst: If you are lazy like me, you can go directly to the Conclusion, otherwise continue to read.

First I create a directory named contest in which we save all our spikes.



The first language to consider is Java


I create a subdirectory for Java and look for Java on Google like any newbie.

Following my breadcrumbs with some comments:
Actually we have spent 6 minutes, but no info worth on. 

So I search for something like “first java program” on the favorite search engine and..... at 8.36 minutes I land here:
The output is OK (after 10 minutes

But.... our exercise doesn't end here, how can we implement a timer?

I search again: “java timer”
I do a bit of c&p, remove all comments, compile and run. It works (17.20).
Now I reduce frills, and change time to 3 seconds

After 18.05 minutes we have our timer:

import java.util.Timer;
import java.util.TimerTask;

public class ReminderBeep {
  Timer timer;

  public ReminderBeep(int seconds) {
    timer = new Timer();
    timer.schedule(new RemindTask(), seconds * 1000);
  }

  class RemindTask extends TimerTask {
    public void run() {
      System.out.println("Time's up!");
      System.exit(0);
    }
  }

  public static void main(String args[]) {
    new ReminderBeep(3);
  }
}

Anyway the Java syntax is not concise nor essential, but 18 minutes is a pretty fast time even if I was almost confident to read the example for windows.




Now let’s pick PHP


I go to:
I try to write the hello.php file and then I point the browser directly on it.
  • vi hello.php 
  • a bit of c&p 
  • open browser on file://......../hello.php it doesn’t work, of course. 
I surf the web again:
I locate and open the Apache configuration file, it should be here: /private/etc/apache2/httpd.conf and I change it accordingly.
Than I go to: /Library/WebServer/Documents and I write a simple phpinfo

<?php phpinfo(); ?> 

Now I restart apache with: sudo apachectl graceful and then I point the browser here: http://localhost/info.php, it works.
Now I move hello.php here: /Library/WebServer/Documents , it works

After 27.00 minutes we have:

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php echo '<p>Hello World</p>'; ?> 
 </body>
</html> 


Now I make a search for a timer on Google: "php timer example"

Finally after 43 minutes that’s the simplified timer: 

<?php
sleep(3); 
echo "wake up<br />";
?>

As you have seen, php drives you to use a web server and to execute the code inside it, even if we have not a real need to build up such infrastructure.
I’d like to underline this concept: of course php scripts run in a web server.... at the end. But now, our focus is to learn the language, all other “mandatory” steps distract us from our goal and put it away. I don’t think about it as a" una tantum" stuff, I think as a clue you have to evaluate the language ecosystem and the intentions of the creator of the language.
Anyway the php example is really minimal compared with java.




And finally CoffeeScript


The usual breadcrumbs:

Now I create the directory coffeescript under contest then
  • sudo npm install -g coffee-script 
  • I test the installation, type: coffee - Cool it runs.
  • I type: number = 42 - OK, it returns 42 it seems working,
  • And I exit the REPL, how? It could be: exit, no, quit, no, bye, no, open sesame no way.
I'm defeated. I google: "exit coffeescript repl"
I exited, but it’s not yet clear to me how to print something, let’s look deeper at the overview and try:
  • alert “hello” - ReferenceError: alert is not defined 
  • coffee -e "console.log num for num in [10..1]" - Gotcha! 
  • console.log "hello" - That's fine.  
 After 29 minutes

Finally let's look for a timer:
http://stackoverflow.com/questions/4917567/coffeescript-timer-and-this-pointer-on-callback - found it.

Now I rearrange the script in the coffee repl to have something like:

setTimeout((-> console.log "wake up"), 3000) - It works!

After 38 minutes



Actually CoffeeScript is really playable and it has a minimal syntax, but I expected a more clear documentation: the repl delayed me a bit


Conclusion

Let's sum up some results:


The chart shows the time spent doing installation plus some initial check and the time spent hacking a timer.

PHP and CoffeeScript result in the same amount of time for the installation, but hacking a simple timer in CoffeeScript is quicker and it's comparable with Java, but I can't tell you more, the trial is not ending here and I still need a couple of posts to show other insights.

Next week time we'll analyze three other languages, keep in touch and... mind the feedback!






Chicken timer by abbey*christine
Installation by zigazou76