Returning/Throwing early

Returning early is very useful for programming as it reduces unnecessairly complex if statements and generally makes code more readable. It can even prevent some unwanted outcomes you may not even be aware of, since failing or already done code might not even reach that far.

For example this function in the dashboard:

/**
 * Checks if a winner exists for a given date, else roll winner and save it.
 *
 * @param string $date The given date
 * @return array|false Returns winner. Returns false incase winner couldn't be drawn.
 * @throws RandomException Incase random int fails
 */
public function checkOrSetWinner(string $date): array|false
{
    $winner = $this->table->getWinner($date);
 
    if ($winner) {
        return $winner; // If winner is already found in database it will be returned, not running the risk of being weirdly overwritten
    }
 
    $pastWinners = $this->preparePastWinnersList();
 
    do {
        $winner = $this->participants[random_int(0, count($this->participants) - 1)];
    } while (in_array($winner, $pastWinners, true));
 
    if ($this->table->saveWinner(['won_at' => $date, 'name' => $winner])) {
        return $this->table->getWinner($date); // If winner can be correctly gotten it will be returned
    }
 
    return false; // The default outcome if no winner can be returned
}

This also applies to exceptions, if you make a check and something isn't like it should be, you should throw the error as early as possible, so the error can be sent back as soon as possible, not like 5 more functions deep.