Last active
August 29, 2015 14:24
-
-
Save Nuclearfossil/7af30ae6012f2c97f42e to your computer and use it in GitHub Desktop.
early return
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
// The question is - which makes more sense in a general case: | |
// early outs or single entry/exit. | |
// Note mystringcopy() returns the number of characters actually copied. mystringcopy() | |
// should also be considered functionally irrelevant - this question is more about | |
// early outs vs single entry/exit. | |
const unsigned int fixedlength = 255; | |
bool destTest01(const char *const valueA, | |
const char *const valueB) | |
{ | |
unsigned int valueALength = strlen(valueA); | |
unsigned int stringLength = 0; | |
char destbuffer01[fixedLength]; | |
char destbuffer02[fixedLength]; | |
if (valueALength != mystringcopy(destbuffer, valueA, valueALength)) | |
{ | |
// Log some stuff - cut for brevity | |
return false; | |
} | |
unsigned int valueBLength = strlen(valueB); | |
if (valueBLength != mystringcopy(desbuffer02, valueB, valueBLength)) | |
{ | |
// Log some stuff - cut for brevity | |
return false; | |
} | |
// do something interesting with valueA and valueB | |
// interesting is something non-trivial. | |
return true; | |
} | |
// However, if we're dealing with something trivial, this is better | |
bool destTest02(const char *const valueA, | |
const char *const valueB) | |
{ | |
bool ok = true; | |
unsigned int valueALength = strlen(valueA); | |
unsigned int stringLength = 0; | |
char destbuffer01[fixedLength]; | |
char destbuffer02[fixedLength]; | |
if (valueALength != mystringcopy(destbuffer, valueA, valueALength)) | |
{ | |
// Log some stuff - cut for brevity | |
ok = false; | |
} | |
unsigned int valueBLength = strlen(valueB); | |
if (ok && valueBLength != mystringcopy(desbuffer02, valueB, valueBLength)) | |
{ | |
// Log some stuff - cut for brevity | |
ok = false; | |
} | |
// check to see if destbuffer02 is in destbuffer01, or something equally | |
// trivial | |
return ok; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment