Thursday, September 8, 2011

Exceptions for Control Flow: Always Wrong?

It is generally accepted that exceptions should never be used for control of flow and should only be used for exceptional conditions. On face value, that assertion seems to make sense. For instance, given a method that iterates over an array, the user of the method should not be expected to catch a NoSuchElementException because the method does not make use of a hasNext() method.

If a state checking method is available to help determine if another method can be called, we would all assume it should be used. However, what about cases where a series of methods must be called in sequence to accomplish a larger overall task? In a scenario I'm thinking off, a series of individual methods must be called, where each in the sequence is called only if the previous call succeeded.

Would disobeying the exceptions rule possibly make for cleaner code in such a case?

To work out my thoughts, I thought of a home building example. Let's simplify building a house to three steps: laying a foundation, putting up the framing and walls, and then adding the roof.

Let's also imagine a contractor who's job is to build a house, ensuring its marked as condemned is any of the construction fails. If the contractor attempts to add the framing without the foundation, or the roof without the framing, a catastrophe would surely happen. To avoid the catastrophe, the builder must check the house to ensure the previous step completed before moving on to the next.

Like an Iterator with a hasNext() method, the House class has methods or state variables indicating whether it has the requirements for the next phase of construction and something similarly for its overall state: whether or not its livable or condemned.

    class House {
boolean hasFoundation
boolean hasFraming
boolean hasRoof
boolean isCondemned
}

The Contractor classs has one public method, buildHouse(), that returns a House, livable or not, and a private method for completing each step in the construction process:

    class Contractor {

public House buildHouse() {
def house = new House()

addFoundationTo(house)

if (house.hasFoundation) {
addFramingTo(house)
if (house.hasFraming) {
addRoofTo(house)
if (!house.hasRoof()) {
house.isCondemend = true
}
} else {
house.isCondemned = true
}
} else {
house.isCondemned = true
}

house
}

private def addFoundationTo(house) {
//...
}

private def addFramingTo(house) {
//...
}

private def addRoofTo(house) {
//...
}
}

This contractor is overly cautious as he wants to condemn the house as soon as possible if one of the constuction steps fails to avoid a catastrophe. Another implementation may wait till the building process is completed before condemning the house:

    class Contractor {

public House buildHouse() {
def house = new House()

addFoundationTo(house)

if (house.hasFoundation) {
addFramingTo(house)
if (house.hasFraming) {
addRoofTo(house)
}
}

if (!house.hasRoof()) {
house.isCondemned = true
}

house
}
}

This contractor only looks at the roof and the end of the process to determine if the house should be condemned, which may lead to a lawsuit if the someone where to change the order in which the steps were executed. The contractor's check at the end could include looking at the foundation and framing as well, but he is overly trusting of his workers not to proceeded and the extra checks may be costly.

What if a different contractor required a complaint be raised by a worker if any of the home building steps fails and condemns the house if he receives the complaint before attempting the next step?

    class Contractor {

public House buildHouse() {
def house = new House()

try {
addFoundationTo(house)
addFramingTo(house)
addRoofTo(house)
} catch (ConstructionComplaint complaint) {
house.isCondemned = true
}

house
}

private def addFoundationTo(house) throws ConstructionComplaint {
//...
}

private def addFramingTo(house) throws ConstructionComplaint {
//...
}

private def addRoofTo(house) throws ConstuctionComplaint {
//...
}
}

Here, a specific, but uniform, exception is given to the builder if any of the steps fail, ensuring the next step in the process is not attempted, avoiding a catastrophe.

An argument for this procedure may include that adding another step, like adding a garge, does not increase the complexity of the building process by adding more evaluation (branching), or require the builder to explicitly check that a garge can be added. Conversely, this approach may be overly trusting in that it requires a each worker method properly files a complaint.

I think it's safe to say we've all seen complex code as least as complex as the first implentation of the buildHouse() method. Is it worth asking that the use of exceptions would make the code cleaner? On the other hand, such cases usually present a situation where the user is required to code around what may be a poor design.

This topic and discussion may be nothing new, but I think it should help give thought to reasoning about the rule stated earlier regarding using exceptions for only exceptional conditions. Here were not talking avoiding programmer errors, such as trying to access a non-existent array element, but using exceptions to simplify a process.

