Skip to main content
Ben Nadel at cf.Objective() 2011 (Minneapolis, MN) with: Steve Withington
Ben Nadel at cf.Objective() 2011 (Minneapolis, MN) with: Steve Withington

On Ending Path Variables With A Slash (Or Not) In ColdFusion

By
Published in Comments (2)

After many years of programming, I have a lot of strong opinions about how I like to organize my code. But, one aspect of application architecture that I'm constantly flip-flopping on is whether or not to include a slash (/) at the end of my path variables.

Path variables can be used to defined server-side paths. As in, use "this directory" for various templates / components / data files, etc. And, path variables can be used to define client-side paths. As in, use "this directory" for various images / CSS / JavaScript files, etc.

Aside: Consuming "client-side" paths is actually a bit of a misnomer. All paths get sent to the server. So, all paths are really "server-side" paths. But, I'm drawing a distinction here in order to illustrate the various contexts in which a path variable might be useful to a developer.

When defining such a path variable, I usually omit the trailing slash. As in:

pathPrefix = "/my/sub/root";

Notice that there is no (/) after root. This way, I can explicitly include the path separator when consuming the variable later on in my ColdFusion code:

#pathPrefix#/template.cfm

Having the (/) here in between the pathPrefix and the template.cfm is an aesthetically correct experience. It makes the path construction easier to read. And it makes the meaning of the pathPrefix extremely clear.

But, while the aesthetics of the consumption are correct, the aesthetics of the variable definition are not. Historically, a path ending without a slash is a "file". And a path ending with a slash is a "directory". As such, the aesthetically correct experience for defining a directory path variable should be:

pathPrefix = "/my/sub/root/";

Notice that there's now a (/) after root. This makes the variable more pleasant and maintainable and I think more "correct". But, it makes the consumption of said variable much less pleasing:

#pathPrefix#template.cfm

This is clearly gross and elicits a strong visceral reaction. Without the inclusion of the (/) at the time of consumption, the aesthetics of this path are all wrong. It's unclear as to whether the variable represents a directory-prefix or a filename-prefix.

And so, path variables represent a dammed if you do, dammed if you don't scenario. If I make the variable definition aesthetically correct, it makes the variable consumption wrong. And, if I make the variable consumption aesthetically correct, it makes the variable definition wrong. Over the years, I've flip-flopped on which of these is the lesser of two evils.

The worst part of this philosophical conflict is that you can actually include a slash in both places and the code will still "work". Meaning, I can do both this:

pathPrefix = "/my/sub/root/";

and this:

#pathPrefix#/template.cfm

And, technically, the code will execute properly and the correct file will be included. However, if you consider the evaluated code, the path becomes:

"/my/sub/root//template.cfm"

Notice that there's now a double slash (//) before the filename. Coincidentally, the server will ignore the empty path segment and the desired template will be referenced. However, this is just a happy coincidence on many servers—it's not an appropriate use of the filesystem. Not to mention, aesthetically, this path is just monstrous.

To get around some of this, you could swing hard in the opposite complexity direction and use a function to join path segments. For example, if this were a Node.js application, we could use path.join() to both concatenate segments and normalize the resultant path:

${ path.join( pathPrefix, "/template.htm" ) }

This would totally work. But, from a complexity and aesthetics standpoint, there's just no comparing it to straightforward string interpolation.

And so, for me, there's no obviously correct answer to how path variables should be defined in ColdFusion. My current preference is to defer the inclusion of the (/) to the moment of consumption, leaving my variable assignments without the trailing slash. It's not the "right" solution; but, it feels like the solution with the least incorrect aesthetics.

What do you all do? What is your preference?

Reader Comments

226 Comments

Firstly, I love that you have these conversations and that you care about such things. I feel less alone, and I'm here for it 💯.

Like you, I strongly lean towards leaving the trailing / out of path variables. Readable code is top priority!

15,811 Comments

@Chris,

Heck yeah! I always want to be able to connect emotionally with the way that I write code. Once I can find the "right" approach to do something, then I believe it will become instinctual. And then I never have to worry about it again 💪

Post A Comment — I'd Love To Hear From You!

Post a Comment

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel