3 ms·
That violates the DRY (don't repeat yourself) principle. Repeatedly entering the same parameter declarations is busy-work, wasting labor and valuable screen sp
by Daniel_Newby 17y ago
That violates the DRY (don't repeat yourself) principle. Repeatedly entering the same parameter declarations is busy-work, wasting labor and valuable screen space.
A better way is to use nested functions. Long-lived state goes in the outer function's variables, so you do not have to repeatedly declare them. Ephemeral variables go in the inner functions. Sub-tasks get encapsulated with zero boilerplate.
If your language does not have nested functions, use a worker class. The algorithm-wide state goes in member variables, initialized from constructor parameters. The sub-tasks are methods. A benefit is that sub-tasks from different jobs can be batched to better use the instruction cache and branch prediction information: a.do_step_1(); b.do_step_1(); a.do_step_2(); b.do_step_2(); ...
- eru 17y agoYes, nested functions are probably good for this. (I was about to suggest use of the reader monad in the original comment, but then left out this suggestion for being to arcane (and did not include the nested function suggestion either).) Nested functions are good for this kind of code, even when they are only used once and do not abstract --- because you have less variables in the innermost scope. (And when you are going to mutate variables at all, then strongly prefer to do it to variables in the innermost scope.)