With many nested levels the code is harder to read and follow, even with proper Indentation.
INCORRECT
CORRECT
INCORRECT
Code
function do_stuff() {
// Things to do
if (is_writable($folder)) {
if ($fp = fopen($file_path,'w')) {
if ($stuff = get_some_stuff()) {
if (fwrite($fp,$stuff)) {
// Things to do
} else {
return FALSE;
}
} else {
return FALSE;
}
} else {
return FALSE;
}
} else {
return FALSE;
}
}
CORRECT
Code
function do_stuff() {
// Things to do
if (!is_writable($folder)) {
return FALSE;
}
if (!$fp = fopen($file_path,'w')) {
return FALSE;
}
if (!$stuff = get_some_stuff()) {
return FALSE;
}
if (fwrite($fp,$stuff)) {
// Things to do
} else {
return FALSE;
}
}