Archive for the ‘java’ Category

I’m a Redliner, are you?

Wednesday, November 21st, 2012

campaign-home-page

This week I kicked off a campaign to increase the awareness and use of Redline Smalltalk, and get some financial support as well. The financial support will enable me to focus full time at least for a short while on getting Redline Smalltalk to Version 1.0. Things have come a long way since this Fan Letter To Redline Smalltalk.

Of course I could not have done this without the help of some very nice individuals: Sean T Allen, Pat Maddox, Peter Michaux, Jeff Heon, Brandon Hays, Robert Roland and Andrew Kiellor.

Hopefully you will help me too. You should follow this link to support Redline Smalltalk. You can be a Redliner too!

Automatically integrating Java with Smalltalk …

Thursday, June 7th, 2012

spock-stuffy

I have been working on Redline Smalltalk for some time, and just now I have made some changes that I am very proud of.

With the new additions it is now possible to automatically integrate Java classes with Redline Smalltalk allowing you to talk to Java classes as though they were 1st Smalltalk objects. There will be a longer more detailed post on this topic in the Redline blog very soon. The 2 second brief on how to integrate a Java class is to put the following in your Smalltalk script or class.

import: ‘com.domain.JavaClass’ as: ‘LocalName’.

What the above does it allow you to refer to com.domain.JavaClass via the identifier ‘LocalName’. The object referenced by ‘LocalName’ is a Smalltalk object and responds to messages just like any Smalltalk object.

Behind the scenes Redline Smalltalk uses Java Reflection to query the Java class and generate Smalltalk code that represents an Adaptor for the class. This Smalltalk code is then compiled on the fly to JVM bytecode and executed.

I’m proud of this work and I really hope you give it a try, and come back to the Redline blog for the full details .

Ruby Best Practice Patterns by Kent Beck …

Wednesday, December 23rd, 2009

 bees

I have not written a post for a while as I have been very busy working, developing Redline Smalltalk and looking after my twin boys. Recently I did get some free time and I needed a break from Redline Smalltalk and I picked up a book by Kent Beck from the top of my pile of books to be read. Yes there is a pile and there are about six books in it right now, there is even another Kent Beck book, which should not be a surprise to anyone as Kent is a great mind and his writing is worth your time.

The Kent Beck book I read was “Smalltalk Best Practice Patterns” and while you may think “what the! Isn’t this posts title about Ruby?” what I want to get across is that this book transcends a specific language and is applicable to any Object Oriented language and to anyone wanting to get a good grounding in “Best” practice and patterns of development. Having done Smalltalk, Ruby, Java and C++ I can see how each and every nugget can be applied. So regardless of your language choice you should choose to get this book.

sbpp-book

So what does “Smalltalk Best Practice Patterns” have in it?

SBPP is a quick and easy read and it took me about 16 hours to finish it, but of course I am still thinking about the material in the book and how best to apply it. There are 92 specific items covered and a very good real world example and discussion of why. The real world, every day nature of the book makes it a must have, each section has the following elements:

  • Title
  • Preceding Patterns
  • Problem
  • Forces
  • Solution
  • Discussion
  • Following Patterns

The appendix having a reference to each point making it great to have near by.  The book contains the following sections and topics:

  • Introduction
  • Patterns
  • Behavior
    Methods
    Message
  • State
    Instance Variables
    Temporary Variables
  • Collections
    Classes
    Collection Protocol
    Collection Idioms
  • Classes
  • Formatting
  • Development Example
  • Appendix A:  Quick Reference

Being a big fan of immutable Objects I especially enjoyed the section on “Getting Method”. If you only adopt one thing from this book I hope it is about private getting methods, sometimes called “getters” or “accessors”. It is hard to stress how much better a large amount of software would be if it adopted this practice. This is how Kent puts it:

“Here’s the real secret of writing good Getting Methods - make them private at first. I cannot stress this enough.” and goes on to say “There are cases where you will publish the existence of Getting Methods for use in the outside world. You should make a conscious decision to do this after considering all the alternatives. It is preferable to give an object more responsibility, rather than have it act like a data structure.”

One comment from a reader, Kyle Brown is “The patterns in this book are absolutely fundamental to good Smalltalk programming. No one who calls himself a Smalltalk programmer should be without the knowledge in this book” and I would go so far as to say that no one who calls themselves an Object Oriented developer should be without the knowledge in this book.

MAVEN: Tuning which tests to run …

Thursday, November 26th, 2009

