在C中设计应用程序的最佳实践 [英] Best practices of desigining an application in C

查看:76
本文介绍了在C中设计应用程序的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


您能否以最佳实践在C语言中设计应用程序方面提出宝贵建议,请帮助我.

像每个用户定义的函数一样,应返回true或false值,以指示其是否失败.
处理空指针.


还告诉我,从函数返回值很昂贵.所以最好避免.

这是真的吗?

请帮忙.

Hi,
Could you please help me with your valuable suggestions on designing an application in C with the best practices.

Like every user-defined function should return either a true of false value indicating if it has failed or not.
Handling null pointers.


I was also told that returning a value from a function is expensive. So better to avoid it.

Is this true ?

Please help.

推荐答案

各种各样的程序员认为每个人都应该做的几百件事,但是对于给定类的项目和团队来说,并不是所有的事情都同样有用.无疑是值得商bat的.我在 http://www.gotw.ca/publications/c+上找到了打印的目录+ cs.htm [^ ]作为讨论的有用基础.是否购买本书以获取完整的解释和基本原理是您的决定,但是如果您没有经验,那么深入了解编程技术可能会很有帮助.

关于您的问题:
1.返回成功或失败是错误传播的一种相当粗糙的方法.在C ++中,首选方法是抛出异常,在C中,它会返回错误代码(即表示问题原因的整数值,而不仅仅是true或false).前者在C中是不可能的,但后者也可以在C ++中使用,而不是使用异常.只要确保不要在任何项目中混用这两种方法即可.

2.在C ++中,解决方案是永远不要使用简单的指针,而要使用智能指针"来跟踪它们所指向的对象是否仍在使用.智能指针根本不能为0.(*确定,这是一个过分的简化-参见下文!)
在C语言中,在使用指针之前,应始终确保它不为0,但不幸的是,这并不妨碍程序的其他部分弄乱它并使数据无效:检查0很重要,但不要这样本身足以防止运行时错误.
* P.S .:与C指针不同,智能指针将知道它们指向的数据何时失效.因此,尽管您仍然可能需要检查它们是否有效,但可以避免运行时错误.

3.返回值会将保存函数内返回值的变量复制到相同类型的新对象.如果创建这种类型的新对象并复制其数据很昂贵,那么按值返回就很昂贵;如果没有,那就不是.如果遇到此问题,最好让调用函数创建包含结果的对象,然后将此对象的引用(或指针)传递给该函数.
There are hundreds of things that various programmers think everyone should do, but not all are equally useful for a given class of projects and teams, and some are surely debatable. I''ve found the Table of Contents printed on http://www.gotw.ca/publications/c++cs.htm[^] to be a useful basis for discussion. Whether or not you buy the book for the full explanations and rationales is your decision, but if you''re inexperienced it might be a great help to understand programming techniques in-depth.

As to your questions:
1. Returning success or failure is a rather crude method of error propagation. In C++, the preferred method is throwing exceptions, in C it''s returning an error code (i. e. an integral value indicating the cause of the problem, rather than just true or false). The former is not possible in C, but the latter can be used in C++ too, instead of using exceptions. Just make sure not to mix these two methods in any project.

2. In C++, the solution is to never use simple pointers, but "smart pointers" that keep track of whether the object they''re pointing to is still being used or not. A smart pointer simply cannot be 0. (*ok, this is an over-simplification - see below!)
In C, you should always make sure that a pointer is not 0 before you use it, but unfortunately that doesn''t prevent other parts of your program to mess with it and invalidate the data: checking for 0 is important, but not by itself sufficient to prevent run-time errors.
*P.S.: unlike C-pointers, smart pointers will know when the data they point to gets invalidated. So, while you still may need to check if they''re valid, you can avoid run-time errors.

3. Returning a value copies the variable holding the return value inside the function to a new object of the same type. If creating a new object of this type and copying it''s data is expensive, then returning by value is expensive; if not, then it isn''t. If you are facing this problem, it may be better to let the calling function create the object containing the results, and pass a reference (or a pointer) to this object to the function.


您正在寻找的是C样式指南,您可以在Google中找到许多指南.一个好的C风格指南很容易包含约100条建议或规则.因此,仅在此处重复最常见的答案就太长了.

