Skip to content

u/oefd

In this archive from Sep 2018 to Jul 2022

0
posts
56
comments
0
media
221
score

Top comments

I'm going to start with what I think is the most important thing I have to say about this article:

Programmers are an opinionated lot. We’re arrogant. Get over it. We are.

...

Call me whatever nasty names you like, but it’s my goddamned code and I’ll write it the way I want and the rest of you PSR purists can go to hell. (See what I mean by opinionated?)

This article mentions, but does less than nothing to fix the arrogant and needlessly violent way of expressing opinion endemic to the field. Lampshading your arrogant and haughty to prove you're self-aware when you're doing it doesn't make it somehow cool now. That doesn't excuse the behaviour.

As for the actual content of the article:

It's been some time since I was last a professional PHP dev, but I keep up enough with things that, for example, I read /r/php. I'm in a similar boat with python - keep up, but it's not what I'm paid to do now and hasn't been what I'm paid to do for some time. From my time in both worlds: I don't get this article. The majority of python programmers never think about PHP because the majority of python programmers aren't web devs. I think most PHP programmers also don't spend much time about python except maybe if it's the language some utility they're using it written or whatever.

This article takes a really high-horse attitude about how all the haters are just following their religion and trying to be cool, then says of the criticisms of PHP:

PHP has too many functions. (Yea, that’s a thing with some people, it’s too convenient and does too much.) The function names are inconsistent. (Yea, that happens when a language is truly democratized as opposed to being written by one guy trying to earn their Ph.D in CompSci.)

Which is in the former example is a misunderstanding of why people that criticize PHP's global namespace function count do so, and in the later example is making some kind of appeal about how PHP is 'democratized' by comparing it (utterly incorrectly) to how other languages, including python, are developed.

Additionally the article is extremely web-focused even though web dev is just one field amongst many:

Interestingly enough, Python has been around since 1991, before PHP ever saw the light of day. But despite having an even longer runway, it failed to achieve the same level of adoption that PHP now enjoys.

Python has way more adoption than PHP has and likely ever will in a number of areas. The same is true in reverse - it's very unlikely python's ever going to unseat PHP in the arenas where PHP is currently more prominent. If you want to talk about web dev fine, but implying that PHP is "killing" python by looking at web dev is very tunnel-visioned.

You want to defend PHP against unfair or even fair criticisms of it? Cool. It's not hard. This article isn't that though.

78 points

Moving along, there isn't types for functions or variables.

Python actually has pretty decent gradual-typing with type hints, mypy and the built-in typing library. You can even express a generics. Check this example out

I worked for a few years on a team writing test harnesses for products and it was all done in python. As some of the test harness code got bigger and more commonly used the previous benefit of python's duck-typing started turning into a liability, but using the typing support on the core parts with mypy built in to the pipeline enforcing correct usage of core components, while still allowing duck-typing for rapid iteration on parts that were less critical or not even guaranteed to be kept around, was pretty great.

Writing code like

def expect_int(x: int) -> int:
    assert isinstance(x, int)
    return x + 1

is actually rather discouraged in the python world: duck-typing is a feature, not a bug. (Yes, of course that's not always true. I mostly program in statically, strongly typed languages and I understand how much better that can be for big serious projects that want long term stability and maintainability, but that doesn't mean it's objectively superior in all cases.)

If you write code expecting an int it's good practice to indicate as much (as we did in that snippet) but if the caller knows that WhateverClass will act like an int for the purposes of that function why gate them from use? If you wanted an extremely strict language for your current use-case you wouldn't be using python, presumably the flexibility and dynamism is why you're using it.

To start, there isn't really a public, private, or protected attribution within class... Python kinda just says "its all open!!! Modify as you like", which I think creates a lot of potential problems.

It sort of does yet really doesn't, and it goes with what I said above about the double-edged sword of trusting the caller to know what they're doing and letting them do what they want while still clearly indicating how you wrote the code.

It's pretty universally accepted in the world of python that a leading underscore attribute/method is not part of the public api, and there are zero guarantees that it'll remain the same through versions, and zero guarantees that you can use them without somehow putting the object into a bad state.

If you want to use it anyway: you're accepting the responsibility the moment you typed some_obj._private_attr

private and protected have no security implications, they're purely a way to indicate to the consuming programmer that something isn't for their use and asking the compiler/runtime to disallow the consuming programmer from trying to use it at all. Python figured it'd cut that last part: it's up to the programmer to consume responsibly.

Maybe you don't like that attitude - and I understand that entirely, I happen to like some languages that are much more strict than PHP is - but it's not objectively true that one is better than the other.

I am new to this topic and do not have enough knowledge to dive into yet, but from my understanding Python is not thread-safe.

Python is entirely thread-safe, but it achieves this through the GIL. In essence: The CPython interpreter (the one basically everyone uses) is perfectly capable of multi-threading for the purposes of concurrency, but not for parallelism. It can only really use one core at a time even though it can spawn and work with multiple threads.

That said it's not hard to spawn multiple python interpreters, one per core, and use IPC to coordinate them as one system... but even then, it's not like in the world of the web you need to worry about that: that's a problem for the application server, not you. (And the application server has dealt with it - it's not hard to get uwsgi systems running in a comparable manner to PHP-FPM with a worker pool that can use all cores on the server.)

