Author Archive
How I Escape the “Reuse Trap”
by Chris Wash on Aug.02, 2008, under Software Engineering
If you’ve checked out my blogroll, you’ll notice I have a link to Basil Vandegriend’s “Professional Software Development” blog. I like a lot of the articles he has written and to a large extent the subject matter I envision for this blog frequently crisscrosses with Basil’s.
While I was reading “The Reuse Trap In Software Design” I found myself thinking,”Me too! Me too!” like a giddy kid on the playground that’s found a new pal with a mutual interest in “pet snakes and/or tarantulas.” As with many of the problems Basil writes about in his blog, I experienced the same problem, investigated it and found the same root causes, and came to many of the same conclusions as those outlined. He describes the “reuse trap” as:
…[A] term I coined to describe the situation when one becomes stuck trying to design new functionality while simultaneously attempting to reuse existing code that needs some modifications.
He then goes on to describe why it’s a tough problem for n00bs and outlines a strategy he uses to get out of the trap–one that’s best described as a two-step process: copy-paste to reuse/refactoring to remove duplication. I encourage you to read his article if you’re not familiar with the terms, or how this strategy can solve the problem described above.
My preferred technique for mitigating the problems involved isn’t copy-paste reuse/refactoring; it’s stubbing the calls to the code-to-be-reused (C2BR) that needs modification. If I can fake out the behavior I need, it lets me focus on the nature of the dependency that exists between the two objects/components. When I go to modify the C2BR, my inputs and outputs (or other consequences that occur as a result of a call to the C2BR) translate directly into a test that I can use to drive the changes I make to that code. It also ensures that the coupling that exists between the objects is loose and legit.
The only thing to watch out for is that care must be taken to then remove the (small) duplication of the stubbed out inputs/outputs and actually wire the two pieces together when finished with the reused code. I usually do that by throwing a TODO:WIRE marker in when I stub something.
I’ve found this is the approach I take more often, though I do take the copy-paste reuse/refactoring route from time to time. This stub/rewire approach is a bit more advanced, but it’s how I envisioned solving the “reuse trap” when, as Basil puts it, “I became aware of it” and all that it entailed.
Do you ever come across this problem? How do you get out of this “trap” or avoid being caught up in it in the first place?
New Apple Wireless Keyboard
by Chris Wash on Aug.01, 2008, under Meta/Blog
On Tim Bray’s recommendation, I just picked up a new Apple Wireless Keyboard:
So far, it seems like a good choice. As mentioned in Tim’s post, I notice my typing speed has dramatically increased immediately, when compared to the built in keyboard, or the stock Dell keyboards I’ve been typing on when not at my Mac recently.
I didn’t think the num pad warranted the extra space it’ll take up when I carry it around with me. The super big command key doesn’t concern me quite as much, because I’ve gotten used to slipping my left thumb down to hit it from hovering over the spacebar, and it’s easier/more natural to do that on this keyboard compared to the one built into the MBP.
And this all goes without saying that the wireless features are a big plus. Whenever you sit down to do something, having to plug in an extra wire is nothing short of a pain in the ass (not to mention taking up valuable port space on my precious laptop).
So far I’m pretty happy with the purchase. If my opinion changes, I’ll let you know, but the spot judgement is that it’s exactly what I was expecting.
Help the Good Guys
by Chris Wash on Jul.31, 2008, under Meta/Blog
I think it’s important to support and give back to products and efforts that are high quality and enhance your productivity, happiness, or everyday life. A List Apart, a web published magazine “for people who make websites” was the first feed I remember adding to my feed reader. It definitely falls into this “need to give back” category for me. Their articles have a web design/development slant, and are written in a consistent, succinct, repeatable fashion. It’s a successful site, and I applaud its efforts.
If you didn’t know about ALA, well now you know. If you do know about them, give something back by participating in their survey.
Pimp My Build
by Chris Wash on Jul.21, 2008, under Software Engineering
In terms of the sheer number of moving parts, extravagance, and complexity, the build-tool landscape is one of bells and whistles that can be a little tough to stay on top of. There are a few important pieces of a robust and valuable build, no matter what tools you use to achieve it, and by breaking the build into modules or categories, we can make a little bit of sense out of the chaos.
In our vast build-tool landscape, there tend to be some zealous cults out there, entrenched deeply in their own camps and unwilling to venture out to explore what the rest of the landscape has to offer. Thus, there have been some religious-like differences of opinion about which build tools are better and why. Sadly you run across these types of biases all too often, which only pollutes the landscape and makes it more difficult to wrap your head the most basic questions underlying all tools:
- What does it accomplish?
- When is it (or isn’t it) right for the job?
- How do I use it effectively and efficiently?
With a pragmatic view, we can try to cut through a lot of the BS and figure out if a tool is worth its weight in:
- dependencies/design complexity.
- ease of use/interface complexity.
- architectural decisions/compromises that it brings to the table.
Does the tool get us to the goal of a single button build/deploy? If the answer is yes, I’ll welcome it with open arms. Does it add me a form of feedback or some kind of statistic that could be a kind of gauge on a dashboard? Then I may be interested in it, but it’s not nearly as vital.
Here’s an outline of what I typically look to include in a project’s build before I am satisfied:
- Compile – Obviously your build script would be for naught if you didn’t include this piece. There’s also something to be said about some dynamic/interpreted languages (that run on the JVM) taking the hit on this step and producing compiled versions. The argument is akin to compiling your JSPs back in the day.
- Unit test suite – Another one of those no-brainers! You’ll obviously want to integrate your ability to run a single test or test suite from a build tool quickly and easily.
- Test reports – Test reports are an important way of visualizing the health of your project and are usually the first place you’ll go when you see something wrong. A must have.
- Transitive dependency management – This is one of those sticky subjects that people like to argue about religiously. Really the important thing to note here is that a crucial part of keeping your build clean and manageable is figuring out how to represent and resolve transitive dependencies, or second-level dependencies. If your code directly depends on a library, odds are that library has its own run-time dependencies that need to be satisified in order for it to work properly. Since managing the different versions of many different libraries can become tough when you start to look at different “stacks” of software, many people advocate having a tool help you resolve these dependencies so it’s not as daunting a task to get the entire stack to work again when you need to upgrade a single library. Maven and Ivy solve this problem by introducing repositories that will house and dynamically resolve these versioning issues, in much the same manner as yum or apt-get does in the Linux world. A lot of newbs will make the mistake of thinking this type of functionality will let them turn off their brain when it comes to think about their project’s dependencies. They will be sorely mistaken! The point of these to make resolving conflicts in transitive dependencies easier, they do not replace your brain, nor your obligation to know the dependencies in your project and how they fit together!
- Utility ant tasks – the daunting part of learning how to write good builds has a bit to do with how much you know about the ant task landscape. Nearly all libraries have tools that plug into ant, but which ones do you really need to know about? I’ve found a few are quite powerful, particularly replace, uptodate, cvstagdiff (and corresponding svn’s), xmltask and dbdeploy. I think these tools should be looked at when you are trying to figure out how to accomplish something specific, and for that reason, they’re a bit more like “bells and whistles” than anything else. The same goes for the next category, which arguably doesn’t even need to live in your build (but if you’re a team player, it’s a definite nice to have):
- Static analysis – There have been a barage of slick static-analysis tools that have entered the Java landscape recently. A few are FindBugs, PMD, CheckStyle. While most of these tools have plugins that integrate nicely into an IDE, it’s also helpful to have them plugged into your build, too. Just to make sure everyone is following the rules. A few others I’ve seen that focus on cyclomatic complexity, cohesion/coupling are Metrics and JDepend.
What build tools fit into your pimped out script?
New Wave Logging
by Chris Wash on Mar.19, 2008, under JBoss Seam
Tired of messy logging logic cluttering your code with a bunch of if/else statements? Don’t let logging cramp your style! The approach Seam takes to logging makes your code pretty again (and other things). What does this mean for you? No more “code guards“!
A nice byproduct of the genius of Seam’s design is that many common problems can be solved by using EL as veritable swiss-army knife. We’ll look at this through a logging example specifically, though it’s just one of many different innovative ways of solving problems using Java5 features and EL as a general approach.
WTF is a code guard?
What goes without saying in most of the discussions regarding logging is that excessive logging can be the largest and most common performance bottleneck in code that meets its functional requirements. Thus, logging APIs introduce the idea of log levels, to ease the burden of excessive logging. Generally, log levels are a configuration setting on a per-environment basis. Development environments will typically log most dependent code at the info level, and cut that up to the debug level for custom code or when things go awry with a piece of dependent code. Testing environments typically log at info while production will often just log at the warn level.
While this is all well and good, we find that for a lot of the logging we do, we typically run across an ugly little flaw in this approach that’s produced an equally ugly hack as a work-around. Enter the code guard. From the log4j docs:
Code guards are typically used to guard code that only needs to execute in support of logging, that otherwise introduces undesirable runtime overhead in the general case (logging disabled). Examples are multiple parameters, or expressions (e.g. string + ” more”) for parameters. Use the guard methods of the form log.is<Priority>() to verify that logging should be performed, before incurring the overhead of the logging method call. Yes, the logging methods will perform the same check, but only after resolving parameters.
What’s all this business about resolving parameters? Let’s look at an example:
log.debug("FOO: " + foo + ", BAR: " + foo.getBar());
if (log.isDebugEnabled()) {
log.debug("FOO: " + foo + ", BAR: " + foo.getBar());
}
So, we need this cluttered if/else around only to ensure that our expressions (concats, usually) won’t get resolved unless we actually want them to. Why is there no other recourse? The answer isn’t surprising – it comes down to the way the API was designed. All log4j logging methods take a single argument — which made a lot of sense, I suppose when the powers that be first designed logging APIs. They soon realized that in order to use their API, one has to use an expression to resolve all parameters into a single argument prior to calling the method! This is why a code guard is needed.
Java5 introduces a feature that allows for varargs, and taking advantage of this can help us with our ugly little logging problem, e.g.
With this approach, our expressions won’t get resolved until after we’re in debug(), and we can let it do the dirty work inside the method. The first thing that our logging method does is check to see if its level is valid, otherwise it’ll fall out immediately. Because the concatenation happens inside the log method, we don’t need a code guard to protect us.
This is the approach that Seam takes to logging. Seam’s Log interface provides not only a solution that removes the need for code guards, it also allows you to use EL in your log messages (if you’re trying to dive into a Seam component…) – e.g.
Much better. By and large, this is the way you do logging in Seam. Because binding occurs late, you never really run into the problem where you need to even think about using something like a “code guard.” Code what you’re supposed to; not noise! This is just one of many novel ways to use EL in Seam, and why it really deserves a look as a way to write better code, faster and as a far more productive platform for Java programmers (fed up with JavaSE/EE 1.4) than Rails/Grails.
See the Web Beans Manifesto for much more on this approach.
Continuous Integration Dissected
by Chris Wash on Mar.13, 2008, under Software Engineering
Setting the Record Straight
A lot gets written about Continuous Integration, particularly on which is the best visual cue to let you know your build is broken or that a test is failing – lava lamps, Beta Brights, Ambient Orbs, and some even suggest traffic lights. But aside from this extraneous (at least to business) nerd-banter, a lot of what I find written about the actual topic of CI is fluffy, ivory tower, or pie-in-the-sky jibber-jabber that leaves out important parts of the big picture or confuses people more than it helps. In hopes of clearing up confusion on what exactly CI is and how it’s supposed to work, I’m ripping out a description that I wrote for a client proposal recently (so my apologizes for the dry-tone). I hope sheds some light on the true nature of CI, why it’s important and how to implement it from a birds-eye point of view.

