Quick tip - using cfthrow as a reminder

This post is more than 2 years old.

I just typed this code in and I thought I'd share it to see if I was the only one who did this.

I'm working on a process where I need to do one of two things. Tonight I'm working on the first branch, but the second branch may not be worked on for a while. In the past I'd do something like so normally:

<cfif condition> what I'm doing <cfelse> </cfif>

If I was half-way awake, I'd maybe even do this:

<cfif condition> what I'm doing <cfelse> <!--- do the other thing ---> </cfif>

That works, but lately, I've been using cfthrow. The point being that if I forgot to actually code the other block, the second it does being to fire, I can't ignore it.

<cfif condition> what I'm doing <cfelse> <cfthrow message="Implement this block"> </cfif>

Anyone else do this?

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Andrew Scott posted on 1/19/2012 at 8:06 AM

Yes I have a huge tendency to do this, but I also use comments and the // ToDo feature of the editor more.

Comment 2 by Matt Busche posted on 1/19/2012 at 8:46 AM

i've never done this before (usually add a comment), but this is a great way to make sure you don't forget.

Comment 3 by Mikel posted on 1/19/2012 at 6:15 PM

I usually use the ToDo feature of my editor but I like this idea!

Comment 4 by Christopher Vigliotti posted on 1/19/2012 at 7:30 PM

I also prefer the TODO feature, as it doesn't break when it goes to production and I haven't gotten around to TODO'ing.

Comment 5 by Raymond Camden posted on 1/19/2012 at 7:40 PM

@Christopher: I'd argue that I'd _want_ it to break - production or not. That's kinda the point - to not let it slip by.

Comment 6 by David Hammond posted on 1/19/2012 at 7:50 PM

I like it! I never thought of doing this before, but I'm with you on producing a big fat error message when things go wrong rather than having them fail silently.

Comment 7 by Andrew Scott posted on 1/19/2012 at 7:54 PM

@Christopher, I think it depends on the logic. If you are in development, then the throwing of an error is good reminder. Personally I tend to use the TODO in comments because CFB keeps track of these.

But I will say that I have done this, when doing large coding and working on many different things. But with this and unit tests, one would not, well at least should not see something like this hit production anyway.

Comment 8 by Sharon posted on 1/19/2012 at 8:10 PM

I usually add those tasks to my editor as well. But I can see the value of something like that for things that just slip off the todo list and into the mists of bugs. It's also a good field check for how often "else" happens. Maybe it never happens and you don't *need* to go back an implement block two.

Comment 9 by CJM posted on 1/19/2012 at 9:18 PM

Because, for some dang reason, my editor TODO comments never seem to work I actually use the throw tactic quite often.

Comment 10 by Sarah Kelly posted on 1/20/2012 at 5:50 AM

I do the first. I plan to start doing the second--great idea!

Comment 11 by David Buhler posted on 1/20/2012 at 8:20 AM

From a Process perspective, you would not need to cover the possible fall-throughs if you had tests in place to cover all possible conditions. When I've added safe-guards in the past to all possible unmet conditions, the code became difficult to read. I also find that writing code for 'future-logic' often sees me writing code that won't be used as requirements change, or I mark the logic in my mind as 'complete', when the code is far from complete, because it isn't tested.

Mind you, I'm yet to see a developer create tests that would cover all possible conditions.

Comment 12 by Paul Bernhardt posted on 1/20/2012 at 9:50 PM

I am really not convinced that deliberately throwing an exception is a good way to remind yourself to implement a suite of code. I'd recommend a couple of things:

1 - Write tests with good coverage
2 - Plan your application development path
3 - Consider alternatives to ColdFusion such as Java or .NET

Comment 13 by Raymond Camden posted on 1/20/2012 at 9:52 PM

@Paul:
Others mentioned unit tests too and I think it's a great idea.

As for planning... well, this IS part of the plan. I know I need to support the other condition, I just plan on doing it later.

As for your last point.... really? How does that impact here? ColdFusion is perfectly capable of testing and development paths. I really feel like your last point is totally out of place and uninformed of what CF can do.

Comment 14 by David Herman posted on 1/23/2012 at 11:55 PM

Since I'm mainly in java these days I tend to throw a fitting exception UnsupportedOperationException

Comment 15 by Raymond Camden posted on 2/1/2012 at 7:38 AM

In case folks are curious -

I did forget.

And the throw reminded me.