每个程序员都必须知道的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
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 a
way 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.
好的程序设计不仅与构建满⾜最终⽤户期望的应⽤程序有关,⽽且与创建易于理解的代码有关。遵循这些编程原则将确保您同时实现两者。
本⽂转⾃:www.chuangzai/post/23846.html转载请注明

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。