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?
Archived Comments
Yes I have a huge tendency to do this, but I also use comments and the // ToDo feature of the editor more.
i've never done this before (usually add a comment), but this is a great way to make sure you don't forget.
I usually use the ToDo feature of my editor but I like this idea!
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.
@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.
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.
@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.
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.
Because, for some dang reason, my editor TODO comments never seem to work I actually use the throw tactic quite often.
I do the first. I plan to start doing the second--great idea!
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.
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
@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.
Since I'm mainly in java these days I tend to throw a fitting exception UnsupportedOperationException
In case folks are curious -
I did forget.
And the throw reminded me.