Coding a batch process with numerous steps that can succeed or fail, while keeping control in calling program whether the whole process has succeeded and, if not, knowing the reason why, can end up with an unreadable program because of the multiple if ... endif or do case ... case ... endcase indented blocks.
Using if m.success... endif ends up with unreadable nested indentations and makes it difficult to change the tasks order:
Using if !m.success... endif repeats code and adds multiple returns, but makes task order changes easier:
Using do case ... case ... endcase requires sub-procedures and is still uselessly verbose and somewhat difficult to understand as it relies on negations:
We found a convenient way to make program more readable by:
moving all concrete work to sub-procedures using a standard scheme for naming, parameters and returned value
chaining the calls to sub-procedures into a single logical expression
Let’s say main procedure is do_task with step1, step2, etc.
First create one procedure per step:
Then create the main procedure:
Very convenient is the ability to change the tasks’ order by just swapping 2 lines.
Of course, do_task_stepX() could be further broken into do_task_stepX_phaseY, etc.