r/programminghorror •

Had to fix this at work

[deleted] — view deleted post

0 Upvotes

194 comments sorted by

776

u/amicloud Dec 19 '18

...no you fucking didn't, no way that's real.

258

u/zdy132 Dec 19 '18

Especially the comment. Maybe op should check with coworkers to see if that's a joke.

37

u/AnAnxiousCorgi Dec 19 '18 edited Dec 19 '18

A legacy system I worked on had a logging table in the DB with an action_id column. The action ID's were mapped to constants in the code. The file that contained all the (130+) action constants and their ID's had a comment at the end:

// Increment this number if you change the max action ID!!!!
// 132

And I don't recall ever seeing it match the actual ID count, so I 100% believe it lol.

EDIT: found it!. The current highest ID appears to be 158.

10

u/zdy132 Dec 20 '18

..at least it's very readable.

479

u/_waltzy Dec 19 '18 edited Dec 20 '18

Obviously its fake, our isEven function looks more like this:

public static int isEven(int a) {
    try {
        StringBuilder sb = new StringBuilder();
        sb.append(IMPORTS);
        sb.append(CLASS_HEADER);
        sb.append(MAIN_METHOD_HEADER);
        sb.append("boolean found = false;%n");
        for (int i=0; i< Integer.MAX_VALUE; i+=2) {
            sb.append(String.format("if (a == %d || a == -%d) found = true;%n",i,i));
        }
        sb.append("System.out.println(found);%n");
        sb.append(END_MAIN_METHOD);
        sb.append(END_CLASS);
        FileUtils.writeTo("IsEven.java", sb.toString());
        Process compileProcess = Runtime.getRuntime().exec("javac IsEven.java");
        compileProcess.waitFor();
        Process isEvenProcess = Runtime.getRuntime().exec(String.format("java IsEven %d", a));
        ByteArrayOutputStream baos = isEvenProcess.getOutputStream();
        isEvenProcess.waitFor();
        String out = new String(baos, Charset.UTF8);
        return Boolean.parse(out);
    } catch (Throwable t){ return false;}
}

271

u/DXPower Dec 19 '18

Shut it down. Shut the entire subreddit down. We can't beat this.

16

u/linuxlib Dec 19 '18

This is the Cthulhu of Programming Horrors.

22

u/too_much_exceptions Dec 19 '18

Yeah ! Java style mofo So dense, so verbose, it hurt my hairs

→ More replies (10)

18

u/[deleted] Dec 19 '18

I see the mistake. It should be: catch (EnterpriseLevelThrowable _elt)

18

u/corner-case Dec 19 '18

This has many capital letters in it, which is a sign of good code. 👌/10 would merge.

40

u/hoovyhauler Dec 19 '18 edited Dec 19 '18

Ah, Java compiling Java, truly a man of culture.

Edit: oopsie fixed, it's java

→ More replies (10)

14

u/[deleted] Dec 19 '18

StringBuilder

Um... No.

sb.append(IMPORTS);

You motherf---

catch (Throwable t)

I'm calling the cops.

10

u/[deleted] Dec 19 '18

This is some Fizz Buzz Enterprise Edition shit

9

u/patga Dec 19 '18

Can it hack NSA? Because it surely looks alike

9

u/space_fly Dec 19 '18

Thanks, I'll put it in production immediately on our Glassfish 2.0 web server. Will let you know how that goes.

4

u/ponybau5 Dec 20 '18

You forgot the ProcessCreationFactoryHelperBean

3

u/amicloud Dec 20 '18

This subreddit is for bad code, not perfection.

3

u/[deleted] Dec 28 '18

I wish I could angry react that shit

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

33

u/HandshakeOfCO Dec 19 '18 edited Dec 19 '18

... I see your time working with offshore development teams has been mercifully limited.

This really IS - EXACTLY - how Indian offshore teams tackle problems. No regard/effort put into anything except the immediate problem.

It makes me want to wrap my lips around the barrel of a gun.

EDIT: "It's a cultural issue." eyeroll.png. Yeah. Amazing how, if I'm an Indian working onshore, getting paid $150/hr, code like this would be a HUGE issue... but since I'm offshore at $20/hr the issues are brushed aside. Hmm.

I think the only "cultural issue" that needs addressing is in the boardroom, and maybe it can be addressed by a hard talk around shortsighted profit taking, and getting what you pay for.

5

u/falconfetus8 Dec 31 '18

It's actually less work in both the short and long term to just say "a % 2 == 0"

20

u/ladidadi82 Dec 19 '18

When the product manager has to write code

4

u/nakilon Dec 19 '18

Sounds real. This smells exactly like when they demand the bug reporter to list all exact steps and then fix only that exact scenario. You get infinite number of tickets to assign/fix/test/deploy/manage in your lovely job where you get paid for time, not for result.

2

u/_realitycheck_ Dec 19 '18

I agree. Nobody's that stupid.

→ More replies (1)

177

u/[deleted] Dec 19 '18

Well they almost had all the even number covered. At least the ones in the range of numbers they know.

10

u/mrheosuper Dec 19 '18

Oh, what could go wrong? a is equal to 16, nah.

11

u/[deleted] Dec 19 '18

Shit, didn't think about that number. Hey, how many even numbers are there anyway?

This is gonna take all day to code..

→ More replies (1)

304

u/pixel_sharmana Dec 19 '18

When you are paid per line of code

69

u/bausscode Dec 19 '18

Then you would have autogenerated this for like a million numbers.

30

u/sevenonone Dec 19 '18

I would have. Them, I'm not so sure.

10

u/disappointer Dec 19 '18

Yeah, because that auto-generated code needs to be able to figure out when a number is even...

→ More replies (3)

2

u/rus64 Dec 20 '18

Serious question, is that a thing?

131

u/[deleted] Dec 19 '18

You just added more checks right?

104

u/pqowie313 Dec 19 '18

Reminds me of the my classmates used to bend over backwards to avoid the % operator in intro CS classes because they couldn't be bothered to understand it.

81

u/AttackOfTheThumbs Dec 19 '18

How do you not understand remainder.

41

u/Kwyjibo08 Dec 19 '18

Right? You learn that before long division.

18

u/linuxlib Dec 19 '18

If you cannot understand what precedes, you cannot understand what remains.

10

u/pqowie313 Dec 19 '18

You'd be surprised how many people forget first grade stuff. In there defense though, I can't really remember having much use for the concept of remainders before I learned to program.

22

u/_realitycheck_ Dec 19 '18

Modulo is like the most useful operator out there. You can add speed to games every n points, make numbers loop on each other in recursions and other fun stuff.

14

u/TheNorthComesWithMe Dec 19 '18

It's useful but also like the least useful. Addition is probably the most useful.

→ More replies (5)

15

u/Nevernerd Dec 19 '18

Reminds me of myself, I forgot the % in an exam in school. We also had to write a piece of code to check if an int is even or not. My solution was to divide the int by two, then multiply it by two and check if it was the same number as the one given... worked like a charm :D

6

u/[deleted] Dec 21 '18

I was about to turn on Caps Lock, until I realized you're probably referring to integer division.

10

u/Coopsmoss Dec 19 '18

Can never remember what is does with negative numbers.

28

u/Reelix Dec 19 '18

That's because it changes depending on the programming language or application :<

7

u/Coopsmoss Dec 19 '18

I knew I wasn't imagining it!

5

u/blue_pixel Dec 19 '18

That's what REPLs are for.

10

u/Reelix Dec 19 '18

Because "%" changes depending on the programming language used.

Half the time it's modulo, half the time it's remainder.

18

u/[deleted] Dec 19 '18

ELI5 on the difference?

20

u/linuxlib Dec 19 '18

They are the same if the dividend and divisor are positive.

But if one or both are negative, then things can get a bit weird. The convention used can also change depending on the programming language. More here.

→ More replies (2)

10

u/adragondil Dec 19 '18

I thought modulo gave you the remainder?

→ More replies (1)

219

u/triggerhappy899 Dec 19 '18 edited Dec 19 '18

Psssh amateurs, you could easily add the ability to check higher numbers

public static int IsEven(int a) 
{
    for(int i =0; i < 10000; i += 2)
    {
        if(a==i) return 1;
    }

    return 0;
}

Just remember to set the number inside the for loop high enough to what you need. Ezpz

Edit: I am so humbled to be in the presence of all you intellectuals

12

u/sebnukem Dec 19 '18

As a good developer I would add a if (a > 10000) throw new RuntimeException("number is too damn high!");

9

u/[deleted] Dec 19 '18 edited Dec 19 '18
public static string GenerateCode(int max, int levelofbaseident) 
{
    string code = "";
    for (int j = 0; j < levelofbaseident; j++)
        code += "\t";
    code += "public static int IsEven(int a)\r\n{\r\n";
    for(int i =0; i < max; i ++)
    {
        for (int j = 0; j < levelofbaseident; j++)
            code += "\t";
        code += "\tif(a== " + i + ") return 1;\r\n";
        for (int j = 0; j < levelofbaseident; j++)
            code += "\t";
        code += "\tif(a== -" + i + ") return 1;\r\n";
        ++i;
    }
    for (int j = 0; j < levelofbaseident; j++)
        code += "\t";
    code += "\treturn 0;\r\n";
    for (int j = 0; j < levelofbaseident; j++)
        code += "\t";
    code += "}";
    return code;
}

Better?

Generating the code is surely much better than looping through it. Because of performance.

Edit: fixed a small issue.

6

u/CreativeAnteater Dec 19 '18

Calm down hackerman

3

u/[deleted] Dec 19 '18

i < Integer.MAX_VALUE

ftfy

0

u/[deleted] Dec 19 '18

[deleted] — view deleted comment

44

u/BrFrancis Dec 19 '18

That's just so right and yet so wrong at the same time. Covers range, accounts for negative inputs... Such concise inefficiency. It's a work of art.

29

u/Amaranthine Dec 19 '18

Four spaces before a line, or for in-line code blocks like this, wrap in a single backtick. Formatted for ease of reading:

public boolean isEven(int n) {    
  n = n < 0 ? n * -1 : n;
  boolean isNumberEven = false;
  for (int i =0; i <= n; i++) {
    isNumberEven = !isNumberEven;
  }
  return isNumberEven;
}

9

u/HandshakeOfCO Dec 19 '18

Wait for a "senior offshore developer" to look at this code then chide you for not writing unit tests, and especially for using a ternary operator "because it's inefficient."

→ More replies (2)

2

u/FourChannel May 12 '19

This is glorious.

→ More replies (1)

25

u/UnchainedMundane Dec 19 '18

there's an easy fix: if (a > 14) return isEven(a - 14);

22

u/Serundeng Dec 19 '18

* QA proceeds to enter -2*

16

u/newPhoenixz Dec 19 '18

// :TODO: Add at least another 4 billion checks

80

u/bsep1 Dec 19 '18

public static int isEven(int a) {

if(a == 0) return 1;

if(a == 1) return 0;

return isEven(a+2);

}

96

u/AmenAndWomen Dec 19 '18

a-2

154

u/cstheory Dec 19 '18

It'll roll around eventually.

42

u/Nulagrithom Dec 19 '18

God that made me puke a little

11

u/zdy132 Dec 19 '18

Oh my god.

14

u/citewiki Dec 19 '18

Really inefficient for the number -2

4

u/marcopennekamp Dec 19 '18

isEven(0) = true

isEven(n) = !isEven(n - 1)

18

u/ladidadi82 Dec 19 '18

return a % 2 == 0;

36

u/[deleted] Dec 19 '18

return !(a & 1);

41

u/uptown_whaling Dec 19 '18

return 1;

It’s right 50% of the time which is better than the current code....

29

u/Bone_Man Dec 19 '18

Current code returns correct answer when number is uneven. Which is 50% of the time. Plus it returns correct answer for few even numbers. Therefore current code returns correct answer more often than just return 1;

→ More replies (3)
→ More replies (1)
→ More replies (2)
→ More replies (1)

18

u/Andernerd Dec 19 '18

You can't be serious... it hurts to think that this person was probably paid more than me.

8

u/IguessUgetdrunk Dec 19 '18

Why do you think so?

14

u/Coopsmoss Dec 19 '18

Unemployed

5

u/Andernerd Dec 19 '18

Because my job is only a student job, I'm not payed a whole lot.

9

u/Isvara Dec 19 '18

Did you add more checks?

3

u/TheKoleslaw Dec 19 '18

Asking the real questions here.

7

u/pxOMR Dec 19 '18

You should return "Yes" instead of 1 and "No" instead of 0, it makes the code look cleaner.
/s

7

u/pootisEagle Dec 19 '18

// TODO: Add more checks.

10

u/Frichjaskla Dec 19 '18

Use the power of recursion to cover more ground

function isEven(int a) {
  if (a < 0) return false;
  if (a == 0) return true;
  return isEven(a-2);
}

5

u/[deleted] Dec 19 '18

I’m literally suffering from an aneurysm right now. Thanks I hate it

1

u/Okkero Dec 20 '18

You should see a doctor :o

5

u/John_Fx Dec 19 '18

How many checks did you add?

4

u/[deleted] Dec 20 '18

If this person has a job, it gives me great hope about my future prospects. I mean how on earth do you spend more than 4 weeks learning CS and not know when to employ a for loop?

3

u/lordpuza Dec 19 '18

Just curious , how will you guys solve this? Best that I could think of IsEven and if less than 14 ?

6

u/Purple-Brain Dec 19 '18

Just do a % 2

Or if you want a Boolean, do a % 2 == 0

