View Issue Details

IDProjectCategoryView StatusLast Update
0005596Composr testing platform / automation goals[All Projects] General / Uncategorisedpublic2024-07-25 19:12
ReporterPatrick SchmalstigAssigned ToPatrick Schmalstig 
SeverityFeature-request 
Status closedResolutionwon't fix 
Summary0005596: Fix unnecessary "else if" nesting
DescriptionPSR-12 does not allow "else if", but this also includes uses like this:

if ($something) {
} else {
    if ($something_else) {
    }
}

This can (and MUST according to PSR-12) be merged into...

if ($something) {
} elseif ($something_else) {
}

Fix all of these occurrences, document it in coding standards, and try to get code quality checker to pick up on these if possible.
TagsRoadmap: Over the horizon, Roadmap: v11 partial implementation, Type: Standards compliance
Time estimation (hours)
Sponsorship open

Activities

Patrick Schmalstig

2024-02-07 03:06

administrator   ~0008293

Added v11 partial implementation... I'm gonna fix any cases I spot as I edit code.

Chris Graham

2024-02-10 19:46

administrator   ~0008306

I just checked PSR-12, and couldn't see anything about not nesting ifs if avoidable.

I'd advise against any kind of slavish interpretation of standards- if an if-else was structured like this then most likely it was for a reason.

Background...

For a set of different outcomes based on some value you'd use switch...

switch ($foo) {
    case 'a':
        something();
        break;
    case 'b':
        something_else();
        break;
}

But often different branching is more complex than just one variable, so we have elseif...

if ($a == 'whatever') {
    something();
} elseif ($b == 'foo') {
    something_else();
} else {
    blah();
}

But there are situations where things are computational equivalent to the above, but not semantically, e.g...

if ($a == 'whatever') {
     something();
} else {
    if ($b == 'foo') {
        something_else();
    } else {
        blah();
   }
}

It all comes down to intent. Are we providing a series of prioritized alternate branches, or does it just happen that one of those branches itself has some nested logic that COULD be folded as another branch but is logically separate enough to be coded as a subbranch.

For code to be readable, I think the semantics of the code are just as important as coding standards or syntax.

Patrick Schmalstig

2024-02-10 20:11

administrator   ~0008313

I've honestly never heard of separating semantics out in if statements by using nested ifs (outside of here).

I believe the if / elseif standard is better for the following reasons:
1. Readability can be clarified with an inline comment and ensuring variable names are clear and concise
2. It eliminates [at least] one additional point of computation (e.g. having to process both the else and the nested if instead of only the elseif)
3. The code looks cleaner because we don't have an unnecessary code indentation for the nested if.
4. The code complexity is reduced as well; nested ifs are considered complex and, for good reason (additional computation, reduced readability, added logical complexity), to be avoided whenever possible.
5. It's counter-intuitive to use nested ifs especially when we try avoiding using too many nested ifs to begin with by using ifs which return instead.

Chris Graham

2024-02-10 20:38

administrator   ~0008317

1 - That's a mitigation not a reason 1-level-of-nesting is itself better
2 - You'd never be able to measure that, it's not even a microoptimization, more of a nanooptimization ;). And, in theory PHP could optimize it away if it hasn't already.
3 - That's subjective.
4 - Well, the logical code complexity is pretty much identical, if all we are doing is shuffling in and out of one level of indentation. It's true multiple-levels of ifs generally correlates with additional complexity, but not in the cases where it would be practicable to remove said indentation. Readability and Computation already covered in prior points.
5 - Counter-intuitive seems a bit subjective too; to me it's more intuitive to separate out logic into trees, where each level of the tree is dealing with one decision domain. Actually you embedded a coding paradigm which itself is controversial in your justification here - the idea of a function having multiple returns. See https://en.m.wikipedia.org/wiki/Return_statement#Multiple_return_statements

All this said, I don't care that much, but I don't believe restructuring code is a very productive use of time given other considerations.

Patrick Schmalstig

2024-02-11 04:47

administrator   ~0008335

Further research concludes the following...

Generally, most developers I've researched agree that if / elseif is more readable than nested ifs.

However, I've found a big exception to the rule: if an if / elseif contains multiple elseifs, the readability and computational benefits go out the window, and arguably nested ifs are actually more efficient in that case.

At the end of the day though, this is indeed pedantic. Furthermore, there are a few conditions in which an if / elseif should not be used (which is probably enough to consider it not worthy of using as a standard):
1) What I mentioned above with having multiple elseifs
2) If the conditions themselves are complex or significantly semantically different from each other
3) If any code, even so much as setting the value on a variable, is shared between more than one if condition (avoiding repetition is generally more important than avoiding nested if statements)

Thus, I'm going to close the issue. I may use if / elseif still when applicable and none of the above conditions apply. But there are too many exceptions, and is too pedantic, to consider it a worthwhile standard.

Issue History

Date Modified Username Field Change
2024-02-07 03:03 Patrick Schmalstig New Issue
2024-02-07 03:03 Patrick Schmalstig Status non-assigned => assigned
2024-02-07 03:03 Patrick Schmalstig Assigned To => Patrick Schmalstig
2024-02-07 03:03 Patrick Schmalstig Tag Attached: Roadmap: v12
2024-02-07 03:03 Patrick Schmalstig Tag Attached: Type: Standards compliance
2024-02-07 03:04 Patrick Schmalstig Description Updated View Revisions
2024-02-07 03:05 Patrick Schmalstig Tag Attached: Roadmap: v11 partial implementation
2024-02-07 03:06 Patrick Schmalstig Note Added: 0008293
2024-02-10 19:46 Chris Graham Note Added: 0008306
2024-02-10 20:11 Patrick Schmalstig Note Added: 0008313
2024-02-10 20:38 Chris Graham Note Added: 0008317
2024-02-11 04:47 Patrick Schmalstig Status assigned => closed
2024-02-11 04:47 Patrick Schmalstig Resolution open => won't fix
2024-02-11 04:47 Patrick Schmalstig Note Added: 0008335
2024-03-26 00:58 Patrick Schmalstig Tag Renamed Roadmap: v12 => Roadmap: Over the horizon
2024-07-25 19:12 Chris Graham Project Composr => Composr testing platform / automation goals