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
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
-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
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
11
8
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
9
u/chrisnlnz Mar 17 '21
At least they're honest and the comment will help anyone who does care, understand why.
1
1
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
1
1
1
1
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