Skip to content

Instantly share code, notes, and snippets.

@Nuclearfossil
Last active August 29, 2015 14:24
Show Gist options
  • Save Nuclearfossil/7af30ae6012f2c97f42e to your computer and use it in GitHub Desktop.
Save Nuclearfossil/7af30ae6012f2c97f42e to your computer and use it in GitHub Desktop.
early return
// 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