Hot answers tagged

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'...
Karl Bielefeldt's user avatar
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 ...
wallenborn's user avatar
  • 1,980
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 ...
Simon B's user avatar
  • 9,571
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 ...
Karl Bielefeldt's user avatar
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 ...
VLAZ's user avatar
  • 788
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 ...
gnasher729's user avatar
  • 43.6k
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 ...
Phill  W.'s user avatar
  • 12.1k
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 ...
Flater's user avatar
  • 47.7k
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 ...
Alexander Pogrebnyak's user avatar
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 ...
Ross Patterson's user avatar
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 ...
JacquesB's user avatar
  • 57.8k
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. ...
user3347715's user avatar
  • 3,124
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 ...
user384771's user avatar
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 ...
eigensheep's user avatar
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 ...
Jörg W Mittag's user avatar
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 ...
IMSoP's user avatar
  • 5,752
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 ...
Vincent Savard's user avatar
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 ...
candied_orange's user avatar
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 ...
amon's user avatar
  • 134k
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....
Hexodus's user avatar
  • 301
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 ...
astreltsov's user avatar
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 ...
casablanca's user avatar
  • 4,934
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 ...
John Bode's user avatar
  • 10.8k
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 ...
Jörg W Mittag's user avatar
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 ...
Bart van Ingen Schenau's user avatar
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 ...
Jörg W Mittag's user avatar
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. ...
Deduplicator's user avatar
  • 8,776
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 ...
DaveGauer's user avatar
  • 1,287
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 ...
Lie Ryan's user avatar
  • 12.3k

Only top scored, non community-wiki answers of a minimum length are eligible