Skip to content

Instantly share code, notes, and snippets.

@manuakasam
Created March 31, 2015 12:54
Show Gist options
  • Save manuakasam/e317f10b6bbf5ecefd10 to your computer and use it in GitHub Desktop.
Save manuakasam/e317f10b6bbf5ecefd10 to your computer and use it in GitHub Desktop.
Is it really this bad to use goto in a switch statement? Imo this is much more user-friendly as far as readability AND code-maintenance is concerned.
<?php
$complexArray = [
//... lots of stuff
'condition' => 'unknown'
];
switch ($someThingElse) {
case 'foo':
default:
$complexArray['condition'] = 'foo';
break;
case 'bar':
if ($otherCondition === false) {
$complexArray = addCondition($complexArray, 'bar');
break;
}
$complexArray['condition'] = 'bar';
break;
}
function addCondition($baseArray, $condition) {
$baseArray['condition'] = $condition;
return $baseArray;
}
$service->call($complexArray);
<?php
$complexArray = [
//... lots of stuff
'condition' => 'unknown'
];
switch ($someThingElse) {
case 'foo':
default:
foo:
$complexArray['condition'] = 'foo';
break;
case 'bar':
if ($otherCondition === false) {
goto foo;
}
$complexArray['condition'] = 'bar';
break;
}
$service->call($complexArray);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment