Hello, maybe I will show up. But otherwise I don’t even know how to describe such a question.

We have a giant function for calculating a bunch of values. At the beginning of the function, many constants and values ​​are accepted, but one of the variables will be calculated closer to the end of the function. It turns out that it is unknown at the beginning. The unknown of this number at the time of passing the function does not affect its calculation. but many variables at the moment of its declaration and its calculation are calculated taking into account this variable, but we obtain its value only at the end of the function. The question is, we need to get the value of the variable at the beginning, which is calculated approximately at the end of the function. Everything happens in the limit of a single function block. There is such a thing in the swift, we write the code and oops we need to jump over a bunch of lines (I don’t remember how it gets called "I would be grateful if someone reminds me"), we write the transition command to the code key (B), marking where in the code this place is. And we go to the place with the key (B), if we don’t need to go back, we don’t write anything, and if we need to go back, we write this code to go to the key (A), but also prescribe the place where this key is (A). If you specify it before jumping to the key B or do not close the transition code from its pass to the transition line to the key (B), the program will open. At the same time, all changes of variables, those that are not inside the internal blocks, are declared in the beginning in the memory. Roughly speaking, we run everything we calculate, then we put the transition team, so as not to pass twice, close it with an if, to get there once. We return to the beginning, already with the knowledge of this variable, and calculate everything anew.

How to implement it in C ++.

  • 2
    It seems to me that this is a consequence of the poor architecture of the function, break it into small ones and / or swap the order of calculations. Specifically on the issue - goto but this is not welcome. - pavel
  • one
    "The unknown of this number at the moment of passing the function doesn’t affect its calculation, but many variables at the time of its declaration and its calculation are calculated taking into account this variable" - what does this mean? Do you have a convergent iterative process that is not sensitive to the initial value of one of the variables? - jfs
  • Yes, I understand, this is a blunder, and not a quality writing, I understand that. I would not have written such a thing myself, but I can't rewrite it. Therefore, it is necessary to resort to such manipulation. - JoMax

2 answers 2

You gave a textbook example of the disgusting noodle-style code — a huge sheet of computation that doesn’t understand how it works. And at the same time, you want to make the situation even worse by closing the transition to the tag.

The correct approach is to properly structure your piece of code.

Structural programming was invented by E. Dijkstra especially for such "clever men" who cannot program just as with a stream of consciousness. He also wrote a great explanation for why GOTO should be sent to the garbage along with the programmers who use it. And even with programmers who use it instead of exception handling.

  • I understand it all. But the code is not mine and I was asked not to edit the global one. When I got it, I spoke exactly like you, but I was asked not to touch anything, just like in the joke about the programmer’s son. - JoMax
  • one
    @JoMax: I understand that any change in this function can introduce a bug that you cannot predict (due to the convoluted structure). Using goto will generate more bugs. Therefore, do not be afraid to radically simplify it, applying the best practices that probably became predominant even before the birth of your parents. Naturally, you should thoroughly test all changes (write new tests if they are not there and / or extend old tests). You can understand the logic when people resist the complete rewriting of curves, but working projects. This logic does not apply to individual functions . - jfs
  • You're right, I'm going through the debugs right now, I'm not looking for bugs? after use, the goto procedure is said to be invalid. But if at least one bug pops up, I will definitely rewrite it. Thanks for the very good advice. @jfs - JoMax

Looks like you want to use the setjmp/longjmp bundle. But this desire speaks about one thing - about the low quality of the code. Redo it normally, and note that the classics - the same Stroustrup - give very good advice: the source code of one function should fit on one screen . Otherwise, this is a poorly thought out function.

Programming is not only coding, but also design.

  • As I unsubscribed already under each. I completely agree with you and at the sight of this code I expressed my opinion just like you are telling me now. But I was asked not to change it globally, but simply to add some things to certain places. - JoMax