Well, it's got 275k items. An array in memory that only lives during runtime probably isn't the most effective way of managing this data. Having Redis or SQL/NoSQL might be more robust and faster
100% agree but we’re making some assumptions on the underlying operating system and hardware.
While Redis, SQLite or another similar product may be a better option, how do we know it is an option?
Either way using an array as a DB is bad, but depending on the reason for this large array it may sway my opinion if this is bad code or a necessary evil.
Probably trying to fix a broken house with an elastic band, but wouldn't i < names.Length + poison.Length be better off as i < names.Length && i < poison.Length
That wouldn't work, what they're checking is I is within the bounds of whichever array is shorter, you're checking if it would be within the bounds of both arrays combined
They're checking if i is lower than the sum of the lengths of both arrays. If there are 50 names and 25 poisons their code would allow i to reach 74, causing an out of bounds error as soon as i == 25 and thus exceeds the size of the potions array.
My code would stop when i == 25 as although i < names.Length evaluates to true, i < poison.Length will evaluate to false, thus making the && boolean operator return false and causing the loop to exit.
I'm other words, exactly the opposite way around from what you're saying.
Of course, their code appears to expect potions to be the larger of the two arrays, hence the error handling around the code which deals with the names array. So they could just use i < poison.Length - or, if they needed to read the entirety of both arrays and had error handling around the poison code they could use i < poison.Length || i < names.Length to check i is within the bounds of both arrays up to whichever is larger.
3
u/kristallnachte Jan 27 '22
Why the modulo here when it's adding the same thing to both commands[0] and commands[1]?
Maybe the order? I guess?
139
u/benjamistan Jan 27 '22
Poison isn't an array. It's a DB pretending to be an array.
35
u/SammyzABanana evil >:) Jan 27 '22 edited Jan 27 '22
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
21
u/madInTheBox Jan 27 '22
Do you lost 275k poisons, or the name suggest you should not touch that array?
21
u/Catch-Phrase27 Jan 27 '22
Wouldnt the two lines with "commands[i % 2]" and "commands[(i+1) % 2]" just be always equivalent to "commands[0]" and "commands[1]"?
Edit: ok I see the actual problem now
3
u/Buddy-Matt Jan 27 '22
Probably trying to fix a broken house with an elastic band, but wouldn't
i < names.Length + poison.Lengthbe better off asi < names.Length && i < poison.Length2
u/recycle4science Jan 27 '22
Nice VBA-style handling of errors accessing
namesthere. "Just try it and if it fails... go ahead."1
u/DemolishunReddit Feb 26 '22
Is it adding 275K choices for a user interface? What?