Continuous Integration Dissected
Any large scale development project needs an automated, repeatable build process. Following best practices while developing a build process properly separates environment-specific configuration concerns from the codebase. This allows new environments to be created quickly and easily by simply overriding any environment-specific configuration values when first executing the build process. Whatever build tool is being used, builds should share a common, consistent process and interface. A consistent, repeatable build will know all of its dependencies and the goal is to be able to build any given module anywhere, independently, at any time.
Automated, repeatable build processes typically begin by obtaining dependencies (which can be specified using a dependency management tool) and a specific working-copy of the codebase (“checking out”) from a SCM system like CVS or Subversion. It is important to note that this codebase includes any code that is responsible for performing automated testing in addition to source code and configuration (and possibly other source-like artifacts).
Once the checkout has completed, the process will compile code and run automated unit test suites for each module in the system. At this point, all automated unit tests should pass, and custom development can begin. Any changes to code must be adequately covered by unit tests (either by changing existing tests or creating new ones), must fully compile without any errors and pass all automated unit test suites before being committed to the repository. The practice of always keeping the code committed to the SCM repository in this state (no compilation or unit test errors) is known as Continuous Integration and ensures that new development is safe to proceed at any point without fear of integration errors.
Subversion (and CVS) support concurrent development by following a Copy-Edit-Merge paradigm; any contention over files is usually caught when a developer tries to commit their changes and notices the underlying files have changed since they obtained their copy. In many cases, Subversion is capable of performing a merge automatically, if there was no contention over the same piece of a file. Sometimes, however, a manual merge will be required. Merging becomes more painful as the number of differences in the conflicting files increase. A good rule of thumb is that every developer should commit their changes at least daily.
Designating a single machine as a Continuous Integration (CI) environment provides many added benefits to a large scale development project. There are many operations which are good candidates to have run “continuously” but quite often are too expensive for developers to perform before every commit. Examples include executing automated in-browser system tests (which, if maintained over multiple releases, can serve as a “mini” regression test suite), performance tests/profiling, producing test metrics, generating documentation, etc. CI servers are an ideal place to schedule these processes to occur in an automated fashion.
Bugs, Detectives, and Test Automation
by Chris Wash on Mar.11, 2008, under Software Engineering
You’ve done a thorough code review with your team and your code has come out with a few nice improvements. Good. You know that many a trustworthy source have speculated code reviews find as many as two times the number of defects that testing does. So you might suppose going to the trouble of creating automated tests, simply for your own devices, might be a bit overkill. As it turns out, it is worth it, and it’s going to save you a lot of time in the long run. Why is it so valuable?
Up And Running
by Chris Wash on Feb.14, 2008, under Meta/Blog
Reinstalled WordPress. I’ve been a little frustrated with other blog efforts that I’ve been roped into in the past few years and have decided that this is as good a place as any to just get stuff written down. And there’s really no good reason not to – so here’goes!

