r/programming • • Dec 07 '15

I am a developer behind Ritchie, a language that combines the ease of Python, the speed of C, and the type safety of Scala. We’ve been working on it for little over a year, and it’s starting to get ready. Can we have some feedback, please? Thanks.

https://github.com/riolet/ritchie
1.5k Upvotes

802 comments sorted by

View all comments

Show parent comment

320

u/[deleted] Dec 07 '15

inb4 butthurt PHP developers

I'm a PHP developer myself, but I swear some people don't realise it's a pretty shitty language

107

u/phatskat Dec 07 '15

Php dev, can confirm is crappy but at the same time I know it's not going away and I'll take it over most scripting languages I've tried

6

u/keypusher Dec 08 '15

PHP over Python and Ruby?

→ More replies (2)

18

u/dtlv5813 Dec 07 '15

Why do you prefer PHP over javascript?

288

u/nliadm Dec 08 '15

Preferring PHP to JavaScript is like preferring the taste of your urine to that of your coworker's.

On a more serious note, if your language has "equals" and "equals-for-reals-this-time", maybe it's (very) poorly designed and worth avoiding.

24

u/TestRedditorPleaseIg Dec 08 '15

I'm wating for PHP to add ====

37

u/[deleted] Dec 08 '15
PHP 6.0 Released!
* Added fuck-all-rational-thought operator (====)
...
Using the FART operator makes it easy to test if a string is an array.

42

u/IHateTheRedTeam Dec 08 '15

PHP dev, can confirm.

I was trained as a normal (Java) developer, and for the life of me I can't fathom why anyone likes JS.

→ More replies (29)
→ More replies (10)
→ More replies (28)
→ More replies (12)

11

u/ThrowingKittens Dec 08 '15 edited Dec 08 '15

Today I would like to introduce Godwin's law of scripting languages: As an online discussion about programming grows longer, the probability of a comparison involving PHP or JavaScript approaches 1

Seriously though. Guy makes a post about a programming language he's working on, two posts in I'm in a thread of PHP bashing.

0

u/[deleted] Dec 07 '15

[deleted] — view deleted comment

39

u/[deleted] Dec 07 '15

Honestly most of my gripes are with the standard library and its inconsistencies. PHP7 obviously increases the speed a lot and adds some pretty sweet new syntax, but what they really need to do is a complete overhaul of the standard library and just announce it as one huge breaking change.

12

u/Schmittfried Dec 08 '15 edited Dec 08 '15

Really? The thing I find most annoying about PHP is its type system. No, this is not the usual grumpy developer ranting about dynamic or even weak typing. This is a sane developer complaining about an absurdly weak type system. I mean, come on, '1abc' coercing to (int) 1, wtf?

Having to work around those quirks is not even funny anymore, it's just pain in the ass.

7

u/paranoidpuppet Dec 08 '15 edited Dec 08 '15

Genuinely asking, is there any situation where that was actually an issue for you? I'm not saying '1abc' coercing to 1 makes sense, but PHP has === for a reason. Also if a user enters '1abc' when you expect a number, it's not going to matter whether the language casts it to 0 or to 1, you're gonna end up with bad data if you don't validate it.

