During code review, a minor suggestion was presented that some implicit behavior be made explicit. The reviewer had skimmed over the code in question, became confused after mistakenly interpreting the code's purpose, and only discovered the cause after some careful analysis. Below are the implicit practice and suggested practice, in that order:
stuffLookup = {--[[functions in here]]}
function doStuff(stuffType, ...)
assert(stuffLookup[stuffType], "stuffType is not a valid stuff!")(...)
end
function doStuff2(stuffType, ...)
if stuffLookup[stuffType] then stuffLookup[stuffType](...) else error("stuffType is not a valid stuff") end
end
assert
is frequently used without invoking another function as in this case, so at first glance the reviewer did not see such a small invocation at the end of assert, and assumed the line served no purpose past a sanity check.
The argument in favor of practice 1 was that it's syntactically correct, that it's used in Lua docs, that it's faster, and that if someone didn't anticipate assert being used like that it was their own fault. The argument in favor of practice 2 was that the nature of practice 1 could easily be misunderstood, that it was an implicit idiosyncrasy that wasn't legible enough, and that considering performance was premature optimization (code was not designed to be used frequently and table lookup time was negligible).
Are there any merits to the concerns and viewpoints of either party?