There's a lot of stuff that can be done in PHP that triggers a notice, but is purely intentional. Depending on ones coding style, it's not that likely that a notice will represent a bug. It's definitely not the cleanest way, but oftentimes can save a lot of extra effort that would need to be spent to work around the notice being triggered. It comes down to preference in my opinion.because they represent likely bugs.
Agreeably a good way. That's where I want to get informed about them after all.(They are blocking in debug mode.)
Unfortunate.(So the short answer is that there isn't anyway built in to not log notices.)
I think that kind of coding style and ignoring such notices will make a developer complacent. Such notices are going to be caused by undefined variables (which could be a misspelling of a variable name, therefore a bug), caused by an undefined index (which could be a misspelling, or data not being where you expect it to be therefore a bug). What kind of E_NOTICE warnings would you be proposing aren't bugs and are safe to ignore?There's a lot of stuff that can be done in PHP that triggers a notice, but is purely intentional. Depending on ones coding style, it's not that likely that a notice will represent a bug. It's definitely not the cleanest way, but oftentimes can save a lot of extra effort that would need to be spent to work around the notice being triggered. It comes down to preference in my opinion.
I think that kind of coding style and ignoring such notices will make a developer complacent. Such notices are going to be caused by undefined variables (which could be a misspelling of a variable name, therefore a bug), caused by an undefined index (which could be a misspelling, or data not being where you expect it to be therefore a bug).
What kind of E_NOTICE warnings would you be proposing aren't bugs and are safe to ignore?
if($entity && $entity->Relation && $entity->Relation->offsetExists($offset) && $entity->Relation[$offset]->Relation2 && $entity->Relation[$offset]->Relation2->property)
if($entity->Relation[$offset]->Relation2->property)
if (!empty($entity->Relation[$offset]->Relation2->property))
{
// $entity->Relation[$offset]->Relation2->property has a non empty value
}
$offset
is undefined, though I definitely can't think of a valid excuse for undefined variables I am used to shorten if-statements, as that's how I've been taught to program PHP.
But you can simplify it
It may not be that critical for user facing code (other that potentially causing frustration for the user when something doesn't work), but for highly automated back-end code (eg payment processing, email sending, integration with other backend systems), it's kind of critical to have your code fail gracefully and identify bug-conditions for you because you have no other indicators that things aren't working.
In my opinion - it is exactly this approach to coding you describe which has given PHP such a bad name amongst professional software developers.
Your goal isn't to write shorter code - your goal is to write correct code.
That way of thinking is exactly why PHP has such a bad rep.I wasn't questioning that. However it comes down to the application. I do not have any payment processing in place, no other backend systems, etc. Nothing "important" to fail. If something is not working as it should, I turn on notice reporting again and then I can debug appropriately. For as long as that, I don't need it. It is a choice, and being able to make that choice is one of the possibilities that PHP offers and one of the choices that I put a lot of thoughts in.
Ironically, ignoring notices about undefined variables etc makes your code way less understandable.My goal is to write code that is easily understandable, so that I can come back to it after a few months or possibly longer and still be able to understand what's happening there. Unnecessary bloat will make this harder. It's not about having the shortest code possible, but about keeping it readable. A 5-line-if-statement is anything but understandable compared to a single-liner that does absolutely the same.
Ironically, ignoring notices about undefined variables etc makes your code way less understandable.
The ability to right click any given variable and hit "go to definition" to see what it is and what it does is incredibly valuable for readability.
Calling proper syntax "unnecessary bloat" is something I could not disagree with more.
My goal is to write code that is easily understandable, so that I can come back to it after a few months or possibly longer and still be able to understand what's happening there.
Unnecessary bloat will make this harder. It's not about having the shortest code possible, but about keeping it readable. A 5-line-if-statement is anything but understandable compared to a single-liner that does absolutely the same.
That's what comments are for.
Get in to the habit of commenting everything you do when you write it - will make debugging so much easier.
Actually, I find completely the opposite to be true - if you make strong use of PHP's coding quirks (shortcuts) to make your code more "brief", it can have the opposite effect of making the exact behaviour of the code unintuitive and give unexpected results in some circumstances.
I find being explicit (rather than relying on implicit rules) makes it much easier to work out exactly what is going to happen with the code - makes code more understandable and less likely to introduce bugs.
I know many developers who prefer your approach of brevity - but I have also found that in many professional development houses, they tend to introduce coding standards which explicitly disallow many of the shortcut conventions for exactly this reason - brevity for the sake of it can introduce unexpected bugs.
We use essential cookies to make this site work, and optional cookies to enhance your experience.