principle of develop

本文总结了18条编程原则,包括避免重复、抽象、简单性、避免过早优化等,帮助开发者编写高效、简洁且易于维护的代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

良好的编程原则与良好的设计工程原则密切相关。本文总结的这些设计原则,帮助开发者更有效率的编写代码,并帮助成为一名优秀的程序员。

1.避免重复原则(DRY - Don’t repeat yourself)

编程的最基本原则是避免重复。在程序代码中总会有很多结构体,如循环、函数、类等等。一旦你重复某个语句或概念,就会很容易形成一个抽象体。

2.抽象原则(Abstraction Principle )

与DRY原则相关。要记住,程序代码中每一个重要的功能,只能出现在源代码的一个位置。

3.简单原则(Keep It Simple and Stupid )

简单是软件设计的目标,简单的代码占用时间少,漏洞少,并且易于修改。

4.避免创建你不要的代码 Avoid Creating a YAGNI (You aren’t going to need it)

除非你需要它,否则别创建新功能。

5.尽可能做可运行的最简单的事(Do the simplest thing that could possibly work)

尽可能做可运行的最简单的事。在编程中,一定要保持简单原则。作为一名程序员不断的反思“如何在工作中做到简化呢?”这将有助于在设计中保持简单的路径。

6.别让我思考(Don’t make me think )

这是Steve Krug一本书的标题,同时也和编程有关。所编写的代码一定要易于读易于理解,这样别人才会欣赏,也能够给你提出合理化的建议。相反,若是繁杂难解的程序,其他人总是会避而远之的。

7.开闭原则(Open/Closed Principle)

你所编写的软件实体(类、模块、函数等)最好是开源的,这样别人可以拓展开发。不过,对于你的代码,得限定别人不得修改。换句话说,别人可以基于你的代码进行拓展编写,但却不能修改你的代码。

8.代码维护(Write Code for the Maintainer)

一个优秀的代码,应当使本人或是他人在将来都能够对它继续编写或维护。代码维护时,或许本人会比较容易,但对他人却比较麻烦。因此你写的代码要尽可能保证他人能够容易维护。用书中原话说“如果一个维护者不再继续维护你的代码,很可能他就有想杀了你的冲动。”

9.最小惊讶原则(Principle of least astonishment)

最小惊讶原则通常是在用户界面方面引用,但同样适用于编写的代码。代码应该尽可能减少让读者惊喜。也就是说,你编写的代码只需按照项目的要求来编写。其他华丽的功能就不必了,以免弄巧成拙。

10.单一责任原则(Single Responsibility Principle) 

某个代码的功能,应该保证只有单一的明确的执行任务。

11.低耦合原则(Minimize Coupling)

代码的任何一个部分应该减少对其他区域代码的依赖关系。尽量不要使用共享参数。低耦合往往是完美结构系统和优秀设计的标志。

12.最大限度凝聚原则(Maximize Cohesion)

相似的功能代码应尽量放在一个部分。

13.隐藏实现细节(Hide Implementation Details)

隐藏实现细节原则,当其他功能部分发生变化时,能够尽可能降低对其他组件的影响。

14.迪米特法则又叫作最少知识原则(Law of Demeter)

该代码只和与其有直接关系的部分连接。(比如:该部分继承的类,包含的对象,参数传递的对象等)。

15.避免过早优化(Avoid Premature Optimization)

除非你的代码运行的比你想像中的要慢,否则别去优化。假如你真的想优化,就必须先想好如何用数据证明,它的速度变快了。

“过早的优化是一切罪恶的根源”——Donald Knuth

16.代码重用原则(Code Reuse is Good) 

重用代码能提高代码的可读性,缩短开发时间。

17.关注点分离(Separation of Concerns)

不同领域的功能,应该由不同的代码和最小重迭的模块组成。

18.拥抱改变(Embrace Change)

这是Kent Beck一本书的标题,同时也被认为是极限编程和敏捷方法的宗旨。

许多其他原则都是基于这个概念的,即你应该积极面对变化。事实上,一些较老的编程原则如最小化耦合原则都是为了使代码能够容易变化。无论你是否是个极限编程者,基于这个原则去编写代码会让你的工作变得更有意义。

作者简介:Christopher Diggins是加拿大一位有25年编程经验的资深技术人员,曾效力于Microsoft和Autodesk,并创办过两家赢利的互联网公司。

他是《C++ Cookbook》的作者之一,并自己编写了一门编程语言Heron。

(rnifeasy/译)

英文链接:artima

With the rapid development of China's economy, the per capita share of cars has rapidly increased, bringing great convenience to people's lives. However, with it came a huge number of traffic accidents. A statistical data from Europe shows that if a warning can be issued to drivers 0.5 seconds before an accident occurs, 70% of traffic accidents can be avoided. Therefore, it is particularly important to promptly remind drivers of potential dangers to prevent traffic accidents from occurring. The purpose of this question is to construct a machine vision based driving assistance system based on machine vision, providing driving assistance for drivers during daytime driving. The main function of the system is to achieve visual recognition of pedestrians and traffic signs, estimate the distance from the vehicle in front, and issue a warning to the driver when needed. This driving assistance system can effectively reduce the probability of traffic accidents and ensure the safety of drivers' lives and property. The main research content of this article includes the following aspects: 1. Implement object detection based on the YOLOv5 model. Conduct research on convolutional neural networks and YOLOv5 algorithm, and develop an object detection algorithm based on YOLO5. Detect the algorithm through road images, and analyze the target detection algorithm based on the data returned after training. 2. Estimate the distance from the front vehicle based on a monocular camera. Study the principle of estimating distance with a monocular camera, combined with parameters fed back by object detection algorithms, to achieve distance estimation for vehicles ahead. Finally, the distance estimation function was tested and the error in the system's distance estimation was analyzed. 3. Design and implementation of a driving assistance system. Based on the results of two parts: target detection and distance estimation, an intelligent driving assistance system is constructed. The system is tested through actual road images, and the operational effectiveness of the intelligent driving assistance system is analyzed. Finally, the driving assistance system is analyzed and summarized.
06-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值