Jan 29 2007

Java 7: The Closure Debate

Closures are code blocks; think of them as function literals that can be assigned to a variable and passed as a parameter to a function. Another name for closures that I have heard use is functors. Ruby and Groovy have closures and both of these languages run on the Java Platform. So why do we need closures in the Java language? Another good question is, but aren’t anonymous inner classes just like closures? The man the is trying to best answer these questions is Neal Gafter. Neal is rallying the Java community to include closures at the language level in next release of Java.

The issue of closures in Java 7 is a hotly debated topic and as software engineers and Java developers we will hear more on the subject for the foreseeable future. To get everybody up to date on closures here are some articles on the subject.

1. Closure – The computer science definition of a closure via Wikipedia.
2. Closure – The definition of a closure code block according to Martin Fowler.
3. Java Closure Poll – As of this written 70% are for closures.
4. Closures for the Java Programming Language – Closures for Java specification page.
5. A Definition of Closures – An detailed article by Neal Gafter.
6. Neal Gafter JavaPolis 2006 Interview – A 9 minute video interview of Neal Gafter.
7. Advanced Topics In Programming Languages: Closures For Java – A two hour talk by Neal Gafter on closures in Java in Google Video.
8. Considering Closures for Java – An interview with Neal Gafter on Artima.
9. Inner Classes or Closures – A detailed blog post on Artima about closures and inner classes.
10. When is a closure not a closure? – More on the subject.
11. Crossing borders: Closures – Bruce Tate has written a tutorial on closures using the Ruby programming language.

I’ll be updating this post with new resources as they are made available.

Technorati Tags: , , , , , , , , , , , , , , ,


Jan 29 2007

MySQL Optimization Tutorial

A while back I wrote a little introductory MySQL Administration tutorial. The tutorial had your basic information about starting the database server and creating databases. Now, I would like to jot down some commands that are useful when trying to optimize a SQL query on MySQL.

The first thing you need is to have a normalized database. This tutorial assumed you do and will not go into how to normalized you database schema.

In my limited experience with MySQL optimization techniques I have found that the single most important thing you can do is to create indices for foreign key columns and other columns used in the where clause. Here is the command to create an index.

[source:sql]
create index <index_name> on <table_name>(<column_name>);
[/source]
Continue reading


Jan 29 2007

Mac OS X F11 Key

I have been using a PowerBook G4 for nearly two years and just today I figured out something that I wish I had known a long time ago.

Here is a scenario… When I have too many application windows opened and I need to see the desktop, I would normally minimize and shuffle around all the windows. As you can imagine, this is very annoying prone. So today not unlike Columbus, I discovered something that might have been known to a whole continent! If you hit F11 on OS X all your windows will fly to the edge of the screen leaving you with an undisturbed view of the desktop. Kewl!

If anyone else has any tips like this holla at me, that is drop me a line!

Technorati Tags: , , , , , ,


Jan 22 2007

Ruby Class Tutorial

Classes and interfaces are the primary building blocks of a typical Java application. Classes are the blue prints used to create objects in a running system. In contrast to Java, Ruby’s building blocks are classes, modules, and mixins. In this post we will take a tour of classes in Ruby.

Classes
To get started, lets define a basic Person class.

class Person
end

The above class is the simplest class type we can define in Ruby. The first thing you will notice is that the curly braces that are so pervasive in other programming languages are omitted here. This is not a typo. It is intentional. In fact, curly braces are rarely used in Ruby code blocks.

As in Java, we use the class reserve keyword to define a class type and the class name is capitalized in camel case. In Ruby, end is the reserved keyword used to demarcate the end of a code block such as an if statement, a method declaration, or a class definition. At this point, the Person class is the simplest class that we can define. Our Person class implicitly inherits from Object.

So far the Person class defined above is not even interesting enough to instantiate. As we know a class typically has state and behavior. Lets add two instance variables to our Person class to hold the first and last name and the necessary getters and setters in a very Java-like fashion.

class Person
  def fname
    @fname
  end
  def fname=(fname)
    @fname = fname
  end
  def lname
    @lname
  end
  def lname=(lname)
    @lname = lname
  end
end

Continue reading


Jan 17 2007

Reopening Ruby Classes

In Ruby the implementation of a class is not closed. In Ruby, it is incredibly easy to add new methods to any existing class at runtime, even core system classes like String, Array, and Fixum. You just reopen a class and add new code. This language feature makes the language incredibly flexible. You might be wondering, why would you need this? Well, take a look at the API documentation of the Java String class. In most development organizations that I have been a part of we have had to write small string libraries that provide far more functionality that provided by the Java String class. The Apache Jakarta Commons Lang project provides a String helper class in StringUtils. StringUtils provides methods such as IsEmpty, Chomp/Chop, and LeftPad. StringUtils. StringUtils is a great class that has saved me from re-inventing the wheel several times, but it is another library to download, configure, learn, and import. Using the StringUtils class is more verbose calling a method on a string.

// Using StringUtils to trim a string.
str = StringUtils.trim(str);
// Calling a trim on a string.
str = str.trim();

Isn’t more concise and easier to read when you invoke a method on a string instead of using StringUtils? Unfortunately, even if an JCP is approved to add your custom string methods to the String class, the whole process is very time consuming. Each incremental release of Java takes years to develop. In Ruby, you can just reopen the String class to add the few methods you feel it lacks. To reopen the Ruby String class we can use the following code:

# reopen the class
String.class_eval do
  # define new method
  def get_size
    # method implementation
    length
  end
end

Continue reading


Jan 16 2007

Top 10 WordPress Plugins

1. Akismet by Automattic is probably the best WP plugin out there. Akismet is a comment and trackback spam filter for your WP powered blog. It is a true mathematical equation that your blogs page rank is exponentially proportional to the number of viagra and penis enlargement spam comments you will receive. Akismet blocks them all. One thing to note about Akismet is that it sometimes flags honest comments as spam, so make sure to manage Akismet spam frequently. To use Akismet you will need a free WordPress API key.

2. If you are using Google Analytics to track your blog’s performance I recommend the Google Analytics plugin by Boakes. It is simple to install and configure, just have your Google Analytics User Account handy.
Continue reading