C: Nested Ifs or Gotos

后端 未结 5 2234
温柔的废话
温柔的废话 2021-02-15 03:45

What is the best way to manage resources for a C program. Should I use a nested if structure or should I use goto statements?

I am aware there is a lot

5条回答
  •  被撕碎了的回忆
    2021-02-15 04:23

    Cleanup using goto has the advantage that it's less error-prone. Having to free each and every resource allocated on each and every return point can lead to someone someday missing some cleanup when doing maintenance work.

    That said, I'll quote Knuth's "Structured Programming with goto Statements":

    I argue for the elimination of go to's in certain cases, and for their introduction in others.

    and Knuth's quote of Dijkstra in that same paper:

    "Please don't fall into the trap of believing that I am terribly dogmatical about [the go to statement]. I have the uncomfortable feeling that others are making a religion out of it, as if the conceptual problems of programming could be solved by a single trick, by a simple form of coding discipline!" [29].

提交回复
热议问题