Our code culture is emphasizing the wrong things to aspiring competitive programmers. It's sick, really.
That assertion would probably require some elaboration as to what competitive means.
You're right. I probably should have written
"Our code culture is emphasizing the wrong things to aspiring programmers interested in honing and comparing their skills against others'." or something like that: I meant "competitive" as in "interested in competitions".
I do not see any problem in a person being competitive (in the sense of being interested in competing against others); I consider that a natural and often healthy aspect of many humans. I would just prefer the object/goal of the competition to be better/healthier; more suited to the actual needs of the industry and society in general.
As it is, I've only encountered code competitions where the goal is minimal runtime (on a specific hardware and compiler and compiler options), or minimal code size (code golf). Neither alone is a healthy metric.
These days, [productivity is] usually a very simple metric which measures how fast a given developer can implement a given feature. That's a very short-sighted approach. Beyond the code quality issues, the long-term cost can actually be much higher than taking a bit more time writing better code.
So very true, and so very depressing, in my opinion.
In the context of the question at hand, the above means that we really should ask a bit more general question:
how should
one write this kind of code in JavaScript?
I believe we (as in members partaking in this thread) have pieced together a reasonable answer in two parts:
1. Use a helper function per variable, to minimize code duplication
2. Use an intermediate table of larger size, to reduce lookups and code size at the cost of a larger table
My own suggestion would be to precalculate 65536-entry intermediate table
z as shown above by oPossum, with helper function
function zmap(x) { return z[x & 0xFFFF] + z[(x >> 16) & 0xFFFF]; }(with the hope that any JS engine compiles/interprets the calls to that function into efficient array lookups), and refactor the code that assigns the values to variables
A through
G so that instead of
var A = ...;
var B = ...;
var C = ...;
var D = ...;
var E = ...;
var F = ...;
var G = ...;
var i = zmap(A) + zmap(B) + zmap(C) + zmap(D) + zmap(E) + zmap(F) + zmap(G);
we'd have
var i = 0;
i += zmap(...);
i += zmap(...);
i += zmap(...);
i += zmap(...);
i += zmap(...);
i += zmap(...);
i += zmap(...);
and, if possible depending on the "..." code, merge the
i += zmap(...); lines into a
for loop.
My core point here is that we don't really have enough information here to suggest an exact form, because the suggested form depends on the surrounding code, and we don't know it. (Syntax Error obviously omitted the full code to avoid copyright issues, so I'm not blaming them at all.)
So, to someone like me, who
really wants to help others write "better" code – "better" as in choosing an appropriate algorithm, and implement it in a robust, efficient manner on the given architecture/language – the "challenge" part in the title is quite depressing. It is like hearing high-school students interested in sports discuss what kind of anabolic steroids they think they're taking, the dosages, and where on the shady net they're buying the stuff; all without medical (or even adult) advice, or care about the long-term effects of such intake will have.
It is interesting, but to even try to sway the thread into a, uh, "healthier" path, I feel like I'm butting in and preaching to people who really don't want to hear it. Apologies.