Skip to content

Instantly share code, notes, and snippets.

@JonathonReinhart
Created March 14, 2017 20:13
Show Gist options
  • Save JonathonReinhart/b109f379719674d7b297de180f610f6a to your computer and use it in GitHub Desktop.
Save JonathonReinhart/b109f379719674d7b297de180f610f6a to your computer and use it in GitHub Desktop.
Places "goto" can improve C code
void *search_without_goto(const char *key)
{
int i;
void *result;
for (i = 0; i < some_length; i++) {
if (strcmp(some_collection[i].key, key) == 0)
break;
}
if (i == some_length)
return -1;
result = some_post_processing(some_collection[i].value);
// More with result
return result;
}
void *search_with_goto(const char *key)
{
int i;
void *result;
for (i = 0; i < some_length; i++) {
if (strcmp(some_collection[i].key, key) == 0)
goto found;
}
return -1;
found:
result = some_post_processing(some_collection[i].value);
// More with result
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment