Monthly Archives: March 2009

On closed range boundaries

Programmers often need to talk about ranges of values (whether or not the language at hand supports the concept explicitly). For example, given the Java array

String[] streetLines = new String[3];

an index i must satisfy

0 ≤ i < 3 (in normal Mathematical notation)

or

0 <= i && i < 3 (in Java notation)

to be a valid index on streetLines. But how does one read those expressions aloud?

I learned to read <= and as “at most” and >= and as “at least” from Dijkstra. Those readings have at least two benefits:

  1. They are quite natural. For example, the assertion

    there are at least six eggs left in the carton

    is at least as obvious in normal speech as the equivalent phrase

    there are six or more eggs in the carton

    and is probably much more obvous than other equivalent phrases, such as

    the number of eggs left in the carton is greater than or equal to six

    or

    there are more than five eggs left in the carton

    even though we’ve seen the equivalent of those in code many times.
     

  2. They avoid introducing extraneous logical operators. The phrasing

    less than or equal to

    seems more complex than

    at most

    because it implies the use of disjunction (logical “or“) in its evaluation. The other equivalent phrase

    not greater than

    introduces negation into a simple comparison.

As a tiny “easter egg”, look for uses of “at least” en passant in the preceding few sentences! If they went by without being obvious, perhaps that serves as evidence of point 1 above. 😉

“Linguistics” vs. Mathematics?

I happened across an interesting post on Chris Okasaki’s blog, titled Less than vs Greater than. Let me suggest that you read it before continuing here.

I would paraphrase his point about errors he observed in students’ programs as follows:

A student who writes an expression such as

expL < expR

often appears to lock on the concept of “something being smaller” and then become mentally stuck on “dealing with smaller stuff”, even if that is not appropriate for the meaning of expL and expR at that point in the program.

He illustrates his point very nicely with a flawed binary search.

For quite some time I’ve tended to write expressions of the form

lowerBound <= someVar && someVar < upperBound

to express that someVar is within the half-open range

[lowerBound..upperBound)

e.g. array subscripts that must be at least zero, but less than the length of the array. The visual hint of placing the variable textually between the limiting values seemed nicely mnemonic to me. (From there, it has also seemed natural to experiment with preferring left-to-right, lesser-to-greater ordering consistently in comparison expressions, although I’m not suggesting that as a universal coding convention).

However, I was quite surprised by the first comment, which described as “linguistically wrong” the common C-language-based idiom

if (5 == i)

(intended to catch typos involving only a single equal sign). The commenter said

Linguistically that’s wrong. You’re not testing 5 to see if it’s equal to i, you’re testing i.

which implies an asymmetrical interpretation of equality as being a “test” on its left-hand value!

By ignoring the fact that == is simply an assertion that its operands are equal, that interpretation fails on many completely valid cases, such as

(a + 1 == b - 1)

and the first clause

lowerBound <= someVar

of the “within bounds” cliché above.

That misinterpretation of == seems consistent with a variety of incorrect or awkward uses of boolean expressions widely seen. Many of us have probably seen code (or read blog posts about code) resembling:

boolean fooIsEmpty;
...
if (foo == null) {
    fooIsEmpty = true;
} else {
    if (foo.length() == 0) {
        fooIsEmpty = true;
    } else {
        fooIsEmpty = false;
    }
}

I suspect two culprits behind this kind of abuse:

  1. Failure to include the right kind and amount of Mathematics in the education of a programmer, and
  2. The C/FORTRAN use of = for assignment.

With respect to point 1, I believe that Boolean Algebra is simply a fundamental skill for programming. The ability to manipulate and understand boolean expressions equally important as the ability to deal with numeric expressions for most programming of the kind I see daily. It is understandable that a programmer whose training never addressed boolean expressions except in the context of if() or while() would be uncomfortable with other uses, but that’s a problem to be solved, not a permanent condition to be endured.

Regarding point 2, much of what I’ve read or experienced supports the idea that FORTRAN—and later, C—were more commonly used in the US than Algol or other alternatives due to political, cultural, and commercial issues rather than technical ones. (I recommend Dijkstra’s “A new science, from birth to maturity” and Gabriel’s “Worse is Better” as good starting points.)

Whether that conclusion is valid or flawed, the decision to use = for the (asymmetrical!) “assignment” operation immediately creates two new problems:

  1. The need to express the “equality test” in a way that won’t be confused with assignment; and
  2. The risk that users of the language become confused about the symmetry of equality, due to guilt by association with the “assignment” operator.

Algol—and its descendants, including Pascal—avoided those problems by using the asymmetrical := for assignment. The ! character is used as an operator or suffix in other languages to express destructive value-setting. But Java, C#, and others, have followed the FORTRAN/C convention, with the predictable effects.

Given such far-reaching consequences from a single character in the source code, I’m reminded again how important it is to make good choices in coding style, API design, and other naming contexts.


Postscript:

The left-to-right ordering of the number line has some interesting cultural and even individual baggage. I recall reading about a mathematician whose personal mental model was of negative values being behind him and positive values receding into the distance in front of him.

