Repository: bitcoin/bitcoin
File: src/init.cpp (C++)

Code before:
        for (auto& index : node.indexes) {
            const IndexSummary& summary = index->GetSummary();
            if (summary.synced) continue;

            // Get the last common block between the index best block and the active chain
            const CBlockIndex* pindex = nullptr;
            {
                LOCK(::cs_main);
                pindex = chainman.m_blockman.LookupBlockIndex(summary.best_block_hash);
                if (!index_chain.Contains(pindex)) {
                    pindex = index_chain.FindFork(pindex);
                }
            }
            if (!pindex) {
                pindex = index_chain.Genesis();
            }

            if (index->CustomOptions().connect_undo_data) {
                if (undo_start && pindex->nHeight >= undo_start.value()->nHeight) continue;
                undo_start = pindex;
                undo_start_name = summary.name;
            } else {
                if (block_start && pindex->nHeight >= block_start.value()->nHeight) continue;
                block_start = pindex;
                block_start_name = summary.name;
            }
        };

        // Verify all blocks needed to sync to current tip are present including undo data.
        if (undo_start) {
            LOCK(::cs_main);
            if (!chainman.m_blockman.CheckBlockDataAvailability(*index_chain.Tip(), *Assert(undo_start.value()), BlockStatus{BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO})) {
                return InitError(Untranslated(strprintf("%s best block of the index goes beyond pruned data (including undo data). Please disable the index or reindex (which will download the whole blockchain again)", undo_start_name)));
            }
        }

        // Verify all blocks needed to sync to current tip are present unless we already checked all of them above.
        if (block_start && !(undo_start && undo_start.value()->nHeight <= block_start.value()->nHeight)) {
            LOCK(::cs_main);
            if (!chainman.m_blockman.CheckBlockDataAvailability(*index_chain.Tip(), *Assert(block_start.value()), BlockStatus{BLOCK_HAVE_DATA})) {
                return InitError(Untranslated(strprintf("%s best block of the index goes beyond pruned data. Please disable the index or reindex (which will download the whole blockchain again)", block_start_name)));
            }
        }
    }

    // Start threads
    for (auto index : node.indexes) if (!index->StartBackgroundSync()) return false;
    return true;
}


Review comment (suggestion):
I think you could write this if/else a bit more readable in the following way:

```c++
// Update starting point (if needed)
bool need_undo = index->CustomOptions().connect_undo_data;
auto& op_start_index = need_undo ? undo_start : block_start;
auto& name_index = need_undo ? undo_start_name : block_start_name;
            
if (op_start_index && pindex->nHeight >= (*op_start_index)->nHeight) continue;
op_start_index = pindex;
name_index = summary.name;
```

Code after:
        for (const auto& index : node.indexes) {
            const IndexSummary& summary = index->GetSummary();
            if (summary.synced) continue;

            // Get the last common block between the index best block and the active chain
            const CBlockIndex* pindex = nullptr;
            {
                LOCK(::cs_main);
                pindex = chainman.m_blockman.LookupBlockIndex(summary.best_block_hash);
                if (!index_chain.Contains(pindex)) {
                    pindex = index_chain.FindFork(pindex);
                }
            }
            if (!pindex) {
                pindex = index_chain.Genesis();
            }

            bool need_undo = index->CustomOptions().connect_undo_data;
            auto& op_start_index = need_undo ? undo_start : block_start;
            auto& name_index = need_undo ? undo_start_name : block_start_name;

            if (op_start_index && pindex->nHeight >= op_start_index.value()->nHeight) continue;
            op_start_index = pindex;
            name_index = summary.name;
        }

        // Verify all blocks needed to sync to current tip are present including undo data.
        if (undo_start) {
            LOCK(::cs_main);
            if (!chainman.m_blockman.CheckBlockDataAvailability(*index_chain.Tip(), *Assert(undo_start.value()), BlockStatus{BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO})) {
                return InitError(Untranslated(strprintf("%s best block of the index goes beyond pruned data (including undo data). Please disable the index or reindex (which will download the whole blockchain again)", undo_start_name)));
            }
        }

        // Verify all blocks needed to sync to current tip are present unless we already checked all of them above.
        if (block_start && !(undo_start && undo_start.value()->nHeight <= block_start.value()->nHeight)) {
            LOCK(::cs_main);
            if (!chainman.m_blockman.CheckBlockDataAvailability(*index_chain.Tip(), *Assert(block_start.value()), BlockStatus{BLOCK_HAVE_DATA})) {
                return InitError(Untranslated(strprintf("%s best block of the index goes beyond pruned data. Please disable the index or reindex (which will download the whole blockchain again)", block_start_name)));
            }
        }
    }

    // Start threads
    for (auto index : node.indexes) if (!index->StartBackgroundSync()) return false;
    return true;
}