Coldfusion check field dynamically Compare len check vs eq "" [duplicate] - coldfusion

This question already has answers here:
len(x) better or x NEQ "" better in CFML?
(5 answers)
Closed 6 years ago.
I have a test in a function to make sure the form fields are there.
Which of these would you use? and why?
instead of trim(form[arguments.fieldname]) eq ""
len(trim(form[arguments.fieldname])) eq 0
Is there a true benefit to either one over the other?

They are equivalent. The choice is one of style. Do note that those who prefer len tend to use the return value as an implicit boolean (so, <cfif len(trim(form[arguments.fieldname]))>, without the eq 0), and this does seem to be generally regarded as more expressive.
But both work, and the performance differences are negligible. It is a question of readability, which is somewhat subjective.
See also.

Related

Is there a C or C++ equivalent to 'pass' in python? [duplicate]

This question already has answers here:
Is there an equivalent of Python's `pass` in c++ std11?
(7 answers)
Closed 2 years ago.
Is there a C or C++ equivalent to 'pass' in python? Also the same with break. For example:
while True:
if x == 1:
break
else:
pass
But in C/C++ instead?
Is there a C or C++ equivalent to 'pass' in python?
Yes. There are actually two equivalents. One is the null statement:
;
Another is the empty block statement:
{}
These are in most cases inter-changeable except the null statement cannot be used in all cases where the empty block statement can. For example, the function body must be a block statement.
In the example case, you can omit the else-statement entirely just like you can in Python.

Regex 1964 to 2007 [duplicate]

This question already has answers here:
Convert integer numeric interval to regex
(3 answers)
Closed 8 years ago.
In PHP, I know that /^((19|20)\d\d)$/ will match all numbers from 1900 to 2099.
What is the most elegant way to match all numbers from 1964 to 2007 ?
/^(196[456789]|19[789]\d|200[01234567])$/ ?
Edit:
Many answers to similar questions recommend using comparison instead of regex, for example because the regex is hard to read and difficult to maintain.
The problem with comparisons is that strings that are not numbers might match. For example:
$num = "";
if($num >= 0 && $num < 10) echo "yes"; // yes
Obviously an empty string is not a number between 0 and 10. So an easily maintainable comparison needs to check for numericity as well, which might make it less superior to a regex.
I would say /^19(6[4-9]|[7-9]\d)|200[0-7]$/ is the most elegant way.
^(196[456789])|(197[0-9])|(198[0-9])|(199[0-9])|(200[0-7])$

Reason for using '5 == myValue' in conditionals [duplicate]

This question already has answers here:
What is the difference between if(CONST==variable) or if(variable==CONST)?
(5 answers)
Closed 9 years ago.
I've come across some code that flips how a condition is checked and was wondering why this would be done aside from a weird personal quirk. I've never seen any text books use it nor have I seen any sample code done this way.
// why do it this way?
if (5 == myValue)
{
// do something
}
// instead of:
if (myValue == 5)
{
// do something
}
I've only seen this way for == operand but not for any other operands.
Some people like doing that because if you mess up and type a single-equals instead of a double-equals, "if (val = 5)" is only a compiler warning, while "if (5 = val)" is an error (you can't assign to a constant).
I think it's sort of ugly, personally, and that you should be checking your warnings just as much as your errors, but that is the reason for that convention (which is thankfully not universal, but is at least moderately widespread. It is also true that it might not universally have been treated as even a warning by much older compilers, and this convention has been around for a long time.)
Fun fact: I was just reading Code Complete (Second Edition), and Steve McConnell agrees with me, stating his personal preference is "to use number-line ordering and let the compiler warn me about unintended assignments". Steve McConnell by definition knows what he's talking about.
Some folks do it for readability when myValue is deemed less interesting than 5; it puts the constant value in a more prominent place. There is no practical reason for it - purely a judgment call on the part of the coder.

for (;;) is this an infinite loop? [duplicate]

This question already has answers here:
Is "for(;;)" faster than "while (true)"? If not, why do people use it?
(21 answers)
for loop missing initialization
(6 answers)
Closed 9 years ago.
Recently while going through a c++ tutorial I encountered a for loop that looked like this:
for (;;){
//Do stuff
}
Is this an infinite loop? Why would I use this rather that while(1)?
Yes, it's infinite. Traditionally, compilers would generate a warning when you use while(1), but not when you use for(;;). I don't know if this is still the case.
It's an infinite loop. More precisely, if the condition in a for is empty, it is considered true. As for while ( true ) vs. for (;;): historically for (;;) was the idiomatic form (used by Kernighan and Ritchie), perhaps partially because early C didn't have booleans. Using while ( 1 ) wouldn't pass code review anywhere I've worked. With booleans, while ( true ) definitely seems more intuitive than for (;;), but while ( 1 ) is confusing. But in pre-boolean times, everyone had a #define for true or TRUE or some such, so it's a weak argument. In the end, if you're an old C programmer, like me, who originally learned from Kernighan and Ritchie, you just instinctively use for (;;). Otherwise... it probably depends on where and from whom you learned C++.
Of course, when at work, you follow the house conventions, what ever they are.
Is this an infinite loop?
Yes.
Why would I use this rather that while(1)?
Because of (bad, IMO) taste. By the way, I would go for while (true), if I really had to create an infinite loop.

Syntactic sugar for if(x==10 || x==12), why not? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I've recently seen this question. Which contained (what I believe is) a very common early programer mistake of writing if(x==10 || 12) when you mean if(x==10 || x==12) which made me wonder. Why (to my knowledge) there is no support for this if(x==10 || 12) seemingly more natural syntax, or at least some similar syntax. So my questions are:
1) Is there some kind of syntactic sugar/macro expansion/shorthand for this kind of expression in any of the more common languages (c,c++,java,c#). If not, why not ?
2) Is there a way using java and eclipse to add this (as a compiler macro expansion or any other useable solution) ?
EDIT: clarification, I did not mean I wish to alter the meaning of if(x==10 || 12). I was looking for syntactic sugar that is shorter than if(x==10 || x==12) but is functionally equivalent.
That's because it's makes more sense for if (x == 10 || 12) to be interpreted exactly as it's written. That's "If x equals to 10, or 12 is truthy". If you want something done, write it explicitly.
You could use a switch case with butted up case statements in C#. You can't mess too much with ifs as you would break more legitimate cases and reduce readability. It would be syntactic salt rather than sugar.
You can use array or set lookup in languages that support compact representation of arrays or sets.
Javascript (as well as PHP and Java) has array literals:
if( [10,12].indexOf(x) != -1) ...
you can also use the bitwise negation as a shortcut for !=-1:
if(~[10,12].indexOf(x)) ...
Php 5.4 has array literals as well, and you can use them without a temp variable from 5.5 (I think). This should be superlinear as well, but generates notices unless you use # or in_array:
if(#[10=>true, 12=>true][$x]) ...
A regex lookup is also an option in languages that support compact regex syntax and you're matching strings. This has the benefit of being potentially faster than array search:
if(/^(10|12)$/.test(x)) ...
A switch has also been suggested:
switch(x){
case 10:
case 12:
...
}
If that was allowed, how would you write a condition like this one:
if( x == a || b )
where x and a are chars or integers and b can be either true or false?
For example:
x = 'a';
b = true;
// later in the program
if( x == 'c' || b )