作为程序员,您如何确保编写出高质量的 C 代码?

发布于 2024-07-12 03:17:20 字数 1432 浏览 10 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

荒芜了季节 2024-07-19 03:17:20

有人提到了一些编译器开关,但拥有语法流畅的代码并不能确保最终产品的质量,因为软件质量远不止于此。

软件质量有多种分类,但这里有一个列表可以用作检查表:

  • 正确性(它是否按照规范工作?)
  • 可靠性(用户可以依赖?)
  • 稳健性(在意外情况下是否有效?)
  • 性能(对于用户来说,它是否足够快地完成工作?)
  • 可用性 (是否方便使用?)
  • 可验证性(其属性是否易于验证?)
  • 可维护性(易于修改吗?)
    • 可修复性(缺陷能否在合理的时间内修复?)
    • 可进化性(可以简单地添加新功能吗?)
  • 可重用性(可以代码是否可以轻松地在其他项目中使用?)
  • 可移植性(是否可以在不同的环境中轻松运行?)
  • 可理解性(维护者是否可以轻松理解它?)
  • 互操作性(合作程度如何?)
  • 生产力(高效且高效的交付)
  • 及时性(按时交付的能力)
  • 可见性(所有步骤记录清楚吗?)

Someone mentioned some compiler switches, but having syntactically smooth code is not going to ensure a quality end-product, because there's more to software quality than that.

There are several classifications of software qualities, but here's a list that you can use as a checklist:

  • Correctness (does it work according to spec?)
  • Reliability (can de user depend on it?)
  • Robustness (does it work in unexpected situations?)
  • Performance (does it do the job fast enough for the user?)
  • Usability (is it user-friendly?)
  • Verifiability (can its properties be verfified easily?)
  • Maintainability (can modifications be made easily?)
    • Repairability (can defects be fixed within reasonable time?)
    • Evolvability (can new functionality be added simply?)
  • Reusability (can the code easily be used in other projects?)
  • Portability (can it run easily in different environments?)
  • Understandability (can maintainers easily understand it?)
  • Interoperatability (how well does it cooperate?)
  • Productivity (efficienct and performant delivery)
  • Timeliness (ability to deliver on time)
  • Visibility (are all steps documented clearly?)
护你周全 2024-07-19 03:17:20

在编译器中启用警告。 对于 gcc,我使用这些标志:

-std=c99 -pedantic -Wall -Wextra -Werror -Wmissing-prototypes
-Wmissing-declarations -Wshadow -Wpointer-arith -Wcast-align -Wwrite-strings
-Wredundant-decls -Wnested-externs -Winline -Wno-long-long -Wconversion
-Wstrict-prototypes

如果您的代码无法更改为不产生警告,请删除 -Werror 或不使用产生警告的特定标志。

Enable warnings in your compiler. With gcc, I use these flags:

-std=c99 -pedantic -Wall -Wextra -Werror -Wmissing-prototypes
-Wmissing-declarations -Wshadow -Wpointer-arith -Wcast-align -Wwrite-strings
-Wredundant-decls -Wnested-externs -Winline -Wno-long-long -Wconversion
-Wstrict-prototypes

If your code can't be changed to not produce warnings, drop the -Werror or don't use the specific flag producing the warning.

爱冒险 2024-07-19 03:17:20

传统上,人们使用 lint 来帮助解决这个问题。

Traditionally, people have used lint to help out with this.

爱要勇敢去追 2024-07-19 03:17:20

使用静态分析工具,传统上称为 lint,但我使用了 splint ,这很好。 请参阅此问题中的建议。 我个人建议启用警告并修复它们。

就规则而言,

  • 不要信任输入数据 - 验证所有内容,大小,类型,内容。
  • 防止缓冲区溢出 - strcat 和许多其他内容并不安全。
  • 请使用单元测试,ddj 文章
  • 请让其他人审查您的代码
  • 不要做出假设。
  • 确保函数简短,并彻底测试每个函数。
  • 使用有意义的名称。
  • 编写可读的代码。
  • 不要偷懒——如果你需要改变某件事以使其更有意义,那么宜早不宜迟。

编辑:
具体到 C,这个 C 陷阱 列表是必不可少的阅读内容,即使它是针对 C++ 的,也值得浏览 CERT C++ 安全编码标准

Use a static analysis tool, traditionally called lint, however I've used splint which is good. See recommendations in this question. Personally I'd recommend enabling warnings and fixing them.

In terms of the rules

  • Do not trust input data - validate everything, size, type, content.
  • Protect against buffer overruns - strcat and many others aren't safe.
  • Do use unit testing,ddj article.
  • Do get your code reviewed by someone else
  • Do not make assumptions.
  • Do keep functions short, and test each one thoroughly.
  • Use meaningful names.
  • Write readable code.
  • Don't be lazy - if you need to change a something to make it more meaningful then do it sooner rather than later.

Edit:
Specific to C, this list of C gotchas is essential reading, and even though it is for C++ it is worth going through the CERT C++ Secure Coding Standard

南…巷孤猫 2024-07-19 03:17:20

