How the Grinch stole array.length access

I never sacrifice maintainability for performance.

I actually can't particularly disprove this statement. Since the example we're talking about is you sacrificing maintainability for nothing. Either that or for the reason of "I will do complex/error prone things because I shouldn't be afraid to"?

and the main cost of maintenance is related to # of lines of code, not to the format of any specific line of code.

That is completely, utterly, and demonstrably false. Which line is more costly to maintain:

  1. if(i) {


  1. if((i & 0xFF) ^ 0x1E)) { i <<= 3 ^ j; ...

the mere fact that it takes you longer to read the second one proves it costs you more to maintain. If your code proportionally has more lines like #2, your code base can be much more costly to maintain. This doesn't even begin to start to explain how wrong that statement is since your architectural decisions can also increase maintenance costs (think structured programming vs goto/spaghetti code) as well as numerous other things that programmers have learned in the past 50 years.

All other things being equal, more LOC is more costly to maintain, but decisions like choosing things that are confusing/error prone and not immediately conceding that perhaps it's not the best way to go when 2 or 3 people get it wrong back to back tell quite a lot about your preference for per-line maintainability.

I've never found a common case that needed the xor case, but if I did I would either write my own swap function (or just consider std::swap at that point, since I'm pretty sure it does the trick under the hood).

The point of my post is that you wouldn't do that trick, ever (well, unless you're on a platform that can't compile it down into a single instruction and you certain that saving the ~4-bytes of stack space will make a significant difference ... maybe the stack is particularly expensive to access?). Because it's harder to understand and slower. It's more complex and less maintainable for no reason other than it being a neat trick for neat trick's sake.

Basically, it's a trick that only exists for programming interviews nowadays.

/r/programming Thread Link -