Language Patterns

I was researching the execution time difference between static and instance method calls today and came across an old Java forum thread about the topic. I was looking for performance information, but everyone in the thread was focusing on “good <abbr title=”object oriented>OO</a> practices”, and I was astonished as to what I was reading. Because of this thread I realized that many “patterns” are created as “good OO practices” because of a flaw or limitation in a language. Then that pattern is said to be correct and good and carried over to any language regardless of whether it has the original language flaw or not.

In this thread they were discussing how you should always use instantiated objects because you will never know what you might need to change the code for later. This threw up two flags in my head:

  1. Why do static methods/classes exist in Java if they aren’t advantages to use in certain situations.
  2. These people have obviously not looked at any dynamic languages like Ruby, Python, Perl, etc…

As for the second point, I will let them slide because the thread was from around 2005, and the dynamic languages were just starting to pick up steam. The first one I can let slide because I understand that even language engineers make mistakes, and could have possibly written a feature that isn’t necessary or good to use (e.g. goto).

But that still leaves me with the problem that this is a language issue not a good OO practice. Java does not allow you to change the behavior of a static method at runtime, or even override it at compile time, but in a language like Ruby you don’t have this limitation. Keep in mind in Ruby you have Class methods not Static methods, but it is basically the same idea. Furthermore you can override any method, class or instance, at runtime in Ruby.

This makes me wonder how many other “good OO practices” have been brought from old languages unnecessarily. I know when I first started writing ActionScript or even C# I fell prey to writing getter and setter methods. This is completely unnecessary but I am sure an experienced Java developer would try to convince you to do it as a good practice. I would imagine even if you convinced him that [insert language here] has the notion of formal properties they would attempt to convince you to do it to make the code more readable, but it is what you would expect to happen.

This really bothers me because if this were the mindset of the original language developers we would probably never have a for loop because a while loop works just fine. That may be a bit of a stretch but I think it is the same mentality. Don’t use the new syntax/feature because the old way is more familiar. If we were to do that, what is the point in writing new languages, or improving old ones. I guess I shouldn’t be surprised that many people fall into this mentality because the programming community up until the last few years had seemed to fall into a rut with language development. “Why develop new language constructs while the old ones work?”.

I guess overall this just further encourages me to learn a new language every once in awhile to make sure I am not falling into this mindset, and make sure I don’t try to make the new language bend to my ideas, but rather bend my ideas to the language. I guess I would equate it to speaking Spanish but not putting your adjective after the noun because that is how we do it in English. I charge you, learn a new language, swap that adjective around. It will probably make you a better developer by giving you a different slant on the problem.

Posted in languages | Tagged , , | 5 Comments

Actionscript 3.0 Object Equality

This weekend I began tinkering around with FlexUnit 2.0 for Actionscript 3.0. So far I am excited about the framework. It doesn’t do much, but it does the basics. When writing some of my tests I found I needed to compare two objects together. I know how to handle this in Java you would define the method equals(Object o) on the Customer object. I searched for a few hours and could find no such solution in Actionscript. Ofcourse I could make it my own standard to define this method but then frameworks like FlexUnit wouldn’t know anything about it.

Finally after searching I came across mx.utils.ObjectUtil. This object has numerous methods on it that expose the innards of objects. It has an especially powerful compare method that will transverse an object and compare each sub element testing for equality. This eliminates the need to write most equals methods on objects, but unfortunately it doesn’t get automatically used when you use the equality (==) operator.

This confuses me because the docs for equality (==) say it actually compares the value of the operand and strict equality (===) compares the memory references. Unfortunately this doesn’t seem to apply to objects as == and === have the same outcome in all my tests.

Although I am a little dissappoitned I can’t write something like:


object1 == object2

I have to instead write:


import mx.utils.ObjectUtil;
ObjectUtil.compare(object1, object2) == 0

I wouldn’t mind as much if FlexUnit took this into account with there assertEquals method. Currently I believe it just runs an equality (==) operation and is done. So my tests have to use the assertTrue.

Anyways I wanted to write this post to do one of two things.

  1. Save someone the trouble of searching for hours to be as dissappointed as me
  2. Have someone point out my flaws and show me the easy great Actionscript way to do equality
Posted in languages, programming | Tagged , | 19 Comments

TDD By Example Reviewed

I recently got a chance to read through Test Driven Development: By Example (The Addison-Wesley Signature Series) by Kent Beck. For those who don’t know, Kent, is the person behind eXtreme Programming and TDD, along with a laundry list of other items. Having not read many technical books cover to cover I wasn’t sure what to expect. I tend to use technical books as references not reading material. I was pleasantly suprised by how easy the book was to read, and how lightly it took itself. Mr. Beck is very passionate about developing in a way that is both productive and exciting. He discusses his frustration with existing approaches to development, and why he feels that TDD is a better approach. In many aspects I agree with Mr. Beck, but I also believe that there are two distinct types of Developers.

The first type are developers who love pretty pictures. My colleague Brian LeGros falls into this category. He enjoys diagramming until he has every nook and cranny defined, and ideally with the right tool he would just have to push go, and the bulk of the code would be generated. This camp tends to end up pushing ideas like MDSD that encourage drawing out everything and letting tools generate the details for you.

The second group loves to read and write code. These are the kind of people who typically get in trouble as systems get large, because they don’t document very well, and when another developer joins the team or takes over where they left off it is usually not pleasant. These people tend to fall into the Agile and XP camps. These are the people that will truly benefit from Mr. Beck’s book. TDD proposes a solution for this group to constantly be writing code, but keep the system documented and scalable.

I find myself each day falling more and more into the Agile/XP/TDD/BDD camps. I would very much prefer to look at code, than a picture. If I can rely solely on my unit tests, and compiler to tell me when I need to fix something that I refactored, I am all about it.

Overall the book cleared up some misconceptions I had about the TDD movement. For starters Mr. Beck is not nearly as strict as I imagined he would be…like a DHH for instance. He encourages many times throughout the book to take TDD and use it in your system slowly. I can remember numerous times he suggested “Doing what works best for you”. Usually he was talking in the context of what to test, and how large of a step to take in between tests.

Regardless of which camp you fall into I would strongly encourage you to check out this book. Even if you don’t adopt the TDD approach you can get many good refactoring ideas, as TDD is based heavily on refactoring. The only part of the book I would recommend skipping would be Part II. Unless of course you enjoy reading about a person implementing an xUnit framework in Python, and testing that very framework, with the framework, as he is writing it. Confused…yeah the section is just as confusing. However if you are interested in seeing how to bootstrap test your code it is a great section to read.

Posted in development, testing | Tagged , | Leave a comment

Buildr Review

I should start this post off by apologizing to the Buildr folks for taking so long to write up this review. I should also point out that I am currently attempting to contribute to this project, I don’t believe that sways my opinion at all since I choose to contribute after I reviewed the product. Now onto the review…

Like Raven, Buildr, is a build tool written in the Ruby language, that builds on top of Rake. Where Buildr differs from Raven is that it actually attempts to create a DSL specific to Building Projects apart from Rake. This may seem confusing, Buildr uses Rake to accomplish it’s tasks but exposes this through it’s own declarative/procedural hybrid language. At first I was turned off by this, because I was familiar with Rake and I was comfortable with writing in the Rake syntax, however after allowing myself to adjust to the Buildr syntax I find it is much more productive.

For those who are currently using Maven there is good news, you can move to Buildr without changing your repository. You simply have to change your build file from a Maven pom file to a Buildr build file.

Like the Raven Review I will now address each point I discussed in the pre-review outline.

Compile Java

As I said in the Raven review compiling is a trivial task, and just like Raven, Buildr keeps it trivial, so much so if you follow the Maven directory conventions and want the code to be compiled using the version of Java set at JAVA_HOME then you don’t have to type anything, aside from creating the generic Buildfile. To create the default build file you have to type Buildr in the folder you wish you create the file. The default build file will look something like this:


# Generated by Buildr 1.2.5, change to your liking
# Version number for this release
VERSION_NUMBER = "1.0.0"
# Version number for the next release
NEXT_VERSION = "1.0.1"
# Group identifier for your projects
GROUP = "project1"
COPYRIGHT = ""

# Specify Maven 2.0 remote repositories here, like this:
repositories.remote << "http://www.ibiblio.org/maven2/"