While I'm not trying to suggest generally that using exceptions to control flow makes for better programs, I do think it is worthwhlie to consider such rules and understand why they may exist in the first place.

Saturday, August 6, 2011

First Steps with Fantom

I'm now finally getting around to taking my first steps with Fantom, even though I thought I would months ago. Where else would I start then "Hello World", and while I'm here, I'll toss in a comparison to the Java and Groovy versions for good measure:

Below is your classic Java "Hello World" program:

    public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

Here is a Fantom version of the same program.

    class HelloWorld {
static Void main(Str[] args) {
echo("Hello World!")
}
}

There are a few differences worth noting:

  • The public keyword is not required.
  • The void keyword maps to a Void type.
  • The equivalent to String in Java is Str in Fantom.
  • The semicolon ending the singular statement in the Java version is not present in the Fantom version, in fact, including it would cause an error.

Also, the argument to the main method in the Java version is not required in the Fantom version:

 class HelloWorld { static Void main() { echo("Hello World!") } } 

Since I spend most of time with Groovy, I thought I'd consider the Groovy version. Like the Fantom version, the public keyword is not required, but the declared argument to the main method is, although we don't have to declare a type:

    class HelloWorld {
static void main(args) {
println "Hello World"
}
}

Additionally, the parentheses are optional and unlike with the Fantom version, an ending semicolon is allowed.

Now back to Fantom and getting the greeting printed out on the screen. The Fantom class can be placed in a file and run using the fan command:

  $ fan HelloWorld.fan
Hello World!

With Groovy, the println call can simply be placed in a file and executed as a script using the groovy command:

  $ cat hello.groovy
println "Hello World!"

$ groovy hello.groovy
Hello World!

Knowing this, I thought I'd try doing something similar in Fantom:

  $ cat hello.fan
echo("Hello World!")

$ fan echo.fan
/home/prystasj/workspace/Fantom/echo.fan(2,1): Expected 'class', not 'echo'
ERROR: cannot compile script

Unfortunately, we don't get the same results, but Fantom does have an interactive shell we can use, fansh:

  $ fansh
Fantom Shell v1.0.59 ('?' for help)
fansh> echo("Hello World!")
Hello World!
fansh> exit

Next, let's have the Fantom class make use of the array representing the arguments passed into the main method. In Fantom, arrays are also zero-based and accessed in the same way we are all used to:

   class HelloWorld {
static Void main(Str[] args) {
echo("Hello and " + args[0])
}
}

Running the progam with a single argument will have the argument included in the output:

  $ fan HelloWorld.fan goodbye
Hello and goodbye

Saturday, July 23, 2011

Groovy: Reducing Duplication with Closures

I've been using Groovy as my primary programming language for a few years now. One of the features that is commonly discussed and I've read about a lot are closures. While I've appreciated what I've read, I've had a difficult time recognizing good places to use them. Like I lot of things, if you "dont use it, you lose it".

One situation where I have been able to find uses for closures recently was using them instead of methods to reduce duplication that I can hopefully demonstrate using the following example scenario. We have a TextAnalyzer class whose sole responsibility is to analyze a string and report the words separated by commas and semicolons and the counts for said delimiting characters.

The TextAnalyzer.analyze() method below travels through a given string, aggregating words and counting commas and semicolons and returns an instance of TextAnalysis that contains the results.

For example, given the string:

    groovy,is,a,language;enjoyable

The result is:

    TextAnalysis(words:[groovy, is, a, language, enjoyable], commas:3, semicolons:1)

Here's the first version of the code. Of note here to me is the duplication that exists when either a comma or semicolon is found. In both clauses, the current word is pushed onto the words list and is reset in preparation for the next word.

    def text = "groovy,is,a,language;enjoyable"

new TextAnalyzer().analyze(text)

@groovy.transform.ToString(includeNames=true)
class TextAnalysis {
List words
int commas
int semicolons
}

class TextAnalyzer {

def analyze(text) {
def words = []
def word = ''
int commas = 0
int semicolons = 0

text.each { c ->
if (c == ',') {
commas++
words << word
word = ''
}
else if (c == ';') {
semicolons++
words << word
word = ''
}
else {
word += c
}
}

if (word) words << word

new TextAnalysis(words:words, commas:commas, semicolons:semicolons)
}
}

