Monday, March 31, 2008

Expectation driven developement in NetBeans IDE 6.1 Beta

Most important feature of professional IDEs is the hints feature. They enable me to develop my programs driven by expectations. NetBeans IDE 6.1 Beta supports hints for programming languages. This is an important feature and I use this feature in a particular way which I call expectation driven development.

Before I write my program I like to make a mental picture of the complete program I am going to develop. Modeling tools help me visualize the mental design . I use ArgoUML for modeling the design. The class diagram thus drawn in ArgoUML is a complete picture of the program I want to write. Once this picture is ready the development of the source code becomes piece of cake.
This class diagrams explains that I expect an EquationExample class to contain my main method. I expect an interface IEquation to be implemented by two concrete classes named SquareEquation and CubeEquation. I also have mentioned my expectation of class EquationFactory through the method declarations which says the class EquationFactory shall be a factory class returning objects of concrete classes SquareEquation and CubeEquation in terms of IEquation reference.

I start my program with the settings as EquationExample as my project name and com.mycompany.equation.EquationExample.main as my main method in the NetBeans IDE 6.1 Beta new project dialog box.


In the main method provided by NetBeans wizard through automatic code generation I write the first expectation that I need an variable equationFactory and instanciate it. The NetBeans editor will not help me autocomplete my statement but I know that and do not want to use the autocomplete feature deliberately. Instead I want to exercise a different feature that is hints feature to create the code I want automatically through NetBeans IDE.


So when I write this statement obviously NetBeans shows me hint in the left side of the editor. As soon as I click on the hint NetBeans offers me options for solving the error by adding some code to my project. This time it asked whether NetBeans shall create a class named EquationFactory for me in the same package. This the exact expectation I have so I clicked on the hint suggested and NetBeans created the class for me.


We can see the class EquationFactory created and opened in the editor of its own. We can see NetBeans has not added public access specifier to the class which I will add eventually while completing the code.


Now I again return back to where we started. I want to complete my main method so I will continue writing the main method as if all the elements already exists and as soon as NetBeans find something not written yet it will offer me hint to complete it. This is great feature and this way I will be using it to fulfill my wishes.

This time I created a reference of IEquation interface named squareEquation and called the method createSquareEquation from equationFactory object. Note that IEquation interface is not written yet and even the createSquareEquation method is not there in the EquationFactory class. NetBeans promptly offers me hints for what is not there and still used.


This time NetBeans offered me to write IEquation class. NetBeans has no way to understand that this is an interface so it is offering a class. Anyway I will accept this option.

So here is my IEquation class. Now I will add the public access specifier to the class and replace the word class with interface. This completes my expectation of having an interface named IEquation.
Coming back to the main method again now NetBeans offers me to write the method createSquareEquation and I will accept the offer again.

NetBeans completed my method with the boilerplate code. I will now write new expectation that this method shall create an object of SquareEquation class and return it as IEquation.

As soon as I write the expectation even if the class does not exists NetBeans offers me a hint to write the class and I will accept again.

The hind to write a SquareEquation class.

Now coming back to main method again. I will complete my program by calling the solve method from the squareEquation reference, note that the method solve is not yet written in the IEquation interface. NetBeans offers hind to write that method.

As soon as I accept the offer a method with name solve is written in IEquation interface. Now coming to the SquareEquation class. I will implement the IEquation interface in this class and NetBeans hints me about the missing method solve. As I am implementing the IEquation as per contract I must write solve method and NetBeans hints me for this. I accept the offer again and the method is written for me as a boiler plate method.


I added some code to the method to complete the expectation of returning a square value of the parameter.


In this way I will go on writing the complete program as expected and NetBeans will help me write my program driven by my expectations.

Even if auto completion feature of NetBeans is powerful, I prefer this way of programming where I already know what is to be developed and then NetBeans offering me code writing hints. I call this way of programming as expectation driven development.

with regards
Tushar Joshi, Nagpur

UPDATE:
Now you can watch a video demo of this post. Please visit Expectation driven development through Netbeans IDE video demo.

2 comments:

  1. You can also use the NetBeans UML modules to generate source code directly from a class diagram. Have a look at http://uml.netbeans.org/ and http://www.netbeans.org/features/uml/index.html

    ReplyDelete
  2. Also, you could have a look at other quick hints - Press Alt + Insert

    and you can write your own hints:
    http://blogs.sun.com/geertjan/entry/java_hint_generator_for_netbeans

    ReplyDelete