3 ms·
Truthiness (e.g. 0 == false, '0' == true, 'false' == true, etc.) makes it easier to introduce bugs. There's a lot of discipline/linting still required in PHP, w
by smt88 9y ago
Truthiness (e.g. 0 == false, '0' == true, 'false' == true, etc.) makes it easier to introduce bugs. There's a lot of discipline/linting still required in PHP, whereas other languages won't even compile if you do something risky.
I wouldn't take that criticism too personally. People also argue that C/C++ can't ever be secure due to the ease of programmer mistakes.
- CiPHPerCoder 9y ago> Truthiness (e.g. 0 == false, '0' == true, 'false' == true, etc.) makes it easier to introduce bugs. Use === instead; except for cryptography, where you want hash_equals(). > There's a lot of discipline/linting still required in PHP, whereas other languages won't even compile if you do something risky. And/or just use scalar type declarations: https://3v4l.org/SJKe1 https://3v4l.org/SJKe1
- bpicolo 9y agoScalar type declarations don't help detect usage of undefined variables, as an example. Phan is the only tool I've found that does, but it's a very heavyweight thing, given how necessary it is to prevent undefined variable exceptions.
- CiPHPerCoder 9y agoThat's a separate concern from truthiness. And if you aren't using nullable types (a 7.1+ feature), passing NULL gives you a TypeError. :)
- muglug 9y agoI wrote a similar tool that's slightly less heavyweight (doesn't rely on a separate extension): https://getpsalm.org/ https://getpsalm.org/
- CiPHPerCoder 9y agoYes, Psalm is great. We use it in a lot of our projects, and in the future will be using it in all of them. We absolutely love it. (It's on my "to blog about" queue.)
- bpicolo 9y agoI'm going to be trying it out. Thanks. The more lints the better when dealing with legacy monoliths!
- smt88 9y agoI agree that there are safe (or safer) ways to do things in PHP, but in business settings, it's often hard to enforce such things. "Discipline" is very hard to maintain consistently across people with different backgrounds. I've actually gone as far as rejecting commits that failed the linter and style checker, but that only works for code bases I have control over. It doesn't apply to the libs. I now prefer languages where the entire ecosystem is passing strict compilation requirements.
- masklinn 9y ago> Truthiness (e.g. 0 == false, '0' == true, 'false' == true, etc.) That's not truthiness, that's implicit coercions. Truthiness is specific to boolean contexts (if, while, explicit conversion to boolean).
- smt88 9y agoI guess that's an interesting nitpick, but I was referring to truthiness. I chose what I now see was a confusing way to express it as text. What I really meant was that you have some $variable, and you test it in an if, while, etc. context. If the value is 0, it's false. But if you didn't realize you got a string from somewhere, it might be true. Unless it's an empty string? (I can't even remember.) None of this changes the initial point, which is that writing "good" PHP requires enormous amounts of discipline, and that still doesn't address the security issues with PHP itself. I remember when the developer of Suhosin basically quit PHP and said he couldn't deal with the way its maintainers treated security issues.