Re: General rules on interface (function) design





lovecreatesbeauty wrote On 05/23/06 10:19,:
Could you talk something about the general rules on the interface
(function) design in C program that recognized publically? Or introduce
some good advice of yourself.

How do you think about some of those advices like following?

a. keep the interface clean and clear (What does clean or clear mean
and how to achieve that?).

"For motherhood and against sin." (A parodist's summary
of politicians' campaign posturings.)

b. avoid using static variables in local function if possible.

See Richard Heathfield's response. Also, what do you mean
by a "local" function, and how does it differ from "national"
functions or "remote" functions or "express" functions?

c. avoid using global variables for local function if possible.

In addition to the points raised by R.H., note that a file-
scope variable has static storage duration and thus shares the
drawbacks of (b).

d. avoid allocating dynamic memory in local function if possible.

Hogwash. Allocate memory when you need it and as it's
needed. The important thing isn't where it's allocated, but
that it's properly kept track of.

And I write following the function to convert an integer to a string.
Your advices are welcome and appreciated.

Requirement: convert an integer to character string, put one blank char
among the string, for example: "1 2 3 4 0 9" for 123409.

/* s : hold the string presentation of an integer.
* num : an integer is to be gotten its string presentation.
*/
void itoa2(char *s, int num)
{
int len = 100;
char s1[len]; /* C99 features involved */

No need, as shown in R.H.'s post. (However, I see no
reason to initialize the array to all zeroes as he does.)

sprintf(s1, "%d", num);
int i = 0, j = 0;
for (i = 0; s1[i]; ++i, ++j)
{
s[j] = s1[i];
s[++j] = ' ';
}
s[--j] = '\0';
}

How do you think about following choices of different interface design:

/* #2 self-contained or self-sufficient */
void itoa2(char *s, int num);

Unattractive, because it ignores the lesson of gets().
Two alternatives seem plausible:

- Let the caller provide both a buffer pointer and a
buffer size, and the function promises to honor the
size. If the buffer is too small for the number,
the function should be able to return a value that
describes the difficulty. See snprintf().

- In the header that declares itoa2(), #define a macro
named ITOA2BUFSIZE or some such. Require (in the
documentation) that the caller provide at buffer of
at least ITOA2BUFSIZE characters.

/* #3 global or static variables or dynamic memory may be required */
void itoa3(int num);

This signature appears to require a global variable to
communicate the result; I don't like it. The variable might
be the result buffer or a pointer to a dynamically-allocated
buffer or a FILE* stream that the caller can rewind and read
back again ... No matter; I don't like it.

--
Eric.Sosman@xxxxxxx

.



Relevant Pages

  • Re: Dealing with ad hominem attacks in comp.programming
    ... buffer which expands as required. ... Do you allow the caller to specify a maximum buffer size? ... And no one design will satisfy everyone. ...
    (comp.programming)
  • Re: Dealing with ad hominem attacks in comp.programming
    ... buffer which expands as required. ... Do you allow the caller to specify a maximum buffer size? ... And no one design will satisfy everyone. ...
    (comp.programming)
  • Re: Headphone Amp Redux
    ... I think the following design ... stage, a buffer, and a class A output stage. ... through Q10 will be replaced by current from the load. ... TL071 all // - a worst choice for an op amp in this application is hardly possible, but in general it is a good approach and with an appropriate op amp an excellent one. ...
    (sci.electronics.design)
  • Re: Headphone Amp Redux
    ... I think the following design ... stage, a buffer, and a class A output stage. ... through Q10 will be replaced by current from the load. ... ...Jim Thompson ...
    (sci.electronics.design)
  • Re: Conversion from UTF32 to UTF8 for review
    ... time, which puts a performance burden on the caller, which should be counted in the total ... you would, upon completion, put a NUL character at the end of the buffer. ... app or an MFC app; and if it were being graded on performance, I'd give it a D- at best, ... The programmer protested that "I have to do malloc each time because I ...
    (microsoft.public.vc.mfc)