-
Notifications
You must be signed in to change notification settings - Fork 129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ternery operators #119
Comments
The same "short hand use" question applies to short hand array declarations |
I don't know if there is a blanket answer for ternery operators. Things I don't think our code sniffer should get into disallowing syntax that On Monday, August 24, 2015, photodude notifications@github.com wrote:
|
I agree there is no easy "blanket answer for ternery operators" If we take your example Thinking about the wording, I think phrasing it in the following way might work as a general best practices in the code style but not necessarily a sniffer item: The only thing I think the sniffer can do is allow or disallow multi-line Ternery operators. As long as we avoid nested or stacked Ternery operators I don't think multi-line Ternery operators are an issue. Generally I agree "our code sniffer should get into disallowing syntax that There is a 3rd party code sniffer project that could help with the PHP versioning https://github.com/wimg/PHPCompatibility |
My opinion is also that single line ternary's are fine. Multi-line ternary's are not in terms of code style |
I think we should enforce that ternary is ONLY allowed for single line operations. But let's limit this to the 2.0 branch since it's kind of a B/C breaking change in the spec. |
👍 |
Looks like the Pear standard allows for multiline Ternary and there is nothing existing in PHPCS that disallows multiline Ternary. (Squiz.WhiteSpace.OperatorSpacing kind of does, but I think it would mess up all multi-line operators) So we'll have to write a custom sniff to enforce that. |
So Squiz.WhiteSpace.OperatorSpacing will disallow multi-line ternary, but it also disallows all multiline operators like // Example usage of multi-line operators (this should still pass)
$test4 = $var1
+ $var2
- $var3
/ $var4
* $var6;
// Example usage for: Ternary Operator (this should pass)
$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];
// Example of usage for multiline Ternary Operator (these should both fail)
$a = $condition1 && $condition2
? $foo : $bar;
$b = $condition3 && $condition4
? $foo_man_this_is_too_long_what_should_i_do
: $bar; |
If only the second one passed I wouldn't be hugely upset... especially if it makes things easier for you? |
I'm not sure what it will take to write the custom sniff to completely disallow multi-line ternary or to only allow in the second format. I'm probably going to put this on the back burner until some of the other items in the PHPCS 2 branch can be sorted out. I'm still trying to figure out what happened in that PR which caused the recent reported phpcbf failure with |
I think something should be done in regards to a rule for these. but as I don't have the time or skills to address I will Suggest voting to close as a Stale issue. |
@mbabker raised a question in this PR About Ternery operators and allowing or disallowing multi-line use.
I think multi-line ternary operators could have a benefit, if they improve the readability of the code. I would definitely suggest disallowing nested or stacked ternary operators as those situations just create a mess for code readability and maintenance.
With that said; based on some things I have recently read, I question even allowing ternary operators. Or at least we should be strongly discouraging them.
In general I think if a code standard or code style guidelines achieve the following then they are probably something that should be considered for adoption.
So applying those questions to ternary operators:
I would also say that there are places where short hand code is discouraged or not allowed in the existing standard (PHP tags for instance). So this maybe another area where the short hand operators may not be the best choice, and should be disallowed or strongly discouraged.
The text was updated successfully, but these errors were encountered: