“Hardest-first” vs. “Easiest-first” and TDD

7 11 2014

The interesting and thought-provoking “Is TDD Dead?” debates, and some explanations and examples of TDD that I’ve seen since then, have reminded me of something that came up whilst studying Computer Science at university (in the days long before TDD).

A few of my fellow students would develop programs by starting with the hardest parts of the problem, but the majority would always start with the easiest parts (obvious edge cases, validating input, “boilerplate” code etc).

In general:

  • The “hardest first” programmers wanted to crack the heart of the problem and eliminate risks and unknowns before bothering with the more straightforward work. They’d start out by just thinking, and wouldn’t worry about not yet writing any code. They were confident they could get the easier stuff done in due course, but didn’t see any point doing it unless and until they’d solved the main problem. They also didn’t want to be burdened or constrained by existing code when tackling the difficult bits.
  • The “easiest first” programmers wanted to start making progress and to immediately feel that they were getting somewhere and weren’t “stuck”. Having “momentum” and seeing progress helped them to continue. They’d generally start hacking something out without particularly worrying about it, and then fiddle with it until it seemed to work, whilst deferring anything that looked difficult until later. They felt the hard parts would be easier to tackle once everything else was already in place, even if some existing code would then need revising. At the very worst, if they did get stuck they’d at least have some code already written.

One of the lecturers noticed this, and the class ended up explicitly discussing it. When questioned, a few of us with previous programming experience generally favoured “hardest first”, but most of the rest thought “easiest first” was the best approach – and still thought that after discussing it. Notably, each group regarded the other approach as a horrible way of doing things.

At the time I assumed this was simply due to most of the students being new to programming, understandably struggling to get to grips with it all, and lacking experience with non-trivial problems.

However, much of the advocacy and explanation of test-first, test-driven development that I’ve seen has struck me as sounding remarkably like the sorts of things that the “easiest first” students might say. Listen, for example, to Kent Beck’s initial explanation of TDD’s origins and how it suited his personality at about 7:10-8:15 in the first “Is TDD Dead?” discussion.

Similarly, whilst I’m not particularly against TDD (and am happy to do it where it seems appropriate, or where it’s how a team is doing things), I do have some doubts and skepticism about test-first, test-driven TDD, and I now think this might reflect my own “hardest first” tendency. Even when doing TDD I often feel I’d have more quickly reached a better design if I’d tackled the “hard” problem of code and design directly instead of having to be led to the code piecemeal through tests and refactoring. Some of David Heinemeier Hansson‘s views in the “Is TDD Dead?” debates also sound rather like this to me.

So I’m starting to think that “test first” and “test driven” is perhaps most natural and beneficial for those who tend to be “easiest first” programmers, but can seem unnatural and unconvincing to those who tend to be “hardest first” programmers. In turn, the almost universal adoption of TDD that I see in local start-ups and job ads strikes me as implying that the “easiest first” viewpoint is now somewhat of an industry standard.

Perhaps most people are just intrinsically “easiest first” – at least to start with, and in the absence of any training to the contrary or when left to their own devices. Practices that work well for “easiest first” people are then perhaps a better industry-wide solution than anything that needs a “hardest first” mindset. I suspect this is now self-perpetuating, given the extent to which TDD is both advocated and demanded.

My only worry is that we’re perhaps denigrating and discouraging anyone that doesn’t fit that mould (especially if that might be me!).

I do hope we’ve not reached the point where it’s automatically seen as wrong to stop and think instead of diving straight into writing a test for some “simple” case.

ObMimic 1.0 Released

22 08 2014

Updated Feb 2018: OpenBrace Limited has closed down, and its ObMimic product is no longer supported.

Release 1.0 of ObMimic is now available from OpenBrace Limited.

ObMimic is a library of ready-made test-doubles for the Servlet API. It provides fully-configurable plain-Java implementations of Servlet API objects for use in out-of-container testing of any code that depends on the Servlet API (including, for example, JSF pages).

You can use ObMimic to write comprehensive, detailed tests for such code, using the same tools and techniques as for normal plain-Java code – without having to deploy and run your code inside a servlet container, and without having to write your own stubs or use general-purpose mocks that rely on your own assumptions about the Servlet API’s behaviour.

At its simplest, your tests can obtain fully-functional instances of Servlet API objects using plain no-argument constructors (e.g. “new HttpServletRequestMimic()” to create an HttpServletRequest).