The % means ‘modulo’ and it takes the remainder of what you divide it by. So you’re essentially saying “return the remainder of ‘a divided by 2’”

And if it’s even then that would be 0 since 2 always divides perfectly into even numbers

5

u/LickableLemon Dec 19 '18

The best way would be to use an inline return statement with the modulus operator, like so...

public static boolean isEven(int a) {
    return a % 2 == 0;
}    

...which divides a by 2 and checks the remainder, if the remainder is 0 then we know that a is perfectly divisible by 2 and therefore is even.

The function signature has also changed to now return a boolean instead of an int.

5

u/BluudLust Dec 19 '18

return !(number & 1)

1

u/Totoze Dec 23 '18

Its stupid Java you can't use the '!' on an int

1

u/BluudLust Dec 24 '18

Then do number & 1 == 0

2

u/corner-case Dec 19 '18

Obviously the param ‘a’ is a 4-bit integer. I see nothing wrong with this.

2

u/Bioniclegenius Dec 19 '18

Wow, this is much better than the

public static int isEven(int a) {
    return (1 - (a % 2));
}

that it was before!

2

u/[deleted] Dec 19 '18

Obviously you just had to replace ifs with else ifs

2

u/globalvarsonly Dec 20 '18

Just rename the function isEvenNonNegativeIntUnderSixteen() Search and replace!

4

u/[deleted] Dec 19 '18

Modulus operator brah, come on.

5

u/_higgs_ Dec 19 '18

Boolean logic brah, modulus is overkill, come on.

1

u/[deleted] Dec 19 '18

Clarify.

14

u/8lbIceBag Dec 19 '18

Check if the last bit is set.

return a & 1 == 0

17

u/[deleted] Dec 19 '18

You're a bit wise man.

3

u/NatoBoram Dec 19 '18

That's genius. I can't imagine how much more processing power is needed to perform a division than just checking the last bit.

→ More replies (2)

2

u/_higgs_ Dec 30 '18

In most languages I’ve used the == 0 is unseeded too.

0

u/Noxium51 Dec 19 '18

Shouldn’t it be 0x1? Does using just an int work

12

u/audoh Dec 19 '18

0x1 is the same as 1.

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

3

u/AttackOfTheThumbs Dec 19 '18

Where do you work. Was this code in production?

1

u/[deleted] Dec 19 '18

At least use a switch statement then

1

u/abdolence Dec 19 '18

The most frightening thing here is that last comment.

1

u/ivanjermakov Dec 19 '18

Everything is so bad with this code

1

u/thesocialpenguin Dec 19 '18

add more checks?!?!

1

u/sebnukem Dec 19 '18

// TODO: terminate myself

1

u/sxule Dec 19 '18

This is my new test I'll give right before FizzBuzz. If they write more than one line, "Thanks for coming down. Now leave while you still can!"

1

u/[deleted] Dec 19 '18

Java doesn't have % operators?

2

u/HBK05 Dec 22 '18

It does. This person is either joking or ... say goodbye to our fallen brother in the pit of unemployment.

1

u/wildlybland Dec 19 '18

That looks like dumb shit I used to write in college just to get an assignment over with when I first started taking programming courses

1

u/theblood Dec 20 '18

so... you added more checks ? :D

1

u/ouattararomuald Dec 22 '18

Best part is the TODO

1

u/papacheapo Dec 29 '18

This is what happens when managers offer their help to get the product out sooner.

1

u/[deleted] Dec 29 '18

Modulo anyone?

1

u/elschaap Jan 29 '19

At least NPM has a package for this

1

u/Dasky14 Feb 05 '19
public static int isEven(int a) {
    int b = 0;
    while(true) {
        if (a == b) return 1;
        b += 2;
    }
    return 0;
}

FTFY /s

0

u/[deleted] Dec 19 '18

[deleted] — view deleted comment

13

u/fukitol- Dec 19 '18

It's because they are using it to create odd numbers oddNum = inputNum + isEven(inputNum)

11

u/TotallyHumanGuy Dec 19 '18

Horrifying fact.

In Python booleans can be treated as numbers i.e

1 + True == 2

13

u/TheThirdPerson_is Dec 19 '18

Wait so does that mean

(((1 + True == 2) == True) + 1 == 2) == True

????

5

u/r3jjs Dec 19 '18

In early BASICs it was common to treat booleans as numbers

hours = 23;  
hour = hour - (hours > 12 * 12)

rather than writing an if statement.

Somewhere I have a a single line of code that translates HH:MM:SS to h:mm:am with no 'if' statements, one nasty print statement using the above tricks.

1

u/Totoze Dec 23 '18

In C too