You often see code like this
function
checkPatron(age) {
if(age<drinkingAge) {
return false;
}
else
{
return true;
}
}
This
can be written more compactly with the conditional operator:
function
checkPatron(age) {
return
(age<drinkingAge) ? false : true;
}
Or better yet:
function
checkPatron(age) {
return
(age<drinkingAge);
}
Similarly, you often see this pattern in code written in JavaScript:
1
var a;
if(condn) {
a=1;
}
else {
a=2;
}
This is more compactly written using the conditional operator:
2
var a=(condn) ? 1 : 2 ;
The conditional operator is a ternary operation, taking a condn and two expressions.
If more than one expression/statement is needed to calculate '
a', function literals can be used:
3
var a=(condn) ?
function(){ return 1; }() :
function(){ return 2; }() ;
The conditional operator is underused, in fact many people advocate against it!
In fact it is a clearer statement on what is happening than the if/else construct:
1) there is 'a'
2) it's value depends on 'condn'
3) the alternate values follow
When '
a' is
only needed in one place, the conditional operator allows you to avoid
naming '
a':
4
getFeedback((condn)
?
function(){ return 1; }() :
function(){ return 2; }() );
Compare 4 to:
5
var a;
if(condn) {
a=1;
}
else {
a=2;
}
getFeedback(a)
or (legal, but not recommended):
6
var a
if(condn)
a=1
else
a=2
getFeedback(a)
Many people claim 5 is clearer, in spite of having to declare '
a', and assign it in 2 different places.
Use of the conditional operator might be clearer if one uses multiple lines and indentation:
7
getFeedback( // a
(condn)?
function(){ return 1; }()
:
function(){ return 2; }()
);
We are a society trained to read from left to right.
So '
?' appearing after '
(condn)' is unfortunate.
The use of a literal function to define a namespace or a block of code is commonplace.
With ColoredScript we can abbreviate this, as well as the '
return' keyword.
Logically everything is the same as before, but to our eyes
use of the conditional operator will appear clearer:
8
getFeedback( (condn)? {^1; } : {^2; } );
In MVC, the controller should provide the other views of the Model, which consists of logical operations.
As an aside, synatically listings 5 or 6 correspond closely to Forth;
in 'PigForth' listings 5 or 6 become
9
if(condn)
1
else
2
getFeedback
fini
Conditionals allow functional
programing
and when applicable are preferable to
if-else constructs.
When
a quantity is used in more than 1 place in a function
you can
enclose code with a function to avoid declaring a variable:
var area=calcArea(w,h);
...
cost=area*material*costPerSqFt;
...
room.volume=area*height;
...
return fn(cost,room.volume);
can be rewritten
return (function(area) {
...
cost=area*material*costPerSqFt;
...
room.volume=area*height;
...
return fn(cost,room.volume);
}
(calcArea(w,h)));
Note, we tend to read top to bottom, so
doing the first operation,
'
calcArea(w,h)', at the bottom is a problem.
Further,
'
calcArea(w,h)'
is widely separated from its corresponding argument name, 'area'.
And
any return statement gets wrapped in a function, necessitating an extra
return statement.