程序员编程培训_每个程序员都必须知道的5条编程原则

程序员编程培训

Thanks to a proliferation of free online tutorials, virtually anyone can learn how to code. Once you’ve been a developer for a while though, you quickly start to realize that all code is not created equal. Programming forums are awash with horror stories detailing ginormous if-else blocks, massive spaghetti-like algorithms and redundant code that serve no purpose.

 

由于免费在线教程的大量增加,几乎任何人都可以学习编码。 一旦成为开发人员已有一段时间,您就会很快开始意识到并非所有代码都是平等的。 编程论坛上充斥着恐怖的故事,详细描述了巨大的if-else块,大量类似于意大利面条的算法以及毫无用处的冗余代码。

While these may seem like rookie errors that only bedevil persons just starting out, many programmers carry on these bad habits well into their career with disastrous results.

尽管这些看上去像菜鸟般的错误,只是让那些刚起步的人迷上了,但许多程序员却将这些不良习惯带入了他们的职业生涯,并带来了灾难性的后果。

Image Source

图片来源

Below are some of the useful principles of programming that you must keep in mind while writing code.

以下是编写代码时必须记住的一些有用的编程原理。

1.简单性 (1. Simplicity)

Simplicity is the ultimate sophistication and perhaps nowhere more than in programming. It all begins with how you document and dissect program requirements. Each requirement should be well articulated to the extent that once you start to code, you can satisfy these requirements using the simplest of techniques.

简单性是最终的复杂性,也许在编程中无处不在。 这一切都始于您如何记录和分析程序需求。 每个要求都应明确阐明,以便您开始编码后就可以使用最简单的技术来满足这些要求。

Complex code not only takes more time to design and write but is also more vulnerable to errors and bugs. A labyrinth of code can make web app monitoring tedious. Beware of feature creep where you start to add new features to the program that the customer didn’t ask for as this only needlessly entangles the software.

复杂的代码不仅需要花费更多的时间来设计和编写代码,而且更容易出错和出现错误。 迷宫般的代码会使 Web应用程序监视 变得 乏味。 当心功能蔓延,在这里您开始向客户不需要的程序添加新功能,因为这只会不必要地纠缠软件。

2.不要重复自己 (2. Do Not Repeat Yourself)

Minimal repetition is a sign of quality code. Avoid duplicating logic and data. To know whether your program has excessive repetition, think about how much code you’d need to modify if you wanted to alter one aspect of the application’s behavior.

重复最少是质量代码的标志。 避免重复逻辑和数据。 要知道您的程序是否重复过多,请考虑如果您想改变应用程序行为的一个方面,需要修改多少代码。

To minimize duplication, identify all sections of code that do the same thing. Abstract this common blocks of code into a single function that you call whenever you need to perform the said task. That way, if you ever need to change how the task is performed, you’ll need only modify the one function as opposed to amending multiple lines of code throughout program.

为了最大程度地减少重复,请确定执行相同操作的所有代码段。 在需要执行上述任务时,将这些通用代码块抽象为一个函数。 这样,如果您需要更改任务的执行方式,则只需要修改一个函数,而不是在整个程序中修改多行代码即可。

3.现在而不是将来的代码 (3. Code for the Now not the Future)

One of the smart principles of programming is to make provisions for future changes in the code. It should be easy for a third party who was not involved in the project, to follow the logic and add or remove functionality as needed.

编程的明智原则之一是为将来的代码更改做好准备。 对于不参与项目的第三方来说,遵循逻辑并根据需要添加或删除功能应该很容易。

Nevertheless, making provisions for future maintenance isn’t the same as coding in functionality that you might need in future. The latter only increases the volume of code while creating a new avenue for bugs. In any case, such functionality is often not needed at all in future.

但是,为将来的维护做准备与将来可能需要的功能编码不同。 后者只会增加代码量,同时 为错误 创建新的 途径 。 无论如何,将来通常根本不需要这种功能。

Coding for the now prevents scope creep. Unless the end user specifically requests for this functionality to be included from the onset, stay away from it.

现在进行编码可防止范围蔓延。 除非最终用户明确要求从一开始就包含此功能,否则请远离它。

4.不要过早优化 (4. Don’t Optimize Prematurely)

When you enroll for a basic programming course, one of the areas of emphasis is the need to create fast efficient algorithms. The mistake some coders make is to run with this one lesson. They get immersed in trying to speed up and optimize the code from the start while losing sight of the primary objective which is a creating a functioning program.

当您注册基础编程课程时,重点领域之一就是需要创建快速高效的算法。 一些编码人员犯的错误是在学习这一课时。 他们全神贯注于从一开始就试图加快代码的速度并优化代码,同时又忽略了创建功能正常的程序的主要目标。

Work on meeting the system requirements first then move on to optimize the code. Optimizing too early is akin to groping in the dark. Before the program is up and running, you cannot really be certain where the bottlenecks will be. You could waste precious time optimizing a function whose speed doesn’t contribute much to overall program performance.

首先要满足系统要求,然后继续进行代码优化。 太早进行优化类似于在黑暗中摸索。 在程序启动并运行之前,您无法真正确定瓶颈将在何处。 您可能会浪费宝贵的时间来优化功能,而该功能的速度对整体程序性能的影响并不大。

Ergo, first satisfy the program requirements then work on resolving the bottlenecks.

因此,首先要满足程序要求,然后再解决瓶颈。

5.聪明的代码之上的可读代码 (5. Readable Code Over Clever Code)

Clever code is programming that is less about creating easily understandable code and more about showing everyone how smart you are. A typical example of clever code is packing an excessive amount of program logic into a single line of code.

聪明的代码是编程,而不是创建易于理解的代码,而更多的是向所有人展示您的聪明程度。 聪明代码的典型示例是将过多的 程序逻辑 打包 到一行代码中。

Minimal code was at one time heralded as a sign of good programming but over time, the industry has recognized that brief code is meaningless if it’s hard for someone else to follow the logic. Any line of code that would take another programmer dozens of minutes to understand should probably be done away with.

极少的代码曾一度被认为是良好编程的标志,但随着时间的流逝,业界已经认识到,如果其他人很难遵循逻辑,那么简短的代码就毫无意义。 可能需要其他程序员花费数十分钟才能理解的任何代码行,都应该删除。

Good programming is not only about building applications that meet the end user’s expectation but also creating code that is easy to understand. Following these programming principles will ensure you achieve both.

好的程序设计不仅与构建满足最终用户期望的应用程序有关,而且与创建易于理解的代码有关。 遵循这些编程原则将确保您同时实现两者。

原创文章,作者:搜够小编,如若转载,请注明出处:http://www.sogoubaike.cn/archives/69151