I'm writing something that needs to implement some tax computations and I want to implement them to follow as closely as possible the forms that are used to report those computations to the government. That way it is easy to be sure they are correct and easy to update them if the rules change.
The way those forms work is something like this:
1. Enter your Foo: _________
2. Enter your Bar: _________
3. Add line 1 and line 2: ________
4: Enter your Spam: _______
5: Enter the smaller of line 1 and 4: _____
6: If line 5 is less than $1000 skip to line 9
7: Enter the smaller of line 2 and $5000: _____
8: If line 7 is greater than line 4 skip to 13
...
With goto you can write code that exactly follows the form: Line1: L1 = Foo;
Line2: L2 = Bar;
Line3: L3 = L1 + L2;
Line4: L4 = Spam;
Line5: L5 = min(L1, L4);
Line6: if (L5 < 1000) goto Line9;
Line7: L6 = min(L2, 5000);
Line8: if (L7 > L4) goto Line13;
...
For some forms an if (X) goto Y
....
Y:
can be replaced by if (!X) {
...
}
because nothing before that has a goto into the body of the if statement. But some forms do have things jumping into places like that. Also jumping out of what would be such a body into the body of something later.Writing those without goto tends to require duplicating code. The duplication in the source code could be eliminated with a macro system but don't most memory safe languages also frown on macro systems?
Putting the duplicate code in separate functions could also work but often those sections of code refer to things earlier in the form so some of the functions might need a lot of arguments. However the code then doesn't look much like the paper form so it is harder to see that it is correct or to update it when the form changes in different years.