Essentials of Javascript - Cultural View

Essentials of Javascript - Cultural View Essentials of Javascript - Cultural View

culturalview.com
from culturalview.com More from this publisher
14.07.2013 Views

?: 55 echo $vehicle; ?> 'feet' ); Due to an unfortunate error in the language grammar, the implementation of ?: in PHP uses the incorrect associativity when compared to other languages, and given a value of T for arg, the PHP equivalent of the above example would yield the value horse instead of train as one would expect. To avoid this, nested parenthesis are needed, as in this example: (($arg == 'A') ? 'airplane' : (($arg == 'T') ? 'train' : (($arg == 'C') ? 'car' : (($arg == 'H') ? 'horse' : 'feet')))); This will produce the correct result of train being printed to the screen. CFML (Railo only) arg = "T"; vehicle = ( ( arg == 'B' ) ? 'bus' : ( arg == 'A' ) ? 'airplane' : ( arg == 'T' ) ? 'train' : ( arg == 'C' ) ? 'car' : ( arg == 'H' ) ? 'horse' : #vehicle# Result type 'feet' ); Clearly the type of the result of the ?: operator must be in some sense the type unification of the types of its second and third operands. In C this is accomplished for numeric types by arithmetic promotion; since C does not have a type hierarchy for pointer types, pointer operands may only be used if they are of the same type (ignoring type qualifiers) or one is void or NULL. It is undefined behaviour to mix pointer and integral or incompatible pointer types; thus number = spell_out_numbers ? "forty-two" : 42; will result in a compile-time error in most compilers.

?: 56 ?: in style guidelines Some corporate programming guidelines list the use of the conditional operator as bad practice because it can harm readability and long-term maintainability. Conditional operators are widely used and can be useful in certain circumstances to avoid the use of an if statement, either because the extra verbiage would be too lengthy or because the syntactic context does not permit a statement. For example: #define MAX(a, b) (((a)>(b)) ? (a) : (b)) or for (i = 0; i < MAX_PATTERNS; i++) c_patterns[i].ShowWindow(m_data.fOn[i] ? SW_SHOW : SW_HIDE); (The latter example uses the Microsoft Foundation Classes Framework for Win32.) When properly formatted, the conditional operator can be used to write simple and coherent case selectors. For example: vehicle = arg == 'B' ? bus : arg == 'A' ? airplane : arg == 'T' ? train : arg == 'C' ? car : arg == 'H' ? horse : feet; Appropriate use of the conditional operator in a variable assignment context reduces the probability of a bug from a faulty assignment as the assigned variable is stated just once as opposed to multiple times. See also • ?? Operator References [1] "BCPL Ternary operator (page 15)" (http://cm.bell-labs.com/cm/cs/who/dmr/bcpl.pdf). BCPL Reference Manual. . [2] http://www.python.org/doc/faq/programming/#is-there-an-equivalent-of-c-s-ternary-operator

?: 56<br />

?: in style guidelines<br />

Some corporate programming guidelines list the use <strong>of</strong> the conditional operator as bad practice because it can harm<br />

readability and long-term maintainability. Conditional operators are widely used and can be useful in certain<br />

circumstances to avoid the use <strong>of</strong> an if statement, either because the extra verbiage would be too lengthy or because<br />

the syntactic context does not permit a statement. For example:<br />

#define MAX(a, b) (((a)>(b)) ? (a) : (b))<br />

or<br />

for (i = 0; i < MAX_PATTERNS; i++)<br />

c_patterns[i].ShowWindow(m_data.fOn[i] ? SW_SHOW : SW_HIDE);<br />

(The latter example uses the Micros<strong>of</strong>t Foundation Classes Framework for Win32.)<br />

When properly formatted, the conditional operator can be used to write simple and coherent case selectors. For<br />

example:<br />

vehicle = arg == 'B' ? bus :<br />

arg == 'A' ? airplane :<br />

arg == 'T' ? train :<br />

arg == 'C' ? car :<br />

arg == 'H' ? horse :<br />

feet;<br />

Appropriate use <strong>of</strong> the conditional operator in a variable assignment context reduces the probability <strong>of</strong> a bug from a<br />

faulty assignment as the assigned variable is stated just once as opposed to multiple times.<br />

See also<br />

• ?? Operator<br />

References<br />

[1] "BCPL Ternary operator (page 15)" (http://cm.bell-labs.com/cm/cs/who/dmr/bcpl.pdf). BCPL Reference Manual. .<br />

[2] http://www.python.org/doc/faq/programming/#is-there-an-equivalent-<strong>of</strong>-c-s-ternary-operator

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!