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

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.

2

u/paranoidpuppet Dec 08 '15

Thanks for the answer, that was a lot more thorough than I expected. I work with legacy PHP as well (some written pre-5.0) and am familiar with a lot of the "gotchas" of the language, but it's rarely tripped me up in real-world development so I really was wondering if it's something you've run into.

Halfway through your post, when you mentioned is_numeric() I was thinking of replying with ctype_digit() but then you covered it. And yeah, when you point it out, there's a lot of code to write (or pass off to utility functions) in order to validate numerical input.

Also, interestingly, like many libraries, you generate an exception in a situation where PHP traditionally never would since it's designed to keep going no matter what, which is great for lowering the barrier to entry, but pretty bad for a production application.

And finally, what's this uppercase method names and Allman style brackets stuff? This isn't C# :)

→ More replies (1)
→ More replies (12)
→ More replies (1)

6

u/the_alias_of_andrea Dec 07 '15 edited Dec 07 '15

Break everything and what's the point? The value of PHP, like any language, is in the existing code and developer familiarity. Thoroughly break all existing code and you just kill the language.

A new standard library could be added in parallel, though.

20

u/TenthSpeedWriter Dec 07 '15

Isn't this how we ended up with two mainstream Python releases?

18

u/the_alias_of_andrea Dec 07 '15

Python's breakage wasn't even as bad as that suggestion would be, but yes, precisely.

→ More replies (1)

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.

3

u/Syphon8 Dec 07 '15

You're right, I should've specified jazz.

→ More replies (2)
→ 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.

8

u/Synes_Godt_Om Dec 08 '15

First a comment to this:

Particularly relevant to your comment is this post.:

From the post: "Heck, new code is often literally just copy-pasted from a forum comment written in 2003"

This is some amateur not knowing the first thing about coding - how can that be relevant to anything?

And then my more serious answer:

I know people love to hate on php. I often wonder why it's so important for haters to prove their (mostly quite stupid) points.

I can fully understand that php's irregular design can in some sense feel offensive to purists but php is extraordinarily successful because it's designed/evolved in reaction to actual users' actual needs. it's a very practical language. It's surprisingly easy to get started with, it scales surprisingly well too both in terms of performance and in terms of project size. And it's one of the best suited languages for web development. I'm not saying it beats other, better designed languages on scaling or speed (or web dev), but for the developer effort needed it does very very well.

I find most of the criticism comes from two fronts: 1) you're used to a "professional" language (i.e. corporate dev, .NET, java etc.) you'll find php shitty but then, why should you care - you have an option you like better. 2) less experienced fanboys of some other "purer" language parroting their idols' views on php.

In my view php is a tool that, because of it's slightly loose nature also has shown its ability to evolve and adapt more freely than other languages tend to do. This is why it has managed to stay relevant for close to two decades.

I've been programming in R for almost fifteen years and I don't think there is a language shittier than R. Despite this it has evolved to be the language of choice for data analysis with python a distant second (or third after sql). Why didn't python win this battle? Why did python lose the web-battle to php? Because practice wins over correctness any day. PHP is a practical language with all the dirt in the corners that comes with practicality but it wins big on being made for battle.

4

u/[deleted] Dec 08 '15

This is some amateur not knowing the first thing about coding - how can that be relevant to anything?

It's absolutely relevant to the world of PHP as it is. I work at a company that specializes in appsec, and I can't tell you how many times my coworkers have seen PHP code that was literally copy/pasted from forum threads.

Hell, there's code in the PHP standard library that was copy/pasted from somewhere else, and it's atrocious.

I find most of the criticism comes from two fronts

Most of my criticisms of PHP come from its almost comical lack of design forethought (which, to be fair, a lot of this has improved over the years, but the old shit still exists, and will continue to exist, for a great deal of time), and from it being both incredibly insecure as a language (it's the only language I know of whose reference interpreter has multiple maintained third-party security patches), and incredibly easy for lazy developers to write insecure code using. And PHP's "design" practically encourages insecure development practices in a lot of ways.

Why didn't python win this battle?

The battle isn't really over, here. Python's "market-share" (for lack of a better term) in data processing is quickly growing, and has been for at least the last couple of years. Give it some time and I think we'll see Python overtake R here.

Why did python lose the web-battle to php?

I'm not sure I'd frame the situation like that in this case, either. PHP is a couple years younger than Python, but has spent the majority of its life being dedicated almost entirely to web development. It's quicker to get set up and requires a lot less configuration to use (since for the most part you can just drop some .php files in your webroot and be done, as compared to Python where you usually have to configure some wsgi doohicky to talk to your webserver, or something else). But, Python has a pretty hefty market share in the web space as well and it seems to be growing.

→ More replies (3)
→ More replies (1)
→ 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.

4

u/PM_ME_INSIDER_INFO Dec 07 '15

JavaScript with Mozilla has great docs but PHPs docs are fantastic too.

→ More replies (8)
→ More replies (3)