HoF Hates Base-64

After some careful observation this morning, it appears that email I
sent do HoF mailing lists is being killed outright.  So basically
I'm cut off form the CF world until I either decide to change my email
address, GMail gets off their ass an switches Base-64 encoding back
off, or everyone fixes their mailing software.

I haven't decided the best way to handle this yet.  But for now, I'll probably just be silent.

More Blank Message Info

After another day of fighting the blank message demons, I've assembled some more information.  On October 1st, Gmail
started using the Base-64 transfer encoding on messages I sent.  I
sent two messages to CFCDev on the 6th, and then it wasn't until the 13th
(when the problem first came to my attention) that I sent any other
messages to that list.  I believe this explains how the problem
went from it's initial cause to where we are today with such a long
delay.  As I said before, the HoF lists don't have this issue,
because they're not going through a listserv, but rather a CF
application.

Interestingly, GMail doesn't seem to be using Base-64 for all
it's sent messages; only those for certain accounts go out with that
encoding.  I checked the sent mail from another account and it is
still sending messages with quoted-printable encoding.  I'd be
interested to know if any other people are sending with Base-64 from
their accounts ('show original' on one of your sent messages).

A couple posts on the BD-interest mailing list revealed the same
issues as on CFCDev.  This confirms (or at least provides heavy
support for it) my suspicion that misbehaving mailing list software is
where the problem resides.  The BD-interest list is using IMail
from IPSwitch, though an older version.  When Ray gets back from
MAX we'll see if he's using the same thing for CFCDev.

'Blank Barney Messages'

Over the past couple days, there have been numerous complaints about
my messages showing up blank in various people's inboxes, and I'm
trying to nail down why.  Here's what I suspect has happened:

GMail
started (at some unknown point) sending messages with a base-64 content
transfer encoding.  Mailing list software often appends a footer to
the bottom of the message, and when that footer is textually appended
to a base-64 encoded message, the result is an invalid message. 
The proper method would be to decode the base-64 message, add the
footer, and then optionally reencode it.  Once you receive such an
invalid message, some client appear to be able to read it, and others
just render a blank message.

Supporting
this is the fact that no one has complained about email coming directly
from me, or via the House of Fusion lists (which don't actually use a
mailing list server).  So, if you receive a blank email from me
(or anyone else), please let me know, either by emailing me, or by
commenting on this post.  Please include your name, email, where
you got the message from (i.e. what mailing list), and what email
client you're using.  Hopefully with some more data, I can get
this problem nailed down, and see about getting fixed.

Thanks. 

ColdFusion and Batik

I've been using SVG for custom charting stuff for several years now,and it's really nice. Only problem is that it requires an SVG plugin, and it's not very common. No big deal for admin sites, but for more public usage, that's pretty much a concept killer.

Enter Batik.

Batik is a Java SVG implementation by the Apache Group that, asidefrom rendering SVG, has a lot of really neat supporting tools. One of them is a group of transcoders that'll take an SVG file and turn it into an image. Do that, and suddenly your SVG charts are consumable by the general public. Obviously the transcoders don't do the animation and scripting that SVG supports, but for charts, that's no big deal.

Installation is a snap, though CF7 has a partial Batik package included that needs to be moved out of the way first. Both standalone and multiserver installs should have a WEB-INF/cfform/jars directory with a few batik-*.jar files. Those need to be moved away (I opted for putting them in a new WEB-INF/cfform/batik-jars directory), and then a full Batik package needs to be installed. For standalone, I found $cfroot/lib to be optimal, and on multiserver they should go in WEB-INF/lib.

Once done, all you need is a bit of magic like this:

















You will, of course, want to update the width and height on the EMBED tag to match your file. I haven't tested any of this on CF6, but it should be even simpler, since there doesn't appear to be the partial Batik install that needs to be moved out of the way. Find a suitable interlocutor and communicate in an anonymous online chat with the ability to send photos. Welcome to live cams girls for online communication and Dating with girls and guys without registration and SMS, completely anonymous!

As near as I can tell, CF itself isn't any worse for wear with the updated Batik, though I can't say I've tested very thoroughly.

The Encapsulated Hack

As is pretty obvious, I'm a big fan of good OO design.  But I'm
a bigger fan of maintainable apps, and one of my favorite tricks when
evolving apps is what I call the "encapsulated hack."

Take, for
example, a CFC that needs access to an
application-scope variable.  What's the right solution? 
Package the variable up in such a way that it can be passed to the CFC,
so the CFC doesn't have to break encapsulation by calling the
application-scope variable.  Much of the time, that's easy to do,
but not always.  When it's not possible, I hack it. 

