> function isEmpty(mystring) { return yesnoformat(not
len(trim(mystring))); }
>
> function isNotEmpty(mystring) { return yesnoformat(len(trim(mystring))); }
>
> Sure, it's niggling, but I suspect this would be more efficient for the
> server. For me it's actually easier to read these than a function with an
> if-else in it.
>
> Incidentally -- I'm never personally bothered by a function with more than
> one return statement. It's like putting <cfexit> in a custom tag. But
then,
> I may not be "a lot of developers". ;)
>
Of course, you're breaking the Numeric vs Boolean rule with this
function....
Yes yes... yesnoformat accepts a boolean or a number and non-zero numbers
are treated as true, but that's only because thats what CF does anyway....
Why on earth are you using a function for this at all? So <cfif
IsEmpty(mystring)> reads nicer, but whats wrong with just checking to see
if the length is greater than zero? Count how many function calls you make
just to see if a variable has a length or not when in reality its one
(possibly two if you include a trim() ) function calls and a valid
comparison! Think about where that UDF is going to be relative to this
condition in the code for when you think about maintainability.
<cfset stringlength = len(trim(mystring))>
<cfif stringlength GT 0>
do stuff
</cfif>
How hard was that? At a glance you can tell that I'm finding out the length
of mystring and checking to see if its greater than zero. I now even have
the length of the string in a variable, available for me to use should the
need arise, rather than having to call len and trim all over again.
What is wrong with keeping things simple? If you keep it simple, your code
is eaily maintainable, you know exactly what is going on at a glance and
your application performance should be pretty good too.
Thank you to Dave Watts, Sean Corfield and Jeffry Houser for making my
pedantry into an interesting debate. I'm glad to see that I am not alone in
my feelings that explict coding is best, even if Dave thinks I shouldn't be
such a pedant... ;o)
Regards
Stephen
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4
Subscription:
http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribe&forumid=4
FAQ: http://www.thenetprofits.co.uk/coldfusion/faq
Signup for the Fusion Authority news alert and keep up with the latest news in
ColdFusion and related topics. http://www.fusionauthority.com/signup.cfm