0

A good practice to prevent (Java) runtime exceptions such as ArrayIndexOutOfBoundsException is to write sanity checks such as the index position variable being within collection bounds.

People write sanity checks before code goes to production, right..? lol. When I think back to my dev days at shittier companies, I remember that one didn't write tests at all, nor sanity checks and then it fell all on me to resolve the masses of problems.

Comments
  • 0
    I wrote java code with no tests, went into production for basically a monopoly in a sector company in Canada. Ran for years. Never a bug.

    Was paid in basically minimum wage. Hardass boss guy memed about me being perfect. I wasn't allowed to write tests actually, our CEO told me he wanted to charge the client "extra" for that. Anyway then me asking for a raise makes me too much trouble. Wasn't even making industry standard pay for the title I had for 3 years by that point... Ah, life. You can be baller doesn't mean anybody else gives a fuck.

    Why try. Why bother. You can make the impossible happen but it is worthless.
  • 0
    @jestdotty (virtually) bug-free code without tests? You're a magician, jestdotty!

    Yeah.. you have to make sure you are happy and that your life is quality.
  • 0
    A better practice is to not access arrays via indices but instead iterate over the objects directly.

    I can’t remember the last time that I wrote code that could even theoretically produce out of bounds errors. Feels like a relic of a distant past.
  • 1
    To be fair, whether you write a sanity check that fails or access an array that fails, the result is still a catastrophic failure. The only thing you can hope for is a better error message or more useful Exception type being passed up.

    Dunno, in practice I write sanity checks for user inputs, but rarely for code where Im reasonably sure I got the it right. Or I just introduce constraints using max/min or other sane fallbacks with a warning in the logs that a fallback had to be taken for X or Y reason.
  • 1
    @Lensflare Ah, well, I've seen a lot of production code where there were a bunch of for(;;) loops with indexes. One of the arguments was that otherwise you wouldn't be able to modify or remove items in a forEach loop, but I see an Iterator allows updating and removal whilst traversing the collection. Interesting.

    Research here is telling me you can't delete elements in an array because in Java arrays have a fixed size. The Iterator would only work on Collections such as (Array)List. Then it also says: "There is no built-in way to replace array elements during iteration without knowing which slot you're modifying.". The chart is showing that Iterator only works on List (in Java) and if you have a classic Array in Java you're screwed on modification of elements.

    Imagine you have legacy code from 1996 where Iterators didn't exist.
  • 0
    You shouldn’t iterate and modify at the same time anyway, unless you are dealing with some performance critical algo and a huge dataset.

    Use for-each or iterators for iteration and functional style operators like filter() for "modification".
Add Comment