I believe it was in connection with the Dutch National Flag Problem that Esdger Dijkstra wrote about a student whose native language was written right-to-left. While other students had designed programs with indices increasing from zero, that student had produced an equally-valid program with an index that decreased from the maximal value.

JPR 2009: Thursday Lightning Talks

The final lightning talk session, like the entire week, went by too quickly! While a technical glitch was being resolved, Bill Pugh and Matt Grommes stepped in with 20-second impromptu lightning talks (spark talks?).

Dick Wall
JFlubber, FlexFlubber, and FXFlubber – Long-time Posse listeners know about Dick’s JFlubber app. In this talk, he presented the results of re-implementing it in JavaFX (steampunk style) and in Flex (with dynamic skinning, courtesy of James Ward).
Jack Leow
GWT in a Traditional Web Development Environment – After touching on the benefits of GWT, Jack described some bumps in the road and how he overcame them, followed by a demonstration.
Bill Pugh
(20-second talk)Donate bone marrow and save someone’s life!
Matt Grommes
(20-second talk)Evernote is a cross-platform system for capturing and managing information, even on your phone!
Oliver Gierke
Coding and Music, Passion and Profession – Oliver, a Java developer and the drummer with Four Sided Cube, talked about the parallels between development and musical composition/performance.
Ron Hafernik
Hacking Hardware for Fun and Profit – Ron talked about low-cost, low-power microcontrollers (such as the ATtiny24, the PIC18F87J11, the Arduino, and the BasicStamp), their uses, and a variety of sites with more information (Evil Mad Scientist Laboratories, Hack a Day, and Instructables).
Joe Nuxoll
Helmet-cam videos – With his photographer’s hat on again, Joe showed us his helmet-mountable camera and solid state recorder, then played some videos from skiing and snowmobiling at this year’s Roundup.
Andrew Harmel-Law
Zombies Ahead – Andrew had us rolling in the floor with his dire warnings about the rising danger from zombies!
Joe Sondow
Working on the front end – Joe demonstrated the use of JavaScript Shell, a web-based”command-line interface for JavaScript and DOM.”
Eirik Bjørsnøs
How to catch a laptop thief using Flex – We all applauded Eirik’s ingenious use of his skills to help catch the burglar who stole a friend’s laptop.
Dick Wall
Doctor Who – Dick summarized the backstory and major characters from this popular BBC series.
James Ward
Sexy apps with Flex – After a quick glance at Tour de Flex, James blew us away with some über-cool user interface demos.
Carl Quinn
Free Electricity From the Sun – Carl showed the design and installation of his home’s solar panel system.
Alf Kristian Støyle
JavaRebel and Scala – Alf demonstrated using Scala on top of JavaRebel, a JVM plugin that reloads modified class files on-the-fly.
Dianne Marsh
What Microsoft Does for Its Developer Community – Dianne reviewed some of the ways that Microsoft supports its developers, including the MVP program.
Todd Costello
Groovy/Swing/GoogleMaps/YQL Mashup – Todd mashed up JXMapKit with Groovy and YQL to plot the home airports of some of the Roundup attendees.

JPR 2009: Wednesday Lightning Talks

As mentioned before, recorded lightning talks will be posted to the Java Posse YouTube channel. Here’s a quick summary of the second LT session.

Joel Neely
The Other Staircase – As a kid, I always wondered why the octave is divided into 12 parts (in Western music). Here’s a reason.
Jason Nerothin
DB Migrations in Java – Jason showed a lightweight approach to bi-directional, development-friendly database change management.
Joe Sondow
Semantic HTML and Skinnable CSS – Joe gave us a quick demonstration of the power of CSS, and pointed us to a couple of useful and interesting web sites.
Joe Nuxoll
CenterLineSoccer.com – Joe serves as a photographer, technical advisor, and blogger to this soccer site. He discussed soccer photography and showed us some great shots.
Ido Green
YQL 101 – Ido demonstrated Yahoo’s YQL tool for querying, filtering and combining web-based data.
Peter Pilgrim
How to start your very own Java User Group – Peter, a Sun Java Champion from England, passed on tips for starting and leading a JUG.
Oliver Gierke
Persistence layers – Inspired by a DeveloperWorks article on generic DAO, Oliver implemented a low-ceremony, annotation-based framework.
Joe Nuxoll
Formula One 2009 – Technology Advances – Joe (who’s also a racing instructor) gave us the basics on the Kinetic Energy Recovery System for F1 race cars.
Alf Støyle and Fredrick Fraalsen
Scala and Wicket – This tag-team presentation looked at using Scala and Wicket to implement on-line registration for JavaZone.
Joel Neely
Why is there a lower-case “A”? – This encore presentation from last year looked at the effect of tools and needs on a very old technology.
D J Hagberg
811: Know what’s below before you dig – Buried utilities can be damaged by thoughtless digging, sometimes with catastrophic results. The 811 system is a response to this risk.
Robert Cooper
Old school computing – Robert’s explanation of basic slide-rule technique showed “an elegant computer for a more civilized time”. (It also had nice screen shots.)
Fred Simon
JFrog and Artifactory 2.1 – Fred introduced us to his clean, functional open-source enterprise repository.
Barry Hawkins
tips for introducing change – Barry’s fantastic talk on… Nah, I can’t do it justice. Just go see for yourself. This one’s already posted on the Posse channel. It was a perfect cap on the evening.

