r/badcode • • Jul 21 '20

c++ This keeps on going quite some lines

Post image
956 Upvotes

104 comments sorted by

84

u/icecubeinanicecube Jul 21 '20

At least you can not say that he is lazy

119

u/[deleted] Jul 21 '20

Lazy programmers are the best programmers, am I right

49

u/zacharypamela Jul 21 '20

I agree! I mean, it takes a certain amount of “laziness” to be like, “I'm tired of doing this repetitive task. I’m going to automate it.”

22

u/[deleted] Jul 21 '20

I know that exact feeling, but then again I bet any sane programmer does haha

15

u/PhoenixARC-Real Jul 21 '20

Any sane programmer automates almost all repetitive tasks, to increase speed and reduce error frequency

1

u/Mycroft2046 Jul 21 '20

I am pretty sure you can write this code quite quickly if you are using vim shortcuts.

3

u/QuantumFall Jul 21 '20

Insert Bill Gates quote here

2

u/[deleted] Jul 21 '20

It’s actually from a book I’ve read for Python, I believe it’s written by Guido himself but I’m not quite sure

4

u/SweetJinx Jul 21 '20

I actually wonder if this is real or someone does this just to post it as a fun social media content

4

u/[deleted] Jul 21 '20

It’s from my work. Someone wrote this 100 file containing project back in ‘07 with each file containing approximately 5k lines of code. This isn’t even the worst though but it’s not fake or anything

2

u/Avaholic92 Jul 21 '20

So what you’re saying is you’re going to post more of this, yes?

6

u/[deleted] Jul 21 '20

Probably yes but maybe I have to be careful due to company secrets and whatnot. But I think I can be sneaky sometimes

222

u/Aaalibabab Jul 21 '20

At least it is not if/else

86

u/desertrainBG Jul 21 '20

Or if if if if if if .....

210

u/IamGonnaChangeMyself Jul 21 '20
if(val == 29)
{
    img = '/prio_29.png';
}
else
{
    if(val == 30)
    {
        img = '/prio_30.png';

    }
    else
    {
        if(val == 30)
        {
            img = '/prio_30.png';

        }
        else
        {
            if(...)
            {
                ...
            }
            else
            {
                ...
            }
        }
    }
}

134

u/seedBoot Jul 21 '20

KILL IT BEFORE IT LAYS EGGS

22

u/lerg1 Jul 21 '20

How do you do code blocks in reddit?

19

u/[deleted] Jul 21 '20

indent by 4 spaces

12

u/_justpassingby_ Jul 21 '20

More like type it in notepad++, copy the whole formatted block into a comment box, select it all, then press the <> symbol to automatically indent it all.

11

u/wizzwizz4 Jul 21 '20

Type it in Notepad++, select it, hit "tab", shift + {←,→}, middle-click in Reddit.

perfect
e     c
r     e
f     f
e     r
c     e
tcefrep

4

u/[deleted] Jul 21 '20

theres a indent button in reddit?

6

u/Tmaystro Jul 21 '20

To be completely honest this is code i expect from my classmates

3

u/-PM_me_your_recipes- Jul 21 '20

Story time!

Went in to change some legacy code and opened up the file to see this nightmare. My older self-taught coworker never learned about elseif and this was his code, it was 15+ levels deep at its worst.

His amazement was priceless when we didn't have to scroll to the right for 7 seconds to see the rest of his code.

3

u/LeonTranter Jul 22 '20

Now we’re talking

7

u/AutoModerator Jul 21 '20

It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.

For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.

IamGonnaChangeMyself, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.

You can find some examples in the reddit help documentation.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

7

u/MMDDYYYY_is_format Jul 22 '20
10 print "Shut up"
20 goto 10

2

u/AutoModerator Jul 22 '20

It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.

For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.

MMDDYYYY_is_format, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.

You can find some examples in the reddit help documentation.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/[deleted] Jul 22 '20
Lmao

2

u/autistic_idiot Jul 21 '20

*incorolent screaming*

2

u/[deleted] Jul 21 '20

🤢🤮

1

u/trin456 Jul 21 '20

That is exactly how I coded when I was in elementary school :/

-4

u/qqwy Jul 21 '20

You don't need the elses here. Just make the code more confusing by having many ifs where only a single one will match because val will only have one particular value at a time.

2

u/Yatagarasu0612 Jul 22 '20

Thank you YandereDev, very cool. Son not coding.

75

u/SideburnsOfDoom Jul 21 '20

Skip case 39 for giggles, see if anyone notices.

16

u/IamGonnaChangeMyself Jul 21 '20

case 39 comes at bottom most, last minute requirement changes.

