187
votes
Why do library developers deliberately break existing code?
A major version upgrade literally means they intend to break things. You shouldn't upgrade to a new major version unless you're prepared to deal with it. Most build systems have a way to specify you'...
125
votes
Accepted
What is so bad with goto when it's used for these obvious and relevant cases?
GOTO itself is not an immediate problem, it's the implicit state machines that people tend to implement with it. In your case, you want code that checks whether the IP address is in the list of ...
100
votes
Is there a general solution to the problem of "sudden unexpected bursts of errors" in software?
An improvement would be to design your system to fail gracefully. If the first step of parsing a file fails, then stop with an error. Don't carry on passing bad data from one step to the next.
The ...
77
votes
Is there a general solution to the problem of "sudden unexpected bursts of errors" in software?
There was a popular blog post on this topic last year called Parse, don't validate. It's an excellent read that's difficult to paraphrase, but the essence is you should put your input data into a ...
72
votes
Accepted
Is it good practice to echo PHP code into inline JS?
No, it's usually a bad practice.
The problem
Any time you echo something from PHP into JavaScript what's happening is that you're trying to generate valid JavaScript code. There is no generic ...
68
votes
Accepted
Is it common practice to validate responses from 3rd party APIs?
Absolutely. For starters, you never know that somebody hasn't hacked into your connection and the reply you receive doesn't come from the API at all.
And some time in the last two weeks I think ...
62
votes
Should I use DATE or VARCHAR in storing dates in MySQL?
I don't understand as to why the DATE data type exists
Always store Date values in Date Fields.
What PHP gives you back when you retrieve those values is a Character Representation of the Date value ...
55
votes
Why do library developers deliberately break existing code?
My point, and question, is: Why do library developers knowingly break existing code like this? Why not at least have it default to either true or false, whichever is the most reasonable?
Because ...
49
votes
Is it good practice to avoid warnings and notices?
If warnings and notices are coming from your code, most definitely fix it. From my experience, in 95% it may be benign, but the 5% highlight a real problem that may lead to countless hours spent ...
43
votes
Is it common practice to validate responses from 3rd party APIs?
Somebody else's API is your external interface. You shouldn't blindly trust anything that crosses that boundary. Your future debuggers will thank you for not propagating the other system's errors ...
40
votes
What is so bad with goto when it's used for these obvious and relevant cases?
There are some legitimate use cases for GOTO. For example for error handling and cleanup in C or for implementing some forms of state machines. But this is not one of these cases. The second example ...
34
votes
Exceptions in DDD
Let's begin this with a short review of the problem space: One of the fundamental principals of DDD is to place the business rules as closely as possible to the places where they need to be enforced. ...
34
votes
Is it good practice to echo PHP code into inline JS?
The real problem here is that you're providing a public API to query whether some arbitrary member has paid.
You can take that URL, change the member ID, and retrieve what should be private ...
29
votes
Accepted
Is it a good idea to define one big private function in a class to maintain valid state, that is, to update the object's data members?
All else being equal, you should express your invariants in code. In this case you have the invariant
$this->tax = $this->taxPC * 0.01 * $this->price;
To express this in your code, remove ...
26
votes
Accepted
Is it good practice to avoid warnings and notices?
if I turn on the warnings and notices on these live production websites, they'll be overloaded with them.
You should always have warnings turned on to the fullest level in development, testing, and ...
24
votes
Should I use DATE or VARCHAR in storing dates in MySQL?
In programming, we are always working with abstractions and representations:
The text string 2021-02-09 12:47:14 UTC is a representation of a particular point in time.
The integer 1612874834 is a ...
23
votes
Accepted
How to properly reverse the if statement when you have two conditions in it?
Build a truth table:
p q p && q p || q !(p && q) !p || !q !(p || q) !p && !q
==========================================================================
0 0 ...
23
votes
Clear way to skip the first element in an index based for loop
I hate both.
Who said you could use magic numbers? If you're going to start at an offset of 1 how about telling us WHY you're starting at an offset of 1. Adding an equally magic zero explains ...
23
votes
Should a trait refer to parent methods?
It's fine if a trait depends on methods in the class into which it is embedded. But these dependencies should be explicit, by declaring an abstract function. PHP can then check that such a method ...
20
votes
Is it bad practice to use <?= tag in PHP?
The PHP-Documentation clearly says that you can use short echo tags safely:
5.4.0 The tag <?= is always available regardless of the short_open_tag ini setting.
Although this is for PHP version 5....
19
votes
Composition over inheritance but
Composition over Inheritance means that when you want to re-use or extend functionality of an existing class, often it's more appropriate to create another class that will 'wrap' the existing class ...
19
votes
How to store a password so that it can be passed to another site/service which is expecting a plaintext input
You really don't want to be storing passwords on your server in a reversible manner, even with encryption - if your encryption key is compromised, all passwords will be exposed. OAuth is the standard ...
18
votes
What is so bad with goto when it's used for these obvious and relevant cases?
As others have said, the problem isn't with the goto itself; the problem is with how people use goto, and how it can make code harder to understand and maintain.
Assume the following snippet of ...
17
votes
How to think in Object Oriented way when it comes to passing messages?
In order to better understand Alan Kay's Messaging Metaphor, we have to look at a little bit of personal and historical context.
Alan Kay's Bachelor degree is in Mathematics and Microbiology, and ...
17
votes
Accepted
Default value variable, null vs empty string vs false
If you don't have a sensible value to initialize your variable with, then you should not be creating that variable at that point in your code.
If you are getting notices that you are using an ...
17
votes
Accepted
Can I compile PHP to hide the code?
Is there a way I can compile or obfuscate the PHP code somehow so that it can still run on the server but cannot be viewed/edited/modified like compiled software?
No.
In order for the code to run on ...
17
votes
Is it common practice to validate responses from 3rd party APIs?
Is your API-boundary also a trust-boundary?
As you are communicating with a remote system, that's nearly a certainty. Even if the remote system itself might be trusted, the medium might not be.
...
16
votes
Accepted
php CMS from scratch vs open source
You'd be crazy to write a CMS from scratch. My kind of crazy.
3rd party software is baggage.
We all depend on some amount of baggage; it's unavoidable (unless you are Charles H. Moore and write ...
16
votes
Is it good practice to echo PHP code into inline JS?
It's ok to do that, but you need to be careful of code injection. If the user can modify the content of the variable, they can inject arbitrary script or tags into the page, which can be used as an ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
php × 2046object-oriented × 260
mysql × 197
design-patterns × 189
web-development × 139
mvc × 139
javascript × 107
design × 96
database × 77
laravel × 71
web-applications × 68
architecture × 56
java × 50
frameworks × 49
object-oriented-design × 48
security × 47
unit-testing × 45
api × 45
html × 45
programming-practices × 41
dependency-injection × 41
python × 37
ajax × 37
rest × 35
sql × 35