The main changes in this release are:

  • First full release: ObMimic is no longer in “beta”.
  • Fix to session handling: A bug in session-handling during RequestDispatcher “forwards” and “includes” has been fixed (see 16Bugs Bug #5 for details).
  • HttpServletResponseMimic.setStatus(int): By default this no longer clears the response buffer as was specified incorrectly by the Javadoc of Servlet API versions prior to 3.0 (but a new ServletContainerBehaviour option has also been added to allow explicit control over this).

For a more complete list of changes, refer to the Changes in ObMimic 1.0.000 page.

As before, the free “Community Edition” and a free (but time-limited) licence-key file for evaluation of the “Professional Edition” facilities are available for download from the ObMimic website’s Downloads / Current Release page.

In addition, “Professional Edition” licence-key files and the “Enterprise Edition” can now be purchased via the website’s Buy ObMimic Licences page.

For full details and documentation please see the ObMimic website at www.openbrace.com.

For previous posts that describe ObMimic (including some example code), see:

ObMimic 1.0 Beta 10 Released

7 11 2013

Updated Feb 2018: OpenBrace Limited has closed down, and its ObMimic product is no longer supported.

Release 1.0-beta-10 of ObMimic is now available from OpenBrace Limited.

The main changes in this release are:

  • Testing of JSF pages: A “how to” guide has been added to cover testing of JSF pages, based on experiments that have demonstrated that ObMimic can be used for out-of-container testing of complete JSF pages (including full control over the ServletContext and ability to access the JSF FacesContext and API within tests).
  • Maven: A “how to” guide and “pom.xml” file have been added to aid in the use of ObMimic within Maven-based projects.
  • Listeners for Servlet API calls: Listeners can now be installed into Mimics to intercept, examine and optionally modify Servlet API calls and their results at the point where they occur, as described in an accompanying “how to” guide. In particular, this can be used as a last resort to work around any questionable Servlet API calls made by third-party code, or to overrule ObMimic’s built-in behaviour for a particular Servlet API call.

Additional changes include:

  • Any attempt to use a Java EE jar that is deliberately limited to “compile-time only” is now explicitly checked for and reported, and documentation has been updated accordingly.
  • Options have been added for configuring how the ServletContext “getResource” and “getResourceAsStream” methods behave when given “directory” paths.
  • Various minor fixes and documentation improvements.
  • ObMimic has been tested against some additional JDKs, including a JDK 8 early-access release.

For more complete details, refer to the Changes in ObMimic 1.0 Beta 10 page.

As before, the OpenBrace website provides a free download of ObMimic (including a free licence-key to unlock the “Professional Edition” features during the beta).

Note that our current intention is that this will be the last “beta” release, with the full 1.0 release to follow next.

Some more testing against different JDKs…

2 08 2013

I’ve recently added a few more JDKs into the compatibility tests of my ObMimic library, and have found a few issues that seem worth noting here.

1  JDKs Added

The specific JDKs added into the testing were:

2  Background

This isn’t a comprehensive examination of the JDKs themselves, but just tests that:

  • The ObMimic code all works correctly on all of the JDKs.
  • The Ant build script delivered as part of ObMimic’s “Enterprise Edition” can successfully build ObMimic from its source code on the Java SE 7 and higher JDKs (including running its tests and generating its Javadoc).
  • The deliverables produced by the “Enterprise Edition” build script do themselves work correctly on all of the JDKs.

For details of the other JDKs that were already included in the testing, further background information and previous findings, refer to the earlier post Some specific issues from ObMimic portability testing.

The only significant changes since that earlier post are that the tests are now run on 64-bit systems (with the Linux system now being Ubuntu 12.10); and the ObMimic “Enterprise Edition” build script is limited to Java SE 7 or higher (as it now needs Servlet 3.1, which in turn requires Java SE 7 or higher).

3  Issues Encountered

The issues encountered on the newly-added JDKs were as follows:

3.1  IBM Java SE 6 Logger doesn’t successfully look-up message keys

The java.util.logging.Logger methods that take a “msg” argument are supposed to accept a message key and look it up in a ResourceBundle to translate it into the actual message to be used (substituting given parameters into the message where applicable). This is supposed to follow the usual rules for finding the most relevant localization of the resource bundle.

There’s one ObMimic test case that relies on this, as it’s testing that a custom Handler correctly processes calls that use a message key. This test case works on all other JDKs, including IBM Java SE 5 and 7, but on IBM Java SE 6 it fails – the message key is written to the log “as is” rather than being converted into the full message from the resource bundle.

I’ve not found any definitive explanation or bug report for this, but there is a comment from “tripl3des” within a Can’t find bundle… post in an IBM discussion forum that says

“ResourceBundle implementation for IBM JDK does not respect the contract described in the API. At least not for 1.6 version…. IBM JDK only works when there is a file for the exact locale requested.”

If true, that sounds similar to what I’m encountering here (though there are other types of message look-ups throughout ObMimic that are working correctly).

The test case that fails isn’t critical to ObMimic (it’s just part of the full testing of an optional feature in some “common” code that’s bundled with ObMimic); there doesn’t appear to be anything wrong with the source code; and it all works OK on all the other JDKs.

At any rate, unless and until I learn any different I’m assuming this is just a bug in this particular JDK – and that anybody using this JDK’s java.util.logging facilities will encounter this in any logging with message keys, not just if they happen to do such logging whilst using the custom “handler” that I’m testing.

So whilst I don’t like having JDK-specific tests, in this particular case I’ve decided the most appropriate solution is to just skip this one specific test when running on IBM Java SE 6.

3.2  IBM Java SE 6 and 7 bootclasspath is more complex than just “rt.jar”

Until now, ObMimic’s “Enterprise Edition” build script has been explicitly specifying a “bootclasspath” that points to the /jre/lib/rt.jar of the JDK it’s using..

This has been done because the build’s default settings specify Java 5 as the “source” and “target” level, and javac issues a warning if these aren’t accompanied by an explicit “bootclasspath”. Explicitly supplying the “bootclasspath” doesn’t really achieve anything other than cutting out the warning message, as the build with default settings doesn’t know of any JDK other than the one it’s running on and can only specify that JDK’s own boot classes. But under the circumstances the warning is superfluous and distracting, and seemed worth cutting out.

Unfortunately, the IBM JDKs for Java 6 and 7 need more than just “rt.jar”. Some of the core Java classes (including, for example, java.lang.String) are in a separate “vm.jar”. To make matters worse, this is in different locations in IBM JDK 6 and IBM JDK 7. Then there are other necessary classes in other jars within the /jre/lib directory structure.

As a result, javac and javadoc on the IBM JDKs seem to work OK when it’s left to its own devices, but once an explicit bootclasspath is given they seem to need lots of jars from various locations within /jre/lib.

There are VM options for adding specific jars into the default bootclasspath instead of completely replacing it, and one could always configure the bootclasspath to include any and all jars within the entire /jre/lib directory structure. However, I’m not sure enough of an appropriate way to set the bootclasspath that will work for the IBM JDKs whilst also being entirely safe and appropriate for all other JDKs.

In the end I’ve modified the build script to not specify the bootclasspath at all. This re-introduces the warning message about it not being specified, but it works on all of the JDKs and is perhaps safer and more “truthful” than artificially suppressing the message.

In any case, the warning can always be avoided by changing the build properties that set the “source” and “target” levels if you don’t actually need compatibility with JDKs earlier than the one you’re building on.

3.3 Stricter Javadoc checking in JDK 8

Building the code on JDK 8 resulted in lots of Javadoc errors!

It turns out that by default Javadoc on JDK 8 carries out much stricter checking of the Javadoc content. This includes checking for incorrect HTML tags, characters that ought to be escaped/encoded, HTML attributes that are necessary for accessibility etc.

This appears to be a result of the “JDK Enhancement Proposal” JEP 172 DocLint.

In my case it found:

  • Quite a few “&” characters and a few “<” and “>” characters that needed escaping;
  • Numerous silly little mistakes in HTML tags (e.g. “<ul>” that should have been “</ul>”);
  • Some tables that were missing “summary” or “caption” attributes.

I can see this coming as a bit of a shock to many people when they switch to JDK 8 and suddenly discover that their builds fail with lots of Javadoc errors!

There’s apparently an “-Xdoclint” option that can be used to turn off some or all of the checks, but of course it makes more sense to actually fix the errors. If your Javadoc is otherwise reasonable there might be a large number of these errors, but each should be pretty trivial to fix.

It took me an hour or two to fix all of these Javadoc errors in the ObMimic code, but as always it feels good to have found and fixed these and to know that this is now checked by each Javadoc run.

3.4  A JDK 8 b99 Javadoc bug?

I also hit an apparent Javadoc bug on JDK 8 build b99 where it gives an error plus a warning for “@throws X” where “X” is a type parameter of the method (it looks like some of the Javadoc processing is taking the “X” as a class name rather than recognising it as the method’s type parameter).

However, there already seem to be JDK changes in progress that might be relevant. So for now I’m assuming this is just an early-access quirk that will get fixed in due course.

I’ve implemented a temporary (rather hacky!) work-around for the single ObMimic method affected by this, but it doesn’t seem worth going into any more detail here on the assumption that this will turn out to be a purely temporary issue.

ObMimic 1.0 Beta 9 Released

24 07 2013

Updated Feb 2018: OpenBrace Limited has closed down, and its ObMimic product is no longer supported.

Release 1.0-beta-9 of ObMimic is now available from OpenBrace Limited.

The main change from the previous 1.0-beta-8 release is compatibility with Servlet 3.1 and Java EE 7 libraries, so that ObMimic can be used with such libraries.

Whilst full support for Servlet 3.0 and 3.1 features is still pending (and intended for future releases), in the meantime this at least allows ObMimic to be used in the presence of Servlet 3.1 or Java EE 7 libraries (i.e. it can now be used with any Servlet 3.1 library as well as with any Servlet 2.4, 2.5 or 3.0 library as before). This also paves the way for the gradual introduction of Servlet 3.1 support in future releases.

In addition:

  • ObMimic’s Javadoc is now supplied in Java 7 style (but including an adjustment to work-around a problem in some versions of the Eclipse IDE that render Eclipse unable to parse such Javadoc).
  • Various minor fixes, revisions and improvements have been made to ObMimic’s documentation.

As before, the OpenBrace website provides a free download of ObMimic (including a free licence-key to unlock the “Professional Edition” features during the beta).

For previous posts that describe ObMimic and show some example code, see:

ObMimic Public Beta for Out-of-Container Servlet Testing

3 06 2013

Updated Feb 2018: OpenBrace Limited has closed down, and its ObMimic product is no longer supported.

ObMimic from OpenBrace Limited is a library of complete and fully-configurable plain-Java implementations of Servlet API objects for use as ready-made test-doubles in out-of-container testing of Servlet code. After a long development – and an even longer hiatus – it’s now available as a public beta release at www.openbrace.com.

You can use ObMimic to write comprehensive, detailed tests for your Servlet API code, using the same tools and techniques as for normal plain-Java code – without having to deploy and run your code inside a servlet container, and without having to write your own mocks or stubs and rely on your own assumptions about the Servlet API’s behaviour.

At its simplest, your tests can obtain fully-functional “mimic” instances of Servlet API objects using plain no-argument constructors — for example, you can create an HttpServletRequest with just:

new HttpServletRequestMimic();

Beyond that, you can configure and inspect the logical state of each such object as necessary for your tests. This includes control over details that would normally be “fixed” when running within a Servlet container (e.g. “init” parameters, Servlet API version, behaviours that are allowed to vary between containers, deliberate throwing of exceptions for testing of exception handling etc). There’s a detailed list of features on the website’s Features page.

If you want to test code that uses the Servlet API but find that detailed testing of such code is harder, more restrictive or slower than for normal Java code, ObMimic may be what you’re looking for.

The website provides a free download of ObMimic (including a free licence-key to unlock the “Professional Edition” features during the beta). The website also has a full copy of ObMimic’s documentation (including comprehensive Javadoc and a set of How To guides) and a set of discussion forums.

For some earlier posts that describe ObMimic and show some example code, see Experiments with out-of-container testing of Servlet code using ObMimic (Part 1) and First use of ObMimic for out-of-container testing of Servlets and Struts (Part 2).

ObMimic: Very, very late – but still alive

21 11 2009

Updated Feb 2018: OpenBrace Limited has closed down, and its ObMimic product is no longer supported.

It’s been a long, long time since I last mentioned ObMimic, so an update seems necessary.

At the time I thought it wouldn’t take more than a couple of months or so to get everything ready and put up a public website, discussion forums, bug list, and everything we need internally to release and support ObMimic.

Well, here we are, well over a year later, and there’s still no public website or ObMimic release.

So it seems worth a quick post to explain that ObMimic isn’t dead; it isn’t forgotten; it has just been much delayed and postponed, and is still “on the way”.

Why this long delay?

Well, partly this is the usual story that everything takes much longer than expected, even after you’ve allowed for the fact that everything takes much longer than expected.

Partly it’s from needing longer than expected to handle all the bugs and kludges encountered in the many third-party applications, tools and services involved, and getting everything to work nicely together.

Partly it’s a deliberate decision to wait until everything is reasonably complete and reliable before going public, even if that causes delay, rather than rushing out a quick-and-nasty website with lots of problems (and leaving us with a backlog of work and ongoing firefighting).

But mainly it’s due to having been sucked into other commitments, and spending time on several other things in order to “keep afloat” in the meantime. Such distractions have always seemed like a bad idea, but over the last year or so they’ve been a necessary evil.

It’s not that we’ve not been working on it. It’s just that there are always so many things that need doing, and the months fly by at alarming speed. As Brooks says in The Mythical Man Month: “How does a project get to be a year late? … One day at a time.” (but as also quoted there, “Good cooking takes time. If you are made to wait, it is to serve you better, and to please you”).

Anyway, during all these delays I’ve been deliberately keeping a low profile, and have wanted to steer clear of talking much about ObMimic until I can be sure that it’s genuinely imminent.

Rest assured though, a public beta and full release are still intended as soon as it’s ready (and we are getting there, despite the absence of any outward signs of progress); the product itself is still being maintained and improved in the meantime; and for anyone that wants an early look at it, a “private beta release” is still available if you contact me.

%d bloggers like this: