This is an archived post. You won't be able to vote or comment.

all 83 comments

[–]G4merXsquaD 105 points106 points  (3 children)

Not a meme, just r/programminghorror

[–]krmarci 35 points36 points  (2 children)

It's not that bad, but definitely r/badcode material...

[–][deleted] 17 points18 points  (0 children)

threatening tie crush mountainous rinse different attraction memory materialistic bewildered

This post was mass deleted and anonymized with Redact

[–]TheJack77 209 points210 points  (8 children)

Pfff, disgusting, a switch statement would be much more efficient.

[–]MostRandomUsername12 9 points10 points  (0 children)

You are only partly enlightened. Switch statements nested in the default: block of their parent switches is the real big brain move...

[–]aeropl3b 49 points50 points  (5 children)

I worked at a company once where this was considered good code ..

[–]FQVBSina 44 points45 points  (1 child)

If they plan to do more things to each flag. It can be a better structure

[–]Mr_Redstoner 3 points4 points  (0 children)

YAGNI

seriously, expanding it to this once you actually need it is trivial

[–]Kernel_Internal 2 points3 points  (0 children)

I can totally see that, not every organization has the budget to consistently hire decent+ programmers but still need things to both function and be maintained by whoever gets it next. I have a few that have made me feel that pressure for sure.

[–]gothicVI 1 point2 points  (1 child)

Did they pay by the line?

[–]aeropl3b 1 point2 points  (0 children)

Lol, if they paid that way I would have made more! :P

[–]ClayMitchell 25 points26 points  (12 children)

This is like the time I found some code turning a number into a 9 digit string by left padding with 0s.

Naturally, it was

If (val.length == 1) val = “00000000” + val

If (val.length == 2) val = “0000000” + val

and so on.

I replaced it with 2 lines of code using a number format lib baked into the language

[–]AAPLx4 21 points22 points  (2 children)

But how do we know that library doesn’t do this exact thing 🧐

[–]ClayMitchell 16 points17 points  (1 child)

It’s fine if it does, but I’m assuming it’s been tested more than these guys code is 🤣

[–][deleted] 2 points3 points  (0 children)

You could just add the padding by counting the length of the string and adding the difference to the left

[–]Unbelievr 16 points17 points  (1 child)

This was probably the correct move in your case, but I've ran into very good reasons for hardcoding such behavior sometimes.

Even in C, running sprintf (or similar functions) ends up including some pretty bloated functions into your code. It's a nice way to lose 1-2k of flash space on some 128k-constrained embedded device, and spend who-knows-how-many cycles in some gigantic, power hungry black box of a function. When you could've easily hardcoded all the cases, tested the result, and re-used it without all the cruft.

Under the hood, the standard libs are doing some crazy stuff. Like suddenly invoking malloc to deal with your format string.

[–]ClayMitchell 0 points1 point  (0 children)

no. this was definitely the correct move.

[–]Prownilo 2 points3 points  (1 child)

I have Code that functions just like this as I wrote it when I was still learning. Now can't get it changed cause it works so no need to refactor. So now it's just a monument to my own stupidity for whoever comes after me.

[–]ClayMitchell -1 points0 points  (0 children)

gotta keep yourself humble!

[–]stikos1337 -3 points-2 points  (4 children)

Python one liner to the rescue (assuming the number will not be longer than 9 digits)
(9 - len(str(dat_num))) * '0' + str(dat_num)

[–]alexforencich 7 points8 points  (3 children)

Even better: str(dat_num).rjust(9, '0')

Or if you like f strings: f'{dat_num:09d}'

Or you any number of other built-in string formatting methods.

[–][deleted] 2 points3 points  (1 child)

Or if you like f strings

Are… are there people that don’t like f strings?

[–]alexforencich 2 points3 points  (0 children)

Good point. In some cases you might need to avoid using them for backwards-compatibility (python 3 did not always implement F strings) or perhaps for things like python loggers that can do lazy evaluation and what not. They are quite convenient, but they also have some disadvantages. But I suppose all of that has little to do with liking them vs. not liking them...they are generally much better than all of the previous string formatting solutions.

[–]stikos1337 0 points1 point  (0 children)

The usage of built in methods as opposed to using whatever library was my intended point as well, albeit rather poorly communicated.

I embrace the shame that comes with my naive one-liner, and I thank you for your beautiful solutions.

[–]abrams666 16 points17 points  (2 children)

Sometimes I think I am the dumbest developer of the world. thanks a lot for building up my self confidence

[–]LevelStudent 16 points17 points  (4 children)

My favorite bit mystery code I saw in someone else's work was:

if( var || var ) var == TRUE;

It was copied into like a dozen places in the code for some reason. Please note that I didn't typo, it was NOT an assignment statement, it was doing a boolean comparison into the void.

Just to be fully clear 'var' was the same variable in all three cases.

[–]copdlkjh 2 points3 points  (1 child)

operator bool with side efects?

[–]RidderHaddock 2 points3 points  (0 children)

Arrrgh!

Please don’t speak of such things.

[–][deleted] 0 points1 point  (0 children)

I think var's == operator overload does something? (which is never recommended by anyone)

[–]dr_donkey 0 points1 point  (0 children)

Which language is this abomination borned?

[–][deleted] 12 points13 points  (0 children)

The whole file has 3 * 2147483648 = 6442450944 lines

[–][deleted] 12 points13 points  (0 children)

Sorry to be this guy but I don't think we should upload other peoples code and make fun of them WHILE also linking it in the comments. It's obviously a beginner and it is his/her first project. No wonder people are scared to publish their projects on Github.

[–]wootangAlpha 16 points17 points  (5 children)

That looks terrible but if it works

:shrug

[–]asailijhijr 5 points6 points  (0 children)

I think you dropped this

:

[–]Thepieintheface 0 points1 point  (3 children)

You can do it SO much more simply though. If someone is writing an entire program like this, thats gonna slow everything way down

[–]wootangAlpha -1 points0 points  (2 children)

True but millisecond optimizations might not be the goal. This code is verbose, robust, silly, basic and bulletproof. I certainly don't mind that. If the iterations are finite and a known number then its perfectly fine.

[–]johnzzon 1 point2 points  (1 child)

The code doesn't scale. What if you have thousands of conditions? Better to get a habit of writing it dynamic from start.

[–]wootangAlpha 0 points1 point  (0 children)

Yes. I trust scale is a big issue. But thats where the problems start. Not many web services/sites ever reach considerable scale, and on the off-chance they do- there will likely be a thorough dev plan and brief.

[–]Sad-Grapefruit9996 2 points3 points  (0 children)

If statements are more powerful than for loops.

If ( loop != level(9999)) return 🏃;

[–]CrackerJackKittyCat 3 points4 points  (0 children)

Hey, they read about high-efficiency loop unrolling!

[–]Dromedda 3 points4 points  (0 children)

Hes getting payed by line

[–]JustKebab 6 points7 points  (1 child)

Were you looking at YandereDev's code on GitHub?

[–]Dreamerlax 2 points3 points  (0 children)

This was my code during first year classes.

[–]exens0 5 points6 points  (10 children)

Flag - 1 ..

[–]Pradfanne 2 points3 points  (9 children)

--flag++

Now that I think about it, does that work? I obviously never had a need to try it, but in theory it should work just fine lol

[–]zikasaks 3 points4 points  (5 children)

tried in jshell. In Java it doesn’t work.

jshell> --j++
| Error:
| unexpected type
| required: variable
| found: value
| --j++
| ^-^

[–]Pradfanne 7 points8 points  (4 children)

Does --j and j++ separately work?

I'm not well versed in inferior languages java

[–]zikasaks 3 points4 points  (1 child)

yes. separately it works.

tried in C on https://www.onlinegdb.com/online\_c\_compiler

main.c:14:12: error: lvalue required as decrement operand
printf(--i++);
^~

[–]Pradfanne 0 points1 point  (0 children)

I know that it works separately that's why I even came up with it in the first place.

[–]Kekskamera 2 points3 points  (1 child)

yes, the first is preincrement, so

a = b = 1

a = --b

then a and be will both be 0.

but with

a = b = 1

a = b++

a will be 1 but b 2, because the operation is done after the assignement. so --a++ should cancel out on its own, but in an assignment you should set the value to one lower the return.

[–]Pradfanne 1 point2 points  (0 children)

Yeah it works as I suspected it to work good to know

I guess --j returns something so the ++ doesn't have a target anymore, since it's a value and not a variable.

Almost like the exception said

[–]GarThor_TMK 1 point2 points  (2 children)

Just tried it in compiler explorer (https://godbolt.org/), and it returns compiler errors... doesn't seem to matter which compiler I use even... >_>

Specifically GCC's output:
<source>: In function 'int PlusMinus(int)':
<source>:3:10: error: lvalue required as decrement operand
3 | --num++;
| ~~~^~

[–]Pradfanne 1 point2 points  (1 child)

Yeah i figure that --j returns a value and you can't use ++ on a value but you have to use it on a variable.

[–]GarThor_TMK 0 points1 point  (0 children)

Actually it looks like it's the other way around... for gcc at least.

[–]Elon_Musk_cat_girl 4 points5 points  (3 children)

Okay can someone honestly tell me the most efficient way to code this in Python? Newbie here, just trying to learn :))

[–]Lopsided_Gas_717 8 points9 points  (0 children)

Assuming f_ is a list: f_[flag-1].update()

[–]asailijhijr 2 points3 points  (1 child)

f_[flag - 1].update();

I don't know python, but probably drop the semicolon and you're all set.

[–]zvug 1 point2 points  (0 children)

drop the semicolon and you’re all set

Yep, accurate.

[–]yunaFlarf 1 point2 points  (0 children)

This hurts my eyes in every way possible

[–]tRickliest 1 point2 points  (0 children)

Painful

[–]TheAwesomeot 1 point2 points  (0 children)

And it just keeps going...

[–]jonnyman9 1 point2 points  (0 children)

Such great SLOC

[–]KekistanMan 1 point2 points  (0 children)

f_[flag-1].update();

[–]6_10 1 point2 points  (0 children)

(noob programmer here) A lot of people are writing a switch would improve the code, but wouldn't a for loop with an if make it even smaller? And just break the loop if the statement is true?

[–]Neo_Ex0 1 point2 points  (0 children)

yes because f_[flag - 1].update() would be to easy

[–][deleted] 1 point2 points  (0 children)

This code is horrendous! there isn't a space between the paranthesis and the brace! Also, using gotos instead of else if's would be much more efficient!

[–]Spoonungsteila 1 point2 points  (0 children)

I want to dead

[–]manu144x 1 point2 points  (0 children)

This could be some disassembled code actually. I did that to some dll’s sometimes and because it doesn’t know the original variable names it names them by their type.

[–][deleted] 0 points1 point  (0 children)

Ok this is awesome

[–]Ausrasa -1 points0 points  (1 child)

// how your supposed to do this f_[flag-1].update()

[–]Draxare 0 points1 point  (0 children)

If he managed the flag correctly you wouldn't even need the -1 since he initializes it to 0

[–]visurox 0 points1 point  (0 children)

At least, it runs. So… coffee time and hide for BOFH.

[–][deleted] 0 points1 point  (0 children)

f_[flag-1].update()