4 ms·
Easily my favourite in the thread: >...i once had to maintain a codebase that dealt with a variety of data in 2d arrays. the previous author (over a decade pri
by jszymborski 3y ago
Easily my favourite in the thread:
>...i once had to maintain a codebase that dealt with a variety of data in 2d arrays. the previous author (over a decade prior) wasn't aware that you could simply myArray[x][y] to do lookups, instead they wrote nested loops for all indexes in each array, and would break the loop when the index matched the one they were looking for. they also didn't use helper functions, so this happened inline throughout the app
- marginalia_nu 3y agoTo be fair, there may be very good data locality reasons to prefer myArray[w*y+x] over myArray[x][y] if you're trying to construct a rectangular 2d array. The two forms are typically not equivalent. One is an array of arrays (i.e. double indirection), and the other is not. The loops I don't have an explanation for though.
- jszymborski 3y agoGood point! The loops, however, don't inspire the sort of confidence that would lead me to personally believe this person was concerned about data locality. I might be wrong though hehe I think that this is a good reminder, however, that there are few "absolutes" when it comes to programming and that there are always edge cases where some things that look like "code smells" or even look plain wrong have their place.
- amomchilov 3y agoIf you design your abstractions correctly, you can get the best of both worlds. You can create a simple Matrix type which stores single array, with a subscript operator that gives a 2D facade. It can let you call it with x/y pairs, but just lower that to an index into your single contiguous buffer.
- marginalia_nu 3y agoYes, depending on the language.