looking for advice on streamlining a function SOLVED.

Ok a truce... thanks for pointing out holme4's error. I was annoyed at his comment - not only was it misguided but also had zero value in assisting with the original problem. But in vindicating the syntactic structure of my code (for which I am grateful) you then unleashed your double-edged sword in alluding to the code not being written "properly". Once again, IMHO - zero value with respect to the context of the thread.

I think the various in's and out's what if's etc... concerning code layout are for another thread. So'll I'll finish with this parting comment and wish you a friendly farewell -

When I refer to my "pragmatic choice" this is in fact based NOT simply on personal projects as you infer but also in maintaining other peoples' code. I personally find it extremely difficult to analyse code on a computer screen or printed page that contains too much white space, having to scroll up/down, turn the page etc.. to get an idea what is going on in some fragment of code that could be easily represented in a few lines. So - if I find an opportunity to reduce white space WITHOUT compromising the readability of the code, I'll take it. I find that it aids my "chunking" process - something I am pretty sure is not a phenomenon unique to myself :)

vulture2600, Since everyone is have fun with coding, I can rewrite reply #12 into this:

// untested example

#define ACCEPT_IF_VALID(a,b,c) if(a>0) { c++; b+=a; }

int8_t average(int8_t near, int8_t far, int8_t out) {
  uint8_t count = 0;
  uint16_t avg = 0;
  ACCEPT_IF_VALID (near, avg, count)  // a ';' behind this or not ?
  ACCEPT_IF_VALID (far, avg, count)
  ACCEPT_IF_VALID (out, avg, count)
  if (count != 0) {
    return avg / count;
  }
  else return 0;
} //end average().

Thank you Caltoa. I’ll mess around with it.

As far as formatting goes, I agree with PaulS:

My comments were initially directed at holme4, who said that a semicolon was unnecessary. I disagreed.

I do not like having more that one statement on a line. You have 4 on one line. Yes, that makes it harder to maintain code. What if you have some issue with that block of code, and need to print a variable’s contents? It is, IMHO, a lot harder to add a print statement to:
Code:

if (out<0) count–; else total+=out;

then it is to add one to:
Code:

if(out < 0)
{
count–;
}
else
{
total += out;
}

While the latter does take more vertical space to display, the intent and structure is far easier to see, in my opinion.

I personally like to see one statement per line with lots of vertical space and thus required scrolling. That is a topic for another thread.

Caltoa: Very nice code lar3ry, you should be a teacher. I like that code, since it can be extended for more values easily.

I would change this : if (avg > 0) { To this: if (count > 0) { Because that is closer to the actual funtionality.

And I would use "avg +=" and "avg /="

Thanks, Caltoa. I'd prbably use += and /= as well, but I always wonder about confusing a beginner.

As for using if ( count > 0 ), I just realized that probably the best would be

     if ( count > 1 ) {
          avg /= count;
     }
     return avg;

No real need to divide by one.

No real need to divide by one.

Excellent point!