One option to remove the duplication is to add another method that would handle the push and reset of the current word, named add below:

    class TextAnalyzer {

private add(words, word) {
words << word
''
}

def analyze(text) {
def words = []
def word = ''
int commas = 0
int semicolons = 0

text.each { c ->
if (c == ',') {
commas++
word = add(words, word)
}
else if (c == ';') {
semicolons++
word = add(words, word)
}
else {
word += c
}
}

if (word) words << word

new TextAnalysis(words:words, commas:commas, semicolons:semicolons)
}

}

This works and produces the same result, however a couple of things bother me about it. First, the method is doing two things, it's adding the current word to the list, and return a value to reset the word. The user is required to know that it needs to assign the return value to the current word.

We could also used a boolean to indicate a word was complete that could of been evaluated at the end of each character evaluation, but that would add a bit more complexity to the analyze method through additional branching.

Another approach that sits better with me is to use a closure that's internal to the analyze method. Let's call it completeWord:

class TextAnalyzer {

def analyze(text) {
def words = []
def word = ''
int commas = 0
int semicolons = 0

def completeWord = { ->
words << word
word = ''
}

text.each { c ->
if (c == ',') {
commas++
completeWord()
}
else if (c == ';') {
semicolons++
completeWord()
}
else {
word += c
}
}

if (word) words << word

new TextAnalysis(words:words, commas:commas, semicolons:semicolons)
}

}

There are a few reasons I like this better besides the reduced duplication. Since the closure is defined within the method, it has access to the variables holding the current word and the list of words, so there's no need to pass the variables around. Also, since there are no other uses for either the previous method or the new closure, the code reads better having the logic to complete a word inside the method itself.

Of course, a really concise version of the analyze method probably would not iterate over the characters in the string at all (I was thinking I could of simply started with this version, but found the iteration helped illustrate things a bit more):

    class TextAnalyzer {
def analyze(text) {
new TextAnalysis(
words: text.split(',|;'),
commas: text.count(','),
semicolons: text.count(';')
)
}
}

The above though still has some duplication in the call to String.count(). We remove it using a closure that counts:

    class TextAnalyzer {
def analyze(text) {

def count = { c -> text.count(c) }

new TextAnalysis(
words: text.split(',|;'),
commas: count(','),
semicolons: count(';')
)
}
}

While we have not seen any huge wins here, in a more complex scenario, defining closures within methods could help make our code more readable and easier to maintain, which is always a win.

Tuesday, July 5, 2011

Spock: Varying Results from a Mocked Method

In the Groovy world, I'm a big fan of the Spock specification framework and have written about it before. One little challenge I ran into was to setup an interaction for a mock to return a different result depending on how many times its been called. To illustrate what I'm talking about, I'll set things up with the following little scenario.

Let's say we have a class, RecordProcessor whose job is to read records from a source until there are no more records to be read. A collaborator of the class, RecordReader, is responsible for the actual record reading, and is the class whose behavior we want to mock.

The method to be called on the RecordReader takes as a parameter, the number of records to read and reports whether or not there are more records left in the source to be read. For simplicities sake, let's assume the responsibility of the RecordProcessor is to simply invoke the RecordReader until it reports that there are no more records to read.

class RecordProcessor {

RecordReader recordReader
int threshold

def process() {
boolean isProcessingComplete = true
boolean recordsRemaining = true

while(recordsRemaining) {
recordsRemaining = recordReader.readRecords(threshold)
}

// would do more processing evaluation here...
processingComplete
}
}

We want our unit test to determine whether or not the class under test can detect when there are no more records to read and report that processing was completed. Here is the base of the Spock specification where the mocking setup is left to a method:

class RecordProcessorSpec extends Specification {

def threshold = 5
RecordProcessor recordProcessor = new RecordProcessor(threshold: threshold)

def "can process all the records accessible by a record reader"() {
given:
recordProcessorHasMockRecordReader()
when:
isProcessingComplete = recordProcessor.process()
then:
isProcessingComplete
}
}