14 points

Depends on the framework / set up.

In general (PHP or otherwise) you don't actually want everything to die at the end of a request cycle, you want everything contextual to that request handling to die.

Restaurants don't kill and replace the chef in between each meal getting cooked up, and for good reason. It's not only illegal, it's also painfully inefficient. You're better off with a system that makes it easy or enforces that some things (like a thread pool for the database) exists persistently, whereas request-specific things should be tied to the request life cycle.

PHP and other programming languages all have means of running that way. They also all have means of being run CGI style and doing only one-offs.

11 points

I think you overestimate the cost of array operations, and vastly underestimate the costs of working with threads. Any system that uses threads exclusively because it's hoping to achieve multi-core parallelism and have multiple CPUs running code (as opposed to something like Apache*) is a system that's taking on a lot of overhead in the hopes that the amount of time it takes to run the work + overhead on a few CPUs is greater than the amount of time it takes to run just the work on one CPU.

Basically threading (on CPU bound tasks) is only ever worth it if you're breaking down a big set of tasks that need >100% of the CPU time to run into smaller chunks that lets you end up with fewer or no tasks that need more CPU time than they're able to get. As long as the actual work wouldn't end up needing all 100% of the CPU time on one CPU you should only use one CPU because then you don't also pay the costs of all the overhead of multi-threading the work.

In PHP what is it someone does that needs anywhere close to 100% of the CPU time? Hopefully nothing - the CPU bound tasks common in the PHP world are usually relatively small things. Resolving a template into an actual response, sorting reasonably sized arrays or scanning them, etc. should all take a tiny bit of CPU time compared to what the CPU has available.

Things that are CPU bound and also 'big' tasks that need a lot of CPU time is generally better handled elsewhere by systems meant to do heavy data processing or the like that can make their own reasonable decision about how to implement the solution.

*footnote: Apache is an example of using threads not exclusively in order to get multiple CPUs able to run code in parallel, it also uses threads because it allows a request's handler to be put to sleep and not need any CPU time when the request is waiting for the network to finish sending the request. Apache can be used on only a single CPU using this architecture and work just fine if you're talking about serving a website that doesn't get that many requests.**

**footnote to the footnote: It's also true that Apache's threading model for handling requests isn't the most performant due to the cost of creating new threads regularly and the OS having to manage scheduling so many threads. Event loops combined with modern OS features like epoll/kqueue mean that you can get away with few to no threads and get great performance.

10 points

Subreddits

Keyboard shortcuts

Move and act

jork
Next / previous post or comment
Enteroro
Open the post
f
Open the media viewer
s
Save
h
Hide (the toast has Undo)
Tab
Right after an action: the toast's Undo
x
Collapse the comment
v
Next layout: cards, compact, grid

Go to

/
Search
gthenh
Home
gthena
All
gthens
Subreddits
gthenl
Library
?
This list
Esc
Close a menu or this list

In the viewer

←or→
Previous / next file
↑or↓
Previous / next post
korj
Previous / next post
Space
Play / pause
m
Mute
z
Zoom
s
Save
o
Open the post (on its own page: back to it)
Esc
Close

Keys do nothing while you type in a box.