desc "The Project1 project"
define "project1" do

  project.version = VERSION_NUMBER
  project.group = GROUP
  manifest["Implementation-Vendor"] = COPYRIGHT
end

The file uses the current folder name to define a couple items in this file. All the Project1 references would be replaced with whatever the name of the folder you are currently in. At this point you have a working Buildfile, and can run any of the standard Buildr commands on your project.

  • clean
  • compile
  • upload
  • install
  • javadoc
  • package
  • test
  • uninstall

The Ruby community seems to implement convention over configuration better than Java as they understand sometimes you need to configure. Just like Raven you can set any of the paths for src or target. Whereas in a Maven you have to adjust your existing projects to their standard. That being said I would say it seems that the Maven suggested directory stucture makes a lot of sense, but if you currently have a different directory stucture you wouldn't be required to change it to use Buildr or Raven.

The Buildr community has developed the compile task a little more than Raven, meaning all compile attributes are avaiable. In addition Buildr uses the RJB to instantiate the Java Standard Library class responsible for compiling a java class file, instead of realying on a system call, like Raven does. This approach seems a little more reliable, unfortunately it makes it relient on JAVA_HOME being set as an environment variable, and RJB currently doesn't have a facade for JRuby, so where Raven will work out of the box with MRI Ruby or JRuby. Buildr will currently only work with MRI Ruby, although I have seen on the forums that a few people are working on making a JRuby fix. Also Buildr requires atleast Java 1.5 be set at JAVA_HOME whereas Raven only required a version of Java be defined in the PATH.

Package compiled Java into jar

I wish there was more stuff to say about this topic but it amazingly easy. In your project definition you need to add one line:


package :jar

You can set additional information using the with, include or exclude commands, but those are outside of the scope of this review. There is a lot of power in the pacakage command, and it will only get better. For instance if you want to package a war instead of a jar you have to change :jar to :war. The output will automatically be put in target. The naming convention is [project name]-[version].[type].

Compile Flex Source Code into Flash Library (swc)

Just like Raven, Buildr, was created to make building Java projects. Unlike Raven there seems to be an interest in expanding the language to incorporate other languages. Aside from myself working on Flex integration, I know there is currently a developer working on Scala integration. My original stab at Flex integration worked for the purposes of the Proof of Concept I was putting together, but after chatting awhile in the Buildr Google Group I got a feel for how the creator intends other languages to be integrated.
The end result if you are compiling flex and your src is in /src/main/flex there is only one line of code you need to add to your flex project:


package :swc

This is a little ackward since there is no explicit compile step before packaging, but you will define all your "compile options" like dependencies, target and source. But as long as you don't care what is happening behind the scenes and just trust that it does what it needs to, to accomplish what you request, there should be no problem.

Compile Flex Source Code into Flash Application (swf)

This works exactly like compiling a swc but you tell it to package type :swf, and you point the source to the Main.mxml. By default the /src/main/flex/Main.mxml is used, but you can override this using the from method on the compile task.

Retrieve and/or Build Dependencies from Other Projects/Third Party Libraries

Defining dependencies seems to makes a lot more sense in Buildr than in Raven. To define a dependency you simply add it to the compile task like so:


compile.with('com.danielroop.project2.jar')

The with method can be called multiple times, or you can pass an array to the method.


compile.with(['com.danielroop.project2.jar', 'org.apache.axis2:axis2:jar:1.2'])

The first element in the array will look locally for the jar, and the second is an artifact definition. Anyone familiar with Maven should recognize the syntax. This will look through all the repositories defined for the project for the axis artifact version 1.2, in this case this ia a jar. You can also define a project as a dependencies, and the only time I see this useful is if you nest projects withint a single Buildfile. For instance one for java, one for flex, and then the encompasing project will use the two children projects as dependencies to build a war.

By default the ibilio repository is included for your project, but you can and probalby should setup your own local repository, that will house all your person artifacts. You can use artifactory or some other maven repository to run the server, then you can add by appending the repository location to the repositories array.


repositories.remote << "http://localhost/maven"

Conclusion

Overall I believe that Buildr is a much more likely to become the defacto ruby build language, than Raven. If simply because of the active community that Raven seems to lack. So if you are up to replacing the trainwreck that is Maven 2 then I would encourage you to look into Buildr.

