Goto fail;



  • @LoremIpsumDolorSitAmet said:

    @PJH said:
    I'll just leave this here...
    I prefer the IF-block version, actually. Also the first conditional can just return straight away without a label. Plus, there are other language features you can use to make this even better.

    Such as a function call and a switch-statement. E.g.:

    int init_device_resources()
    {
    	if (allocate_memory() != SUCCESS)
    		return INIT_ALLOC_FAILED;
    	if (setup_interrupts() != SUCCESS)
    		return INIT_INTERRUPTS_FAILED;
    	if (setup_registers() != SUCCESS)
    		return INIT_REGISTERS_FAILED;
    	return INIT_SUCCESS;
    }
    

    int configure_device()
    {
    // ...
    }

    int init_device()
    {
    switch(init_device_resources())
    {
    case INIT_REGISTERS_FAILED:
    teardown_interrupts();
    case INIT_INTERRUPTS_FAILED:
    free_memory();
    case INIT_ALLOC_FAILED:
    default:
    return ERROR;
    case INIT_SUCCESS:
    configure_device();
    return SUCCESS;
    }
    }


  • ♿ (Parody)

    @Faxmachinen said:

    Such as a function call and a switch-statement. E.g.:

    It depends on how you value easily understood (and robust) code vs how much you like comments such as, "you are not meant to understand this."


  • Discourse touched me in a no-no place

    @Faxmachinen said:

    @LoremIpsumDolorSitAmet said:

    @PJH said:
    I'll just leave this here...
    I prefer the IF-block version, actually. Also the first conditional can just return straight away without a label. Plus, there are other language features you can use to make this even better.

    Such as a function call and a switch-statement. E.g.:

    int init_device_resources()
    {
    	if (allocate_memory() != SUCCESS)
    		return INIT_ALLOC_FAILED;
    	if (setup_interrupts() != SUCCESS)
    		return INIT_INTERRUPTS_FAILED;
    	if (setup_registers() != SUCCESS)
    		return INIT_REGISTERS_FAILED;
    	return INIT_SUCCESS;
    }
    
    int configure_device()
    {
    	// ...
    }
    
    int init_device()
    {
    	switch(init_device_resources())
    	{
    		case INIT_REGISTERS_FAILED:
    			teardown_interrupts();
    		case INIT_INTERRUPTS_FAILED:
    			free_memory();
    		case INIT_ALLOC_FAILED:
    		default:
    			return ERROR;
    		case INIT_SUCCESS:
    			configure_device();
    			return SUCCESS;
    	}
    }
    
    Yay for function call overheads. Especially in kernel code that doesn't only get run once on initialisation.


  • @Faxmachinen said:

    Such as a function call and a switch-statement

    Wow, that is a great solution... Why confine variables to local scope when you can just make everything a global?



  • @orokanasaru said:

    @Faxmachinen said:
    Such as a function call and a switch-statement

    Wow, that is a great solution... Why confine variables to local scope when you can just make everything a global?

    Lets be fair, he could encapsulate all the variables in a struct and then copy it around for every single function call. That would obviously be far better.



  • @Faxmachinen said:

    @LoremIpsumDolorSitAmet said:

    @PJH said:
    I'll just leave this here...
    I prefer the IF-block version, actually. Also the first conditional can just return straight away without a label. Plus, there are other language features you can use to make this even better.

    Such as a function call and a switch-statement. E.g.:

    int init_device_resources()
    {
    	if (allocate_memory() != SUCCESS)
    		return INIT_ALLOC_FAILED;
    	if (setup_interrupts() != SUCCESS)
    		return INIT_INTERRUPTS_FAILED;
    	if (setup_registers() != SUCCESS)
    		return INIT_REGISTERS_FAILED;
    	return INIT_SUCCESS;
    }
    
    ...
    
    I do not like
    if (condition)
    
    statement;</pre>blocks. Have we not just spent days bagging Apple for the predictable consequences of using them?<pre>if (condition) statement;</pre>or<pre>if (condition) {
    statement;
    

    }

    please.



  • @flabdablet said:

    I do not like

    if (condition)
        statement;
    blocks. Have we not just spent days bagging Apple for the predictable consequences of using them?
    if (condition) statement;
    or
    if (condition) {
        statement;
    }
    please.
    So true. That's why this is an actual coding standard in many places I've worked. Fortunately, Eclipse also has code cleanup tools to automatically put them in, as well as static analysis which will bring up potential errors like this. Not sure if Eclipse works with Obj-C though.

     



  •  int init_device_resources()
    {
    if (!allocate_memory() != SUCCESS)
      if<font color="#000000"> </font>(!<font color="#000000">setup_interrupts</font>()<font color="#000000"> </font>!=<font color="#000000"> SUCCESS</font>)
        if<font color="#000000"> </font>(!<font color="#000000">setup_registers</font>()<font color="#000000"> </font>!=<font color="#000000"> SUCCESS</font>)
    return<font color="#000000"> INIT_SUCCESS</font>;
    else
    <font color="#000000"> </font>return<font color="#000000"> INIT_REGISTERS_FAILED</font>;
    <font color="#800000">else</font>
     return INIT_INTERRUPTS_FAILED;
    else
    r
    eturn INIT_ALLOC_FAILED;
     }

     

     

    FIXED!!!! 



  • @TheCPUWizard said:

     int init_device_resources()
    {
    if (!allocate_memory() != SUCCESS)
      if<font color="#000000"> </font>(!<font color="#000000">setup_interrupts</font>()<font color="#000000"> </font>!=<font color="#000000"> SUCCESS</font>)
        if<font color="#000000"> </font>(!<font color="#000000">setup_registers</font>()<font color="#000000"> </font>!=<font color="#000000"> SUCCESS</font>)
    return<font color="#000000"> INIT_SUCCESS</font>;
    else
    <font color="#000000"> </font>return<font color="#000000"> INIT_REGISTERS_FAILED</font>;
    <font color="#800000">else</font>
     return INIT_INTERRUPTS_FAILED;
    else
    r
    eturn INIT_ALLOC_FAILED;
     }

     

     

    FIXED!!!! 


    What if SUCCESS is 1 and the error codes are all ≥ 2?



  • @savar said:

    It's C. http://www.opensource.apple.com/source/Security/Security-55471/libsecurity_ssl/lib/sslKeyExchange.c

    Interesting. Apparently HashSHA1Update (alias SSLHashSHA1.update) always returns 0.



  • @fatbull said:

    @savar said:

    It's C. http://www.opensource.apple.com/source/Security/Security-55471/libsecurity_ssl/lib/sslKeyExchange.c

    Interesting. Apparently HashSHA1Update (alias SSLHashSHA1.update) always returns 0.

    At first glance, it looks like bloody everything in that file returns 0. I wonder if they've heard of the keyword 'void'...

     



  • Looks like Apple is not the only perpetrator of sloppy SSL certificate handling.



  • @article said:

    This time, instead of a single misplaced "goto fail" command, the mistakes involve errors with several "goto cleanup" calls.
    So now can we agree that goto should never be used? You might still say if-blocks are ugly, but ugly is better than wrong.


  • ♿ (Parody)

    @LoremIpsumDolorSitAmet said:

    @article said:
    This time, instead of a single misplaced "goto fail" command, the mistakes involve errors with several "goto cleanup" calls.

    So now can we agree that goto should never be used? You might still say if-blocks are ugly, but ugly is better than wrong.

    We could agree, except you brought up the concept of wrong. And you're wrong about never using goto.


  • Discourse touched me in a no-no place

    @LoremIpsumDolorSitAmet said:

    So now can we agree that goto should never be used?
    If you want to play at being Mrs Slocombe, you can be unanimous in that if you want to be. But otherwise, no.


  • Discourse touched me in a no-no place

    @LoremIpsumDolorSitAmet said:

    So now can we agree that goto should never be used?
    No. The problem is that what many people propose to use instead (e.g., absolutely monstrous if conditions) is even worse in terms of comprehensibility. Keeping the code understandable by programmers (and computers too) is more important than almost every other consideration.

    I've also seen people object to break, continue and return on the grounds that they're “like a goto!” Dijkstra would not have agreed…



  • @boomzilla said:

    We could agree, except you brought up the concept of wrong. And you're wrong about never using goto.
    I'm surprised you're all sticking up for a directive that should only be used at the assembly level. Oh well... Your loss. I'll just carry on using nice languages if you don't mind.

    @PJH said:

    If you want to play at being Mrs Slocombe, you can be unanimous in that if you want to be. But otherwise, no.
    I'm no movie buff, but I assume you mean working alone. Better than working with bad coders, in my opinion.

    @dkf said:

    No. The problem is that what many people propose to use instead (e.g., absolutely monstrous if conditions) is even worse in terms of comprehensibility. Keeping the code understandable by programmers (and computers too) is more important than almost every other consideration.

    I've also seen people object to break, continue and return on the grounds that they're “like a goto!” Dijkstra would not have agreed…

    Return is fine. Much preferred over having a 'returnvalue' variable held all the way through a method. Break and Continue are okay in moderation, but if you end up using more than a few in a single function, it's time to refactor.

    Seriously, code should be easy to understand, and describe the intentions of the programmer, not set up a minefield of conditions to wade through. I think you guys could learn a bit from Java and Scala, and the tools like Eclipse/IntelliJ that make refactoring and simplifying code quick and easy. Still stuck using C? It's a shame nobody has updated the syntax or capabilities of the compilers since 1800, but if you want to use try/catch/finally in C, there are ways.


  • Discourse touched me in a no-no place

    @LoremIpsumDolorSitAmet said:

    @PJH said:
    If you want to play at being Mrs Slocombe, you can be unanimous in that if you want to be. But otherwise, no.
    I'm no movie buff, but I assume you mean working alone. Better than working with bad coders, in my opinion.
    You assume wrong. It's to do with your opinion, and the fact that you assume we all (want to) share it. Which we demonstrably don't.


  • ♿ (Parody)

    @LoremIpsumDolorSitAmet said:

    @boomzilla said:
    We could agree, except you brought up the concept of wrong. And you're wrong about never using goto.
    I'm surprised you're all sticking up for a directive that should only be used at the assembly level.

    People who are wrong are often surprised at wrong things. This isn't a surprise to non-wrong people.

    @LoremIpsumDolorSitAmet said:

    Seriously, code should be easy to understand, and describe the intentions of the programmer, not set up a minefield of conditions to wade through.

    It's amusing that you can't see how a goto can occasionally cut through the crap and precisely make code easy to understand and spot and avoid these minefields.


  • Discourse touched me in a no-no place

    @boomzilla said:

    @LoremIpsumDolorSitAmet said:
    Seriously, code should be easy to understand, and describe the intentions of the programmer, not set up a minefield of conditions to wade through.

    It's amusing that you can't see how a goto can occasionally cut through the crap and precisely make code easy to understand and spot and avoid these minefields.

    It must be working with all those toy languages he uses. Turns the brain to mush.



  • @PJH said:

    You assume wrong. It's to do with your opinion, and the fact that you assume we all (want to) share it. Which we demonstrably don't.
    @boomzilla said:
    People who are wrong are often surprised at wrong things. This isn't a surprise to non-wrong people.
    You can demonstrate all you like and tell me I'm wrong, but think about this: why do none of the modern programming languages have goto? It's because we have better alternatives now. The professional developers and the committees that designed them learned from past experience of what made bad code. In the case of Java, James Gosling said goto was "needless". The only reason why they didn't take the concept further was because it was critical that their new language was similar enough to C++ as to allow developers to migrate to it.

    @boomzilla said:

    It's amusing that you can't see how a goto can occasionally cut through the crap and precisely make code easy to understand and spot and avoid these minefields.
    It might be useful on occasion, but I assure you there are alternative ways to deal with them that are just as elegant. You just need to take a step back and look at the higher level. Most of the time, the need to cut through a whole bunch of logic is due to error handling, and try/catch works perfectly well for that.


  • Discourse touched me in a no-no place

    @LoremIpsumDolorSitAmet said:

    It might be useful on occasion
    Thank you. @LoremIpsumDolorSitAmet said:
    but I assure you there are alternative ways to deal with them that are just as elegant.
    Debatable, but regardless, unfortunately, they tend to take either longer to execute, or take up a lot more memory than using goto (or both.) Neither of which are in any way acceptable under certain circumstances.



  • @LoremIpsumDolorSitAmet said:

    You can demonstrate all you like and tell me I'm wrong, but think about this: why do none of the modern programming languages have goto?

    You must be cherry-picking to fuck and back to not include C# in the list of "modern programming languages".



  • @PJH said:

    Debatable, but regardless, unfortunately, they tend to take either longer to execute, or take up a lot more memory than using goto (or both.) Neither of which are in any way acceptable under certain circumstances.
    I knew the subject of performance would come up again, and I don't really want to fall into that hole because I write applications where correctness and maintainability are more important than speed.

    Have you heard of the phrase "Premature optimization is the root of all evil"? Back when Java was slow, 'clever' developers would shave off milliseconds of execution time by making 'clever' changes to the code that usually resulted in poorer maintainability... and as the JVM technology improved, these techniques were quickly rendered obsolete, and in some cases, the code that used to run faster now ran slower than the simpler, original version. That includes exceptions - the JVM can now keep track of how often these occur and optimize itself at runtime so that the most commonly taken path is the fastest, whether that be exception throwing or not. Also, as far as I'm aware, the memory needed to keep track of an exception is little more than an object pointer for each thread stack.

    Now, on the other hand, if you have to write real-time trading systems, go nuts. Write in ASM. I'm just glad I don't do any of that.



  • @PJH said:

    @LoremIpsumDolorSitAmet said:
    It might be useful on occasion
    Thank you. @LoremIpsumDolorSitAmet said:
    but I assure you there are alternative ways to deal with them that are just as elegant.
    Debatable, but regardless, unfortunately, they tend to take either longer to execute, or take up a lot more memory than using goto (or both.) Neither of which are in any way acceptable under certain circumstances.
    Your compiler can't optimise your code for memory/speed when necessary? Perhaps you'd be better off writing in straight assembly, where pretty much every control structure is built using some sort of goto. Your compiler should be smart enough to do that on its own.



  • @blakeyrat said:

    You must be cherry-picking to fuck and back to not include C# in the list of "modern programming languages".
    Ah... C#... a clone of Java that only succeeded because of Microsoft. On the other hand, it's a lot better than VB.NET and classic ASP. Funnily enough, I'm writing a C# web app at the moment, so thanks for letting me know about the goto... I'll be sure to avoid it.



  • @LoremIpsumDolorSitAmet said:

    think about this: why do none of the modern programming languages have goto?
     

    If you're going to dispense wisdom, make sure what you're saying is factually correct, or at least somewhat supportable.

  • ♿ (Parody)

    @dhromed said:

    If you're going to dispense wisdom, make sure what you're saying is factually correct, or at least somewhat supportable.

    You must be new here.



  • @LoremIpsumDolorSitAmet said:

    Ah... C#... a clone of Java that only succeeded because of Microsoft.

    I assume there's some kind of negative implication here, but I'm failing to see what it is. Saying it succeeded because of Microsoft is pretty "duh", since Microsoft paid the language designers, built the IDE and other development tools, etc.

    @LoremIpsumDolorSitAmet said:

    Funnily enough, I'm writing a C# web app at the moment, so thanks for letting me know about the goto... I'll be sure to avoid it.

    Oh so your declaration that C# wasn't a modern language wasn't a result of bravado-fueled lying, but was actually a result of incompetence-fueled ignorance. Good to know.



  • @boomzilla said:

    @dhromed said:
    If you're going to dispense wisdom, make sure what you're saying is factually correct, or at least somewhat supportable.

    You must be new here.

     

    Apparently I have only slightly more than eleven and a half posts, so tha



  • @dhromed said:

    If you're going to dispense wisdom, make sure what you're saying is factually correct, or at least somewhat supportable.
    Oops! Guess I hit the pedantic trigger. Guess I should have said 'nearly none'. Regardless, just because it's there doesn't mean it was a good decision and certainly doesn't mean you should use it.

    Further reading (notice how many times they refer to the 1970s and 1980s in that section?)

     


  • ♿ (Parody)

    @LoremIpsumDolorSitAmet said:


    Oops! Guess I hit the pedantic trigger. Guess I should have said 'nearly none'.

    Oh, yeah, you mean like people who responded to you did? In other news: words mean things.

    @LoremIpsumDolorSitAmet said:

    Regardless, just because it's there doesn't mean it was a good decision and certainly doesn't mean you should use it.

    Further reading (notice how many times they refer to the 1970s and 1980s in that section?)

    I'm not sure why you think anything anyone else said here goes against those (ObPedanticDickweek: in a large SO thread you can find a multitude of opinions that are contradictory).


  • Considered Harmful

    @boomzilla said:

    ObPedanticDickweek

    I don't think it should take a whole dickweek.


  • Discourse touched me in a no-no place

    Ok, since some of us* are trying to be highly selective in our evidence lets have a look at some of it...@LoremIpsumDolorSitAmet said:

    Further
    @From that link said:
    A well thought out classic paper about this topic, to be matched to that of Dijkstra, is Structured Programming with go to Statements, by Donald E. Knuth.


    @LoremIpsumDolorSitAmet said:
    reading
    @From the second link said:
    Many programmers adopt a moderate stance: goto's are usually to be avoided, but are acceptable in a few well-constrained situations, if necessary: as multi-level break statements, to coalesce common actions inside a switch statement, or to centralize cleanup tasks in a function with several error returns

    Both of which contradict your opinion. Both of which you appear to be using as evidence for your opinion.




    • i.e. you.


  • @blakeyrat said:

    Oh so your declaration that C# wasn't a modern language wasn't a result of bravado-fueled lying, but was actually a result of incompetence-fueled ignorance. Good to know.
    In the case of goto, I believe ignorance is bliss. It's a shame I had to find out about it here, but ultimately it changes nothing.

    Anyway... I was just wondering again, why are so many people here championing goto? If this was an internal discussion amongst my fellow colleagues, we would have just talked it over for 2 minutes, laughed for a bit and carried on working, in the sensible, agile way we always have done. Then again, this is TDWTF forums, where personal insults mean more than making a good argument. I'll just let you guys carry on until a good argument in favor of goto is made, that doesn't have a structured programming alternative.


  • Considered Harmful

    @LoremIpsumDolorSitAmet said:

    Anyway... I was just wondering again, why are so many people here championing goto? If this was an internal discussion amongst my fellow colleagues, we would have just talked it over for 2 minutes, laughed for a bit and carried on working, in the sensible, agile way we always have done.

    The problem is just that you're wrong. You're perfectly entitled to continue being wrong, but we have some obligation to at least point out your wrongness for the benefit of whoever else might be lurking so that your wrong ideas aren't promulgated.


  • ♿ (Parody)

    @joe.edwards said:

    @boomzilla said:
    ObPedanticDickweek

    I don't think it should take a whole dickweek.

    Frankly, if it lasts more than four dickhours, you should see a doctor.


  • ♿ (Parody)

    @LoremIpsumDolorSitAmet said:

    Anyway... I was just wondering again, why are so many people here championing goto?

    No one is championing goto. We just enjoy insulting people who are wrong.

    @LoremIpsumDolorSitAmet said:

    I'll just let you guys carry on until a good argument in favor of goto is made, that doesn't have a structured programming alternativebecause I can't be trusted to read with comprehension.

    FTFY


  • Considered Harmful

    @PJH said:

    Structured Programming with go to Statements, by Donald E. Knuth.

    Yeah but that Knuth guy is such a hack. He doesn't know anything about programming.


  • Discourse touched me in a no-no place

    @LoremIpsumDolorSitAmet said:

    In the case of goto, I believe ignorance is bliss.
    @LoremIpsumDolorSitAmet said:
    Anyway... I was just wondering again, why are so many people here championing goto?
    It's your ignorance that's causing your confusion. You cannot conceive of a situation where it might be useful, and you are stubbornly refusing to accept that such situations exist, and instead pour forth with unsuitable solutions in order to avoid its use.@LoremIpsumDolorSitAmet said:
    Then again, this is TDWTF forums, where personal insults mean more than making a good argument.
    Reducing the symptoms of the Dunning-Kruger effect may reduce your feelings of being bullied.



  • @LoremIpsumDolorSitAmet said:

    Anyway... I was just wondering again, why are so many people here championing goto?

    It's already been discussed here several times. Loop within a loop, error condition requires breaking both loops-- goto is the only sensible way of doing this. Even JavaScript contains a goto-equivalent to be used for this exact purpose.



  • @blakeyrat said:

    @LoremIpsumDolorSitAmet said:
    Anyway... I was just wondering again, why are so many people here championing goto?

    It's already been discussed here several times. Loop within a loop,
    error condition requires breaking both loops-- goto is the only sensible
    way of doing this. Even JavaScript contains a goto-equivalent to be
    used for this exact purpose.



    I think LoremIpsum's point,
    and one that I mostly agree with him on, is that most of those
    situations aren't things that should occur in modern development. Not
    because they won't occur in design, but because there are better
    mechanisms for handling them that basically abstract away the goto for
    you. And by abstracting away the goto and sticking to a better known set of language functions, you avoid confusion.

    For example, the loop within a loop is better handled with an exception and a try catch. Not only does it maintain the same functionality, (a) it makes it clear that we are breaking because of an unexpected error condition, and (b) it maintains the brackets+indent method of code aggregation that improves readability.

    In other situations, code with a GOTO can be rewritten. This may involve the tradeoff of adding additional lines of code. However, in most cases in 2014, a few extra lines of code is not a problem, either for CPU clocks or for memory.

    Where he is wrong is with a blanket statement that goto is never needed. Every tool has its use, however narrow.


  • Considered Harmful

    @Snooder said:

    Where he is wrong is with a blanket statement that goto is never needed. Every tool has its use, however narrow.

    See, this was the problem I had with Douglas Crockford's Javascript: The Good Parts. He proscribes entire sections of the standard, and while his reasoning for it is mostly cogent, I feel the dichotomy between "good parts" and "bad parts" is a false one.


  • Discourse touched me in a no-no place

    @Snooder said:

    Where he is wrong is with a blanket statement that goto is never needed.
    If you have enough other, higher-level control-flow primitives, goto may be completely unnecessary. I'm not sure if your favourite language has sufficient primitives (or the ability to create new ones) but for sure C and C++ don't. When you don't have the high-level primitive for what you're really trying to do, you may need the low-level version instead (for that's what a goto is; it's what compilers produce lots of).

    For the record, having exceptions, auto-cleanup techniques, and coroutines (or first-class continuations) greatly reduces the need for working with a raw goto. The only case I'm aware of that isn't covered by that is a true state machine (and there are some ugly ways to hack that, such as a switch in a loop, but there are nasties in there that show a lack of proper integration).

    There is one other advantage to C specifically having a goto: it makes it easier to have the language be a target for a compiler.



  • @joe.edwards said:

    The problem is just that you're wrong. You're perfectly entitled to continue being wrong, but we have some obligation to at least point out your wrongness for the benefit of whoever else might be lurking so that your wrong ideas aren't promulgated.
     

     

    Someone is wrong on the internet



  • @oheso said:

    @joe.edwards said:

    The problem is just that you're wrong. You're perfectly entitled to continue being wrong, but we have some obligation to at least point out your wrongness for the benefit of whoever else might be lurking so that your wrong ideas aren't promulgated.
     

     

    Someone is wrong on the internet


    I don't get it.


  • Considered Harmful

    @Ben L. said:

    @oheso said:

    @joe.edwards said:

    The problem is just that you're wrong. You're perfectly entitled to continue being wrong, but we have some obligation to at least point out your wrongness for the benefit of whoever else might be lurking so that your wrong ideas aren't promulgated.
     

     

    Someone is wrong on the internet


    I don't get it.

    No I think it's going to get you.



  • Also, just for future reference, the proper way to embed xkcd 386 is as follows:

    Duty Calls



  • @LoremIpsumDolorSitAmet said:

    You can demonstrate all you like and tell me I'm wrong, but think about this: why do none of the modern programming languages have goto?

    Well, how "modern" would you like? "Go"? See  Why does Go have a “goto” statement. Or, Perl6? goto in the reference.. So, what's "modern" to you?

     

    @LoremIpsumDolorSitAmet said:

    It's because we have better alternatives now.
    Not always, and not often enough.

    You are probably looking for "old" features, like UNWIND-PROTECT, and macros to hide that cleanup code; these would commonly named with-*. Yeah, try/catch/etc. is now typical, but macros still are not ... and, IMO, I'm not sure how they could be integrated in languages that have syntax (ie. where the AST isn't written manually ;). [Yeah, I'm looking at you, Perl6.]



  • @orokanasaru said:

    Wow, that is a great solution... Why confine variables to local scope when you can just make everything a global?

    Are you high? There are no explicit variables in the code at all.


Log in to reply