this post was submitted on 11 Oct 2025
434 points (99.5% liked)

Programmer Humor

27412 readers
1647 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS
 
top 50 comments
sorted by: hot top controversial new old
[–] theunknownmuncher@lemmy.world 116 points 1 month ago* (last edited 1 month ago)

Whenever you sit back and smile proudly to yourself about how clever the block of code you just wrote is, your next move should be to delete and rewrite it.

This is a clever block of code! Great job, now rewrite it to be sane πŸ˜‚

[–] douglasg14b@lemmy.world 63 points 1 month ago (2 children)

Still linear time at least, could always be much MUCH worse

[–] dfyx@lemmy.helios42.de 41 points 1 month ago (2 children)

There could be a hidden quadratic cost because the string needs to be reallocated and copied multiple times.

[–] Jerkface@lemmy.world 40 points 1 month ago (1 children)
[–] aaaaaaaaargh@feddit.org 11 points 1 month ago

This is the spirit

[–] kogasa@programming.dev 5 points 1 month ago

Not quadratic in the length of the input. Assuming replace is linear this is also linear

[–] lugal@lemmy.dbzer0.com 17 points 1 month ago (1 children)

True. Lost opportunity to blow things up with useless recursivity

[–] bleistift2@sopuli.xyz 15 points 1 month ago (2 children)

The word you’re looking for is recursion (see recursion).

[–] Gonzako@lemmy.world 5 points 1 month ago (1 children)

Nah, I'd like to un-see recursion. It was way overblown on uni, I barely ever use it.

[–] Cethin@lemmy.zip 10 points 1 month ago (2 children)

Recursion is amazing for a small selection of problems. Most of the time you don't need, or want, it. When it is useful though, it tends to be really useful.

I don't understand people's issue with it. I always found it easy. Maybe that's why I feel this way. Maybe if you find it challenging you want to avoid it, even when it's a good solution.

[–] kamstrup@programming.dev 4 points 1 month ago (1 children)

Most devs I know like recursion. Trouble is that many popular languages don't support tail recursion, but throw a stackoverflow error after a few thousand levels. So you have to keep track of max recursion depth manually, and it starts to look like a complicated solution

load more comments (1 replies)
[–] lugal@lemmy.dbzer0.com 4 points 1 month ago

Thanks. I knew something was off

[–] CookieOfFortune@lemmy.world 62 points 1 month ago (1 children)

This isn’t sufficiently enterprisey for Java. There should be a Roman numeral factory followed by relevant fromString and toInteger methods.

[–] vithigar@lemmy.ca 14 points 1 month ago

Ugh. Literally refactored multiple factories into straightforward functions in the most recent sprint where I work.

Someone saw a public factory method which was a factory for a reason and just cargo culted multiple private methods using the same pattern.

[–] anugeshtu@lemmy.world 38 points 1 month ago (1 children)

Why don't you just ask Chat-GPT o3 every time? Works like a charm!

[–] lugal@lemmy.dbzer0.com 44 points 1 month ago

Because there are better random generators

[–] TootSweet@lemmy.world 29 points 1 month ago* (last edited 1 month ago)

My first thought was something along the lines of a "zip bomb". For every "M" in the input string, it'd use more than a KiB of memory. But still, it'd take a string of millions of "M"s to exhaust memory on even a low-end modern server. Still probably not a good idea to expose to untrusted input on a public networked server, though. And it could easily peg a CPU core for a good while. Very good leveraged target for DDOSing.

[–] rooroo@feddit.org 21 points 1 month ago (1 children)

It also works the other way round: wanna convert Arabic n to Roman? Just write n times β€˜I’ and revert these replacement in inverse order.

[–] lugal@lemmy.dbzer0.com 7 points 1 month ago (2 children)

I don't know what happens when the substring overlaps. Like for the number 6, will it replace the first 5 I's with V and end up correctly with VI or the last ones and come to IV? I would guess the former and maybe you know but I never thought about it before

[–] Atlas_@lemmy.world 6 points 1 month ago (1 children)

Also does not handle 'IIIIIIIII' -> 'IX' properly

[–] pitiable_sandwich540@feddit.org 2 points 1 month ago (1 children)

If the substitution went right to left it might work.

[–] rooroo@feddit.org 2 points 1 month ago
[–] rooroo@feddit.org 2 points 1 month ago (8 children)

I’ve written it that way and it works, as in it will replace left to right and you replace iiii to iv after iiiii to v

