Created
March 14, 2017 20:13
-
-
Save JonathonReinhart/b109f379719674d7b297de180f610f6a to your computer and use it in GitHub Desktop.
Places "goto" can improve C code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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