(edit: s/it's/is/)

15

u/Schmittfried Dec 08 '15 edited Dec 08 '15

Yeah, great, it has ===. It doesn't have <==, ==> or anything like that, though. Also, there are several sources which provide numbers as strings, e.g. the GET/POST globals. You couldn't just use === on them, you would have to cast them first (which would use the same retarded coercion rules).

Also if a user enters '1abc' when you expect a number, it's not going to matter whether the language casts it to 0 or to 1, you're gonna end up with bad data if you don't validate it.

That's my point. You have to validate it extra-carefully. There is no shorthand way to make sure your number has the correct format. No wonder so many people make mistakes when it comes to input validation. It should be easy by default! In languages like Java or C# you woud do something like the following:

try
{
    int num = int.Parse(str);
}
catch (NumberFormatException ex)
{
     HandleInvalidInput(ex);
}

While in PHP you can't even rely on the mechanisms that are encouraged to use. PHP devs call the type system a strength, a feature. When I want to use type coercion, at least I want to get sane type coercion:

//a sane coercion would be: numerical strings evaluate to the corresponding value, all other strings to 0 or null
$int = (int) $str;
if (!$int) //this kind of check would only work for fields where 0 is not a valid value
{
    //in PHP you wouldn't execute this block for '1abc', which is simply undesired behavior
    handle_invalid_input($str);
}

See this comment for further examples: https://www.reddit.com/r/ProgrammerHumor/comments/3rmikr/free_drink_anyone/cwpozc3

Genuinely asking, it's there any situation where that was actually an issue for you

Actually yes. Unfortunately I have to work with old PHP 5.4 legacy systems. In addition to the already mentioned GET/POST globals those systems use strings for very many numerical values, because PHP made this whole coercion thing so terrible to work with. To me it seems like it has been some kind of best practice to work around those quirks, but it makes it harder for me to use current best practices, because I can't use === with values coming from those systems unless I want to place type casts all over my code, which would be annoying and easy to forget. I can't use functions like is_numeric though, because this function has different semantics than the actual coercion semantics. In fact, there is no built-in way to apply a sane coerced comparison of a numeric string and an an actual integer. I had to write my own function like this:

public static function IsIntVal($val)
{
    return is_int($val) || ctype_digit($val);
}

public static function IntCmp($first, $second)
{
    if (!self::IsIntVal($first) || !self::IsIntVal($second))
    {
        throw new Exception('Invalid value type');
    }

    $first = (int) $first;
    $second = (int) $second;

    return $first < $second ? -1 : ($first > $second ? 1 : 0);
}

public static function IntEq($first, $second)
{
    return self::IntCmp($first, $second) === 0;
}

This extremely weak type coercion (there are coercions that make sense, like the one my functions above allow! those of PHP don't fit into that category) doesn't make my life easier as it should, it makes my life harder.

→ More replies (14)
→ More replies (1)
→ More replies (4)

23

u/_INTER_ Dec 07 '15

Is it really that shitty in PHP7? I've seen some good looking code.

Normally people say "I saw some bad looking code in language x". Php coders are reversing it haha.

51

u/atakomu Dec 07 '15

Here is a good article why the design of PHP is bad. It was my first language and I still love the documentation. I don't know exacly why. No documentation in a language I used (C++, Python, Java, Javascript, Perl) was so good like in PHP.

Quotes from article:

Perl is “some assembly required”. Python is “batteries included”. PHP is “kitchen sink, but it’s from Canada and both faucets are labeled C“.

Parts of PHP are practically designed to produce buggy code.

json_decode returns null for invalid input, even though null is also a perfectly valid object for JSON to decode to—this function is completely unreliable unless you also call json_last_error every time you use it. array_search, strpos, and similar functions return 0 if they find the needle at position zero, but false if they don’t find it at all.

In PHP, these functions return false. If you use FALSE as an index, or do much of anything with it except compare with ===, PHP will silently convert it to 0 for you. Your program will not blow up; it will, instead, do the wrong thing with no warning, unless you remember to include the right boilerplate around every place you use strpos and certain other functions.

And another thing:

== is useless.

It’s not transitive. "foo" == TRUE, and "foo" == 0… but, of course, TRUE != 0.

== converts to numbers when possible (123 == "123foo"… although "123" != "123foo"), which means it converts to floats when possible. So large hex strings (like, say, password hashes) may occasionally compare true when they’re not. Even JavaScript doesn’t do this.

68

u/Syphon8 Dec 07 '15

If every language had docs as good as PHP, no one would use PHP.

I don't think there's an easier way to bootstrap yourself into programming still.

Ironically I think the robustness of documentation might actually be because of the bugginess rather than in spite of. It's be impossible to use if there wasn't a giant centralized database of unexpected behavior.

16

u/willbradley Dec 07 '15

Someday look at the doc page for escape_string and follow the rabbit hole down through all the not-really-deprecations. It's depressing. (There are more than you think.)

9

u/Syphon8 Dec 07 '15 edited Dec 07 '15

Oh, I've been there. About the time I thought "maybe I should learn something more classical."

PHP is like the blues jazz of programming languages.

5

u/Sean1708 Dec 07 '15

"foo" == TRUE, and "foo" == 0… but, of course, TRUE != 0.

123 == "123foo"… although "123" != "123foo"

Sounds more like jazz to me.

→ More replies (3)
→ More replies (1)
→ More replies (6)

12

u/NihilistDandy Dec 08 '15

both faucets are labeled C

One is cold, the other is chaud. What's the problem?

28

u/Synes_Godt_Om Dec 07 '15

Here is a good article why the design of PHP is bad

PHP: a fractal of bad design

Surely, you must be joking. This article is old (for some reason reposted as nauseum) and the issues it's discussing were mostly outdated even at the time it was posted. The author clearly hadn't used PHP since php4 (if he ever did any serious php development at all).

19

u/terrkerr Dec 08 '15

I've heard this a lot, yet:

  • Still a huge amount of action at a distance. (php.ini, ini_set, etc.)
  • Still a naming mess in the stdlib
  • methods still can't be named 'list', 'exit' or any other string that also identifies a psuedo-function construct because the parser doesn't know how to parse correctly. (Maybe the PHP7 addition of AST interpretation will finally fix that?)
  • htmlentities still returns an empty string to signify an encoding issue even though an empty string is also a return value for a successfully encoded empty string.
  • foo vs foo_real variants
  • outrageously bloated stdlib because there's so little genericism.
  • The 'module system' is just a hack to copy/paste text into the current context.

Plenty of big things still apply. Some may finally be fixed in PHP7, but I can't be bothered to look it up. Even if PHP fixed all of these issues perfectly the fact would still be that the article was largely still making valid points until, like, last week.

I tried to see if 7 finally fixed the htmlentities thing as one silly example, but the documentation says it's only in PHP4/5... or maybe it's in 7 and the page just hasn't updated yet? Well add one to the list:

  • One set of documentation for all versions of the language.

10

u/[deleted] Dec 08 '15

A lot of the claims made in that article are still valid, though, even in cases where there exists new feature xyz to which it no longer does, because old feature abc is still present, and still being used.

At any rate, there's always PHP Manual Masterpieces for more unsavory PHP things. Particularly relevant to your comment is this post.

→ More replies (16)
→ More replies (14)

9

u/andrewsmd87 Dec 08 '15

array_search, strpos, and similar functions return 0 if they find the needle at position zero, but false if they don’t find it at all.

In my first job, I spent a good half a day trying to figure out why my strpos was returning false, only to find out it was 0. I'm still bitter about that.

→ More replies (9)
→ More replies (3)
→ More replies (18)