Slashdot Mirror


Developer Argues For 'Forgotten Code Constructs' Like GOTO and Eval (techbeacon.com)

mikeatTB quotes TechBeacon: Some things in the programming world are so easy to misuse that most people prefer to never use them at all. These are the programming equivalent of a flamethrower... [But] creative use of features such as goto, multiple inheritance, eval, and recursion may be just the right solution for experienced developers when used in the right situation. Is it time to resurrect these four forgotten code constructs?
The article notes that the Linux kernel uses goto statements, and links to Linus Torvalds' defense of them. ("Any if-statement is a goto. As are all structured loops...") And it points out that eval statements are supported by JavaScript, Python, PHP, and Ruby. But when the article describes recursion as "more forgotten than forbidden," it begs the inevitable question. Are you using these "forgotten code constructs" -- and should you be?

19 of 600 comments (clear)

  1. Doing it wrong? by marc.pn.beaupre · · Score: 5, Insightful

    Honest question: Am I not supposed to use recursion? Am I missing something?

    1. Re:Doing it wrong? by j_kenpo · · Score: 5, Insightful

      I'm just as baffled by this. I wasn't aware that recursion went out of style. Just another tool in the algorithm and design pattern toolbox. Did I miss the memo that it was taboo as GOTO?

    2. Re:Doing it wrong? by religionofpeas · · Score: 4, Insightful

      2) It's possible to write any traversal algorithm using loops, without any recursion.

      Sure, but if that requires building your own stack, you haven't really gained anything.

    3. Re: Doing it wrong? by Anonymous Coward · · Score: 3, Insightful

      Elegance.

    4. Re: Doing it wrong? by K.+S.+Kyosuke · · Score: 4, Insightful

      Every time you make a function call, some amount of bookkeeping data has to be stored on the stack

      Not necessarily, really, these days. But yes, if your compiler is really dumb, like in the 1970s, the difference can be significant.

      If you do "manual recursion", with a loop and a resizable container, then you can achieve lower overhead.

      Chances are that if you can do that easily, you should have done it in the first place, and if you can't, there was a reason for that.

      --
      Ezekiel 23:20
    5. Re: Doing it wrong? by K.+S.+Kyosuke · · Score: 3, Insightful

      They can make the code look neat but that also hides horrible performance bombs and security issues.

      Then they're lousy abstractions. Abstractions are here to hide irrelevant details that you'd get wrong if you had to do stuff by hand identically in many places and correctly all the time. Doing something right just once is the exact opposite of having a security issue.

      Regarding stack allocation, that ought to be O(1), anyway.

      --
      Ezekiel 23:20
    6. Re: Doing it wrong? by religionofpeas · · Score: 4, Insightful

      So you consider it easier to change perfectly running code (for what reason?) instead of fixing the compiler settings?

      Sometimes, yes. The project was for a medical application, with 99% of the code written by others. I'm not going to change global compiler settings and risk exposing some optimization error, when I can simply change a few lines of my code.

    7. Re: Doing it wrong? by Anonymous Coward · · Score: 5, Insightful

      Well, most compilers convert most recursions into loops anyway: it is called tail recursion optimization.

      Correction: *some* compilers will convert *some specifically structured* tail-recursive function calls into loops.

      There are lots of ways to make recursive function calls not tail-calls which renders them ineligible for compiler optimization.

    8. Re: Doing it wrong? by david_thornley · · Score: 4, Insightful

      Some algorithms are naturally recursive. For example, in-order traversal of a binary tree is easiest described as: deal with the left child, deal with this node, deal with the right child. Tower of Hanoi is easily solvable with: Move all disks above the one you want to move to the other peg, move the disk you want to move to the peg you want to move to, move the disks you moved earlier to on top of the disk you wanted to move.

      In these cases, if you use loops, you're going to be making up all the stuff recursion is good for, and you're going to be maintaining your own stacks. There's no advantage to doing this rather than using recursion. If you were going to get into a loop and recurse indefinitely, if you translate it into loops you're going to get into a loop and push indefinitely.

      --
      "When you have eliminated the unacceptable, whatever is left, however improbable, must be the truthiness" - Holmes
    9. Re: Doing it wrong? by allo · · Score: 4, Insightful

      That's a termination condition: If you ever visit a node, which was visited before, stop.

      You do not follow the symlink 1000 times and then abort. You follow it one time and the next time you see a mark "followed it" and stop. Without error as successful termination of a directory traversal.

      You think such a link would be an invalid condition, but it is actually not. And it isn't even a special case to the algorithm, which has the invariant "always take a node, which wasn't visisted yet until there are no such nodes".

  2. Re:Recursion is dead! by vux984 · · Score: 2, Insightful

    How is goto return better than just return?

    In C in particular, which is the ONLY place I'll use goto... i might have a pattern like something like...


    {
    a = malloc(something)
    if malloc failed goto e1
    b = malloc(something2)
    if malloc failed goto e2
    c = malloc(onemorething)
    if malloc failed goto e3 ...some code...
    open a file... if error happened goto e3 ...some code...
    some other error happened goto e3 ...some code... /*cleanup*/
    e3:
    free (a)
    e2:
    free(b)
    e3:
    return;
    }

    The goto sequence cleanly handles the memory free. Obviously you wouldn't want to just return after the 3rd malloc failed.

  3. Obvious answer by aaribaud · · Score: 3, Insightful

    It depends.

    In some cases, you want to allow goto statements, for instance because they help manage failure handling without adding condition or exception constructs.

    In some case, you want none of these gotos, because you are using processes or tools which are (partly or entirely) not compatible with them, and you need these tools to work more than you need gotos.

    In some cases, you don't want recursivity because the contex does not favor them (think embedded SW with restricted stack size).

    In some cases you want recursion because it makes code simpler and closer to the principles behind it, thus more maintainable.

    In some cases, you want class-like constructs in C be don't want C++ because the legacy code, people involved, time alloted, or general context just does not allow you to rewrite the whole thing.

    Etc.

  4. I know what will happen one day. by LordHighExecutioner · · Score: 4, Insightful

    Somebody will publish a paper entitled: "Class statement considered harmful." and he will be applauded as the new IT guru!

    1. Re:I know what will happen one day. by cheesybagel · · Score: 4, Insightful

      IMO class inheritance is useless. Interfaces and properties are a good idea though.

  5. Re:Recursion is dead! by Anonymous Coward · · Score: 4, Insightful

    You should read "GOTO considered harmful" before you bash it.

    "Most programmers have heard the adage "Never use goto statements", but few of today's computer science students have the benefit of the historical context in which Dijkstra made his declaration against them. Modern programming dogma has embraced the myth that the goto statement is evil, but it is enlightening to read the original tract and realize that this dogmatic belief entirely misses the point."
    http://david.tribble.com/text/...

    In the bad old days, all you had was goto, and every program looked like spaghetti. Now that we have if...then...else, loops, switch-case statements,
    goto should only be used as a last resort (and every use should be justified). I've been a professional programmer for twenty years; last year I used goto *twice*.

    And never forget https://xkcd.com/292/

  6. Stay away from the Knives, and the Stove too! by Anonymous Coward · · Score: 3, Insightful

    I'm tired of this mentality. I'd rather we favored those with skill rather than those with a lack of it. We as a people would go much farther.

    Do you cripple the use of bicycles by forcing everyone to ride with training wheels? Or do we in fact favor those who can ride and instead burden the new-comer with the difficulties around obtaining and installing training wheels on very poor low-end bicycles?

    Why should coding be any different? Sometimes people craft very complex and difficult pieces of software that tie together more than 20 libraries all which have their own quirks. I need the ability to share raw pointers, I need the ability to avoid ref-counting or shared_ptrs. I need to sometimes work with systems that have their own scheduler (Erlang, cough) and then bind C libraries into that ecosystem which doesn't allow blocking for more than 1ms. So I need crazy thread logic sometimes and odd code to support linking two separate mutex idioms from 2 different libraries so the lock works across the boundry....

    Sometimes I just wish to be left alone in a complex space where another soul's mere presence is essentially proof of their abilities and understanding of logic. Similar to how adults sometimes wish to leave behind children and mingle only with other mature adults, I desire this of a programming language. Something to scare away all the posers and poor misguided (but righteous and well meaning) individuals.... It's not elitist thinking just like Adults aren't really being rude when trying to mingle with other like-minded adults.... it's more of a time-saver for people who find that many "adults" are actually children in disguise and only after 30 minutes of talking can you determine they are fake. I grow tired of wasting my time and eventually wish to move to a place where it's harder for the fake to blend in. It was amazing going from finding 1-2 good people every 50 to instead finding 1-2 good every 10.

    For me that language has been C++ and simply put it's the most amazing thing I've ever discovered in my programming career. I also love how everyone still is scared to death of it and clamoring for it's deprecation while simultaneously using programs written in C++ to post these complaints.

  7. Re:You're probably using GOTO every day by Anonymous Coward · · Score: 3, Insightful

    Goto is considered harmful for humans. Who cares what happens under the covers. Hot liquids are harmful for humans. The fact that there are hot liquids under the covers when you drive your car is irrelevant.

  8. GOTO is dead.We live in COMEFROM age. by 140Mandak262Jamuna · · Score: 3, Insightful
    I wonder what Djikstra would say about the modern development API and the GUI. He railed against unstructured GOTO statements, back in 1968.

    But the entire modern GUI API is based on "event driven" programming. Replete with "OnRightButtonDown()" , "OnWindowClose()" ... . These are nothing but COMEFROM statements. COMEFROM could be as harmful or even more harmful than GOTO. With a good design based on a valid state machines and object oriented code we not only handle these with east, we are successfully developing incredibly complex code.

    So, no. We did not forget GOTO just because some authority figure railed against it. We replaced it with a better concepts like event loop, event dispatching, object orientation.

    --
    sed -e 's/Chuck Norris/Rajnikant/g' joke > fact
  9. Re:Some of these things are not like the others by lgw · · Score: 3, Insightful

    goto is vital to safe C code. You want to be able to jump to your clean-up code from each place something might go wrong. The alternative is to add another layer of indentation under an "if" for each place something might go wrong, the stuff of nightmares.

    --
    Socialism: a lie told by totalitarians and believed by fools.