On a recent project we needed to tune MAVEN to run unit tests and integration tests separately.

When we execute “mvn test” we want just our unit tests to run, and when we execute  “mvn integration-test” we want integration tests to run. Of course the unit tests will run as well because they are a dependency.

This is not as simple as it could be in MAVEN so I’m sharing the working plugin configuration with you.  Here it is:

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/*ITest.java</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>integration-tests</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>${integration.test.skip}</skip>
<includes>
<include>**/*ITest.java</include>
</includes>
<excludes>
<!– Ensure Maven inhierits exclude –>
<exclude>must-be-here-for-maven</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>

Agile Java Developers wanted !

Tuesday, June 10th, 2008

Would you like to come and work with me on a green-fields Java, Web 2.0, Social Network application using frameworks like Seam and using Agile and TDD techniques? If yes, then please apply through this Seek advertisement. Updated advertisement.

Come on Java closures …

Tuesday, May 6th, 2008

serving

I can’t wait for Java closures because they will make our code simpler and cleaner. For example, we can replace

FileInputStream input = new FileInputStream(fileName);
try {
    // use input
} finally {
    try {
        input.close();
    } catch (IOException ex) {
        logger.log(Level.SEVERE, ex.getMessage(), ex);
    }
}

with this

with (FileInputStream input : new FileInputStream(fileName)) {
    // use input
}

AWESOME !

(The example above was taken from Neal Gafter’s Blog.)

Closures would have made the code in my East example more clear too.

SEAM: Weaving the layers …

Friday, May 2nd, 2008

which-box

For a short while I have been looking into JBoss Seam and working through the examples and coding my own project.

My last few projects were Spring/Spring MVC based and architected with distinct layers to keep the code clean and focused, and I wondered how Seam supported this approach. Initially I thought I could just get Seam to inject the items I needed to form different layers and it does, but this isnt idiomatic Seam.

I think it is important to understand the idioms of a framework so you understand how it is intended to be used and therefore get the most out of it. So I posted a question about layering to the Seam Forums.

There were a couple of responses but one in particular from Andy Gibson spelt it all out so clearly that I had to post it here, since it applies to a wider audience than just Seam users.

 

Hey James,

Shane is right, it really depends on the application context. Another important factor (I think) is whether code is going to be re-used later on, either from another point in the same application, or from another client elsewhere.

These things can always be refactored out later but if you know about it ahead of time, it can impact design decisions earlier.

The first major layering decision though is whether to mix business logic and view logic in the same session bean. A lot of times we’ll see code to handle button clicks in the same bean as the code to perform actions on entities. If we want to perform the same actions from other places in the code or even other applications, we may want to separate that out which is fine.

You can further demarcate the layers based on state and function. The ‘backing bean’ session beans can hold the view state and logic, but any general functional code can be placed into stateless beans which have state passed in to them.

The obvious benefits are that we put the method in one place and always use it to perform that action so if we decide to change it, or always send an email to someone when that action is performed, we can just put it in that one method.

This is probably obvious to many as it is just good OO principles, but Spring always seemed to to put as many layers in as possible because of the (overstated IMO) need to change any layer at any given point in time. We started with MVC, and then Spring threw in service layers, repositories, Daos and so on.

Seam is a little different, especially since it is based around a stack that probably 80% of people are going to use anyway, hence no need to swap out layers. With Seam you can just put layers where they are logical, or it is prudent to do so.

Cheers,

Andy Gibson

Seam handles the weaving (I had to get in a sowing term) of web and services so well that the need for heavying layering is removed, at least for the immediate development. However, I am still a little weary of putting EntityManager interaction logic across more than one class and still choose to put this into a Repository object that is in turn injected into the Action/Service.

Happy seaming !

REST: What are my OPTIONS ?

Thursday, April 24th, 2008

acorn

I was reading a post on the blog by Simon Harris about a suggested RESTful way of requesting information in ranges, and I wondered why we don’t use the OPTIONS keyword more, since we appear to be right at home with GET, PUT, POST and DELETE?

There is no standard for REST that I am aware of and maybe this is the reason and maybe we need someone to champion a draft or suggested ’standard’ for using REST and REST-enabling web sites.

I’d like to propose that sites use OPTIONS to enable a method of discovering what resources a site exposes. For example, an OPTIONS request to www.acme.co may respond with an XML representation like this (this is rough but I’m sure you can follow it):

<resources>
<services>
    <service name="consulting" uri="/consulting"/>
    <service name="support" uri="/support"/>