I do however believe the project needs some work. So to wrap it all up I will point out a couple things that I would like to see addressed.

  1. Formal Language Integration Strategy

    Currently I have been trying to base my implementaiton strategy for Flex on what was done by Scala. Which is working for now, but I would like to see a document describing some best practices for how to utilize the build DSL that has been developer for Buildr for other languages.

  2. Project Type Definition

    I go back and forth on this one, but I believe it might be necessary to define what type a project is. Currently the system is decided based on file types, which seems like it will not be robut enough, but time will tell.

  3. war type integration

    This may simply be the world I am coming from but I see a need for specialzed wars. Meaning wars for specifc langues. In my case I am looking for a Flex and Coldfusion war. This would allow me to set some property so all the coldfusion libraries would be included. I might just not understand the whole war/jar system enough to figure this out through normal dependencies.

  4. Is it BuildR or Buildr

    I personally like the project a lot more when I thought it was BuildR. I am just tired of the whole Flickr naming convention.

That wraps this review up. I hope this helps you in your decision making process when evaluating build tools. I will continue to post updates on my site as I maintain and update the flex integration into Buildr.

Posted in build | Tagged , , | 6 Comments

2nd Player Video Game Podcast

Myself and two of my friends have decided to take a stab at the whole podcast thing. The content is video games (console and pc) and the format is conversation. I think the first episode turned out well and I think we will only get better as time passes. If you are interested in video game news and “funny” commentary, I would recommend you stroll over to 2ndPlayer.net and check it out.

Posted in gaming | Tagged | Leave a comment

Quoth the raven, Nevermore.

I felt this classic line from Edgar Allen Poe’s The Raven was appropriate to start off my review of Raven “the build tool for Java programs based on Rake and Ruby Gems”. Now that my eyes have been opened to the world of using dynamic scripting languages to build Java projects I can safely say that I want to use classic Java build tools namely Ant, and Maven…Nevermore.

Raven, as I stated above, is a build tool for Java programs written in the Ruby language. It is essentially a set of RakeTasks that helps you build Java projects. If you understand Rake you should be able to use Raven. To handle dependencies Raven has chosen to use gem repositories. Much like Maven has ibiblio.org, Raven has it’s own public gem repository. This is important because it allows you to look in a central place to get common libraries (e.g. log4j, apache commons). If you don’t understand Rake or Ruby don’t worry, the syntax is super simple and you should be able to pick it up very quickly. Before I go on I would also like to discourage people from treating this as learning a new language, like learning a new Java, or C++. It is simply learning a new Domain Special Language, Although you get the full benefit of Ruby if you need it. The basic tasks require little to no understanding of how the Ruby language itself works.

Now for the review. I will cover each topic I mentioned in my previous post then go into any further detail I believe is necessary to show the strengths or weaknesses of Raven. In each section I will briefly describe how to accomplish the task, followed by any code necessary to make the test work.

Compile Java

Compiling Java is a trivial task, and Raven keeps it trivial. If your project follows an apache structure, meaning your Java source is under project_root/src/main/java then you simple have to add and name the command to your Rakefile like so:

javac :compile

Where javac is the command and compile is the name. That seems pretty simple, but lets say you don’t follow the standard folder structure, maybe you place your source in project_root/src/java. Then you code would look something like this


javac :compile do |task|
  task.build_path << 'src/java'
end

For those of you unfamiliar with Ruby I just passed a block to the task. In that block I append a directory to the build_path variable. When you pass a block to a function you typically define some sort of variable. In this case I defined task, this is basically an input to the block, and for this input I will be receiving a handle to the RakeTask that will be performing the javac task. If you look at the RDocs for Raven you will see that build_path is a attribute of JavacTask.

With simplicity and early versions usually comes loss of power, and unfortunately Raven seems a little immature in this area. Meaning, all the javac compiler options are not exposed through the task. This became a problem for me in my tests because I wanted to have Java 1.5 set as my default SDK but for a specific project I needed java to compile using source and target of 1.4.

Before I move on to Packaging I want to make one mention of how you include associate dependencies with a compile task. I will cover defining dependencies a little bit later. To associate a set of dependencies with a compile task you simple add the dependency task as a Rake dependency of your compile task.


javac :compile => :compile_dep do |task|
  task.build_path << 'src/java'
end

