Saturday, January 20, 2024

from perl to php to life with ChatGPT

On a private Slack, I quoted this line from yesterday's email domain validation learning -
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
(so filter_var returns false if it's not a match) and a friend pointed out:

`filter_var()` is exactly the kind of API abomination PHP is known for, a+++ language design

He's not wrong! That's pretty ugly - the name of the function is kind of negative, so you're asking people to think in double negative to use it properly (vs a more typical isValid() type naming)

I was trying to reconstruct my history with PHP; I taught myself Perl in college, and .cgi saw me through a number of years. PHP was a boon because the useful functions were built in - often installing libraries for Perl required root access I didn't always have, and for a long time I was always copying this boiler plate into my CGI scripts:

sub chugCGI { local (*in) = @_ if @_; local ($i, $key, $val);
          if($ENV{'REQUEST_METHOD'}eq"POST") { read(STDIN,$in,$ENV{'CONTENT_LENGTH'});
                           }else {$in = $ENV{'QUERY_STRING'};} @in = split(/&/,$in);
          foreach $i (0 .. $#in) { $in[$i] =~ s/\+/ /g;
($key, $val) = split(/=/,$in[$i],2);$key =~ s/%(..)/pack("c",hex($1))/ge;
$val =~ s/%(..)/pack("c",hex($1))/ge; $in{$key}.= $val;}return length($in);}


which read CGI variables and put them in a assoc. array when CGI.pm or CGI_lite.pm wasn't at hand.
So PHP was obviously a step up. But when I first tried learning it (in the first dot com crash) around Y2K, it really was even less ready for prime time - IIRC if you walked an array you had to like reset an iterator that was part of the array somehow? and 2 dimensional arrays were a right mess, and the documentation was wrong... basically it felt slapdash in a way that Perl never did, even though later I found out Perl was actually a glue language over C Unix System Calls - which blew my mind because I thought Perl (with its first class strings and associative arrays and lack of memory management, or compiling for that matter) was like that anti-C.

But over the years it became my default for backend stuff - the Apache monolith LAMP model just gets out of the way of the front end stuff I really care about (and I still direct folks to the "Taking PHP Seriously" article when feeling defensive about that) but the weird thing was - since I learned it in a google era, I hardly memorized any of it. Its API does have a lot of weird edges (especially in terms of if you have a function with arguments of an array and a single member, which argument is likely to appear first? for in_array() the needle comes before the haystack, but for array_push() the array comes first... which makes some kind of sense in terms of allowing you to add multiple things at once but still doesn't scream consistency.) But still, relative to Perl, there were so many useful functions builtin... and also it didn't have all that weird syntax that makes "Perl golf" a hobby of some.

And yet, I had most everything in Perl memorized in a way I never have with PHP... but now rather than googling to find my PHP bit on stackoverflow, I have ChatGPT. Like David Winer put it:

ChatGPT is like having a programming partner you can try ideas out on, or ask for alternative approaches, and they're always there, and not too busy to help out. They know everything you don't know and need to know, and rarely hallucinate (you have to check the work, same as with a human btw). It's remarkable how much it is like having an ideal human programming partner. It's the kind of helper I aspire to be.
or as Salvatore Sanfilippo put it:
In the field of programming, perhaps [LLM's] ability would have been of very little interest up to twenty or thirty years ago. Back then you had to know a couple of programming languages, the classic algorithms, and those ten fundamental libraries. The rest you had to add yourself, your own intelligence, expertise, design skills. If you had these ingredients you were an expert programmer, able to do more or less everything. Over time, we have witnessed an explosion of frameworks, programming languages, libraries of all kinds. An explosion of complexity often completely unnecessary and unjustified, but the truth is that things are what they are. And in such a context, an idiot who knows everything is a precious ally.
I'm starting a new job and exploring new territory for me and I am grateful to have ChatGPT by my side.

No comments:

Post a Comment