</services>
<products>
   <product name="acme torch"  uri="/product/torch"/>
   <product name="acme battery" uri="/product/battery"/>
</products>
<gateways>
    <gateway name="shopping cart" uri="/cart"/>
</gateways>
</resources>

From this the whole offering of the site is discoverable in a reasonably uniform way. Further OPTIONS request would make possible finer inquiry into the supported options and functionality.

This approach would keep the HEADers free to do what they do in regards the client to server conversation and the supported representations of resources.

If we all took this approach then clients could become smarter and provide more automated and regular features on top of the human readable site. Imagine asking your browser to find the cheapest iPod and it going through your bookmarks or google search results and interrogating each site for the best price, heck, it could even make the purchase too.

Want to work more on this, then drop me a line.

Qi4J: Mixin a little love …

Thursday, April 3rd, 2008

mixins

Last night the Melbourne Patterns Group was canceled but ThoughtWorks invited the few who turned up to participate in their internal study group.

They posted a couple of topics on the board and people signed up for the ones they would most like to discuss, and Qi4j was one of the topics.

What is Qi4j ? From the web site:

The short answer is that Qi4j is a framework for domain centric application development, including evolved concepts from AOP, DI and DDD.

Qi4j is an implementation of Composite Oriented Programming, using the standard Java 5 platform, without the use of any pre-processors or new language elements. Everything you know from Java 5 still applies and you can leverage both your experience and toolkits to become more productive with Composite Oriented Programming today.

Why should you care ?

Because it brings mixins to Java (in the absence of closures - soz, couldn’t resist plugging them again)

Now you can keep the behavior and state related to that behavior physically separate from other state and behavior, making your classes simpler. No more changing the state of a variable and finding something else no longer works. Ok, this can still happen but you get my drift.

In practical use you can keep the behavior and state related to persistence separate from the rest of the domain state and behavior. So now you will know that the Id field is related to Hibernate persistence and not some domain specific Id, and you also have the persistence methods like save and find separate. Similarly you could keep validation behavior and state separate from other logic and state.

The benefit is not just this separation and support for the composite pattern, but a great mechanism for ensuring behavior is implemented only once, yet available to anyone who needs it mixed into their class. Less code hopefully means simpler and easier to understand code and less bugs.

If you get a chance, then check it out.

Software has entropy, so make it more maintainable …

Thursday, February 7th, 2008

Gian Lorenzetto presented an insightful and thorough review of “Implementation Patterns” by Kent Beck last night at the Melbourne Patterns Group and it reminded me of discussions I have had with the development team on making code more understandable and therefore more maintainable. What follows is the gist of the discussion ….

The systems we build are ore more organic than they are mechanical. Brent said this better than I and described software systems as having an entropy.

This is a very good analogy, since we grow applications and maintain them over time. When left neglected they take more time to maintain and bring back to life or move in another direction. Like a garden.

I think it is worth pondering the above for a little while.

Related to the above is this code I saw in an application:

void setMessage(String message) {
    if (message.length() > MAX_SIZE) {
        this.message = message.substring(0, MAX_SIZE);
    } else {
        this.message = message;
    }
}

I have the following observations about this code and suggested improvements to make it more clear and therefore more maintainable:

(I know it is a simple setter but this shouldn’t remove the need to make more clear)

1. It is surprising that a setter also truncates

2. MAX_SIZE of what ?

Here it is applied to the message length, but can it be applied to another place, maybe.
A name that would be more clear would be MAX_MESSAGE_SIZE. When someone sees it declared as a constant at the top of the file they will immediately know what size we are talking about and that they should not use it when dealing with the maximum size of a subject line.

2. The complexity of the setter is higher than it needs to be and in solving this complexity we can also make the method more clear. Currently there are two paths through the method, meaning we need more tests around it and it is harder to understand.

Here is a suggested alternative that I think is more clear and reduces complexity:

void setMessage(String message) {
    this.message = truncateTo(message, MAX_MESSAGE_SIZE);
}

String truncateTo(String thingToTruncate, int sizeToTruncateTo) {
    if (sizeToTruncateTo < thingToTruncate.length()) {
        return thingToTruncate.substring (0, sizeToTruncateTo);
    }
    return thingToTruncate;
}

The method truncateTo() probably belongs elsewhere in a String utility class, making the code in this class simpler again. I haven’t just moved the conditional elsewhere, since it doesn’t have multiple paths through the code, so the npath complexity is lower.

Even simple setters can be made more simple and more clear, which helps us understand and grow our garden.