Example 1:
int length()
{
return strlen(random_string);
}
Example 2:
int length()
{
int str_length = 0;
str_length = strlen(random_string);
return str_length;
}
Question:
I have come across many functions where a single line of code could satisfy the requisite for that function, but I recall something about avoiding this kind of shortcut.
Are there certain situations where one is more appropriate than another or should I always go for the simpler piece of code.
One reason to have a a multi-step approach is that if you ever decide to add a line to print the value of length, it’s a lot less hassle:
Or if you want to add some extra assert:
Other than that, it’s all about what you feel is most appropriate [unless you have strict coding standards to follow, of course!]