load more comments (8 replies)
[–] TheLazyNerd@europe.pub 16 points 1 month ago

Since Roman numerals have an upper bound, the time complexity is always O(1).

[–] Zangoose@lemmy.world 13 points 1 month ago (1 children)

They forgot "CM" so this doesn't work for any number that ends in 900s

[–] trxxruraxvr@lemmy.world 50 points 1 month ago* (last edited 1 month ago)

No, M will be replaced by DD and then CD will be picked up, so it will go

  1. CM
  2. CDD
  3. CCCCD
  4. CCCCCCCCC
  5. ......
[–] olafurp@lemmy.world 9 points 1 month ago (1 children)

It's not too bad, it's readable and easily optimised by adding intermediate sums and removing whatever power of 10 you're working on.

load more comments (1 replies)
[–] SpaceNoodle@lemmy.world 7 points 1 month ago (1 children)
[–] trxxruraxvr@lemmy.world 31 points 1 month ago* (last edited 1 month ago) (2 children)

IIV would never be used. In Roman numerals at most one smaller unit can come in front of a larger one. The code doesn't do any validation though.

[–] Mirodir@discuss.tchncs.de 14 points 1 month ago

While it doesn't say anything about IIV specifically, they sure got creative enough to sometimes subtract more than one of the smaller units from a larger one.

load more comments (1 replies)
[–] eah@programming.dev 7 points 1 month ago* (last edited 1 month ago) (4 children)

It's got some code duplication. Who can code ~~gulf~~ golf this?

[–] tourist@lemmy.world 45 points 1 month ago
public static int convertRomanNumeral(String numeral) {
    return 4; // todo
}
[–] grue@lemmy.world 26 points 1 month ago* (last edited 1 month ago) (1 children)

Code gulf, you say?

public static String
convertRomanNumeral(String numeral) {
    numeral = numeral.replace("America", "Mexico");
    return numeral;
} 
[–] deadbeef79000@lemmy.nz 5 points 1 month ago
[–] ray@sh.itjust.works 3 points 1 month ago (1 children)
public static int convertRomanNumeral(String numeral)
{
  numeral = numeral.replace("M", "DD")
    .replace("CD", "CCCC")
    .replace("D", "CCCCC")
    .replace("C", "LL")
    .replace("XL", "XXXX")
    .replace("L", "XXXXX")
    .replace("X", "VV")
    .replace("IV", "IIII")
    .replace("V", "IIIII");
  return numeral.length();
}
[–] qaz@lemmy.world 6 points 1 month ago
public static int convertRomanNumeral(String numeral)
{
  return numeral.replace("M", "DD")
    .replace("CD", "CCCC")
    .replace("D", "CCCCC")
    .replace("C", "LL")
    .replace("XL", "XXXX")
    .replace("L", "XXXXX")
    .replace("X", "VV")
    .replace("IV", "IIII")
    .replace("V", "IIIII")
    .length();
}
load more comments (1 replies)
[–] panda_abyss@lemmy.ca 6 points 1 month ago (1 children)

Should do a regex find all then iterate over each chunk recursively until unchanged.

[–] ZTechnical@programming.dev 8 points 1 month ago (1 children)

there was no regex in ancient rome

[–] felbane@lemmy.world 6 points 1 month ago

Actually there were seven kings prior to the establishment of the republic, at which point they expelled the rulers... a reg-ex if you will.

[–] Olap@lemmy.world 5 points 1 month ago

until(original=new) { run convertOriginal }

[–] Wynnstan@lemmy.world 5 points 1 month ago

Alternatively pip install roman.

[–] mkwt@lemmy.world 3 points 1 month ago (1 children)

You missed "CM," which was common in copyright statements in the 20th century.

[–] webadict@lemmy.world 9 points 1 month ago

No, they didn't.

CM becomes CDD, which becomes CCCCD which becomes CCCCCCCCC.

[–] BlackEco@lemmy.blackeco.com 3 points 1 month ago (1 children)

Depending on the language, you may be mutating the input value, which isn't great.

[–] qaz@lemmy.world 2 points 1 month ago (1 children)

I'm pretty sure it's Java (due to the syntax and Eclipse editor default color scheme), so that isn't an issue

[–] BlackEco@lemmy.blackeco.com 2 points 1 month ago* (last edited 1 month ago)

Oh right, my Java is a bit rusty. But if it was Javascript, that would have been a problem.

[–] Zyansheep@programming.dev 3 points 1 month ago
load more comments
view more: next β€Ί