In the above example compile_dep is the name of the dependency task I defined elsewhere in my build file. In that dependency task you can define local libraries like jars in a lib directory or remote dependencies from your remote/local gem repository.

To run this task you simple need to type a simple two words at the command line


rake compile

That’s it, that is how easy it is to compile Java code using Raven.

Package compiled Java code into jar

There isn’t much to say about packaging a jar except how easy it is. You simply define the jar task, give the task the name you want the jar to be, and finally set any compile tasks as dependencies that you want included in the jar.


jar 'myJarName.jar' => :compile

Note here that instead of using the a symbol like :compile I choose to just pass a string. Either way is acceptable, I just wanted to show that you could use either.

The only thing I would change about this task myself would be to allow a block to define the name of the jar instead of the task itself. I like using the task name, but I would prefer a shorter syntax at the command line. Using the above example I would have to type something like


rake myJarName.jar

With long jar names like com.danielroop.project1.jar the command gets longer to type and a little more unwieldy. I would propose a syntax like this


jar :package => :compile do |task|
  task.jar_name = 'com.danielroop.project1.jar'
end

You have to write a little more in the build file, but it is a little more self explanatory and the command line syntax looks cleaner. If you look at the source for the flex tasks I define later you will see I allowed for both task definitions.

Compile Flex Source Code into Flash Library (swc)

Raven does not support Flex out of the box, so I implement two flex tasks that make it possible to compile your Flex source using Raven. Dependencies work just like in java, except I only include swc files in the command to the Flex instead of .class files.
The syntax for compiling a swc looks like this


compc :swc => :swc_dep do |task|
  task.src = 'src/flex'
  task.output = 'com.danielroop.project1.swc'
end

Let me break this down piece by piece. The task you are running is compc, I name it this because that is what flex calls there component compiler which is how you build swc files. The task name can be either a custom name like I defined here swc or it can be the name of the swc you want to create. If you have not defined a output variable in your block then the name of the task will be used by default. swc_dep would be the name of a dependency task that that will contain swc files to be included with the compc call. Finally the src value is not necessary if you place your source in src/main/flex. The short syntax would look like this


compc 'com.danielroop.project1.swc' => :swc_dep

Compile Flex Source Code into Flash Application (swf)

This task was another task I have defined in the flex_tasks.rb file. It works almost exactly like compc but you are using the mxmlc compiler instead. The key differences are src should now point to your main mxml file (e.g. Main.mxml) and if output isn’t defined the main mxml file has it’s extention changed to .swf by default. So if your main file was MyApplication.mxml, then output by default would be MyApplication.swf. Your syntax ends up looking like this


mxmlc :swf => :swf_dep do |task|
  task.src = 'Main.mxml'
end

Retrieve and/or Build Dependencies from Other Projects/Third Party Libraries

The final piece of the puzzle is defining your dependencies. Raven does this by creating dependency tasks that are set as dependencies on other tasks as you have seen in the above examples. The dependency task has two collections that can be manipulated to define dependencies libs and deps. The libs property should be used to include local libraries, this would be used if you have a lib directory in your project that you stored all your dependencies in. The deps property is used to define remote depndencies that should be retrieved from a gem repository and unpacked. The syntax ends up looking like so


dependency :compile_dep do |task|
  task.deps << [{'Log4J' => '1.0'}, {'jdom' => '0.9'}]
  task.libs << Dir.glob('../lib/**/*.jar')
end

Here I have requested that log4j.jar version 1.0 and jdom.jar version 0.9 be retrieved from a gem repository. The version number is optional and the string is the project name. I have also told the build file to include all jar files in my projects lib directory as dependencies. I could have been more specific but for this example I wanted to show how easy it was to include a large group of files.

If this were a Flex task the syntax for remote dependencies would be the same, because the swc would just be bundled in the gem instead of the jar, but for the libs section you would change the line to look like this:


task.libs << Dir.glob('../lib/**/*.swc')

That is it, pretty simple. Now for the remote repositories. You can tell Raven where to look for these remote jars/swcs by defining sources at the top of your build file.


set_sources(["http://localhost:2233", "http://gems.rubyraven.org/"])

And finally you can have Raven deploy a remote jar that you have build by using the gem_wrap_inst task. The way this works is you call the command give it a name, and the dependencies should be the items you want included in the gem, lastly you should set the version of the gem. So you end up with something like this


