Skip to content

Instantly share code, notes, and snippets.

@thomo
Created November 14, 2012 06:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomo/4070617 to your computer and use it in GitHub Desktop.
Save thomo/4070617 to your computer and use it in GitHub Desktop.
Coding Horror: do while with goto instead of break
Function(parameters){
Boolean found = false;
do{
for(counter){
such etwas in einem Array
if(bedingung) {
found = true;
goto do_end
}
}
if(bedingung){
found = true;
goto do_end;
}
do_end:
;
} while(0)
if(found) return wert
else wert_unbekannt
}
@thomo
Copy link
Author

thomo commented Nov 14, 2012

Pseudo Beispielcode aus einem Kundenprojekt

@thomo
Copy link
Author

thomo commented Nov 14, 2012

Meine Anmerkung/Vermutung zu dem Beispiel:

  • Vermutlich stand statt dem goto ursprünglich ein break in der do-while loop (und das Sprungziel gab es da auch nicht). Mit break konnte dann aus der do-while rausgesprungen werden und es ging danach weiter.
  • Dann wurde der Code kopiert bzw. der Compiler wechselte und unterstützte kein break in while loops mehr (oder es gab eine Kodierregel/vorgabe, die breaks in do-while verbot).
  • Jedenfalls hat dann ein findiger Entwickler die breaks durch gotos ersetzt – und konnte den Rest so lassen.

@oleglebov
Copy link

Motivation:
-> höhere Performance durch Überspringen von zur Laufzeit nicht länger benötigten Anweisungen
-> Erfüllung von MISRA 14.7: A function shall have a single point of exit at the end of the function.

Mittel:
-> frühzeitiges Abbrechen von Schleifen
-> Direktsprünge zu Anweisungen

Auswirkung:
-> Verletzung von MISRA 14.6: For any iteration statement there shall be at most one break statement used for loop termination.
-> Verletzung von MISRA 14.4: The goto statement shall not be used.

Frage:
Wieviele Anweisungen muss man sparen, um eine der beiden Regeln (MISRA 14.6 oder MISRA 14.7) verletzen zu dürfen?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment