Oct 27 2005

Yes Comments

There is more than one way to say it. A rose in any other name is still a rose, or better yet a comment in any other language is still a comment. Every C++ and Java programmer will tell you that end of line comments start with two forward slashes such as:

// This is a C/C++/Java comment.

For some reason, many script languages such as Perl, Ruby, and Jython choose the pound/bang sign for end of line comments.

# This is a script comment.

C inspired languages, such as C++ and Java have comment blocks. Comment blocks start with /* and end with */ and everything in between will be treated as comments such as:

/*
 * This is a C/C++/Java
 *    comment
 *       block
 */

Of course, Java can produce online documentation if you use the /** variation of the comment block. You can also place comments in HTML/XML documents, for example:

<!-- This is an HTML/XML comment -->

If you work with XSLT files, which are just XML files, and want to produce a comment you need to place your comments inside xsl:comment tags, such as:

<xsl:comment>
   This is not a comment.
   This will produce a HTML/XML comment.
</xsl:comment>

And of course, you can comment a JSP page using the HTML comment construct but if that comment is JSP specific and is not supposed to be sent to the client you can use the following comment construct:

<%--
   This is a JSP comment,
      won’t be sent to the client.
--%>

And like everything else that Microsoft does, Visual Basic comments are not based on any of the constructs listed here. Visual Basic end of line comments start with the single quote character. The following is a Visual Basic comment:

' This is a VB comment.

Just remember that developers can speak in code but not everyone that reads your code is a developer. So whatever you’re preferred language is please comment your code, at a minimum fill in the pre/post conditions.


Oct 24 2005

Random Shell Script

Welcome to the random shell script of the week! This shell script is trivial but useful, especially if you don’t write this kind of stuff every other day. The following shell script will iterate over every file in the current directory and print the filename out.

#!/bin/sh

for file in ./*
do
   echo "$file"
done

You can modify the ./* to ./*jar to loop through all jar files.


Oct 24 2005

Ruby On Rails, J2EE On Dope

I remember those ‘this is your brain on drugs’ commercials from back in the day when they showed an egg in a frying pan. Well, having worked on a J2EE project and following programming developments I feel that many developers feel like this after a big J2EE project. There is a paradigm shift in the enterprise way of thinking. There is a move to lightweight containers such as Spring and PicoContainer. With the growing popularity of Ruby on Rails, convention is being favored to XML configuration. In the Java space, the frameworks to follow are Trails, Karma MVC, and Rife.


Oct 23 2005

MySQL Admin

If you do any web development either with PHP, JSP, and/or Ruby on Rails you most likely will use MySQL. The following is all that you need to know to get started with MySQL. I am currently using MySQL 4.1.14. To install MySQL just unzipped it to the c:\ drive. To start the MySQL server cd to the bin directory and execute the following:

bin>mysqld

Once the server has started you can connect using the command line tool mysql. From a command prompt, just type the following:

bin>mysql

Once logged on you can view the available databases by running this command:

show databases;

To create a new database use the create command as in:

create database <database_name>;

Of course, you will need to replace <database_name> with an appropriate name. To start using a specific database from those available enter the following command:

use <database_name>;

It is nice to list all the defined tables in a database schema, to do so in MySQL enter this next command:

show tables;

And if you are like me and suffer from development amnesia, you will fail to remember those hardly ever used table column names. Well, the next command describes a given table’s metadata such as column name and type:

describe <table_name>;

Once you have created your database and database tables, you will most likely need to create a user for that database schema. To create a user, execute the following:

grant all on <database_name>.* to
<user_name>@localhost identified by '<password>';

And when you are done you can stop the server by executing the following:

bin>mysqladmin -u root shutdown

There is a lot more to MySQL than this short tutorial. Like Oracle and other databases, MySQL provides import and export functionality (see load data command). In addition to the typical selects, updates, and delete statements these are the only other commands that I regularly use when using MySQL.


Oct 20 2005

Eclipse Tool Tip #2: Local History

Yet another reason to love Eclipse is its Local History feature. Yeah, you might have a version control system but what happens if your recent changes are accidentally wiped out before you commit them to CVS, Perforce, or whatever else you use? If you are using Eclipse, you compare your current source file against locally saved changes. To see this in action, from the Package Explorer right click on a source file you have recently modified. Click on Compare With > Local History to open the Compare with Local History tool. You can diff between your current source and a previously saved file from your local history file. This Eclipse feature has saved me many a time, hopefully you also find it useful.


Oct 18 2005

Hear Me Ro(a)R

Ruby on Rails (RoR) Rocks! It is off the chain, off the hook, and off the tracks!! Like Struts, RoR is a Model 2 framework used to build web applications. With RoR you define actions in controller classes and delegate the presentation to rthml files. RoR favors convention over configuration, which means no XML configuration files such as struts-config.xml. Ruby on Rails is getting a lot of press and hype these days. There already is a book on RoR and more are on the way while Ruby on Rails is just at version 0.12.

The power of Ruby of Rails, in mind, is the tight integration with the O/R mapping tool used (ActiveRecord). For example, once you configure Rails with your database via the database.yml file, the only configuration file I was able to find, a model class for a Product consists of the following code:

class Product < ActiveRecord::Base
end

In the database you could use the following SQL to define the products table.

create table products (
   id int not null auto_increment,
   title varchar(50) not null,
   description varchar(50) not null
);

Just with the Product model and the products table defined above, you can have Ruby code in you action such as:

def show
  @product = Product.new
  @product.title = "This is my title"
  @product.description = "This is my description"
  if @product.save
    flash[:notice] = "Product saved!"
  else
    flash[:notice] = &qout;Product was not saved!&qout;
  end
end

If you thought that the code for the model was too much to type, you can use the Rails scripts to generate it!! You can generate controllers, actions, and models using Rails scripts.

On the view side you have the same scriptlet tags as in JSP, such as in the show.rhtml page associated with the show action above, you can have the following code to print out a product’s title:

<B><%= @product.title %></B>

How easy is that? I am starting to play with Ruby on Rails so I am sure to be writing more on it.