gem_wrap_inst :gem => ['com.danielroop.project1.jar', :swc] do |task|
  task.version = '1.0'
end

Conclusion

That wraps the individual tasks I laid out to discuss. I was a breathe of fresh are from using xml syntax to define this stuff. Although I do believe Raven is a little immature at this point, and the community doesn’t seem very active. Which is not true about the Buildr community which I will be going over next. All in all I think Raven is a better solution than Ant or Maven, and I like how they created libraries to work with Rake instead of changing Rake as you will see Buildr has done. Do not let gem repositories scare you it is essentially a webbrick server that hosts gems, which are just a tar (.tar) that contains two tar balls (tar.gz). So if you rename mygem.gem to mygem.tar you can use winzip or zipgenuis or whatever your favorite unzipper is and see the contents just like you could any other tar file.

This post turned out a lot larger than I anticipated, and I hope the size did not scare you off before you read this line. I apologize if my grammar is bad, I am sure I will read over it later tonight to make some grammatical updates. I will try to get the Buildr post turned around a little quicker than this post. I had a lot of stuff going on lately like playing God of War and attending the inaugural Adogo Meeting.

Posted in build | Tagged , , | Leave a comment

Build Tool X vs Build Tool Y

We finished our intial proof of concept work for four build tools at work. The build tools were Maven 2, Ant with Ivy, Raven and Buildr. In the next series of post I would like to give my perspective on the two tools I reviewed (Raven and Buildr) in comparison to what I know about the other two (Maven 2 and Ant with Ivy).

I would like to use this post to lay out the criteria I am judging the tools.

  • Compile java source
  • Package compiled java code into jar
  • Compile Flex Source Code into Flash Library (swc)
  • Compile Flex Source Code into Flash Application (swf)
  • Retrieve and/or Build Dependencies from Other Projects/Third Party Libraries

I plan on using these criteria as the basis for my judgement, but I will point out any extra advantages or disadvantages I see along the way.

Posted in build | Tagged , , , , | Leave a comment

JUnit 4.4

A few months back I gave a presentation on testing software where I work. I believe the talk went well, and it definitely got me interested in testing, and different ideas and practices to ensure your code is working properly. I did all my examples using Java, and the JUnit framework, or derivatives there of. Most of the more powerful testing tools however where not part of the JUnit framework, and I am happy to see this is all changing with JUnit 4.4. It seems that the JUnit team has looked at its users and decided that new syntax sugar should be added to allow for more complete and accurate tests.

If you want to see all the neat new syntax that comes with JUnit 4.4 check out the release notes. Unfortunately I will not be able to enjoy the fruits of their hard work for some time, since it seems most of the exciting stuff like theories and assumptions require Java 1.5 or greater, and my team is stuck on Java 1.4 because of ColdFusion 7.

Posted in testing | Tagged , | Leave a comment

Alternatives to Ant

A very exciting thing happened recently at my office. I was given the go ahead to do a proof of concept build process using Ruby (Sica if you are reading this calm down and enjoy your new baby!). The task was to use two exciting projects in our repository, and build them. The first project had Java that needed to be bundled in a jar, and Flex that needed to be compiled into a Library (swc). The second project was Flex that needed to be compiled into a Flash Movie (swf) with a dependecy on the swc from the first project.

Initially I was only supposed to look into Raven, but after some intial digging I found a second project Buildr. I was pleased with what I found with both projects, and after I finish my analysis at work I hope to do a post on each in comparison to each other and existing Java projects like Ant with Ivy and Maven 2.

Posted in build | Tagged , , , , , | Leave a comment

Adogo Is Official

I have very good sources that tell me that the Adogo User Group has been accepted as the official Orlando User Group by Adobe.  And I am posting this because I wanted to scoop them on the story.

In other exciting news, they have posted the topics for the first meeting.  Adam is going to be giving a talk about ColdSpring with Flex and ColdFusion, which sounds like it should be pretty good.  Also I have been asked to put together a talk at one of the next few meetings.  The topic hasn’t been announced yet, but I will be busy putting everything together over the next few weeks so I can have the primer ready, and all that good stuff.

Posted in community | Tagged , | 3 Comments