Type - 1
if($data) {
set_option('name', 'programming');
set_option('age', '60');
set_option('creater', 'Ada');
}
Type - 2
if(!$data) return false
set_option('name', 'programming');
set_option('age', '60');
set_option('creater', 'Ada');
I prefer type-2 which avoid lots of nested if conditions and saves lots of tabs/spaces
As far as readability goes, the early-return style is the better approach. Just add a blank line for better separation. I additionally like to keep things consistent, so I also always add curly brackets to ifs and a space after the keyword, but that's purely my taste :)
Talking performance, in your case, the early return might safe further if branches later on, so it might be more performant. If you have to use an else-branch, always try to put the more common case into the truthy statement, as most interpreters and compilers optimize for that.
// Example:
function doSth(a,b) {
if (typeof b !== 'number') {
return 0;
}
// the following branch is not evaluated if b is not a number
if (a > b) { // <- likely branch for app-specific use-case
return a * b;
}
else {
return a;
}
}
if (! $data)
return;
set_option('name', 'programming');
set_option('age', '60');
set_option('creater', 'Ada');
I prefer Type - 1.
I think it is more readable and the tab/space problem is solved be the editor.
Like Sandeep Panda said, returning early will get invalid cases out of the way in the beginning of the execusion. It's a pattern that I follow constantly.
I would choose the second type, but with one small correction:
if(!$data) return false set_option('name', 'programming'); set_option('age', '60'); set_option('creater', 'Ada');Adding meaningful blank line will increase the code's readability. It will better distinguish the different logical parts of the code block.