整洁的代码的原则

Posted wust-hy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了整洁的代码的原则相关的知识,希望对你有一定的参考价值。

1. Model is everything

  models are  the heart of your app. If you have models separated from th rest of your application, maintaince will be much easier,regradless of how complicated your application becomes. Even for complicated applications, good model implementation can result in extremely expressive code.And to achieve that, start by making sure that your models do only what they meat to do ,and don‘t concern thenselves with what the app built around it does. Furthermore, it doesn‘t concern itself with what the underlying data storage layer is:does your app rely on an SQl database,or dose it store everything in text files.

  As we continue this article, you will realize how great code is a lot about separatiuon of concern.

  数据模型是一个应用的心脏,当数据模型与应用的其它模块分离开来的时候,应用的可维护性将变得容易。一个应用的数据模型应该保持单一职责原则,不应该考虑建立的数据模型上的其它模块,同时也不应该关注数据是如何存储的。 (MVC)

  面向对象编程的5个原则:

  <1>单一职责原则

    是指一个类或者模块应该有且只有一个去改变它的理由,这意味着一个类应该只有一项职责,并且该功能应该由这个类完全封装。 

  <2>开发封闭原则

    一个类应该对扩展是开放的,但是对修改是封闭的.意味着一个实体允许在不改变它的源代码的前提下变更它的行为

  <3>里氏替换原则

    里氏替换原则是对子类型的特别定义,该原则描述为:派生类对象能够替换其基类对象被使用。

  <4>接口隔离原则

    接口隔离原则推荐拆分庞大的接口成为更小更具体的接口,客户端不应该被被迫依赖它们不使用的方法。

  <5>依赖反转

    实体必须依靠抽象而不是具体的实现,它表示高层次的模块不应该依赖低层次的模块,它们都应该依赖于抽象。

  整洁的代码

  1.clean code is a consistent style of programming that makes your code easier to write,read ,and maintain.

  2.clean code is DRY (don‘t repeat yourself)

  3.clean code is predictable and testable

  4.clean code is self-commenting

  5.naming things

    1.Boolean variables,or functions that return a boolean value, should start with "is","has","should"

    2.Functions should be named for what they do,not how they do it.In other words ,don‘t expose details of the implementation in the name

  6.clean code follows proven design patterns and best practices

    1.use samll fuctions,each with a single responsibility.Ensure that each function does one job and does it well . that also will lead to better testability.

    2.Be on the lookout for leaky abstractions. In onther words, don‘t impose your internal requirements on consumers of your code.

    3.Follow stric linting rules. This will help you write clean , consistent code.

以上是关于整洁的代码的原则的主要内容,如果未能解决你的问题,请参考以下文章

代码整洁之道 读书笔记

代码整洁之道 读书笔记

原来这样写的Javascript才整洁

11个让你HTML代码整洁的原则

11个让你HTML代码整洁的原则

读C#代码整洁之道笔记01_C#的编码标准和原则