git每个开发人员一个分支
Being a good software engineer is not just about coding, it’s about solving problems in the most effective and efficient manner. This can be achieved through algorithms, unit testing, and time-space complexity analysis. There are also a set of tried and true principles that have been developed over the years to refine the art and science of developing software applications. Those principles serve as a guide towards better and cleaner code that is easier to debug and understand.
乙eing一个优秀的软件工程师不只是编码,它是关于解决在最有效的方式解决问题。 这可以通过算法,单元测试和时空复杂度分析来实现。 多年来,还开发了一组经过实践检验的真实原则,以完善开发软件应用程序的技巧和科学。 这些原则为更好更干净的代码提供了指南,这些代码更易于调试和理解。
Here’s our list of the most important principles of software engineering.
这是我们列出的最重要的软件工程原理。
This principle originates from the book “The Pragmatic Programmer” by Andy Hunt and Dave Thomas, who defined it as:
该原理源自Andy Hunt和Dave Thomas的“实用程序员”一书,他将其定义为:
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
每条知识都必须在系统中具有单一,明确,权威的表示形式。
This means that every piece of data should have a single reference point or single source of truth (SSOT), such that changing a single part of that data doesn’t require changing logically unrelated elements. That’s because all occurrences of that data refer back to a single location. This is in contrast to code that requires changing every instance of say a variable whenever you change a single instance of it in your source code. Code that doesn’t abide by the DRY principle is said to be WET, which stands for “write everything twice”, “we enjoy typing”, “write every time”, or “waste everyone’s time”! Applying DRY to code reduces redundancy and makes it easier to manage later on.
这意味着每个数据段都应具有单个参考点或单个真理源(SSOT),从而更改该数据的单个部分不需要更改逻辑上不相关的元素。 这是因为该数据的所有出现都引用回一个位置。 这与需要在源代码中更改变量的单个实例时更改变量的每个实例的代码形成对比。 不遵守DRY原理的代码被称为WET,它代表“将所有内容写两次”,“我们喜欢打字”,“每次写”或“浪费每个人的时间”! 将DRY应用于代码可减少冗余,并使以后更易于管理。
According to The Routledge Dictionary of Modern American Slang and Unconventional English, this principle was devised by the U.S. NAVY in 1960. They observed that most systems work best when kept simple and plain, as opposed to complicated and complex. Experience has shown that complexity causes bugs because there are more (unnecessary) parts to keep track of and fewer engineers can understand how they connect. KISS makes your code cleaner and less likely to have bugs. Such code is also easier to debug and maintain.
根据《现代美国S语和非常规英语的Routledge词典》 ,这一原理是由美国海军于1960年提出的。他们观察到,大多数系统在保持简单明了的同时,而不是复杂的情况下,效果最佳。 经验表明,复杂性会导致错误,因为有更多(不必要的)部分需要跟踪,而更少的工程师可以理解它们如何连接。 KISS使您的代码更整洁,并且更少出现错误。 这样的代码也更容易调试和维护。
YAGNI comes from the software development methodology called Extreme Programming (XP). Who better to define it than XP co-founder, Ron Jeffries, who said:
YAGNI来自称为极限编程(XP)的软件开发方法。 有谁比XP联合创始人Ron Jeffries更好地定义它:
Always implement things when you actually need them, never when you just foresee that you need them.
始终在真正需要它们时执行它们,永远不要在仅仅预见到需要它们时就执行它们。
It’s not uncommon for developers to add features to their application in anticipation of a potential need. The ability to delete a comment might be tempting for some, but it’s becoming quite standard now to leave out the ability to delete it altogether. Platforms like Youtube and LinkedIn, for example, don’t allow you to delete your comments. Avoiding features that are unnecessary is a good practice in software engineering. However, there are cases where anticipating a future use or need for a feature might be useful.
对于开发人员而言,在预期潜在需求的情况下向其应用程序添加功能并不罕见。 删除评论的功能可能吸引了一些人,但现在完全删除评论的功能已成为一种标准。 例如,Youtube和LinkedIn等平台不允许您删除评论。 在软件工程中,避免不必要的功能是一种很好的做法。 但是,在某些情况下,预期将来使用或需要某个功能可能会很有用。
It consists of design principles that first appeared in Robert C. Martin’s 2000 paper entitled Design Principles and Design Patterns. They are:
它由最早出现在罗伯特·C·马丁(Robert C. Martin)2000年的论文《设计原理和设计模式》中的设计原理组成。 他们是:
Single-Responsibility Principle: Every class, method, or module should have a single responsibility, such that only one part of an application should be able to affect the class if that part is changed. It makes sense in real life. Your bedroom is not meant for showering, nor is your shower meant for sleeping. Classes and methods can be thought of as rooms with separate purposes and responsibilities. This makes your code more organized and easier to follow.
单一职责原则:每个类,方法或模块都应具有单一职责,因此,如果更改了一部分,则应用程序的仅一部分应能够影响该类。 在现实生活中这很有意义。 您的卧室不适合淋浴,也不适合睡觉。 可以将类和方法视为具有不同目的和职责的房间。 这使您的代码更有条理,更易于遵循。
Open-Closed Principle: This means open for extension and closed for modification.
开闭原理:这意味着可以扩展,而可以修改。
Open for extension means that existing code should be developed with the ability to build on top of it, without having to start from scratch. We shouldn’t need to change existing code in order to add functionality or features to it.
开放扩展意味着现有代码应具有在其之上构建的能力,而不必从头开始。 我们不需要更改现有代码即可为其添加功能。
Closed for modification means that once existing code is working we should try not to make unneeded changes to it. The reason for that is that it can open a can of worms by forcing us to also change all the code that is related to it, unbeknown to us. To avoid giving ourselves more work than needed, existing code should be left alone, ideally.
关闭进行修改意味着一旦现有代码正常工作,我们应该尽量不要对其进行不必要的更改。 这样做的原因是,它可以迫使我们也更改与它不相关的所有代码,从而打开蠕虫病毒罐。 为了避免给自己增加不必要的工作,理想情况下,应将现有代码保留下来。
Please keep in mind that there often very valid reasons for refactoring code that may be clumsy or difficult to follow. When refactoring can provide clarity or improve performance, exceptions to this principle ought to be made.
请记住,重构代码通常有非常合理的理由,可能笨拙或难以理解。 当重构可以提供清晰性或改善性能时,应对此原则进行例外处理。
Liskov Substitution Principle: This means that your subclasses should behave the same as the class they inherit from. In other words, you should be able to substitute the child for the parent class and expect the same basic behavior. For instance, say you have a vehicle class with the property of wheels and method drive().
Liskov替代原则:这意味着您的子类应具有与其继承的类相同的行为。 换句话说,您应该能够将孩子替换为父类,并期望具有相同的基本行为。 例如,假设您有一个具有车轮属性和方法drive ()的车辆类别。
class Vehicle { wheels: 4; drive(){ System.out.println("Vroom!!!"); }}class Sedan extends Vehicle { occupants: 4;}class Bicycle extends Vehicle { occupants: 1;}According to the Liskov substitution principle, you should be able to replace a parent object with a child object and vice versa and the code should behave the same way. Here, although a Sedan object can be replaced with a Vehicle object, you can’t do the same for a Bicycle object here. A bicycle, albeit a vehicle, shouldn’t have a ‘drive’ option, nor does it have 4 wheels. My Vehicle class would have to be changed to be more generic. We can fix that as follows below.
根据Liskov替换原则,您应该能够用子对象替换父对象,反之亦然,并且代码的行为应相同。 在这里,尽管可以将Sedan对象替换为Vehicle对象,但是在这里不能对Bicycle对象执行相同的操作。 自行车(尽管是车辆)不应具有“驱动”选项,也不应具有4个车轮。 我的Vehicle类必须更改为更通用。 我们可以通过以下方法解决此问题。
class Vehicle { Vehicle (chosenBrand, chosenModel, numSeats){ brand: chosenBrand; model: chosenModel occupants: numSeats; }}class Sedan extends Vehicle { drive(){ console.log("Vroom");}class Bicycle extends Vehicle { const numWheels = 2; ....}Interface Segregation Principle: This is best defined by Martin himself:
接口隔离原则:最好由Martin本人定义:
Many client-specific interfaces are better than one general-purpose interface.
许多特定于客户端的接口比一个通用接口要好。
It’s more practical to have separate interfaces that handle separate tasks than one that handles many. For example, in an application, it’s better to have separate tabs that deal with separate matters, like ‘Settings’, than to have one that deals with unrelated issues.
具有处理不同任务的单独接口比处理许多任务的接口更实际。 例如,在一个应用程序中,最好有单独的选项卡来处理诸如“设置”之类的单独事项,而不要有一个选项卡来处理不相关的问题。
Dependency Inversion Principle: High-level modules should not depend on low-level modules. The interaction between the two modules should be thought of as an abstract interaction between them, not a concrete one.
依赖倒置原则:高级模块不应依赖于低级模块。 这两个模块之间的交互应被视为它们之间的抽象交互,而不是具体的交互。
This refers to the fact of assigning a separate responsibility to each function, class, or component. It’s often very tempting to have a single function handle more than one task, however, it is best to divide the labor among specialized functions, or components. Say you’re developing a weather app, instead of having a single function that generates the weather forecast for a given day, it would be wiser to have a function that calculates the temperature, one that returns the humidity, and yet another that determines the date. This makes for cleaner code that is easier to read and troubleshoot.
这是指为每个功能,类或组件分配单独职责的事实。 单个功能处理多个任务通常很诱人,但是,最好将工作分担到专门的功能或组件中。 假设你正在开发一个天气应用程序,而不必产生天气预报某一天一个单一的功能,这将是明智的有一个函数,计算确定的温度,它返回的湿度,另一个日期。 这使得代码更清晰,更易于阅读和解决。
I want to note that although it may seem identical to the ‘single responsibility principle’, it’s not. Single responsibility says that every class or function should have its own responsibility. Separation of concerns says that you should break that single responsibility into smaller parts that have each their own responsibility. In our weather app example, you can have one function take the single responsibility of generating the weather. Ideally, you’d want to have a separate function for the date/time, one for the humidity, etc. That’s the separation of concerns. Therefore, it can be thought of as a subset of the single responsibility principle.
我想指出的是,尽管它看起来与“单一责任原则”相同,但事实并非如此。 单一责任表示每个类或职能都应承担自己的责任。 关注点分离表明,您应该将单一职责分解为较小的部分,每个部分都有各自的职责。 在我们的天气应用程序示例中,您可以让一个功能完全负责生成天气。 理想情况下,您希望为日期/时间设置一个单独的功能,为湿度设置一个功能,等等。这就是关注点的分离。 因此,可以将其视为单一责任原则的子集。
One of the benefits of the separation of concerns is that it allows for code reusability. Because each method was written to handle a separate task, it can now be reused in the future for similar objectives. So, in our weather app, our date-calculating function can be reused in a different part of our code that may also require the date.
关注点分离的好处之一是它允许代码可重用。 因为每种方法都是为处理单独的任务而编写的,所以现在可以在将来将其重复用于类似的目标。 因此,在我们的天气应用程序中,我们的日期计算功能可以在可能需要日期的代码的不同部分中重复使用。
Those are just a few of the principles of software engineering that are highly recommended for developers to keep in mind when developing applications. They allow for code that is easier to read, debug, and maintain over long periods of time. Please see the links below for additional resources on the topic.
这些只是软件工程的一些原则,强烈建议开发人员在开发应用程序时牢记这些原则。 它们使代码易于长时间读取,调试和维护。 请参阅下面的链接以获取有关该主题的其他资源。
Thanks for reading! Happing coding! :)
谢谢阅读! 正在编码! :)
翻译自: https://medium.com/swlh/6-principles-of-software-engineering-that-every-developer-should-know-7868f362b633
git每个开发人员一个分支
相关资源:微信小程序源码-合集6.rar