When laziness meets syntax.

From some point on, I found myself replacing

if (cond) {
	cmd1;
} else {
	cmd2;
}
with
(cond) ? cmd1 : cmd2;
which is not only way shorter but also way less readable. Granted, this if-construction always seems to need an else-branch. But wait. What is it doing? The ?-operator evaluates cond, then either returns the value of cmd1 or cmd2 (where cmd1/cmd2 are expected to be expressions). Which also leads the way to overcome two major issues in the use of ? as replacement for if (which is, I can only repeat myself, ONE FUCKING CHARACTER LONGER -- think of the space savings! Before you find the : and the 1...).
You want no else-branch? Replace it with a numeral:
(cond) ? cmd : 1;
But there's still an issue unsolved: What if you need to execute more than one command? The ?-construction expects an expression, not a command/block of commands. So you cannot go for
(cond) ? { cmd1; cmd2 } : 1;
... which is when you should think back to that one piece of C syntax you almost never use -- the comma!
(cond) ? cmd1, cmd2 : 1;
Now isn't that a progress in terms of readability, effictiveness, and comfort compared to
if (cond) {
	cmd1;
	cmd2;
}

Stichworte:


Impressum