Here's what I'd do:

<cffunction name="getSomeVar" access="private" …>
  <!—TODO: encapsulated hack —>
  <cfreturn application.someVar />
</cffunction>

"Ick!,"
you say, "that's breaking encapsulation!," and you'd be right. 
But notice that I'm breaking encapsulation in exactly one place,
and that place is itself encapsulated.  Now, when I do it the right
way, I can change the implementation of this one method, and nothing
else has to change.

Speaking from experience working with a single large
app over several years, I can tell you that this is very helpful in
allowing you to move forward with a good OO design, without having to
do everything
all at once.  Just make sure you clearly
indicate that you've got a hack, and that it needs to be fixed. 
If you're using CFEclipse, you can use a TODO comment as I have, open
the 'tasks' view, and you'll get a listing of all such todos in your
code.

Designing an OO Backend

As follow-up for my 'Impetus for an OO Backend' post from a couple
weeks ago, I wanted to talk about some approach designing an OO
backend. Again, I'm going to attempt brevity and undoubtedly end up
being long winded, and I'm going to again intentionally skip
implementation details. Finally, there will be almost no mention of UIs
at all; I'm talking exclusively about the backed of an application. UIs
will use the exposed APIs to do "stuff", based on user input. How they
do it is a totally different discussion.

For the sake of having a consistent example, I'm going to be talking
about some unnamed app that deals with users. No need for more specific
than that, I don't think, as pretty much every CF developer has had to
build such functionality for one app or another, or at the very least,
had to use such an application.

To jump into the meat of things, the first concepts are really the
central tenets of good OO design: abstraction and encapsulation.
Abstraction refers to the separation of a thing's interface from it's
implementation. In other words, someone can be told what this thing
does without having to know how it does it. Encapsulation refers to
the state of being self-contained; not depending on other things. The
"black box" on an commercial airliner is a good example of both.
Everyone knows what it does (records everything about the plane), but
most people don't know how (for instance, what's the recording media?),
and it's also totally self contained so that if the plane completely
breaks, it'll keep functioning. These two core concepts are the central ideas of almost
every good OO design.

But on to our user management app. Pretty simple, right? Add a user,
delete a user, edit a user, and get a list of users in the system.
First, where does abstraction fit in? Well, I just told you what the
app needs to do, but I haven't told you anything about how it does it,
so I'd say we've found it. But how do we implement the abstraction? For that, we
need a service object.

Service objects are objects that expose "business operations" to
something. I listed the four core business operations of our app above
(add, edit, delete, and list users), so that'll be what's exposed in
our service object. Services are also singletons, which means that
there should never be more than a single instance of them within an
application. Since they're singletons, that also means that they must
be thread safe, but again, that's a whole different topic.

So now that we've got our service (named UserService, of course),
how do we use it? The easiest answer is to do <cfset application.userservice = createObject("component", "userservice").init(application.dsn) /> during application
startup (in Application.cfc or Application.cfm), modified
to reflect whatever specific parameters the service needs. Now the
whole application can access the service (via the application scope),
and since the createObject call only happens at application startup,
we're [mostly] assured that there will only be a single instance of the
component. As a note, this isn't my recommended technique for larger
apps, but for this scenario, it's perfectly adequate.

Now it doesn't seem like we've gotten much out of packaging the
business operations up in this fashion. We have a nice object with
everything in it, sure, but that just makes it harder, because it's a
single large file, right? Those are valid concerns (which can be dealt
with later), but that arrangement brings a very important benefit that
isn't quite so obvious. Your UI knows nothing about the business logic
of the app, except that a given method of application.userservice, when
passed a certain set of parameters, will do a certain thing.  I.e., the what but not the how.  That's a
ridiculously beneficial thing to be able to say, because it means the
implementation of the methods can change at will, and the UI needn't
care at all, as long as the end result is the same. If you don't see
why that's a wonderful trait to have, keep reading.

Lets say we've got our app running in production, and then the
managers come back with a set of changes, as they always do. But
they're not just little tweaks, they're some pretty massive changes to
core functionality. So you go off and basically gut your UserService
object to implement these changes. But while you're doing, you're
careful not to change any of the cffunction and cfargument tags. When
you're done, guess what? You're done. Since the methods didn't change,
your UI needn't change, so you don't have any more coding to do, and
your testing should be equally streamlined. That, my friends, is the
power of abstraction.