到目前为止,人们已经提到了工具。 然而,超出某一点,实际上只有一件事可以真正提高您编写的代码的质量:

编写代码。

People have so far mentioned tools. However, beyond a certain point, there is really only one thing you can do to really improve the quality of the code you write:

Write code.

爱的十字路口 2024-07-19 03:17:20

编写单元测试!

与更现代的语言相比,用 C 编写单元测试可能有点麻烦,但仍然非常值得。

我想说,适当的单元测试是确保任何代码质量的第一方法。 您可以使用您想要的所有静态分析工具和代码审查,但没有什么比实际运行代码并验证结果更好的了。

Write unit tests!

It might be a bit more cumbersome to write unit-tests in C compared to more modern languages, but it is still very much worth it.

I would say that proper unit tests are the #1 way to ensure the quality of any code. You can use all the static analysis tools and code reviews you want, but nothing beats actually running the code and verifying the results.

尝蛊 2024-07-19 03:17:20

这部分取决于“优质 C”代码的含义。

该程序的一个重要方面是“它是否达到了设计目的”? 这很难衡量,但却至关重要。

然后,您需要知道代码是否可以被编译器接受 - 使用 Cristoph 提供的一组 GCC 编译器选项 将表明代码状态良好。 (尽管我会对 -Wno-long-long 争论不休,但这取决于您的代码可能需要移动到的位置)。

代码布局很重要。 代码对于人类和编译器来说都是可读的吗? 布局是否统一? 它是否采用标准格式之一 - 有几种,都具有主要的以下格式,只要您一致地使用其中一种,代码就应该没问题。 评论是否恰当? 这意味着有足够的评论,但不要太多! 该文件应该有一个标头注释,指示其中的内容 - 可能还包括编写它的人,以及分发它所依据的许可证。 SO 上有多个与此相关的问题,包括专业#include 评论。 不过,按照良好的布局标准编写代码很快就会成为惯例。

文档可能是相关的——通常是相关的。 其他人如何知道该代码存在、它的作用、如何使用它、何时使用它、何时不使用它?

代码应该使用足够好的算法来编写 - 它不应该使用过多的内存、磁盘或 CPU 时间。 它也不应该泄漏资源。 将性能增强的最后一个 CPU 周期从每次运行程序时使用一次的例程中提取出来也是没有意义的,该例程在程序启动时持续几毫秒,除非您能够证明它是程序的性能瓶颈。程序作为一个整体。

Wouter van Nifterick 给出了 一组优秀的指针

It depends in part on what you mean by 'quality C' code.

One important aspect of the program is "does it do what it was designed to do"? That is hard to measure, but is crucial.

Then you need to know whether the code is acceptable to compilers - using the set of GCC compiler options provided by Cristoph would indicate that the code is in good shape. (Although I would quibble over -Wno-long-long, that depends on where your code might need to be be moved to).

The code layout is important. Is the code readable by humans as well as compilers? Is the layout uniform? Is it in one of the standard formats - there are several, all with major followings, and as long as you use one of them consistently, the code should be fine. Is it appropriately commented? That means enough comments, but not too many! The file should have a header comment indicating what's in it - and probably who wrote it, and maybe the licence under which it is distributed. There have been multiple questions on SO about that, including Professional #include comments. Writing code to a good layout standard is routine after a short time, though.

Documentation may be relevant - usually is relevant. How would someone else know that the code exists, what it does, how to use it, when to use it, when not to use it?

The code should be written with good enough algorithms - it should not use exorbitant amounts of memory, disk, or CPU time. It should also not leak resources. There's also no point in wringing the last CPU cycle of performance enhancement out of a routine that will be used once per run of a program, for a few milliseconds, when it starts up, unless you can demonstrate that it is a performance bottleneck for the program as a whole.

Wouter van Nifterick has given an excellent set of pointers too.

感情废物 2024-07-19 03:17:20

很难亲自判断自己是否在编写高质量的代码,当然有大量的自动化和标准,但是如何应用他们所看到的一切呢?

这就是我非常喜欢将同行评审作为判断代码质量的方法。 让其他人看到(并学习)您的代码,这将是质量的评判。

It's tough to see for your own self whether or not you are writing quality code, sure there's tons of automation and standards out there, but how can one apply everything they've seen out there?

This is where I'm a big fan of peer review as a method of judging code quality. Let other see (and also learn) from your code and that'll be the judge of quality.

奈何桥上唱咆哮 2024-07-19 03:17:20

一些加强代码质量的现代软件生命周期实践:

  • 集成测试(在项目启动时计划)
  • 同行评审 代码(在开发期间)
  • 结对编程(在开发期间)
  • 使用统一库(而不是“重新发明轮子”方法的一部分)

后一种方法可能特别适用于 C 语言。

Some modern software lifecycle practices that enforce quality of code:

  • integration tests (planned at project startup)
  • peer reviews of code (during development)
  • pair programming (during development)
  • the use of consolidated libraries (instead of a 'reinventing the wheel' approach)

The latter one may in particular apply to the C language.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文