5 ms·
He uses GOTO quite liberally!
by supportengineer 3y ago
He uses GOTO quite liberally!
- pjscott 3y agoIt's the normal use of goto in C: you run into some exceptional circumstance, you don't know how to deal with it, so you goto some code that cleans up any unfinished business you might have, like memory allocations – and then you return some error code as soon as you can, once everything is in a good state.
- flohofwoe 3y agoGoto in C is the tamed and civilized structured-programming goto, it has not much to do with the "considered harmful" unstructured goto from days of yore (e.g. you can't jump out of a function into a completely unrelated part of the program with C's goto like you can in BASIC.
- Mikhail_Edoshin 3y ago'goto' is more universal and may make cleaner code even in loops. E.g. to loop backwards without 'goto' one has to resort to an idiom like that: for (i = n; i--; ) and I wouldn't say this idiom is that clear. You can get used to it, of course. Another interesting case is printing a list of items with separators. C loop constructs are biased cases of frequently used loops but they do not cover all the possible cases. 'goto' is unbiased. A similar case are Python loops. There are “Pythonic” ways: for item in iterable ... They look sweet but are useful only in some cases. E.g. if you need to modify `iterable`, the Pythonic way won’t work. There are Pythonic workarounds, but they may involve copying a whole iterable, which is hardly a good idea. But there is also a less Pythonic way: i = 0; n = len(iterable) while i < n: ... This works all the time and serves all possible cases.
- p4bl0 3y agoNothing to important but, just for readers who may be unfamiliar with Python, your last example can be rewritten in a quite idiomatic Pythonic way: for i in range(len(iterable)): ...
- lou1306 3y agoOr, in case you need both `i` and `iterable[i]` within your loop: for i, item in enumerate(iterable): ...
- Mikhail_Edoshin 3y agoYes, all this works. These are Pythonic ways. But see, here the `enumerate` function will allocate a new number and a tuple for the number and the item. Then `i, item` will unpack that tuple and deallocate the tuple memory. All this happens quickly (in a loop it may be essentially the same memory for every tuple) and CPython is well optimized for that, of course, but it is not free. With an integer cursor (`i`) there are no unique numbers for each item and no temporary tuples. This is slightly cheaper. And also more flexible, which is the main point I'm trying to make. Other loop constructs are meant to be used in certain way and will not work otherwise. Integer cursors are not meant to be used in any special way, you can combine them as you wish. They give the maximum possible flexibility from the beginning. You can loop in any direction, by multiple items or by a variable number of items, over many iterables at once, changing the iterables as you go, and so on. (A special case in Python are list comprehensions; these are useful because most of that “comprehending” happens inside CPython and thus they are faster.)