用户定义的函数仅限于返回true为false:在我看来,这不是一个好的通用规则.有许多函数应该只返回一个数值,例如sin,cos等.但是对于执行大量工作并通过参数返回多个输出值的函数来说,返回一个bool值作为成功指示符并不是一个坏主意. .

在C中,返回值通常并不昂贵.但是,按执行时间返回大量数据按值(与按引用或通过指针相反)是昂贵的,因为这需要将数据复制到复制数据中或从复制数据中复制数据.堆栈.这对于许多其他编程语言也是正确的.

要获得有关C的最佳实践书籍的良好概述,仅举几例:

-代码完成,麦康奈尔
-有效的C ++,Scott Meyers(专为C ++而设计,但很多东西也适用于C)
What you are looking for is a C style guide and you can find many of them with Google. A good C-style guide contains easily some 100 recommendation or rules. Hence it would be too long of an answer to just repeat even the most common ones here.

User defined functions limited to returning true of false: This is no good general rule in my opinion. There are many functions that should just return a numerical value, like sin, cos, etc. But for functions that perform quite an amount of work and that return several output values via parameters it is no bad idea to return a bool value as success indicator.

Returning a value is not generally expensive in C. Returning large amounts of data by-value (as opposed to by-reference or via a pointer) is however expensive in terms of execution time, because this requires the data to be copied to and from the stack. And this is true for many other programming languages as well.

For a good overview of best-practices books about C. Just to name a few:

- Code Complete, McConnell
- Effective C++, Scott Meyers (geared towards C++, but many things apply to C as well)


您好Zerr0cool,

关于可以帮助您设计应用程序的最佳实践,我无法提供详尽的清单,但是您可以从以下开始:

-在您跳入IDE之前,您可能需要花点时间寻找可以被划分为一个或多个模块化功能的组件(以降低维护代码的复杂性)

-尝试将每个类的类分隔为1个cpp文件+ 1个头文件(您可以将简单/小类除外,后者除外)

-尝试使用易于理解的名称(变量名称和文字便于您或其他人阅读)

-有时您可能会写注释,请尝试将其写在头文件中.如果它与函数有关,则在实现的第一行之后写它:
Hi Zerr0cool,

Regarding the best practices that you may help you designing your application, I can''t give you an exhaustive list, but you may begin with :

- Before you jump to write into your IDE, you may take a reflexion time looking for what can be devided into one or more modular fucntions (for decreasing the complexity of maintaining you code)

- Try to seperate classes to 1 cpp file + 1 header file for each class (you may except simple/small classes to this latter)

- Try to use understandable (variable names and litterals to make easy for you or other to read)

- Sometime you may write comments, try to write them in the header files. And if it is related to function write it just after the implementation first line :
int SumOfTwo(int a, int b)
{
   // This function return the sum of two litterals a and b which are integers
   return(a+b);
}



-尝试使用contarcts:先决条件/后置条件检查并尝试...尽可能多地捕获块.当您在运行时遇到应用程序的意外行为时,这可能会对您有所帮助.




- try to use contarcts: preconditions/postconditions check and try ... catch bloks as much as you can. This may help you when you got an unexpected behaviour of your application at run-time.


报价:

我还被告知,从函数返回值非常昂贵.因此最好避免.

I was also told that returning a value from a function is expensive. So better to avoid it.


返回内置类型(如整数)或微小对象对于C ++编译器来说很容易优化.两种函数(返回引用或值)的速度应该一样快.但是对于复杂的用户定义变量(例如类的实例),返回值的函数是消耗型,而不是使用引用的那个.

您可能会在此站点上找到更好的解释 C ++:返回值与输出参数 [ ^ ].

希望我能帮上忙,对不起我的英语:D


Returning built-in types (like integer) or tiny objects are easy for the C++ compiler to optimize. They should be equally fast with both kind of functions ( returning references or values). But for complex user defined variables such as instance of classes the function returning a value is expension rather than the one useing references.

You may find a better explanation in this site C++: Return Value versus Output Parameter[^].

Hope I helped, sorry for my english :D


这篇关于在C中设计应用程序的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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