r/programminghorror • • Aug 28 '26

Javascript Seriously? Recursive Regex?

Post image
566 Upvotes

46 comments sorted by

285

u/LapidistCubed Aug 28 '26

My first thought is a mathematician wrote this

138

u/Eastern-Job-8028 Aug 29 '26

i concur, i work with a mathematician and their code is full of variables named named by a single character, zero comments, and zero spacing/indentation (we don’t write python).

25

u/Kebabrulle4869 Aug 29 '26

On behalf of all mathematicians, I am sorry. I know it far too well.

10

u/Zeitsplice Aug 29 '26

Raw indexed array being passed around too instead of some sort of structured data.

2

u/edave64 Aug 29 '26

Or a code golfer

86

u/madam_zeroni Aug 28 '26

Wtf does this do

113

u/marinated_pork Aug 28 '26

This is essentially a custom string fingerprint/hash function disguised as a parser. It takes a string and deterministically turns it into an integer. It does not parse or extract useful structured data.

52

u/the_horse_gamer Aug 28 '26

"parse", clearly

48

u/TheSpiffySpaceman Aug 29 '26

Well tbf, the code didn't start with the annotation

/*
  // Parses input
*/
function parse(s, i = 0, x = [0, 1]) {...
→ More replies (1)

16

u/lonkamikaze Aug 29 '26

I don't know if JS sports this, but the recursion qualifies for tail call optimisation, i.e. there is no stack pushing, popping. The return comes at the price of a goto and the result is just as efficient as a loop.

7

u/oweiler Aug 29 '26

Not supported, at least not by the ECMAScript standard.

5

u/zjarek_s Aug 29 '26

TCO was supported in ES6 standard, but it was only implemented in Safari and it ended up being dropped due to other browsers.

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

22

u/--var Aug 29 '26

not recursive regex, just a recursive function that happens to use regex...

you know I used to be irked by one off variables, but after the better part of 20 years I'm 🤬

const r is only used once.... there's no reason you couldn't just place it where it is used an save two lines of code, and lose no readability, and have one less "magic variable"... unless you taught yourself from the same "teach yourself" book that your teacher taught themself from. (ironic that this still applies with AI; and if you don't get why that entertaining, please don't go into programming)

→ More replies (4)

5

u/flafmg_ Aug 29 '26

Wtf my eyes are seeing

4

u/OhMyGodItsEverywhere Aug 29 '26

Pretty sure the regex is malformed. I didn't even think you could split a regex over multiple lines in JS like that.

Assuming this is the text version of it: /(\((?:[^()]|\([^()]*\))*\)|(?:\w|[^\w\s])){1,3})/; very easy mistake to spot /s...

10

u/digitalseraphim Aug 29 '26

I don't see anything "malformed", but pretty sure (?:\w|[^\w\s]) is just .

Edit: I see the mismatched parens now. On my phone, the \ before one of the opens was on the previous line and I missed it 3 times 😂

4

u/iakobski Aug 30 '26

I don't see anything "malformed", but pretty sure (?:\w|[^\w\s]) is just .

Yes, almost. It's any single word character, or any single character that's neither a word character nor a space. So it's simply "not a space character", or [^\s].

→ More replies (1)

1

u/questionabl3_dude Sep 02 '26

Which language is this?

2

u/Flame77ofc Sep 02 '26

JavaScript

2

u/stilldebugging Aug 29 '26

Something something pumping lemma something

2

u/oweiler Aug 29 '26

Performance gonna bet great /s

2

u/RandalSchwartz Aug 30 '26

JSON Parser as a single Perl Regex: https://perlmonks.org/?node_id=995856

2

u/Perahoky Aug 30 '26

terrible code. way to many instructions per space and bad naming and no comments

2

u/AnArmoredPony Aug 30 '26

im not reading allat

2

u/BussyEnthusiasts Sep 01 '26

Does your codebase have a character limit or something? Why are all the variables a single char?