Author Topic: XKCD was interesting today  (Read 12630 times)

0 Members and 1 Guest are viewing this topic.

Offline IanB

  • Super Contributor
  • ***
  • Posts: 9681
  • Country: us
Re: XKCD was interesting today
« Reply #25 on: November 17, 2015, 03:26:27 am »
I have read a fair bit about this on stackexchange and other sites, and it seems that people will go to (pun not intended, sorry) extreme lengths, writing convoluted code, just to avoid a goto!  That's when it seems to me like more dogma than engineering!

One of the most important things about good code is that the algorithm and logical flow should be clear to a human reader. Inappropriate use of goto's can go against this, which is why goto's are frowned upon. If an occasional goto is used in a clear and logical way, for example to jump to an error handler, then that should not be seen as a problem.
I'm not an EE--what am I doing here?
 

Offline Delta

  • Super Contributor
  • ***
  • Posts: 1225
  • Country: gb
Re: XKCD was interesting today
« Reply #26 on: November 17, 2015, 03:45:27 am »
That's my understanding, Ian.  But it seems a lot of people think that goto should categorically not be used, at all, ever.  That's what seems daft to me.
 

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2248
  • Country: nz
Re: XKCD was interesting today
« Reply #27 on: November 17, 2015, 04:08:14 am »
good ... should be clear to a human reader. Inappropriate use of goto's can go against this

I pretty much agree a 'goto' is a special tool for a special case. Could it be that source code has inherently one-dimensional aspects to them (the flow of the program counter / instruction pointer through the code, the way the call stack works), and a goto works against this mind set by allowing unstructured jumps?

I've never seen anybody say that flowcharts are bad, as they allow you to draw arrows across logical boundaries, maybe they need a rule that you should never 'cross the streams'?

Neither in formal state transition diagrams, where you can jump between arbitrary states however you see fit. When you convert a FSM to code you basically end up with spaghetti.

I definitely remember the "no goto" rule being around in the early 80s in the time of structured BASICs, PASCAL and COBOL... Maybe a lot of it is hangover from old languages where your GOTOs used the line number as the target for jump, rather than a label. Those days were really nasty - if you renumbered code you could shoot yourself in the foot sooooo badly. These days the destination for a goto is has limited scope, and so at least it has a bit of structure to it.

One C coding standards I had to follow in the 90s recommended this construct for processing errors:

Code: [Select]
void some_func(void)
{
  do {
    if( ! something_that_might_error() ) /* e.g. malloc or fopen() */
      break;

    if( ! something_else_that_might_error() )
      break;

    <do the real work>

    return 1; /* Success */
  } while(0);

  <error handling code>
  return 0; /* Failure */
}

It was a poor man's way of exception handling, or abusing C to give 'goto' like functionality without actually using the word.

Anyhow - just idle musings rather than me saying anything of any value.
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 

Offline suicidaleggroll

  • Super Contributor
  • ***
  • Posts: 1455
  • Country: us
Re: XKCD was interesting today
« Reply #28 on: November 17, 2015, 02:49:51 pm »
I'm sure it can be written without using a lot of things!  So surely that's not a reason by default. 

I have read a fair bit about this on stackexchange and other sites, and it seems that people will go to (pun not intended, sorry) extreme lengths, writing convoluted code, just to avoid a goto!  That's when it seems to me like more dogma than engineering!

Go spend a week debugging/flowcharting an old FORTRAN 77 research code written by a scientist.  You'll light your hair on fire and run out of the building screaming before you make much headway, but from then on every time you see or write the words "goto" or "common" in any program in any language, it will give you a mild case of PTSD.

Using one here or there for error handling/cleanup is fine, but it's too easy to fall into their "lazy" trap and start using them WAY too liberally, which ultimately ends up making the code nearly impossible to read or debug.  It's best to just avoid them altogether, at least until you have a good grasp of the damage they can cause and know ways you can avoid them while maintaining readability.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf