So I think I'm missing some fundamental principle of software design. In an effort to build in better handling of errors and unexpected results, I keep finding myself burying my code in deeper and deeper sets of nested conditionals. Especially with cURL apps and scrapers where I commonly have 15 actions in a routine where each successive action should only be executed based on the previous action's successful outcome.
One work around I sometimes use when working within a function or method is to have multiple return paths, but I'd kind of like to avoid that.
A simplified example of what I mean:
And this could go on and on. I'm thinking there has to be a more elegant and logical way to approach this.
One work around I sometimes use when working within a function or method is to have multiple return paths, but I'd kind of like to avoid that.
A simplified example of what I mean:
Code:
$page = download_page($url1);
if ($page['SUCCESS'] == true)
{
$data = regex_functions($page['FILE']);
if ($data == $expectedData)
{
$page = download_page($url2);
if ($page['SUCCESS'])
{
$data = regex_functions($page['FILE']);
if ($data = $expectedData)
{
...
}
else
{
echo "Could not retriee expected data from page 2";
}
}
else
{
echo "Could not download url 2";
}
}
else
{
echo "Could not retrieve expected data from page 1";
}
}
else
{
echo "Could not download url 1";
}