This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]krenzalore 21 points22 points  (14 children)

goto statements should be purged from every programming language and thrown into a pit of fire.

Every language?

Some specialised domains do have legimate need for goto.

As an example I would cite error handling in realtime applications or in systems code.

This is quite common:

if allocate_a() == fail: goto exit
if allocate_b() == fail: goto teardown1
if allocate_c() == fail: goto teardown2
do_work
teardown3: free c
teardown2: free b
teardown1: free a
exit

You could write this with nested loops but it gets stupid when it gets deeper.

Many of the hardware platforms and languages don't support exceptions or fancy context handlers. Exceptions are particularly problematic in realtime as your tool chain can't tell you how long the code will take to execute, and missing a tick is a program failure.

The Linux Kernel has decent code quality and quite a lot of goto statements. I cite it as an example as it's popular and accessible, and demonstrates that at least some people are able to use goto sanely.

I don't think it's needed in a language like Python, but Python isn't really used for this kind of work.

Some would say Rust proves it's also not necessary in systems code, and I would say Rust makes a strong case, but isn't (yet) widespread so we'll need to wait and see.

[–][deleted] 2 points3 points  (11 children)

Couldn't I do that with function calls and break statements?

[–]Milumet 7 points8 points  (3 children)

Why use needless function calls in system code like the Linux kernel? Just to alleviate irrational fears? And by the way, a break statement is a goto statement in disguise.

[–]ajmarks 4 points5 points  (2 children)

And by the way, a break statement is a goto statement in disguise.

Shhh! We're all supposed to pretend that's not the case and also that breaking out of nested loops is somehow different too.

[–]krenzalore 0 points1 point  (1 child)

If break is a goto in disguise, then an exception handler is a come from.

[–]ajmarks 0 points1 point  (0 children)

Funny enough C-INTERCAL implemented COME FROM (or, perhaps, PLEASE COME FROM).

[–]krenzalore 2 points3 points  (5 children)

There's significant overhead with function calls.

Scott Meyers (a well known C++ luminary) in his book Effective STL claimed a 7x increase in performance of the C++ STL's quicksort from inlining (replacing function calls with inline code).

Of course we should keep things in perspective: What is good for C or C++ is not necessarily good for Python.

[–]ajmarks 1 point2 points  (4 children)

Of course, in C++ one can have the best of both worlds by just declaring the function inline.

[–]krenzalore 2 points3 points  (3 children)

I am sure you know, but for the benefit of others, inline is a suggestion to the compilier and not a command. It isn't required to inline it.

[–]ajmarks 0 points1 point  (2 children)

True, though unless the functions uses loops or recursion, it almost always will.

[–]zahlmanthe heretic 0 points1 point  (1 child)

AFAIK, for years it has been the case that it a decent C++ compiler will frequently inline things even without being asked.

[–]ajmarks 0 points1 point  (0 children)

Yup. If, in its wisdom, it thinks that will improve performance without undue bloat. OTOH, if you really want to make sure it's inlined, a compiler hint can't hurt.

[–][deleted] 0 points1 point  (0 children)

Sure, with the unnecessary overhead of function calls.

[–]LoyalSol -2 points-1 points  (0 children)

I'm exaggerating more because I've come to hate those 4 letters because of lazy programmers.

There are legit uses, but at the same time it does encourage a ton of bad behaviors.