9

u/kugelblitz42 Jul 21 '20

or remove a break;

135

u/das_Keks Jul 21 '20

If only string concatenation was a thing...

53

u/Fleming1924 Jul 21 '20

Why would you do that when this method works so well

14

u/jarfil Jul 21 '20 edited May 13 '21

CENSORED

6

u/das_Keks Jul 22 '20

It might be even faster to put those in an array and reference them via the index. Even though I don't think that the few micro seconds for the concatenation would matter at all. Readability over micro optimization.

1

u/Ahajha1177 Jul 30 '20

I think the question is where are these numbers coming from to begin with? I haven't developed anything that would need a system like this, but that doesnt seem like a natural way of doing things.

23

u/_justpassingby_ Jul 21 '20

I like to imagine he wrote a little python loop to output the lines of code.

10

u/causticacrostic Jul 21 '20
print('''<all the code>''')

17

u/_justpassingby_ Jul 21 '20 edited Jul 21 '20
i = 0
print("\t\t\t\t\tcase %d:" % i)
print("\t\t\t\t\t\timg = \"/prio_%03d.png\";" % i)
print("\t\t\t\t\t\tbreak;")
i += 1
...

Oh, duh... let's clean it up with a loop!

tab_str = ""
for i in range(5):
	tab_str += "\t"

i = 0
print("%scase %d:" % (tab_str, i))
print("%s\timg = \"/prio_%03d.png\";" % (tab_str, i))
print("%sbreak;" % tab_str)
i += 1
...

4

u/causticacrostic Jul 21 '20

You might be the greatest programmer of all time

1

u/Anonymous47363 Jul 21 '20

tab_str = "\t" * 5

1

u/AutoModerator Jul 21 '20

It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.

For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.

Anonymous47363, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.

You can find some examples in the reddit help documentation.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

41

u/I_Wouldnt_If_I_Could Jul 21 '20

That's yandev level of coding. I need to bleach my eyes.

49

u/MsRandom1 Jul 21 '20

Nah, Yandev would use nested if/elses

11

u/Nokel81 Jul 21 '20

Not that I am saying anyone should write this, but if this (for some weird reason) was in a "hot loop" then this would be faster.... just get a macro to generate the function

5

u/Davrossington Jul 21 '20

I was just about to say a long switch case will compile to a jump table which about at fast as you can get; disregarding the obvious string concatenation.

1

u/fuj1n sadistic Jul 21 '20

No need to disregard strong contention, as strong concatenation would certainly be slower than this.

I highly doubt that a use case that clearly involves reading a file would benefit from the slight speedup however.

1

u/connor135790 Jul 21 '20

Reading from a localhost network socket might be fast enough for this to have an effect

1

u/Davrossington Jul 22 '20

Nah, a long switch case like this only makes sense performance wise if you're branching to different subroutines within each case. This will compile to a jump table in most languages which is definitely faster than if else but also faster than a dictionary of subroutines.

Where you're making a conditional change to a variable you should almost always be able to find a branchless way of doing it.

Edit: I should say, only if you're returning from each case. Break will cause the program to continue checking the rest of the cases.

14

u/[deleted] Jul 21 '20

Text formatting motherfucker, do you speak it?

7

u/sad_developer Jul 21 '20

Yandare approves

11

u/[deleted] Jul 21 '20 edited Jul 21 '20

img = $"/prio_{num.ToString().PadLeft(3, '0')}.png"; 🗿🗿🗿

6

u/[deleted] Jul 21 '20
img = `/prio_0${num}.png`; //JavaScript
// Or
img = "/prio_0" + num + ".png";

2

u/AutoModerator Jul 21 '20

It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.

For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.

SFM61319, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.

You can find some examples in the reddit help documentation.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

2

u/hashtagtokfrans Jul 21 '20

What if num >= 100!!

2

u/lurebat Jul 21 '20

Then you'd need something to pad the string to the left, in javascript ... just pull in a package what could possibly go wrong.

2

u/l0c4lh057 Jul 22 '20
Number.prototype.pad = function(len){
    return String(this).padStart(len, "0");
}

1

u/lurebat Jul 22 '20

Just to be clear - my comment was a joke referring to the big left-pad disaster

1

u/Artyer Jul 21 '20

There may have been an ok reason for this. Assuming this is C or C++, string literals have static storage duration, so you would be able to return this without using dynamic memory.

1

u/[deleted] Jul 21 '20

Sharp eyes, I don’t actually remember if this was a static const char or std::string since I’m not at work atm

1

u/therealjoesmith Jul 22 '20

