KEEP K.I.S.S.

tk's blog

推荐一篇文章

tk posted @ Dec 18, 2013 07:50:53 PM in 业余 with tags 读书 , 1623 阅读

Notes on Programming in C

文章讲述C编程里的一些注意点,涉及命名、风格、数据结构。作者是 Rob Pike

其中关于注释、复杂度、数据驱动以及函数指针这几小节特别值得一读。

简要笔记:

注释

作者倾向于少写注释,只写必要的

  1. 代码清晰,命名良好的代码应该是自解释的
  2. 编译器不会检查注释,不能保证注释是正确的,尤其是代码已经修改而注释没有更正
  3. 注释会打乱代码的排版
  4. 注释用于解释全局变量或类型的用法,介绍不寻常或者重要的子程序,将大量计算划分小节
  5. 避免花哨的注释
  6. 给数据写注释要比算法注释有用的多
  7. 如果代码需要注释来帮助理解,那么应该考虑重写以便于理解

复杂度

作者认为大部分程序写的过于复杂源于不好的设计,但是没有讲设计而是给了6条规则

  1. 避免过早优化
  2. 在没有测定程序运行效率前,不要进行优化
  3. 高级算法在数据规模小的时候会变慢,除非确信数据规模很大,不要使用高级算法。就算 数据规模很大,首先考虑规则2
  4. 高级算法比简单算法更难实现,更容易滋生bug,使用简单算法和简单的数据结构
  5. 数据主导。使用了正确的数据结构,那么算法就很清晰明了
  6. 没有规则 6

总结起来2点:杜绝过早优化,数据主导

包含文件

包含文件不应包含已经被包含的文件

避免编译依赖和物理耦合

PS:这里介绍两个技巧

  1. 前向声明,用于避免引用整个头文件
  2. 不透明指针,在C++中常称作 Pimpl idiom,可以完全接偶接口(头文件)和 实现(源文件),修改实现可不必重新编译其他引用了模块的部分,可以用来保证二进制 兼容性(在制作动态库时很有用)。
AP 8th Class fa 1 Qu 说:
Sep 23, 2022 03:31:58 PM

AP 8th Class FA-1 Model Paper 2023 Pdf for all course languages and disciplines based on the updated curriculum & syllabus. Every year, the school education board publishes example paper recommendations for BSEAP students in both government and private schools. Download the Formative Assessment-1 practise test questions and assignments for all topics. AP 8th Class fa 1 Question Paper The practise paper with revision questions for the First Formative Assignment examinations has been created and recommended by leading educational institute topic specialists or teaching staff of the board.

SEO 说:
May 14, 2023 03:58:04 PM

There you can download for free, see the first of these data. 에볼루션

SEO 说:
Jun 04, 2023 04:13:44 AM This is helpful, nonetheless it can be crucial so that you can check out the following website: 출장안마
SEO 说:
Jun 06, 2023 03:35:04 PM

thanks this is good blog. 파워볼

Johny 说:
Aug 31, 2023 04:54:56 PM

Interesting and amazing how your post is! It Is Useful and helpful for me That I like it very much, and I am looking forward to Hearing from your next.. last minute naar hermagor pressegger see

Johny 说:
Oct 22, 2023 01:35:04 PM

I really appreciate this wonderful post that you have provided for us. I assure this would be beneficial for most of the people. ufabetทางเข้าเล่น

dark web sites 说:
Dec 05, 2023 06:22:05 AM

DarkWeb.Link's examination of privacy concerns becomes a digital ethical discourse, raising questions about the boundaries between personal anonymity and societal accountability.

tech 说:
Dec 10, 2023 04:06:41 PM

It proved to be Very helpful to me and I am sure to all the commentators here! UFABETกติกาแทงบอล

jsimitseo 说:
Feb 02, 2024 10:47:37 PM

It is great, yet take a gander at the data at this address.  concierge doctor naples

alchemai 说:
Mar 11, 2024 04:42:58 AM

Each piece of clothing has its own story, its own history, and its own unique way of making us feel when we wear it. alchemai


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter