Groovy Class for Analyzing java code

July 8, 2009

A lot of times when i sit down to review code, there are some basic things like the size of the class, the total lines, the maximum width of a line etc, which can help in figuring out bad smells in code. In order to automate the task i wrote a small groovy class which can be run on a directory to figure out these metrices. Although there are lots of tools available, the simplicity and the customization which can be done to a small utility class far outweigh the advantages.

class CodeAnalyzer {
Read the rest of this entry »


Eclipse templates for auto code generation

July 6, 2009

Working with an IDE tends to make life quiet simple. However we still do not leverage the full potential of using an IDE like Eclipse. During development, i have found creating a few templates really helps to speed up things. Here are the few most common ones i use which are not part of the standard templates.
1) Log4J Logging
Read the rest of this entry »


June 30, 2009

A lot of times, when doing TDD, we make small refactorings and then have to manually go ahead and run the tests to figure out whether the changes we made are consistent and have not broken the tests. Wouldn’t it be good if the tests were run on each Save to ascertain that we have not broken any of the tests. This also lets us know which changes are the culprit. Using eclipse we can automate running the unit tests on each change and save we make. Following are the steps needed in order to accomplish this.
1. Create a main class

import junit.framework.TestSuite;
import junit.textui.TestRunner;
public class TestBootstrap
{
public static void main(String[] args)
{
TestSuite testSuite=new TestSuite();
testSuite.addTestSuite(TestClass.class);
TestRunner.run(testSuite);
}
}

2. Create an ant build file

Read the rest of this entry »


Reading RSS feeds using groovy

June 15, 2009
def channel=new XmlParser().parse(url).channel[0]
println channel.title.text()
println channel.link.text()
println channel.description.text()
println ‘\nStories:\n———‘
def items=channel.item
for(item in items){
println item.title.text()
println item.link.text()
printlnHeres a  item.description.text()
println’——-‘
}

Here is a small groovy script to read from any RSS feed.

def url='http://feeds.dzone.com/dzone/frontpage'

def channel=new XmlParser().parse(url).channel[0]

println channel.title.text()

println channel.link.text()

println channel.description.text()

println '\nStories:\n---------'

def items=channel.item

for(item in items){

println item.title.text()

println item.link.text()

println item.description.text()

println'-------'

}


Google Wave – A First look

June 11, 2009

I took a look at the presentation of Google Wave, conducted by Lars Rasmussen and his technical team. Although its still in sandbox, it appears to be a very interesting product. The capabilities of what can be achieved in a browser are mind blowing. A very interesting point about Wave is that it would be open sourced. Primarily i think Google would need a lot of contributions from the community to really leverage the power which Wave promises. Wave brings the power of IM and email together and adds a different dimension to it. An early release of the wave API’s are available .So go ahead and get your hands dirty. The API’s available are the extension API’s for building wave gadgets, robots and the Embed API’s.


Fan — Another New Java

June 11, 2009

Fan has recently been doing the rounds and i spent some time looking at it. It appears to be pretty much in the same space as Scala in most aspects. A deeper look at Fan reveals some interesting points. Similarity with Scala is it runs on the vm, has both dynamic and static typing, closures, mixins are built in etc etc. However a few interesting features in Fan over and above these are
1) Core support for the JVM, CLR and Javascript in the browser.
2) No support for global variables
3) Almost Erlang like concurrency semantics
4) Types are non nullable by default
5) Simpler namespaces and less verbose API’s
6) JSON like serialization
On the while it appears has a groovy like simplicity with all the power of scala.


Proejct Euler Problem 1

June 3, 2009

Got to know about Project Eular.
So thought about posting a few solutions using groovy. For the 1st problem, the solution is pretty simple

def series3=0;
def series5=0;
def series15=0;
0.step(1000,3){series3+=it}
0.step(1000,5){series5+=it}
0.step(1000,15){series15+=it}
def result=series3 +series5 – series15


Export Data from Flat files to database:The Groovy way

June 3, 2009

Recently i was supposed to port a lot of data from flat files into a database. Looking at alternatives i thought it would be neat to accomplish the same using a small groovy script. Here’s the crux of the script

def sql = groovy.sql.Sql.newInstance(‘jdbc:mysql://localhost:3306/test’ ,
‘root’, ‘ ‘, ‘com.mysql.jdbc.Driver’ )
new File(“C:/data/”).eachFile{file->
file.eachLine{line->
def col1
def col2
def col3
def args=line.split(“,”)
if(args.length==3){
col1=args[0]
col2=args[1]
col3=args[2]
sql.execute”””
INSERT INTO table (col1, col2, col3)
VALUES (${col1}, ${col2}, ${col3});
“””
}
}
}


Unit Testing Recipes

April 22, 2009

A lot of times i tend to come across unit tests where the developer has to create a List and compare the equality of a returned List with some expected List. As an example consider the following test Case

public void testResultsHasSameElements(){

List()expectedResults=new ArrayList();
expectedResults.add(“James”);
expectedResults.add(“Jack”);

List customerNames=CustomerDTO.getCustomerNames();
for(String name:customerNames){
assertEquals(name,expectedResult.get(0));
assertEquals(name,expectedResult.get(1));
}

}

If we look at the above testcase, it involves a lot of typing. We can simplify the above test using the fact that 2 Lists are equal if they contain the same objects at the same index. Another optimization which can be used is the Arrays.toList method to create the expectedResults list
The improved version of the test would look like

public void testResultsHasSameElements(){

List()expectedResults=Arrays.asList(
new String[]{“James”,”Jack”}
);

customerNames=CustomerDTO.getCustomerNames();
assertEquals(customerNames,expectedResult);
}

}


Getting teams started quickly on projects

September 30, 2008

The ability to get teams started on a project is something that takes a lot of time and resources. A few ways to mitigate that and get teams to speed fast are
1) Use Buildix(http://buildix.thoughtworks.com/) for setting up the infrastructure for the project like version control, continuous integration, agile project management and wiki and bug tracker. Buildix is open sourced under the apache license and automates setting up the basic infrastructure required fro the project.
2) Use Panopticode(http://www.panopticode.org/) to setup tools for gathering code metrics. Panopticode provides customized build scripts to integrate tools like Emma, CheckStyle, JDepend,JavaNCSS, Simian etc.
3) Use a VMWare mirror image to setup a developer box so as to remove any discrepancies across developer environments.