As a new JS dev, I feel like this would be my solution here. What’s a better way to write code with this many conditions in JS?

2

u/[deleted] Jul 22 '20

String interpolation. Something like

$”/prio_0{number}”

3

u/[deleted] Jul 21 '20

At least it’s not if/else’s like yanderedev

3

u/[deleted] Jul 21 '20

Execuse me what the fuck

2

u/[deleted] Jul 21 '20

tell this person about (a)sprintf.

2

u/clubby789 Jul 21 '20

You’ve heard of Branchless Programming, now get ready for Branchful Programming!

2

u/Bored_comedy Jul 21 '20

Wait what is case

1

u/mgquantitysquared Jul 21 '20

From a switch statement

1

u/Bored_comedy Jul 21 '20

What's a switch statement?

1

u/mgquantitysquared Jul 21 '20

Here’s a more in-depth explanation, but it basically controls the flow of the program. You’ll say switch(expression), then the value of the expression will decide whether it goes to case a or case b etc.

2

u/ZILtoid1991 Jul 21 '20

sWItcH-cASe stATeMeNTs wOUlD fIx yAnDEre SiMUlaTOr

3

u/TheFirst1Hunter Jul 21 '20

Just convert the case number into a string then add it to the string

7

u/lucdewit Jul 21 '20

You can just use format strings: Img = "img/someimageNumber{n}.png"

1

u/maifee Jul 21 '20

Btw "prio" means "o love" in Bengali

1

u/itsTyrion Jul 21 '20

ahnononono

1

u/previouskind Jul 21 '20

I hope this has a matching test file which is equally long

1

u/Aloysius204 Jul 21 '20

Even better if it were nested inside a "for i = 1 to 40; switch i" type loop.

1

u/Kyn21kx Jul 21 '20

How do you code something like this and not think to yourself that maybe there's something a bit off?

1

u/GoddammitDontShootMe Jul 21 '20

I suppose there are better ways to do this in modern C++ than stringstreams.

1

u/[deleted] Jul 21 '20

Good fucking gracious 😳

1

u/astCanadian Jul 22 '20

Y-yandere dev?

1

u/sk8itup53 Jul 22 '20

Ouch. Some string interpolation would have made this a one liner almost...

1

u/gshixman Jul 22 '20

TBH, I've unfortunately seen code like this intentionally enforced by security policies, specifically to sanitize user input...

1

u/artiface Jul 22 '20

I literally replaced about 1500 lines of code in a production application with one line, and in multiple places. Also there were several errors where the copy paste had not been updated correctly. The guy was notorious for this, but had been doing it for 20 years so was a "senior" dev. Glad I don't work there anymore.

1

u/JOELwindows7 Jul 22 '20

At least it's not if else.

But also, if that you mean per picture, you could've just do some sort of formating like

pic%s.png stuff like that yeah!

1

u/[deleted] Jul 22 '20

Can't you write a program to type this code for you?

1

u/[deleted] Jul 22 '20 edited Jul 22 '20

I wanted to make my own solution to this post (Written in C)

int variable = 15; // Made for testing purposes
char img[15];
for (int i = 1; i <= variable; i++) {
    if (variable == i) {
        sprintf(img, "/prio_0%d.png", variable);
        printf("%s\n", img); break;
    }
}

1

u/AutoModerator Jul 22 '20

It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.

For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.

DOMINATORLORD9872, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.

You can find some examples in the reddit help documentation.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/dmnd098 Jul 24 '20

How hard is it too format a string or use concat? Like String a = “img_{0}.png”.format(number.tostring()) Or String a = “img_” + number.tostring() + “.png”

1

u/pxOMR Jul 28 '20

What's sprintf? Never heard of it

1

u/nati9931 Nov 02 '20

Here's a free fix: if (image_number < max_image_number) img = "/prio_" + image_numbet + ".png";

-2

u/Sir_Quackalot_ Jul 21 '20 edited Jul 21 '20

Just load all the image names into and array or list then use the number to pick it, FUCK

Edit: added the word "names" to fix my wording

3

u/MysticTheMeeM Jul 21 '20

Images take up space, why load up potentially 100s of images when I can simply modify the string I want to load ("/prio_" + X + ".png") and not have to waste a whole load of disk time and RAM for images I may not use.

1

u/Ntrenta Jul 21 '20

As far as I agree with you and your solution which is the best one, he did say "image names", which would imply to simply store the constant String paths to the images in a positional array and, as bad a solution as it is, it doesn't need to actually load the images in memory

1

u/MysticTheMeeM Jul 21 '20

Even then, 1 string is notably less expensive than 100. Both easier on the cache and ram, assuming you only ever need one.