JPR 2009: Tuesday Lightning Talks

At the first Java Posse Roundup (2007), lightning talks were added after the conference began. It was a great idea, but novel enough for some of us that the slots filled up slowly. This year, the three lightning talk sessions were booked solid shortly after the sign-up sheets were posted. By the end of the first LT session, it was clear that the continuous improvement trend was continuing in fine style.

I’m going to list only a very brief summary for each of the lightning talks; all of them will be posted to the Posse’s YouTube Channel.

Joel Neely
Fair Allocation – I showed a small algorithm for weighted division, which illustrates a useful heuristic for discovering correct design.
Joe Sondow
The Selenium IDE – Joe demonstrated the Selenium web testing tool that works well with Firebug.
Bill Pugh
Becoming 3/4 of the man I was – Bill talked about how he lost 60 pounds.
Joe Nuxoll
The Art of the Photo – Joe illustrated fundamental photographic concepts with lots of great photos.
Ido Green
High Gear – Ido introduced his new company, High Gear Media, which supports the “long tail” car-buying market with support for content management and rich site publishing.
Fred Simon
Physics of the 20th Century: the Incomplete Revolution – Fred hit key points regarding general relativity, quantum mechanics, string theory, and loop quantum gravity.
Peter Pilgrim
Animation in JavaFX – Peter talked about SceneGraph, binding, and timelines in JavaFX.
Joe Nuxoll
Racing 101 – This was an encore performance of Joe’s intro to key racing concepts.
Dick Wall
GMail Keyboard Shortcuts – Dick showed us several tips to speed up our GMail sessions.
Bill Venners
ScalaCheck – Bill demonstrated the use of ScalaCheck, a powerful tool for automatic unit-testing of Scala and Java, which offers automatic test case generation and minimization of failing test cases.
Dave Briccetti
Scala Twitter Client – Dave demonstrated features of his Simple Twitter Client in Scala, which he’s worked on this week with other Roundup attendees.
Bill Pugh
Your eyes suck at blue – By separately manipulating the color channels of an image, Bill demonstrated how much less sensitive our eyes are to blue than to red and green.

JPR 2009: Monday

After beginning the day with Camp 4 Coffee, we assembled at the Posse House to dive into a day of coding dojos on alternate JVM languages. Various groups split off to different rooms and houses to discuss Scala, JavaFX, Groovy and Grails, and Jython. The plan for the day also included a discussion of Fan; that session was deferred to the afternoon when Fred Simon (of AlphaCSP and JFrog) experienced an unexpected travel delay.

You’d think that the gate agent would think to wake a sleeping passenger, instead of just issuing last call and launching the aircraft!

I started at the Posse House in the Scala session. Dick Wall was interested in trying to implement linq-like functionality in Scala. Dianne reviewed the first part of Bill Wagner’s article and I recapped the generic sort we worked out last year. Dick showed some sample code that demonstrated several Scala techniques for nice, clean, concise code.

It wasn’t as formal as that may sound; it was more conversation than presentation.

After a lunch break, I went to Chestnut House where Bill Venners met with some Possee attendees to work on his ScalaTest project.

After supper many early-arrivers re-convened at the Posse House for a series of informal wrap-up talks which summarized the activities of the various meetings for everyone. It was a great way to overcome the frustration of not being everywhere at once.

JPR 2009: Sunday

As usual, getting from Memphis to Crested Butte had a bit of drama, although less this year than before.

A winter storm blew through the Mid-South Saturday afternoon and night, leaving about two inches of snow in the Memphis area. The temperature had been well above freezing, and the snow began with big wet flakes, so the first few hours’ worth melted on streets and other hard surfaces. Overnight the temperature dropped well below freezing, so sunrise revealed abundant ice below packed snow on the streets.

This sort of thing is infrequent enough that Memphis simply doesn’t have the gear to respond rapidly. That’s understandable. However, after arriving at the airport and getting to the gate, it became apparent that at least one airline at Memphis International Airport is equally ill-prepared.

Three hours of entertainment (watching staff trying to reroute passengers, watching an idle glycol trailer, watching solar energy deice our plane, eavesdropping on staff talking why deicing wasn’t happening, etc.) finally ended and we walked out to the plane over ice-crusted concrete. After another post-boarding delay, the pilot announced that there was a computer problem and proceeded to reboot the aircraft. Powering off everything, waiting a couple of minutes, and starting back up seemed to satisfy the crew, so we got underway.

Fortunately for me, my missed connection from Denver to Gunnison was not the last of the day. I ended up with the flight carrying the entire Java Posse, along with Dianne Marsh, Bill Pugh (of FindBugs fame), and several others heading for the Roundup.

We arrived in plenty of time to make a supermarket run, including picking up a pound of Sledgehammer for morning wake-up assistance.

This year I’m staying at Chestnut House with a great group of folks: Dianne Marsh, Jason Nerothin and Kurt Heilman of NimbleGen, and James Ward of Adobe.