The wiki page describing mocking and interactions can be found My first pass at setting up the mock looked like the below, where the first two calls to readRecords method would return true and the third would return false (for more on how mocking is done with Spock, see Interactions).

    def recordProcessorHasMockRecordReader() {
def mock = Mock(RecordReader)
2 * mock.readRecods(threshold) >> true
1 * mock.readRecods(threshold) >> false
recordProcessor.recordReader = mock
}

This didn't do the trick however as 3 invocations were detected for the first definition of how the method should behave:

  Too many invocations for:

2 * mock.readFrom(threshold) >> true (3 invocations)

I found one answer in using a closure to define the return value of the method invocation that had a bound variable to tracking the number of reads that were asked for:

    def recordProcessorHasMockRecordReader() {
def mock = Mock(RecordReader)
def readsAttempted = 0
3 * mock.readRecods(threshold) >> {
readsAttempted++
readsAttepted < 3 ? true : false
}
recordProcessor.recordReader = mock
}

With each invocation of the readRecords method by the RecordProcessor, the readsAttempted variable defined outside of the closure is incremented. With the first two calls, the RecordProcessor is told there are more records to process, and with the third call, it is told that all the records have been read.

Are there any other approaches anyone has found to approach a similar problem? Thanks.

Thursday, June 16, 2011

Experimenting with a Groovy Template Engine and PermGen

I had a Groovy application that was using a SimpleTemplateEngine was running out of PermGen space after running for a long period of time. The app was using version 1.7.10 and was running in a Tomcat 6 container.

Load tests consistently showed a linear increase in the PermGen consumption (monitored by JConsole). Originally, the template engine was used to create the template from the template text with every request. Also, the template was injected as a resource to the class using the engine.

After switching the class to create and cache the created template, we were still experiencing an OutOfMemoryError during the load test. Only after removing the use of the engine in its entirety, where we able to observe the PermGen leveling off.

A run of jmap -permstat PID against the application process showed a large number of entries of the form of:

  0x00000000f96bf800      24      233928  0x00000000f7f19558      dead    groovy/lang/GroovyClassLoader$InnerLoader@0x00000000fd784720

To investigate things, I thought I would try to see if I can reproduce the behavior using only a script run by Maven. As I go, I'll record what I see below. To start, I'll use Groovy 1.8.0.

The script will loop and complete a template with every iteration. The first version of the script will use a singe instance of SimpleTemplateEngine, but will call the createTemplate method with every iteration:

  def runs = 5
def templateText = 'Run number: ${runNumber}'
def engine = new groovy.text.SimpleTemplateEngine()

runs.times { i ->
def binding = [runNumber:"$i"]
engine.createTemplate(templateText).make(binding)
}

We'll start the script with verbose class loading on so we see what classes are loaded into PermGen during execution:

  $ export MAVEN_OPTS="-verbose:class"
$ mvn -o exec:java -Dexec.mainClass=TemplateEngine | tee /tmp/out
$ grep SimpleTemplateScript /tmp/out
[Loaded SimpleTemplateScript1 from file:/groovy/shell]
[Loaded SimpleTemplateScript2 from file:/groovy/shell]
[Loaded SimpleTemplateScript3 from file:/groovy/shell]
[Loaded SimpleTemplateScript4 from file:/groovy/shell]
[Loaded SimpleTemplateScript5 from file:/groovy/shell]

We might suspect that behavior since not only are reading the template everytime through the loop. A new version of the script below again uses the same SimpleTemplateEngine instance and only creates the template once:

  def runs = 5
def engine = new groovy.text.SimpleTemplateEngine()
def createdTemplate = engine.createTemplate(templateText)

runs.times { i ->
def binding = [runNumber:"$i"]
createdTemplate.make(binding)
}

With this version, we only see one class load:

  $ grep SimpleTemplateScript /tmp/out
[Loaded SimpleTemplateScript1 from file:/groovy/shell]

This is encouraging, but I want to make sure PermGen truly doesn't increase linerally over time. To do so, I'll add a sleep at the beginning of the script so I can attach JConsole to the Maven process and set the number of runs to a really high number:

  sleep(30000)
println "Starting..."
def runs = 50000

The PermGen graph is pretty jagged in the case where the createTemplate method is called each time through the loop. However, it appears that space was recovered as well so we do not end up running out of space:

