接口(功能)设计的一般规则 [英] General rules on interface (function) design

查看:77
本文介绍了接口(功能)设计的一般规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能谈谈关于界面的一般规则吗?在C程序中公开认可的
(函数)设计?或者介绍

给自己一些好的建议。


您如何看待其中的一些建议?


a。保持界面干净清晰(干净或清晰是什么意思

以及如何实现?)。

b。如果可能的话,避免在本地函数中使用静态变量。

c。如果可能的话,避免将全局变量用于本地函数。

d。如果可能的话,避免在本地函数中分配动态内存。

....


然后我按照函数编写,将整数转换为字符串。 />
您的建议是受欢迎和赞赏的。


要求:将整数转换为字符串,在字符串中放入一个空白字符串

,例如:1 2 3 4 0 9对于123409.


/ * s:保持一个整数的字符串表示。

* num:一个整数将被它的字符串表示。

* /

void itoa2(char * s,int num)

{

int len = 100;

char s1 [len]; / *涉及C99功能* /

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'';

}


您如何看待以下不同界面设计的选择:


/ *#2自包含或者自给自足* /

void itoa2(char * s,int num);


/ *#3全局或静态变量或动态内存可能是必填* /

void itoa3(int num);

-

lovecreatesbeauty

解决方案

2006年5月23日07:19:18 -0700,

lovecreatesbeauty< lo ************** *@gmail.com>在Msg写了

。 < 11 ********************** @ j55g2000cwa.googlegroups .com>

你怎么看待一些那些建议如下?

保持界面干净清晰(干净或清晰是什么意思
以及如何实现?)。
b。如果可能,请避免在本地函数中使用静态变量。
c。如果可能,避免使用全局变量进行局部函数。
d。尽可能避免在本地函数中分配动态内存。


1.什么是本地功能?


2.如果我避免使用全局变量for本地函数,首先是全局变量是什么?

您如何看待以下不同界面设计的选择:

/ *#2自包含或自给自足* /
void itoa2(char * s,int num);

/ *#3全局或静态变量或动态内存可能是必需的* /
void itoa3(int num);




好​​吧,怎么样你是否考虑过它们?


#3肯定是最简洁的界面;你可以在不损坏功能或破坏额外编码的情况下更好地整理它



标准规则:


void itoa4(void);


罗伯特


PS:在你发布之前先想想,让思维的成功反映出你的b $ b张贴。但我猜这个建议在Google Groups上丢失了。

海报。


lovecreatesbeauty说:

你能谈谈C语言中接口
(功能)设计的一般规则,这些规则是公开认可的吗?或者介绍一下你自己的一些好建议。

你如何看待其中的一些建议?

a。保持界面清洁和清晰(干净或清晰是什么意思
以及如何实现?)。


像许多简短的问题一样,这个问题没有一个简短的答案。一个

或两个提示,以获得干净,清晰的界面:


(1)避免在typedef中隐藏* s(有些人会说要避免使用typedef
也是,但我不是其中之一);

(2)最小化耦合 - 函数对其他函数的依赖性,

文件范围对象,以及状态对象一般来说;

(3)在你的

界面中保持一致性(但不是愚蠢);

(4)don'发布不必要的信息(例如,也许你的模块

在内部使用二进制搜索树,但可能你的用户不需要

知道这一点 - 告诉他们可能会让他们依赖

这个事实,因此当你稍后切换到哈希

表时可能会破坏他们的代码。


该列表与详尽无遗。我担心,b / b
定义一个干净,清晰的界面的特征要比你看到它们时b / b
识别它们要困难得多! br />
b。尽可能避免在本地函数中使用静态变量。


绝对。首先,无状态函数比包含静态的

函数更容易理解。另一方面,虽然C不支持

多线程,但许多C实现都做了 - 而且静态不是

线程安全。

c 。尽可能避免将全局变量用于本地函数。


文件范围对象的问题是:


(1)它们引入了函数之间的强耦合,使得更难以

从程序中取出一个函数并将其粘贴到库中;

(2)因为任何可以看到该对象的函数都可以改变它的值,

调试不必要地变得困难;

(3)阴影可能会成为一个严重的问题。


d。尽可能避免在本地函数中分配动态内存。


为什么?

我写下了将整数转换为字符串的函数。
欢迎您提出建议并表示赞赏。

要求:将整数转换为字符串,在字符串中放置一个空白字符,例如:1 2 3 4 0 9对于123409.

/ * s:保持一个整数的字符串表示。
* num:一个整数是得到它的字符串表示。
* /
void itoa2(char * s,int num)
{
int len = 100;
char s1 [len]; / * C99功能涉及* /
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'';
}


您可以按如下方式删除对C99的依赖:


void itoa2(char * s,int num)

{

char s1 [(sizeof(int)* CHAR_BIT + 8)/ 3 ] = {0};

char * t = s1;

sprintf(s1,"%d",num);

while (* t!=''\''')

{

* s ++ = * t ++;

* s ++ =''' ';

}

* - s =''\ 0'';

}

如何你是否考虑过以下不同界面设计的选择:

/ *#2自包含或自给自足* /
void itoa2(char * s,int num);


如果您要返回指向null

终止符的指针,用户可能会很方便。

/ *#可能需要3个全局或静态变量或动态内存* /
void itoa3(int num);




没有意义。它引入了不必要的复杂性。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)




Richard Heathfield写道:

< snip>

您可以按如下方式删除对C99的依赖:

void itoa2(char * s,int num )
{s / s {[sizeof(int)* CHAR_BIT + 8)/ 3] = {0};
char * t = s1;
sprintf(s1," ;%d",num);
while(* t!=''\''')
{
* s ++ = * t ++;
* s ++ ='' '';
}
* - s =''\''';
}




感谢理查德回复。


还有一件事是如何进行异常/错误检查。例如,参数s的

可用性。功能itoa2不是

验证。如何在这些方面保证指针参数(我的b $ b也没有检查我的代码片段中的指针参数):


1.是吗请参考有效的内存?

2.内存的大小是否足以容纳数据?


其他程序问题的异常处理策略如何?

应该多关注一下?


-

lovecreatesbeauty


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?).
b. avoid using static variables in local function if possible.
c. avoid using global variables for local function if possible.
d. avoid allocating dynamic memory in local function if possible.
....

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 */
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);

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

解决方案

On 23 May 2006 07:19:18 -0700,
lovecreatesbeauty <lo***************@gmail.com> wrote
in Msg. <11**********************@j55g2000cwa.googlegroups .com>

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?).
b. avoid using static variables in local function if possible.
c. avoid using global variables for local function if possible.
d. avoid allocating dynamic memory in local function if possible.
1. What are "local functions"?

2. If I avoid using global variables "for" local functions, what''s
the point of having global variables in the first place?
How do you think about following choices of different interface design:

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

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



Well, "how" do YOU think about them?

#3 certainly has the least cluttered interface; you could unclutter it
even more without hurting functionality or breaking additional coding
standards rules:

void itoa4(void);

robert

PS: Think before you post, and let the success of the thinking reflect
in your posting. But I guess this advice is lost on "Google Groups"
posters.


lovecreatesbeauty said:

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?).
Like many short questions, this one doesn''t have a useful short answer. One
or two hints for clean, clear interfaces:

(1) avoid hiding *s in typedefs (some people would say to avoid typedefs
too, but I''m not one of them);
(2) minimise coupling - the dependence of a function on other functions, on
file scope objects, and on "state" generally;
(3) aim for consistency (but not to the point of foolishness) in your
interfaces;
(4) don''t publish unnecessary information (for example, maybe your module
uses a binary search tree internally, but probably your users don''t need to
know this - telling them is likely to have the effect of making them rely
on the fact, thus perhaps breaking their code when you switch to a hash
table later on).

That list is nothing like exhaustive. I''m afraid that it''s far harder to
define the characteristics of a clean, clear interface than it is to
recognise them when you see them!
b. avoid using static variables in local function if possible.
Absolutely. For one thing, a stateless function is easier to grok than a
function containing statics. For another, although C doesn''t support
multithreading, many C implementations do - and statics are not
thread-safe.
c. avoid using global variables for local function if possible.
The problems with file scope objects are:

(1) they introduce strong coupling between functions, making it harder to
lift a function out of a program and stick it in a library;
(2) because any function that can see the object can change its value,
debugging is made unnecessarily hard;
(3) shadowing can become a serious problem.

d. avoid allocating dynamic memory in local function if possible.
Why?
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 */
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'';
}
You can remove the dependence on C99 as follows:

void itoa2(char *s, int num)
{
char s1[(sizeof(int) * CHAR_BIT + 8) / 3] = {0};
char *t = s1;
sprintf(s1, "%d", num);
while(*t != ''\0'')
{
*s++ = *t++;
*s++ = '' '';
}
*--s = ''\0'';
}
How do you think about following choices of different interface design:

/* #2 self-contained or self-sufficient */
void itoa2(char *s, int num);
It may be convenient to the user if you were to return a pointer to the null
terminator.
/* #3 global or static variables or dynamic memory may be required */
void itoa3(int num);



No point. It introduces unnecessary complexity.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)



Richard Heathfield wrote:
<snip>

You can remove the dependence on C99 as follows:

void itoa2(char *s, int num)
{
char s1[(sizeof(int) * CHAR_BIT + 8) / 3] = {0};
char *t = s1;
sprintf(s1, "%d", num);
while(*t != ''\0'')
{
*s++ = *t++;
*s++ = '' '';
}
*--s = ''\0'';
}



Thank Richard for the reply.

One more thing is how to do the exception/error check. For example the
availability of the parameter "s" of the function itoa2 doesn''t be
validated. How to guarantee the pointer parameters in those aspects (I
also didn''t check the pointer parameter in my code snippet):

1. does it refer to a valid memory?
2. is the size of the memory enough for holding the data?

How about the exception handling strategies on other program problems
that much more attention should be paid to?

--
lovecreatesbeauty


这篇关于接口(功能)设计的一般规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