系统环境编码_编码环境中的设计

系统环境编码

This is a story about why basic design principles should be important to you as an object orientated programmer.

这是一个故事,说明为什么作为面向对象的程序员,基本设计原则对您来说很重要。

What is design in its basic form? When used with an object, to design is to plan and fashion artistically or skillfully. Design is the act of preparation for work to be executed.

基本设计是什么? 与对象一起使用时, 设计就是艺术地或熟练地进行规划和时尚化。 设计是准备要执行的工作的动作。

什么是代码设计,为什么要实现它? (What is code design and why should you implement it?)

In the context of a program, design is the manner in which a developer organizes, arranges, and composes their code. Some may argue that the manner in which you organize your code may seem irrelevant to the overall goal of writing code that actually works the way you want it to. Basic design principles, however, can function to secure the functionality of your code in the future.

在程序的上下文中,设计是开发人员组织,安排和组成其代码的方式。 有人可能会争辩说,组织代码的方式似乎与编写按您希望的方式实际工作的代码的总体目标无关。 但是,基本的设计原则可以在将来用于保护代码的功能。

As a developer, the end-result matters. When does it end? It shouldn’t, and it won’t. Good web apps will require change along with growth. It is a matter of how to adapt to your super viral app or website so that it serves the users needs and can optimally function as traffic grows. On the client-side, what if on the last day of that big project your client has a major change they need to implement? Do you just quit? Of course not, you wrote flexible, well designed code that accommodates changes on a whim. You are a design first developer.

作为开发人员,最终结果很重要。 什么时候结束? 它不应该,也不会。 好的Web应用程序将需要随着增长而变化。 这取决于如何适应您的超级病毒应用或网站,以使其满足用户需求并随着流量的增长而发挥最佳作用。 在客户端,如果在该大型项目的最后一天您的客户需要实施重大更改,该怎么办? 你刚退出吗? 当然不是,您编写了灵活,设计良好的代码,可以随心所欲地进行更改。 您是设计第一的开发人员。

Another huge reason to appreciate design as an object oriented programmer is your very own happiness. It’s all about looks and moods, isn’t it?

欣赏设计作为面向对象的程序员的另一个重要原因是您自己的幸福。 都是关于容貌和情绪的,不是吗?

“I wanted to minimize my frustration during programming, so I want to minimize my effort in programming. That was my primary goal in designing Ruby. I want to have fun in programming myself. After releasing Ruby and many people around the world got to know Ruby, they said they feel the way I feel. They came up with the phrase the principle of least surprise. But actually, it’s often misunderstood.”

“我想在编程过程中,尽量减少我的无奈 ,所以我要尽量减少我的编程工作 。 那是我设计Ruby的主要目标。 我想在编程方面玩得开心 。 在发布Ruby之后,世界各地的许多人都认识了Ruby,他们说他们有我的感受。 他们想出了最不惊奇的原则。 但是实际上,它经常被误解。”

-Yukihiro Matsumoto, creator of ruby.

-Ruby的创造者松本幸弘(Yukihiro Matsumoto)。

As developers, we are part engineer and part artist. The code you write should be designed with three F’s in mind; Functional, Flexible, and Free of nonsense.

作为开发人员,我们是部分工程师和部分艺术家。 您编写的代码在设计时应注意三个F ; 功能齐全,灵活且毫无意义。

Realistically, the work of honing your design skills as a programmer will take place over hours and hours, and hours of practical application in your life and career. However, today we touch on a very simple starting point to some of the basic principles that I keep in mind as I continue to learn more about object oriented Ruby.

实际上,磨练您作为程序员的设计技能的工作将花费数小时甚至数小时,以及数小时的实际应用工作。 但是,今天,当我继续学习有关面向对象的Ruby时,我将牢记一些基本原则,这是一个非常简单的起点。

Design is not the act of following a fixed set of rules, it’s a journey along a branching path wherein earlier choices close off some options and open access to others. During design you wander through a maze of requirements where every juncture represents a decision point that has consequences for the future. Just as a sculptor has chisels and files, an object-oriented designer has tools — principles and patterns.

设计不是遵循一组固定规则的行为,而是沿着分支路径的旅程,在此过程中,较早的选择会关闭某些选项并打开对其他选项的访问权限。 在设计过程中,您会在各种需求的迷宫中徘徊,每个关口都代表一个对未来有影响的决策点。 就像雕刻家有凿子和文件一样,面向对象的设计师也有工具-原理和图案。

-Sandi Metz, Principles of Object-Oriented Design

-Sandi Metz面向对象设计原理

单一责任原则和关注点分离。 (The Single responsibility principle and separation of concerns.)

