<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
    <channel>
        <title>Chris Rohr - Blogs at Near Infinity</title>


        <link>http://www.nearinfinity.com/blogs/</link>
        <description>Employee Blogs</description>
        <language>en</language>
        <copyright>Copyright 2010</copyright>
        <lastBuildDate>Wed, 18 Nov 2009 16:20:17 -0500</lastBuildDate>
        <generator>http://www.sixapart.com/movabletype/</generator>
        <docs>http://www.rssboard.org/rss-specification</docs>
        
        <item>
            <title>Be careful with Groovy&apos;s optional parens on method calls!</title>
            <description><![CDATA[<p>My current project (a web application) has a module that is almost entirely written in Groovy.&nbsp; <b>Note:</b> We are using the latest version 1.6.5 of Groovy.</p><p>In one of the methods we had some logic similar to the following:<br /><br /></p><code> def myMethodToProcessTheList(def listOfThings) {<br /></code><blockquote><code>if (listOfThings &amp;&amp; listOfThings.size &lt; 0) {<br />&nbsp;&nbsp;&nbsp; //Do my logic<br /></code><code>}<br /></code></blockquote><code>}</code><br /><br />When the above code is run using Java 5 everything works wonderfully.&nbsp; However when we pushed the code out to our server running Java 6 we got an interesting error stating that it couldn't compare java.util.ArrayList to java.lang.Integer.<br /><br />The problem is actually that the size method needs the parens after it (<b>Note:</b> The Groovy docs actually do state that methods with at least <u>ONE</u> param can omit the parens).&nbsp; I realize now that it would make sense that parameter-less methods would need parens because how else would Groovy distinguish between methods and property accessors?&nbsp; I did find one forum <a href="http://old.nabble.com/TestApp.groovy-pre-Grails-1.1-td22437010.html">post</a> that indicated that in Groovy, lists have special properties that act like the spread-dot operator.&nbsp; That is why the listOfThings.size returned the ArrayList instead of a number.<br /><br />So why does this work in Java 5 but not Java 6?&nbsp; That is a great question, one of which I really don't have a definitive answer.&nbsp; I'm not completely sure if this purely a Java 5/Java 6 difference or simply a difference between the IBM Java 5/Java 6 JVM.<br /><br />If anyone has a theory as to why it would work one way on Java 5 and the other in Java 6, I would love to hear it.<br /><code></code><blockquote><code></code></blockquote>]]></description>
            <link>http://www.nearinfinity.com/blogs/chris_rohr/becareful_with_groovys_optiona.html</link>
            <guid>http://www.nearinfinity.com/blogs/chris_rohr/becareful_with_groovys_optiona.html</guid>
            
                <category domain="http://www.sixapart.com/ns/types#category">Groovy</category>
            
            
                <category domain="http://www.sixapart.com/ns/types#tag">ArrayList</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">groovy</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">ibm</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">jvm</category>
            
            <pubDate>Wed, 18 Nov 2009 16:20:17 -0500</pubDate>
        </item>
        
        <item>
            <title>Agile in School</title>
            <description><![CDATA[I have recently been taking some graduate classes in the field of Information Technology Management.&nbsp; After the first few core classes, I have come to the conclusion that there are WAY too many higher education institutions that are avoiding Agile methodologies when they begin teaching software development practices.<br /><br />To give some background on where I'm coming from, the program that I had started included a class on project management.&nbsp; The semester had a week or two going into detail about the standard software development life cycle (SDLC).&nbsp; The way they described it was exactly the way waterfall works.&nbsp; All work was done in phases, requirements, design, development, test, deployment, all of which revolves around mountains of documentation and paperwork.<br /><br />Seeing as I have been working on an Agile based project for over 3 years, I thought I would inject some of my thoughts for the class on how things could be better.&nbsp; I couldn't believe how much negativity I got from other students and even the professor.&nbsp; I was told numerous times how the Agile methodologies (in general, not specific implementations) wouldn't work for large projects and is only good for prototyping functionality.&nbsp; The professor even made the comment that Agile was too restrictive when it comes to the time constraints (this I had a good laugh at).<br /><br />To make matters worse, if I was to continue the program, the last 5 classes are all different steps in the waterfall process.&nbsp; There is a class on Requirements, there is a class on Design, etc., all leading up to a final course that puts it all together.&nbsp; I don't see a problem with teaching students different ways to gather requirements or how to go about thinking about and designing a system.&nbsp; What I do have a problem with is that these higher education institutions are still forcing a specific system down the throats of students when they should be training the students on how to be most effective in their jobs and provide them with as many tools to accomplish that goal.<br /><br />Agile isn't just a new fad, it has been around for quite a while now and has taken some of the best practices from years of experience and made those the focus.&nbsp; Furthering oneself through education should benefit the student not waste their time.&nbsp; My hope is that one day there will be a reform in the software development education programs so that new software development managers will have the necessary tools to be effective rather than being restricted out of the gate.&nbsp; If there isn't some change soon, the industry is going to pass the education system by and getting a masters isn't going to do anyone any good.<br />]]></description>
            <link>http://www.nearinfinity.com/blogs/chris_rohr/agile_in_school.html</link>
            <guid>http://www.nearinfinity.com/blogs/chris_rohr/agile_in_school.html</guid>
            
                <category domain="http://www.sixapart.com/ns/types#category">Agile Development</category>
            
            
                <category domain="http://www.sixapart.com/ns/types#tag">agile</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">education</category>
            
            <pubDate>Wed, 20 May 2009 15:33:24 -0500</pubDate>
        </item>
        
        <item>
            <title>Groovier Processing</title>
            <description><![CDATA[<h1>Introduction</h1>

<p>
<a href="http://processing.org/">Processing</a> is a basically a proprietary Java-like language compiled into Java bytecode and used for generating dynamic and interactive data visualizations. Though still in its infancy, it seems to be a very nice intuitive way to build visualizations of data into an application.  The download for Processing contains the compiler, library and an editor.  The file format for Processing files is .pde.  Inside of these files you can treat the language just like a scripting language.  For instance:
</p>

<pre class="prettyprint">size(400,400);
background(192, 64, 0);
stroke(255);
line(150, 25, 270, 350);
<br /></pre>

<p>
This block of code is from the book <a href="http://www.oreilly.com/catalog/9780596514556/toc.html">"Visualizing Data"</a> by Ben Fry and will just set the window size, set the background color, set the line color and draw a line. The output will look like:
<br />
<img src="http://www.nearinfinity.com/blogs/assets/crohr/processingImg1.PNG" title="Image 1" alt=" " />
<br />
There are 2 methods that processing will look for to do initial setup and then the refreshing of the canvas.  Adding those methods and adding some mouse interaction we get the following code:
</p>

<pre class="prettyprint">void setup() {
   size(400, 400);
   stroke(255);
   background(192, 64, 0);
}<br /><br />
void draw() {
   line(150, 25, mouseX, mouseY);
}
</pre>

The output from this block of code will look like:<br /><br />
<img src="http://www.nearinfinity.com/blogs/assets/crohr/processingImg2.png" title="Image 2" alt=" " /><br /><br />
There are a lot more convenient functions and properties available when using the Processing API but that will have to wait for another blog.  The editor that comes with Processing is functional, but is lacking in features that many of the more modern editors contain.  So I wanted to try and see if I could use my standard editor and still build Processing apps.<br /><br />

<h1>Eclipsing the IDEA</h1>

<p>
Processing so nicely comes with a jar <tt>core.jar</tt>.  This jar then can be added to a project in the editor of your choosing.  Once you are in Java world there are couple of things that need to change with your Processing code.  Mainly you have to now adhere to Java programming rules.  Our code from above in the Java world would look like the following: 
</p>

<pre class="prettyprint">import processing.core.PApplet;<br /><br />
public class BlogExample extends PApplet {
    public void setup() {
        size(400, 400);
        stroke(255);
        background(192, 64, 0);
    }<br /><br />
    public void draw() {
        line(150, 25, mouseX, mouseY);
    }
}
</pre>

As you can see, the code becomes a little lengthier.  We need to add all of the modifiers on and the class definition and all of that, but we are at least in the IDE of our choice now and can just run this file and get the same output as the Processing editor.  Now lets say we we want to add boxes to our output.  We can add a class called Box to the project (Processing editor supports multiple classes as well).  The code for the Box class would be: 
<pre class="prettyprint">public class Box {
    private String title;
    private String boxText;
    private int width;
    private int height;
    private String backgroundColor;
    private String textColor;<br /><br />
    public int getWidth() {
        return width;
    }<br /><br />
    public void setWidth(int width) {
        this.width = width;
    }<br /><br />
    public String getTitle() {
        return title;
    }<br /><br />
    public void setTitle(String title) {
        this.title = title;
    }<br /><br />
    public String getBoxText() {
        return boxText;
    }<br /><br />
    public void setBoxText(String boxText) {
        this.boxText = boxText;
    }<br /><br />
    public int getHeight() {
        return height;
    }<br /><br />
    public void setHeight(int height) {
        this.height = height;
    }<br /><br />
    public String getBackgroundColor() {
        return backgroundColor;
    }<br /><br />
    public void setBackgroundColor(String backgroundColor) {
        this.backgroundColor = backgroundColor;
    }<br /><br />
    public String getTextColor() {
        return textColor;
    }<br /><br />
    public void setTextColor(String textColor) {
        this.textColor = textColor;
    }
}
</pre>

Then we add some code to our main class to use the Box.
<pre class="prettyprint">public class BlogExample extends PApplet {
    public void setup() {
        size(400, 400);
        stroke(255);
        background(192, 64, 0);
    }<br /><br />
    public void draw() {
        line(150, 25, mouseX, mouseY);
        <strong>List<box> boxes = getBoxes();
        
        int xPos = 0;
        int yPos = 0;
        for (Box box : boxes) {
            drawBox(box, xPos, yPos);
            xPos += box.getWidth() + 5;
            yPos += box.getHeight() + 5;
        }</box></strong>
    }
    
    <strong>private void drawBox(Box box, int xPos, int yPos) {
        //Draw the box on the screen;
    }<br /><br />
    private List<box> getBoxes() {
        List<box> boxes = new ArrayList<box>();
        
        Box box = new Box();
        box.setTitle("Box 1");
        box.setBoxText("Text in Box 1");
        box.setBackgroundColor("blue");
        box.setTextColor("white");
        box.setWidth(100);
        box.setHeight(100);
        boxes.add(box);
        
        //Add more boxes here
        
        return boxes;
    }</box></box></box></strong>
}
</pre>

Now you can see we have added a lot of code to do some simple processing of boxes.  This isn't very script-like anymore.  I was thinking of how to get this code base a little smaller.  I then thought of what would happen if I added Groovy to it.<br /><br />

<h1>Groovifing the Code</h1>

<p>
After adding the groovy jar, I could change my .java files to .groovy files and start shrinking the code base.  Here is what the above code became:
</p>

<pre class="prettyprint">class BlogExample extends PApplet {
    void setup() {
        size(400, 400)
        stroke(255)
        background(192, 64, 0)
    }<br /><br />
    void draw() {
        line(150, 25, mouseX, mouseY)
        def boxes = getBoxes()<br /><br />
        def xPos = 0
        def yPos = 0;
        boxes.each {box -&gt;
            drawBox(box, xPos, yPos);
            xPos += box.width + 5;
            yPos += box.height + 5;
        }
    }<br /><br />
    private void drawBox(def box, def xPos, def yPos) {
        //Draw the box on the screen;
    }<br /><br />
    private List<box> getBoxes() {
        return [
                new Box(title:"Box 1", boxText:"Text in Box 1", backgroundColor:'blue', textColor:'white', height:100, width:100)
                //Add more boxes here
        ]
    }
}<br /><br />
class Box {
    String title;
    String boxText;
    int width;
    int height;
    String backgroundColor;
    String textColor;
}
</box>
</pre>

<p>
Making the code a little Groovier has reduced the amount of lines that I need to write to get my visualization up and running.  So now I've got the simpler syntax of a scripting language and all the power of my IDE and all of my Java libraries.  I know that this example is very primitive and you don't necessarily get as much bang for your buck on something this small, however, as this application grows into something bigger the gains will become more apparent.</p>]]></description>
            <link>http://www.nearinfinity.com/blogs/chris_rohr/groovier_processing.html</link>
            <guid>http://www.nearinfinity.com/blogs/chris_rohr/groovier_processing.html</guid>
            
                <category domain="http://www.sixapart.com/ns/types#category">Java</category>
            
            
            <pubDate>Sat, 26 Apr 2008 00:00:00 -0500</pubDate>
        </item>
        
        <item>
            <title>The Not-So-SimpleDateFormatter</title>
            <description><![CDATA[This posting is Part 1 of 2 related to using Dates in Java webapps.
<h3>Problem</h3>

<p>
Your application has a form with a date entry on it.  This entry needs to allow multiple formats and perform some basic date validation on the input.
Valid formats allowed are:
</p>

<ul>
<li>MM/dd/yyyy</li>
<li>MM/yyyy</li>
<li>yyyy</li>
<li>ddMMMyyyy</li>
<li>MMyyyy</li>
</ul>

<h3>Solution #1</h3>

<p>
The first attempt of solving this problem was to write a custom parser that did a whole bunch of checking on string lengths and splits based on certain cases (ie. slashes and spaces).
</p>

<h4>Example code</h4>

<pre class="prettyprint">
public boolean isValidDate(String dateStr) {
	switch(dateStr.length()) {
		case 4:
			//Is a year
			//Check to see if valid year
		case 7:
			if (dateStr.indexOf('/') &gt; 0) {
				//Is MM/yyyy
				//Check to see if valid month and year
			} else {
				//Is MMMyyyy
				//Check to see if valid month and year
			}
		case 9:
			//Is ddMMMyyyy
			//Check to see if valid day, month and year
		case 10:
			//Is MM/dd/yyyy
			//Check to see if valid day, month and year
		default:
			return false;				
	}		
}
</pre>

<h4>Outcome</h4>

<p>
This solution proved to be problematic for a number of reasons.  First, it caused a lot of ugly code to be written.  I think the cyclomatic complexity was about 15 and the code made up of at least 60-70 lines of code.  Second, it was very error prone.  There were a couple different iterations where every case was handled and then was found not to be true (sometimes found by real users!).</p>

<h3>Solution #2</h3>

<p>
After about 5 tries of getting the date validation correct, a new approach was taken.  The date string was parsed using the <tt>java.lang.text.SimpleDateFormatter</tt> and then passed into a <tt>java.util.Calendar</tt> object.  The parts that made up the date were then verified against the original string to make sure they were the same.  The reason for using the <tt>java.util.Calendar</tt> object is because by default the <tt>java.lang.text.SimpleDateFormatter</tt> is very lenient when it comes to parsing.  If the date 13/25/2007 is passed in, it will return a <tt>java.util.Date</tt> object but with the date Jan. 25th, 2008.</p>

<h4>Example code</h4>

<pre class="prettyprint">
public boolean isValidDate(String dateStr, String...formats) {
	for (String format : formats) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			Date parsedDate = sdf.parse(dateStr);
			
			Calendar cal = Calendar.getInstance();
			cal.clear();
			cal.setTime(parsedDate);
			
			//Get year from dateStr  (requires figuring out the parts of the date based on the format)
			//Does the year values equal each other?
			
			//Get month from dateStr 
			//Does the month values equal each other?
			
			//Get day from dateStr
			//Does the day values equal each other?
			
		} catch (ParseException e) {
			//Not a valid date based on format
		}
	}
	
	return false;
}
</pre>

<h4>Outcome</h4>

<p>
This solution handled all of the cases that were problematic in Solution #1.  The code base was slimmed down some but it was still a lot of logic for parsing a simple date including still having to figure out the date parts.  Also, for date validation we didn't want the validator to "fix" the date for us.  
</p>

<h3>Solution #3</h3>

<p>
We then needed to find a way to further slim down our code.  A <a href="http://www.nearinfinity.com/blogs/page/clee">coworker</a> then found that the <tt>java.lang.text.SimpleDateFormatter</tt> had a setter for leniency.  By setting this to false, the parser will not accept 13/25/2007 as a valid date.  <strong>Note:</strong> We couldn't just use the <tt>org.apache.commons.lang.DateUtils.parseDate(String str, String[] parsePatterns)</tt> to parse the date.  That method keeps the leniency as true, leaving us in the same state we were before.
</p>

<h4>Example Code</h4>

<pre class="prettyprint">
public boolean isValidDate(String dateStr, String...formats) {
	for (String format : formats) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		sdf.setLenient(false);
		try {
			sdf.parse(dateStr);
			return true;
		} catch (ParseException e) {
			//Ignore because its not the right format.
		}
	}
	return false;
}
</pre>

<h4>Outcome</h4>

<p>This piece of code was a very simple, clean solution to the problem.  However, there was one issue that crept into this example.  If I made the call <tt>isValidDate("25BAD2007", "MM/dd/yyyy", "MM/yyyy", "yyyy", "ddMMMyyyy", "MMMyyyy")</tt> the return value would be true.  The reason for this is because even though the date had more than just a year, the 2007 part passed yyyy.  The only solution to this issue that I have been able to come up with is to add some logic outside of the <tt>isValidDate</tt> method that quickly determines the formats to pass in.  Though this seems like the parsing logic was just moved from inside the method to out, it is actually less logic in the long run.  Here is the example to call the <tt>isValidDate</tt> method:</p>

<pre class="prettyprint">
boolean validDate = false;
if (dateStr.length() == 4) {
	validDate = isValidDate(dateStr, "yyyy");
} else if (dateStr.indexOf('/') &gt; 0) {
	validDate = isValidDate(dateStr, "MM/dd/yyyy", "MM/yyyy");
} else {
	validDate = isValidDate(dateStr, "ddMMMyyyy", "MMMyyyy");
}
</pre>

<h3>Summary</h3>

<p>
The moral of the story is Date validation is hard when multiple formats are allowed.  Beware of what SimpleDateFormat is actually doing; You may be getting different results than expected because the parsing didn't fail.
</p>]]></description>
            <link>http://www.nearinfinity.com/blogs/chris_rohr/the_not_so_simpledateformatter.html</link>
            <guid>http://www.nearinfinity.com/blogs/chris_rohr/the_not_so_simpledateformatter.html</guid>
            
                <category domain="http://www.sixapart.com/ns/types#category">Java</category>
            
            
            <pubDate>Sun, 11 Nov 2007 17:30:43 -0500</pubDate>
        </item>
        
    </channel>
</rss>
