-
Re: PM System PHP
Quote:
Originally Posted by
Wizkidje
Lol, that's your reliable source on the ever-popular "One Line If Statement"? Give me a break, that person can't even speak any normal languages correctly- let alone PHP. It's called the incorrectly implemented ternary statement. Not an "if statement" or forcibly one line. Plus, that person stated that they use the ternary for making code less clutterred. According to your source, you are using it incorrectly- as shoving all the code into one line makes code much less clear and more cluttered.
-
Re: PM System PHP
Nesting ternary operators results in horribly difficult to read code. They're one of the primary tools used in the IOCCC competitions for this reason. Instead of ifs they use nothing but ternary statements, since the resulting code is more terse but less readable. They also have the benefit of using 2 individual characters instead of 2 keywords "if" and "else" which can't be split up with whitespace to make fancy drawings. See: http://www.ioccc.org/2012/deckmyn/deckmyn.c
Therefore, a better practice is to consider ternary statements only to reduce unnecessarily verbose if/else blocks in a way that doesn't reduce readability. This generally happens when both the if and else blocks are present (and only an if/else, no else ifs) and both consist of one statement:
Code:
if (a) {
b;
} else {
c;
}
//====
a ? b : c;
Additionally, it tends to un-clutter if/else assignments very well, which is the most common use case I see for it:
Code:
var x;
if (a) {
x = b;
} else {
x = c;
}
//====
x = a ? b : c;
Anything else needs some justification, or should be left as if/else.