The single responsibility principle or (SRP) is the concept that classes in object oriented programming should have one job, one responsibility, and their services should be aligned with their job. If you consider all living things as a class, under that class you would have Plant and Animal classes. Under Animal you could have Mammal, Reptile, and so forth. Looking at Mammal you could operate through Primate and Human. The idea is that each level down is more specific to the one above it. For a more authoritative stance you should consider the words of Robert C. Martin, the creator of the single responsibility principle;

单一责任原则(SRP)是一种概念,即面向对象编程中的类应具有一项工作,一项职责,并且其服务应与其工作保持一致。 如果您将所有生物都视为一类,那么在该类下您将拥有植物动物类。 在“动物”下,您可以拥有“ 哺乳动物” ,“ 爬行动物”等。 看着哺乳动物,你可以通过灵长类人类来运作。 想法是,每个级别的下移都针对其上一个级别。 对于更权威的立场,您应该考虑单一责任原则的创建者Robert C. Martin的话

Gather together those things that change for the same reason, and separate those things that change for different reasons.

将由于相同原因而改变的那些东西聚集在一起,并把由于不同原因而改变的那些东西分开。

SRP can fit like two peas in a pod with SoC (separation of concerns). A concern is any piece of interest or focus in a program. Typically, concerns are synonymous with features or behaviors. SoC is taking care of each of the concerns: one concern at a time. SoC is the process of breaking a computer program into distinct features that overlap in functionality as little as possible. You wouldn’t go to your favorite taco spot to order a slice of pizza, would you? We have high-level responsibilities that break down into lower-level responsibilities, and each component at each level of this hierarchy should have a single responsibility in context to that level. Your taco spot is where you go for tacos, probably burritos, and nachos too. Your pizza spot is the go to for that extra large pepperoni pizza, calzone, and I think you get the picture? By having classes that have their own responsibilities we create organization, logic and accommodation.

SRP可以像两个豌豆一样放入SoC的吊舱中(关注点分离)。 关注点是程序中的任何关注点或关注点。 通常,关注与功能或行为同义。 SoC正在处理每个问题:一次关注一个问题。 SoC是将计算机程序分解为功能尽可能重叠的不同功能的过程。 您不会去自己喜欢的炸玉米饼的地方订购一片披萨,对吗? 我们具有高级职责,这些职责可分解为较低级别的职责,并且此层次结构中每个级别的每个组件都应在该级别的上下文中具有单个职责。 您可以在玉米面豆卷上找到炸玉米饼,墨西哥卷饼和玉米片。 您的披萨店是超大意大利辣香肠披萨(calzone)的去处,我想您知道图片吗? 通过让班级承担各自的职责,我们可以创建组织,逻辑和适应。

抽象,不要重复自己(干) (Abstraction and don’t repeat yourself (dry))

This one is pretty simple. In object orientation there is a principle called DRY. Don’t Repeat Yourself. Don’t repeat the same line of code again and again. Maybe consider refactoring. You want to try and achieve the same behavior with less code. If you are writing code more than 3 times, it should raise a red flag of poor design to you. Having abstracted attributes wrapped inside methods instead of having code rely on literal values can be beneficial. The main benefit? You don’t have to repeat this logic in other views when you need it & when you need to change the code… it only has to change in one place.

这很简单。 在面向对象中,有一个称为DRY的原理。 不要重复自己。 不要一次又一次地重复同一行代码。 也许考虑重构。 您想要尝试使用更少的代码来实现相同的行为。 如果您编写的代码超过3次,它会给您带来设计不佳的危险。 将抽象属性包装在方法中而不是使代码依赖于文字值可能是有益的。 主要的好处? 在需要时以及在需要更改代码时,您不必在其他视图中重复此逻辑……只需在一个位置进行更改。

方法和类的行数限制 (Line limits on methods and classes)

Of the principles of Object Orientation include the notion that methods should not exceed 5 lines of code and classes should not exceed 100. Obviously this is not set in stone but it should be a point to consider wherever possible.

对象定向的原则包括以下观点:方法不应超过5行代码,类不应超过100行。显然,这不是一成不变的,但是应该尽可能地考虑这一点。

结论 (Conclusion)

This was meant to be a very brief intro to some of the design concepts in object orientated ruby. Hopefully this can be a point of reference and doesn’t limit you. We just scratched the surface when it comes to OO design. The more hours you put in coding, the more you’ll learn about design. Feel free to check out some resources that have helped me when you’re ready to take a deeper dive into object oriented design.

这旨在对面向对象的Ruby中的一些设计概念进行非常简短的介绍。 希望这可以作为参考,并且不会限制您。 面向对象设计时,我们只是在表面上作了铺垫。 您投入编码的时间越多,您将学到的设计知识越多。 当您准备深入研究面向对象的设计时,请随时查看一些对我有所帮助的资源。

翻译自: https://medium.com/@anthonyalexanderpolito/d-e-s-i-g-n-in-the-context-of-coding-43c80de63cd

系统环境编码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值