本文介绍了接口(功能)设计的一般规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 你能谈谈关于界面的一般规则吗?在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 解决方案 好​​吧,怎么样你是否考虑过它们? #3肯定是最简洁的界面;你可以在不损坏功能或破坏额外编码的情况下更好地整理它 标准规则: void itoa4(void); 罗伯特 PS:在你发布之前先想想,让思维的成功反映出你的b $ b张贴。但我猜这个建议在Google Groups上丢失了。 海报。 没有意义。它引入了不必要的复杂性。 - Richard Heathfield Usenet是一个奇怪的地方 - dmr 29/7/1999 http://www.cpax.org.uk 电子邮件:rjh在上面的域名(但显然放弃了www) 感谢理查德回复。 还有一件事是如何进行异常/错误检查。例如,参数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 introducesome 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 meanand 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 charamong 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 解决方案Well, "how" do YOU think about them?#3 certainly has the least cluttered interface; you could unclutter iteven more without hurting functionality or breaking additional codingstandards rules:void itoa4(void);robertPS: Think before you post, and let the success of the thinking reflectin your posting. But I guess this advice is lost on "Google Groups"posters.No point. It introduces unnecessary complexity.--Richard Heathfield"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.ukemail: rjh at above domain (but drop the www, obviously)Thank Richard for the reply.One more thing is how to do the exception/error check. For example theavailability of the parameter "s" of the function itoa2 doesn''t bevalidated. How to guarantee the pointer parameters in those aspects (Ialso 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 problemsthat much more attention should be paid to?--lovecreatesbeauty 这篇关于接口(功能)设计的一般规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-21 13:49