To test the version where we only create the template once, I'm going to up the number of runs to 5,000,000 as execution is much faster without the repeated calls to the createTemplate method. The PermGen graph rises quickly but levels off:

So far, based on the verbose class loading and graph results, it looks like things are a definite improvement over what I was seeing in the original applcation. Just to be sure, I'm going to same experiment creating the template only once with version 1.7.0. as well.

With the single instance approach, the PermGen increased at a very slow rate. Using the approach where we call the createTemplate method with each iteration, the graph is again jagged:

At the end of this little experiment, I don't see any significant differences between Groovy 1.7 and 1.8. My work application was running in a Tomcat container which may also factor into things, along with a slightly different JVM version. Outside of the container, while the PermGen does rise with every load of a SimpleTemplateScript class, the memory is recovered.

I'll likely have to do some more experimentation to understand the behavior I was seeing in the original applcation.

Wednesday, April 27, 2011

JMeter Plugin: NonGuiDriver NullPointerException & SOAPException

When using the Maven JMeter Plugin to try and send web service requests, I ran into the following exception:

  Error in NonGUIDriver java.lang.NullPointerException

My jmeter.log presented the following:

2011/04/27 14:20:46 ERROR - jmeter.save.SaveService: Conversion error com.thoughtworks.xstream.converters.ConversionException: org/apache/soap/SOAPException : org/apache/sp/SOAPException
---- Debugging information ----
message : org/apache/soap/SOAPException
cause-exception : java.lang.NoClassDefFoundError
cause-message : org/apache/soap/SOAPException
first-jmeter-class : org.apache.jmeter.save.converters.TestElementConverter.unmarshal(TestElementConverter.java:108)
class : org.apache.jmeter.save.ScriptWrapper
required-type : org.apache.jmeter.protocol.http.sampler.WebServiceSampler
path : /jmeterTestPlan/hashTree/hashTree/hashTree[2]/WebServiceSampler
line number : 741
-------------------------------
2011/04/27 14:20:46 ERROR - jmeter.JMeter: java.lang.NullPointerException
at org.apache.jmeter.gui.tree.JMeterTreeModel.addSubTree(JMeterTreeModel.java:91)
at org.apache.jmeter.JMeter.run(JMeter.java:728)
at org.apache.jmeter.JMeter.startNonGui(JMeter.java:706)
...

The solution was to add a dependency on soap:soap:2.3 to my plugin definition:

      <plugin>
<groupId>org.apache.jmeter</groupId>
<artifactId>maven-jmeter-plugin</artifactId>
<version>1.0</version>
<configuration>
<reportDir>${reportDir}</reportDir>
</configuration>
<dependencies>
<dependency>
<groupId>soap</groupId>
<artifactId>soap</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
</plugin>

After adding the dependency, the tests ran fine:

  Starting the test @ Wed Apr 27 14:42:57 EDT 2011 (1303929777586)
Tidying up ... @ Wed Apr 27 14:42:59 EDT 2011 (1303929779839)
... end of run

Sunday, April 17, 2011

Fantom: Getting FWT to run on Linux 64-bit

Today I decided to start playing around with the Fantom language. After working through a couple of examples on the Hello World page, I ran into a problem running the example that uses the Fantom Widget Toolkit on my 64-bit machine.

After following the instructions from the Fantom Setup Page, I set my FAN_HOME in my .bash_profile to:

  FAN_HOME=/opt/fantom-1.0.58

I later tried running the Hello FWT example below:

  using fwt
class FwtHello : Test {
Void main() {
Window { Label { text = "Hello world" }, }.open
}
}

Which produced the following error:

  hello $ fan hello::FwtHello
sys::Err: java.lang.UnsatisfiedLinkError: Cannot load 32-bit SWT libraries on 64-bit JVM

I found a swt.jar here:

  /opt/fantom-1.0.58/lib/java/ext/linux-x86/swt.jar

The setup page recommended running fan to get a read on the platform:

 $ fan -version | grep platform
fan.platform: linux-x86_64

I created a directory, linux-x86_64, under java/ext, and copied the swt.jar downloaded from the Eclipse site to it.

Afterwhich, the example ran fine.