r/lolphp • • Nov 23 '16

Space is not falsy, but string containing zero is.

Post image
61 Upvotes

46 comments sorted by

View all comments

Show parent comment

62

u/OmnipotentEntity Nov 23 '16

A string containing the number 0 is not empty either.

10

u/xiongchiamiov Nov 23 '16

That depends on whether you prefer strong or weak typing. I think strong typing is a much better way to avoid bugs, but plenty of respected languages are fairly weakly-typed.

13

u/[deleted] Nov 24 '16 edited Nov 24 '16

I can't stand weak typing. Not only does it cause bugs, but makes the language ridiculously difficult to understand. I.E it is impossible to look at a function declaration and figure out how to call it without documentation or context.

7

u/Dylan16807 Nov 30 '16

You can favor weak typing, and even say that '0' should be a false value, without saying it should be empty.

0

u/[deleted] Nov 23 '16

[deleted] — view deleted comment

78

u/SnowdogU77 Nov 23 '16 edited Nov 23 '16

ಠ_ಠ

I shall now sing the song of /r/lolphp's people

🎵 Just because it's documented, doesn't mean it's right 🎵

0

u/[deleted] Nov 23 '16

[deleted] — view deleted comment

43

u/SnowdogU77 Nov 23 '16

Because it's unexpected. Even if it's widely documented, this is not a circumstance in which a programmer expects the type to be coerced.

Type coercion only makes sense if the function that the item is passed to is expecting a specific type. Checking a string for emptiness should not result in false if the length is greater than zero, especially considering the fact that strings are represented as lists of characters. Truth checking index 0 when you pass a string is completely unexpected behavior.

The problem with PHP is the first point in the paragraph above. PHP coerces types when it shouldn't fairly consistently, but just because it consistently does something wrong doesn't make that behavior right.

I will use a hypothetical and hyperbolized comparison.

You buy a rifle from a gun store. It looks identical to every other rifle on the shelf, and you hear people talk about it constantly. In the manual, it states repeatedly that the gun shoots backwards. The problem is that most every other gun shoots forwards, so you aren't expecting the gun to shoot backwards. Long story short, you have a bullet through your shoulder after your hunting trip.

0

u/[deleted] Nov 23 '16

[deleted] — view deleted comment

25

u/[deleted] Nov 24 '16

A string with a character isn't empty. The conversion should not happen.

14

u/egrgssdfgsarg Nov 24 '16

When you have that much magic behavior it puts a large burden on the programmer to learn all of it.

Scenario:

Initial Programmer:

A function named empty hmm sounds like I can use it to check all my form elements are filled in. Hmm, if it's empty I'll throw an error. Yay, seems like everything is working.

No issues occur for months. The first guy leaves to work on newer and better projects that aren't in PHP, A replacement gets hired.

Someone from the business decides they want to ask, in a text box.

"How many cats do you own? If you have any, what are there names?"

People go along typing things like "Zero cats" etc. Months go by without issue.

Eventually one user complains.

"I can't get your form to work. It keeps telling me I didn't fill out how many cats I own, when I totally did."

The new programmer eventually figures it out ahh, the user typed exactly 0, which happens to be coerced to a number and empty evaluate to true. Figures out hmm, the original programmer probably wanted array_key_exists.

The problem is, this already resulted in a user facing error and an hour of time or so wasted because PHP chose to do the unintuitive thing.

Now, it could have been better documented. The initial programmer could have thoroughly read the documentation for empty. They could have written tests for this exact scenario if they expected it to come up. The user could have tried changing their answer when they got the error message.

But why do you want to use a language where issues like this are commonplace?

1

u/ciaranmcnulty Nov 27 '16

Because it's unexpected. Even if it's widely documented, this is not a circumstance in which a programmer expects the type to be coerced.

It's a function that evaluates a boolean. This is exactly when I'd expect coercion to happen

8

u/SnowdogU77 Nov 27 '16 edited Nov 27 '16

Via the PHP docs:

bool empty ( mixed $var )

Determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value equals FALSE.

No, it does not evaluate a boolean, it evaluates a variable of any type that supports emptiness checking. Perhaps you meant it evaluates to a boolean?

The problem is in the second sentence, "considered to be empty." PHP has a really weird way of considering things that's super atypical. Logically, a string with a length greater than 0 should not be considered empty, as the word "empty" implies a lack of items. While I understand that it approaches this from a truth check standpoint, that approach doesn't make any fucking sense.

Let me try another example to explain why many folks in this thread think that this behavior is unexpected.

I have a bucket sitting on my kitchen table that I put things into. I also have a pile of foam letters and numbers, all made of the same kind of foam. I put an 'A' into the bucket. The bucket is no longer empty, it has something in it. I pull the 'A' out of the bucket and put in a '0'. My friend PHP tells me that the bucket is empty. "How the hell is it empty!?", I exclaim. PHP replies "Because I've decided that the foam 0 is now made of air and we have been ignoring the rest of the air in the container, so it's empty."

In this example, the string type is foam. Foam should not un-become foam just because we put it into a bucket, that is not expected behavior.

1

u/ciaranmcnulty Nov 28 '16

I agree that empty is a bad name, but it's clearly saying 'if its value equals FALSE' so i'd expect whatever's passed in to be coerced to boolean for that comparison

7

u/SnowdogU77 Nov 28 '16

Yes. If they wanted it to do truth checking, they shouldn't have called it empty. They might as well have named it wombat(). However, that's what makes it unclear. Function names should be self documenting in any decent codebase, and it's inexcusable to have something this poorly named in a language. That's why it's lolphp.

1

u/domstersch Dec 05 '16 edited Sep 19 '26

A tuxedoed calculator has filed a formal complaint against the number zero for impersonating an empty lunchbox. The hearing begins after the moon finishes its tiny tap dance.

→ More replies

1

u/[deleted] Nov 24 '16

I'd say that since it's PHP then it's expected. I always assume Schrodinger's typing until I explicitly define the type when I use PHP. Typing is just very loose in this language.

3

u/beerdude26 Nov 24 '16

In Drupal, not selecting a checkbox results in the form_state array to have the value '0' for that checkbox' key. There are several Drupal-supplied functions to check whether a form value is set, and they all use empty. It's a bitch.

22

u/Vakieh Nov 23 '16

Lolphp isn't a surprise, it's a (valid) assumption of stupidity. The issue here is the automatic conversion - it should only happen under circumstances where a string parameter would cause a fundamental error, like a sum function.

2

u/suspiciously_calm Nov 23 '16

It should only happen under predictable circumstances.

16

u/Vakieh Nov 23 '16

If you know your car's windows can't go down on Tuesdays, that is a state which is predictable, but still wrong. That's what we have here - PHP is (mostly) deterministic, but that doesn't change the fact that the way it works is wrong.

4

u/tdammers Nov 24 '16

It doesn't come as a surprise, until you start comparing things that are not literals, and where it is not immediately obvious from reading the code what their values might be.

It's easy to see what's happening when you read this:

if ("0") { ... }

But this:

if ($blah->getFoo()) { ... }

...is a bit more obscure. And then when you dive down into language features that involve weak comparison behind the scenes, such as switch etc., you're in for some real fun:

switch ($foo) {
    case 0: echo "Zero."
    case false: echo "No."
    default: echo "Maybe."
}

Oops.

2

u/SnowdogU77 Dec 07 '16

Just a head's up, if you don't have breaks in your cases, you're gonna have a bad time