So what have we learned? Having a clearly defined API (exposed via
service objects) that your UI connects to can save you enormous amounts
of work as changes happen over time. That same API also lets you
redesign the backend, as well as change the business logic, without affecting anything else.

Now lets say you're working along, as it comes time to add some new
functionality that doesn't tie directly to users (lets say document
creation). Time for another service object: DocumentService, which will
have the corresponding effect of expanding our application's API. Same
rules apply, except now our UserService isn't the entire backend, so
we're not longer completely encapsulated. It's possible, likely even,
that the DocumentService will need to call methods on the UserService.
This is different then the UI calling methods, since the API the UI
uses doesn't know about implementation, but the guts of the
DocumentService does (the guts, after all, are the implementation).  We also don't want the service objects talking to the application scope, because there's no guarentee that application.userservice will always be there.  It might be server.userservice, or application.services.user.

We still have our abstraction, just like before, but the
encapsulation needs some work. Enter a factory. Not literally, but the
Factory design pattern (you may all now cringe and shudder in fear).
The Factory design pattern simply says "instead of instantiating
objects directly, create an object whose sole purpose is to create
those instances for you." Again, not very earth-shattering at first
blush, but digging deeper will reveal some great advantages. The more important question, however, is
how do we design it?

Remember where we called createObject to instantiate the UserService
directly into the application scope? Well instead, we'll create a
factory that has a getUserService method, and instatiate the factory
into the application scope, and then we'll immediately call that method
and assign the result to application.userservice. This second step
isn't necessary (or desirable, even), but it's important, because our
app currently depends on application.userservice being available, and
we don't want to have to change all the references to it right now,
though that's something that should be done.

Inside the factory, the getUserService method does about what you'd expect, except that it
caches the instance it creates in the factory's variables scope and uses
that cached instance for subsequent requests so that the instances are
true singletons.

Now back to our original problem: how does the DocumentService call
methods on the UserService? Well, both services, in addition to their
"normal" constructor arguments should be changed to accept a reference
to the factory as well. So now either service can easily obtain a
reference to any other service in the backend. The factory is allowing
the backend as a whole to remain encapsulated, and it's also allowing
each individual service to be abstracted from the others. Note that
this means the service objects are now exposing two APIs, one for the
UIs to use, and one for other services to use. The former is almost
certainly a subset of the latter, so it doesn't really appear to be two
different APIs, but it is.

Ok, quick recap to this point. We have a factory singleton
instantiated into the application scope of the app. It can be asked for
singleton instances of the application's service objects. Each of those
service objects also has a reference to the factory, so they can
request instance of other services for complex business operations. The
backend as a whole is fully abstracted from the UI and fully
encapsulated, and each individual service object is also fully
abstracted and mostly encapsulated from the other service objects. I
used the 'mostly' hedge because it's impossible to get fully
encapsulated within the backend (otherwise it'd be multiple separate
backends), but we want each piece to be as isolated as is reasonable to
reduce the maintenance headache.

Once we have this arrangement, the possibilities are nearly
limitless. We can wantonly reimplement any of our service objects
without affecting anything else. So when you're ready to give using
entity objects (business objects, entity beans, etc.) and their
corresponding DAOs a whirl, you can do it with a minimum of fuss. As
long as everything remains encapsulated behind the service objects, no
other pieces of the application (both the backend and the UI) need care.

It's probably worth a bit more discussion of encapsulation at this
point. Encapsulation isn't a wall around an object. It's a wall around
a system, where a system can be quite large or very small. All but the
simplest systems are made up of smaller subsystems, each with their own
wall. A system can't see outside it's wall, but it can see inside it's
subsystem's walls. Note that I'm talking only about encapsulation here,
not abstraction. A system can see it's subsystems' interfaces , but not
their implementations, but a system can't even see the interface of
it's parent system.

Two more concepts to mention (by name) before I go: coupling and cohesion.

Coupling is when objects are tightly bound together in some way.
This is pretty obviously a Bad Thing, as it means that encapsulation
and abstraction are both missing to some degree or another. In dollars
and cents, it means that any changes to one thing have a great chance
of the "ripple effect", which usually means a lot more wild goose
chases across your codebase fixing bugs, and at the very least,
requires a longer testing and QA cycle for every code change.

Cohesion is the idea that a given thing (be it a system, and object,
or a method) does a single, clearly definable thing. This has the
benefit of making your code easier to follow, and it also reduces the
possibility that a method will semantically change, and therefore
require an API adjustment, which reduces the ripple effect, and again
saves you money with reduced maintenance costs.  Finally, it
promotes code reuse, since small atomic blocks are easier to reuse then
larger blocks.

