r/badcode • • Mar 16 '21

java Ahh yes. Debugging is overrated

Post image
1.4k Upvotes

43 comments sorted by

398

u/HappyMonk3y99 Mar 17 '21

Ah yes let me just decrement by 1 until I reach 1 less than a known value, this couldn’t possibly be written any better

208

u/miseleigh Mar 17 '21

They did say they don't care

154

u/[deleted] Mar 17 '21

That sounds like an early comedy sketch. "Is 99 equal to 40? No. Is 98 equal to 40?"

64

u/HappyMonk3y99 Mar 17 '21

Oh the classic beer on the wall sea shanty

interval_index bottles of beer on the wall, interval_index bottles of beer, take one down and pass it around, interval_index-1 bottles of beer on the wall.

interval_index-1 bottles...

I have to say, I think I prefer the original

14

u/bob_mouse Mar 17 '21

That's because you didn't assign the value back to interval_index. Maybe the song gets better with

"... pass it around, interval_index = interval_index - 1 bottles of beer on the wall"

17

u/bigdatacrusher Mar 17 '21

And they were already getting the size. Why not just do it right then and there.

80

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Mar 16 '21

Image Transcription: Code


// calculate which interval the value belongs to
int interval_index = (int) ((measurement_i - min_value) / width);

// For some reason sometimes interval_index overshoots and causes IndexError
// Instead of pulling my hairs out I've decided that I don't care
while (interval_index >= available_interval_start_values.size()) {
    interval_index--;
}
// obtain interval value key
float interval_key = available_interval_start_values.get(interval_index);

I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!

36

u/lmestre14 Mar 17 '21

Good human

150

u/ZedTT Mar 17 '21 edited Mar 17 '21

Ok, at least it's commented.

However, it should at least be a TODO, and the while loop with -- is silly.

Just set it to the max value you're ok with it being, because that's what the loop does anyways, just way slower.

Basically not only did he put in a hacky solution (which I could understand), he put in a stupid hacky solution.

// Because I'm doing something weird with integer division and casting, sometimes interval_index overshoots and causes IndexError
// TODO: learn how integer division works and fix this before it causes more bugs
// In the meantime, this stops the program from throwing exceptions
if (interval_index >= available_interval_start_values.size()) {
    interval_index = available_interval_start_values.size() -1;
}

edit: added -1 (go upvote /u/The_JSQuareD please)

82

u/The_JSQuareD Mar 17 '21

-1

28

u/ZedTT Mar 17 '21

Oops! Thanks.

I actually thought of that Originally but it's hard to code on mobile in a reddit comment.

34

u/SanianCreations Mar 17 '21 edited Mar 17 '21

Or just use Math.min()

interval_index = Math.min(interval_index, available_interval_start_values.size() - 1);

Edit: changed Min to min, this is java not C#

7

u/ZedTT Mar 17 '21

Much better than mine :)

-21

u/DurianExecutioner Mar 17 '21

No. The programmer should have to consult documentation and header files as little as possible while reading or developing code.

interval_index = interval_index > available_interval_start_values-1 ? available_interval_start_values-1 : interval_index ;

Is much clearer.

15

u/SanianCreations Mar 17 '21 edited Mar 17 '21
  • Java has no header files

  • Math is a standard library, with that logic you might as well not use System.out.println()

  • Math.min is about as straight forward as it gets, just about any math library across any language will have a Min method/function and they all do the same thing.

1

u/darksounds Mar 17 '21

Where do you think you are?

8

u/SanianCreations Mar 17 '21 edited Mar 17 '21

On r/badcode in a comment thread where people try to give genuine advice on how to improve said bad code

-3

u/DurianExecutioner Mar 17 '21
  • Java's questionable design is not my responsibility, the fact that its interfaces are not easily viewable without web browsers and god knows what makes no difference to the essence of the point being made, don't be a pedant.

  • I should jolly well hope that println is banned, it's performance is abysmal

  • C does not have an integer min function why because it is not necessary.

1

u/SanianCreations Mar 17 '21

Okay. So you just reinvent the wheel every time you need one?

I'll be honest, I really don't care if someone uses some library or a ternary conditional operator like you proposed in order to clamp a value but when you start applying what you're arguing here to larger tasks you really just end up wasting time in return for minimal performance and/or readability gain.

If a library uses functions with logical names then that should be enough to understand what's going on, Math.Min is a pretty clear example of that. If you're using a more complex function then reading the source code in your own project files is arguably just as easy as looking up the library documentation. But if not, even if your own code is so clean people can absorb what it does just by glancing at it, you're still not saving any time because writing a complex function by hand takes far longer than the extra time it takes to look up library documentation.

I'm done arguing, if you're not convinced then let's just agree to disagree. Have a nice day, I'm going to bed now and hopefully not dream about pointless discussions, I'd much prefer dreaming about cute and soft sheep or something. Or dogs.

1

u/DurianExecutioner Mar 18 '21

Dogs considered harmful

EDIT: never, EVER trust API naming conventions to not be misleading. Always read the documentation; or, better, the header files.

1

u/[deleted] Mar 20 '21

If you have an IDE like IntelliJ IDEA you don’t need a web browser, this isn’t the 90s.

I’m sure there are more efficient Java printing libraries than the standard sout.println, but in like 99% of applications who cares? It’s not like computers only have MB of ram.

C is a horribly outdated language that should be abandoned in favor of newer, more memory-safe languages like Rust, Zig, D, etc. unless absolutely impossible to avoid.

7

u/ZedTT Mar 17 '21

This is some kind of sarcasm, right?

1

u/darksounds Mar 17 '21

This is r/badcode.

1

u/ZedTT Mar 17 '21

I honestly don't see your point. Please explain.

2

u/darksounds Mar 17 '21

This is a subreddit for bad code. The person you responded to provided bad code.

50

u/runnerx01 Mar 17 '21

I can see the thought process now...

Ok... sometimes it’s off by 1.... hmm. Alright I’ll just use an if and subtract 1...

But what if it goes over by more for some reason...

Ok, I can just subtract 1 as many times as I need.

33

u/MarquisDan Mar 17 '21

This is amazing. Might be my favorite post on the subreddit

11

u/[deleted] Mar 17 '21

Is there a sub for funny programming comments? Seems like something reddit would have.

8

u/myfunnies420 Mar 17 '21

"for some reason"

7

u/rokyed Mar 17 '21

i was really not paying attention and then it clicked when i was that whatever = size -1 could have saved the code feom hanging from time to time hahahaha

7

u/Duckslikebreads Mar 17 '21

HavingHair > NoHair

9

u/chrisnlnz Mar 17 '21

At least they're honest and the comment will help anyone who does care, understand why.

1

u/fugogugo Mar 17 '21

what if interval_index minus tho?

1

u/[deleted] Mar 18 '21

Not gonna happen. The data should've been unsigned but java don't have unsigned

1

u/Flyberius Mar 17 '21

Is it a rounding error?

2

u/[deleted] Mar 17 '21

Math.floor would solve this problem

2

u/kukisRedditer Mar 17 '21

I don't care how bad is that code, the comment made the post so much better lol

1

u/RemusWT Mar 17 '21

What is that color scheme

1

u/DoodleFungus if ((x == 5) == true) Mar 17 '21

The default one from IntelliJ. Dracula, I think?

1

u/gabryGone Mar 17 '21

at least there are comments

1

u/Blackhaze84 Mar 17 '21

how I met windows ME

1

u/VariantArray Mar 23 '21

At least they commented it....