Remove unnecessary "else" in the code

Created on 11 February 2023, over 1 year ago
Updated 12 February 2023, over 1 year ago

Problem/Motivation

Inside of hook_theme_suggestions_HOOK_alter there is this code:

if (is_numeric($index)) {
  array_splice($suggestions_rev, $index, 0, $block_type_suggestions);
  $suggestions = array_reverse($suggestions_rev);
}
// If block__block_content isn't present as a suggestion.
else {
  $suggestions_rev = array_merge($suggestions_rev, $block_type_suggestions);
  $suggestions = array_reverse($suggestions_rev);
}

Which works fine. But I was reading a recommendation to avoid using else if possible to have the code more easy to read. As we can see on this article: https://levelup.gitconnected.com/how-you-can-avoid-using-else-in-your-co...

So on this article we can use "early-return" to improve that. Example:
From:

To:

Proposed resolution

We can implement the early return and remove the unnecessary else on this case

Remaining tasks

User interface changes

API changes

Data model changes

✨ Feature request
Status

Fixed

Version

1.4

Component

Code

Created by

πŸ‡§πŸ‡·Brazil RenatoG Campinas

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.69.0 2024