Should I use E_NOTICE enabled on live server?
As I can see in Why should I fix E_NOTICE errors?
everybody is again with using isset()
to omit notices. I have read pros, but how about code readability?
Let's examine some code:
if ($this->_oldattributes['category_id'] != $tattr['category_id']
|| $this->_oldattributes['deal_id'] != $tattr['deal_id']
) {
Clear to understand what I want here, right?
Lets add isset()
to omit notices:
$old_cat_id = isset($this->_oldattributes['category_id']) ? $this->_oldattributes['category_id'] : null;
$new_cat_id = isset($tattr['category_id']) ? $tattr['category_id'] : null;
$old_deal_id = isset($this->_oldattributes['deal_id']) ? $this->_oldattributes['deal_id'] : null;
$new_deal_id = isset($tattr['deal_id']) ? $tattr['deal_id'] : null;
if ($old_cat_id != $new_cat_id || $old_deal_id != $new_deal_id) {
Not bad, but now I need more time to understand what is happening here.
A few bits about debugging - I never wasted a day to debug errors caused by not initialized variable. Yes, maybe half of a hour was wasted to slide down into the classes/functions but this not a big waste when comparing to losing code readability IMHO?
A few bits about performance - http://seanmonstar.com/post/909029460/php-error-suppression-performance
The difference was that suppressing the notice took 100% as long as just checking if it existed first" Test was performed on 1000000 supressions. How many of them appears in your code, even if you use Zend+Doctrine+something else? And how many time does it takes in comparison to DB connections, etc.?
Maybe I just not used to write correct code(used only PHP for long time now).
For context we have moved our project to server which displaying E_NOTICEs, that's why I'm asking this. I don't know why this is a big problem to switch them off?
Another example:
class Item extends CActiveRecord {
private $_oldattributes = array();
public function afterFind()
{
// Save old values
$this->_oldattributes = $this->getAttributes();
$this->_oldcategory = $this->category;
$this->_olddeal = $this->deal;
}
so I will have null if old attributes not exists and:
$tattr = $this->getAttributes();
$this->_oldattributes['category_id'] != $tattr['category_id']
which will give me what I need, and there are no errors, right?
Same with not an object error, in Yii when I have set up relations some objects may not be initialized, yes there is a kind of error, because have null value and not initialized in this case is real error sometimes:
$this->deal->item->id != $this->_olddeal->item->id
This may trigger a notice but will work as intended, so you ignore the notice:
if ($_GET['foo']) ...
The problem is that I can't ignore this, because it still throws notice and every time when i use this I must write:
if (isset($_GET['foo']) && $_GET['foo']) ...
or:
if (@$_GET['foo']) ...