How do you ensure that you as programmer have writ

2019-01-24 09:23发布

I m looking to write some quality C code. Can someone point me to some articles , websites..whatever I need something with examples. I have already seen and read K&R C book.

But times have changed, some one must have more to say on quality C Code. and another important thing is How do you ensure that you as programmer have written quality C code??

11条回答
倾城 Initia
2楼-- · 2019-01-24 10:11

There are a lot of aspects for quality of code and tons of articles, books, blogs

but I can advice you this ones as beginning:

Code complete

Code secure

查看更多
趁早两清
3楼-- · 2019-01-24 10:12

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.

查看更多
再贱就再见
4楼-- · 2019-01-24 10:13

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

查看更多
放我归山
5楼-- · 2019-01-24 10:13

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.

查看更多
迷人小祖宗
6楼-- · 2019-01-24 10:17

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.

查看更多
登录 后发表回答