Two scary terms, but speaking from experience, you don't have to
think about them much. If you've done your homework and designed a system that is both
abstracted and encapsulated, you'll usually end up with loose
coupling and tight cohesion without having to think about it. I only
mention them because they're helpful checks along the way while
designing a system. When you get stuck, ask yourself which alternative
is more cohesive and more loosely coupled, and that's usually the right
way to go.

In conclusion (if it's a conclusion at all), the APIs you build into
your backend are of utmost importance.  The rest of the design is
pretty much meaningless without this piece.  If it's present,
however, your developers will be enormously more productive and able to
meet changing needs without massive costs to rework the application as
a whole.

Good OO design is difficult, almost always requiring an
iterative approach, but using the coupling and cohesion tests along the
way can help you avoid potential pitfalls.  But most of all, it
just takes a lot thoughtful practice and experimentation to learn what
works and what doesn't.

So I'm again signing off after more typing than I intended. I
realize now that I didn't offer up a whiskey break breather in the middle,
but hopefully you'll forgive me.

CF7, Web Services, and null

I discovered today (after many hours of debugging), that if you have
web service method declared to return 'any' on CF6.1, you can return
null without incident.  However, if you're on CF7, it causes an
NPE to be thrown.  To illustrate what I mean, take this
[abbreviated] code:



That function will work happily if served by CF6.1 and consumed by
CF6.1 and CF7.  However, if it's served by CF7, it will error
out.  The solution is to do something like this:





<


The structKeyExists call is checking for null (it will return false
if the key exists but has a null value – something that I'm not sure is
documented anywhere).  If the result is null, it's reset to the
empty string before being returned.

Note that non-web service
method invocations on CF7 can return null with a declared returntype of
'any' without incident; this is a web services-only problem.

online casino real money nz

Remote Diff

Ever wanted to do a diff between a local file and a remote one?  I have, so I wrote a simple shell script that'll do it for you.  It requires scp, diff, and rm to be available on your system, which should be the case on any modern *nix.  The -b option to diff tells it to ignore white-space difference.  If you don't have password-less SSH authentication set up, you'll want to remove the >& /dev/null trailers from the two scp lines so you get your prompts.

To run, just drop this into /usr/bin (or somewhere else on your path), make it executable, and call it just like diff, except with scp-compatible file paths.

#!/bin/sh
#
# this acts as a remote diff program, accepting two files and displaying
# a diff for them.  Zero, one, or both files can be remote.  File paths
# must be in a format `scp` understands: [[user@]host:]file

if [ "$1" = "" -o "$2" = "" ]; then
echo "Usage: `basename $0` file1 file2"
exit 1
fi

scp $1 rdiff.1 >& /dev/null
scp $2 rdiff.2 >& /dev/null
diff -b rdiff.1 rdiff.2
rm -f rdiff.1 rdiff.2

Update: Horst and Amit  both posted a far better solution that leverages SSH directly.  Back when I wrote this I didn't appreciate SSH's power for more than interactive shells.  Here's his command (replaced the brace-wrapped strings as needed):

ssh {remote_host} cat {remote_file} | diff {local_file} -

Good UI Design

I'm a stickler for good UI design.  I don't claim to be a wiz
at it myself, though I'd like to think I'm better than many.  I
love coming across web sites that are a breeze to use.  Hell, I
love coming across web sites that aren't painful to use.  In a fit
of spastic home-all-alone Googling, I hit the "I'm Feeling Lucky"
button on a search for "monkey nuts", hit the 'enter' button on the
splash page, and was rewarded by this site,
which had a quasi-prominent UI element that not only made me smile, but
happened to be EXACTLY what the page needed to elevate it bad to quite
good.  Enough of a shift that I couldn't help sharing.

For
those of you who don't see it, it's the handwritten element.  The
one that says, "hey, I'm a dumbass and put a scrolling frame in the
middle of my page, but I have the courtesy to flat out tell you I did
it, and help you deal with it, because while I know it's not a very
good way to do things, it was the only way to get what I wanted, so I'm
going out of my way to make it as painless as possible for you: 'this
is a scrollbar –>'."

I love it.

Eclipse's Incremental Find

Little Eclipse gem I just found.  On the in-file Find dialog,
there's an "incremental" checkbox.  If you check it, Eclipse will
find the first match as you type, updating for every keystroke,
just like FireFox's in-page text searching.  I've longed for that
feature for months, and never noticed that it was sitting there right
under my nose.