Here’s another quick C thing…
One of the jobs I had used a pretty complete coding style guide for C. One of the things they insisted on was only one “return” in any function that returns values. For example:
int function(int x)
{
if SOMETHING
{
return 100;
}
else SOMETHING ELSE
{
return 200;
}
else
{
return 0;
}
}
The above function returns values 100, 200 or 0 based on the input (1, 2 or anything else). It has three different places where a value is returned. This saves code, compared to doing it like this:
int function(int x)
{
int value;
if SOMETHING
{
value = 100;
}
else if SOMETHING ELSE
{
value = 200;
}
else
{
value = 0;
}
return value;
}
Above, you see we use a variable, and then have three places where it could be set, and then we return that value in one spot at the end of the function. This probably generates larger code and would take longer to run than the top example.
But if you can afford those extra bytes and clock cycles, it is a much better way to do this — at least form a maintenance and debugging standpoint.
I have accepted this, but only today did I run in to a situation where this approach would have saved me some time and frustration. In my case, I was encounter a compiler warning about a function not returning a value where it was defined to return a value. I looked and confirmed the function was indeed returning a value. What was going on?
The problem was that it used multiple returns, and did something like this:
int function(int x)
{
int value;
if (!ValueIsValid(x)) return;
if SOMETHING
{
value = 100;
}
else if >OMETHING ELSE
{
value = 200;
}
else
{
value = 0;
}
return value;
}
Somewhere in the program was a check that just did a “return” and the compiler was seeing that, but my eyes were looking at the lower portion of the program where a value was clearly being returned.
I am guessing the function originally did not return a value, and when a return value was added later, that initial “return;” was not corrected, leaving a compiler warning. This warning may have been in the code for a long time and was simply left alone because someone couldn’t figure it out (my situation) or wasn’t concerned about compiler warnings.
Today, the warning bugged me enough that I did a deep dive through the function, line-by-line, trying to figure out what was going on. And I found it. A simple correction could have been this:
int function(int x)
{
int value;
if (!ValueIsValid(x)) return 0; // FIXED: Add missing return value.
if SOMETHING
{
value = 100;
}
else if SOMETHING ELSE
{
value = 200;
}
else
{
value = 0;
}
return value;
}
That resolved the compiler warning, but still left two spots where a value was returned, so I ended up doing something like this:
int function(int x)
{
int value;
if (ValueIsValid(x) == true) // do this if valid
{
if SOMETHING
{
value = 100;
}
else SOMETHING ELSE
{
value = 200;
}
else
{
value = 0;
}
}
else // Not valid
{
value = 0;
}
return value;
}
Now there is only one place the function returns, and it only processeses things if the initial value appears valid.
I will sleep better at night.
I sleep on a soap box.
