代码 干净_必须知道的干净代码原则

news/2024/7/10 2:41:52 标签: java, python, vue

代码 干净

重点 (Top highlight)

In a recent article, I wrote an article called: “40 Tips that will change your coding skills forever”, which is based on the things that have given me more value in this profession. In this new article, I am going to write about one of the books that have most inspired me: The Clean Code book, written by the God Robert C Martin, also known as ‘Uncle Bob.’

在最近的一篇文章中,我写了一篇文章:“ 40条将永远改变您的编码技能的技巧 ”,其依据是使我在该行业中拥有更多价值的事物。 在这篇新文章中,我将写一本最启发我的书:《 清洁密码》书,由上帝罗伯特·C·马丁 ( Robert C.

The book is one of the most important that exists for anyone who wants to be a good software developer. It has a lot of fantastic recommendations and is highly recommended for anyone who wants to improve their development skills.

对于任何想成为优秀软件开发人员的人来说,这本书都是最重要的书之一。 它有很多很棒的建议,强烈建议任何想要提高其开发技能的人使用。

Learning to use different libraries or frameworks is fine, but what is going to move you forward is learning the pillars of this profession. I think the first time I read it was in 2008 or 2009, and my way of developing software changed forever, for better. These principles will serve you forever, no matter what language you learn or what framework you will use.

学会使用不同的库或框架是可以的,但是要带动您前进的是学习该专业的基础。 我认为我是在2008年或2009年第一次阅读该书,并且我的软件开发方式永远都在改变,以求更好。 无论您使用哪种语言或使用哪种框架,这些原则都将永远为您服务。

I am going to select some of the ideas in it (and others that time has taught me), but if you have not read it, I strongly advise you to do so.

我将选择其中的一些想法(以及其他一些当时教会我的想法),但是如果您还没有阅读,我强烈建议您这样做。

Let’s start.

开始吧。

遵循的通用原则: (Universal principles to follow:)

  • Follow standard rules: experiments always end in disaster.

    遵循标准规则:实验总是以灾难告终。
  • Avoid duplication in the code (DRY principle or Don’t Repeat Yourself ).

    避免在代码中重复( DRY原理或请勿重复自己)。

  • We must apply The Boy Scouts rule to our profession: Leave the campground cleaner than you found it.

    我们必须将“童子军”规则应用于我们的职业:离开露营地要比发现的要干净。
  • Follow SOLID principles to write clean classes and well-organized APIs.

    遵循SOLID原则编写干净的类和组织良好的API。

命名规则: (Names rules:)

  • Use names that can be pronounced well.

    使用可以很好发音的名称。
  • Choose descriptive and clear names.

    选择描述性名称和明确名称。
  • Use searchable names.

    使用可搜索的名称。
  • Make it easy to remember them.

    使记住它们变得容易。
  • Use names according to the context.

    根据上下文使用名称。
  • Use names that are consistent with each other. For example, it’s confusing to have “retrieved” and “get” as equivalent methods in distinct classes.

    使用彼此一致的名称。 例如,在不同的类中将“检索”和“获取”作为等效方法是令人困惑的。
  • Use the same language in the names of variables, functions: English, French, etc. In my case, I prefer to use English because it’s a standard.

    在变量,函数的名称中使用相同的语言:英语,法语等。就我而言,我更喜欢使用英语,因为这是一种标准。
  • Avoid encodings and don’t append prefixes or type information.

    避免编码,并且不要附加前缀或类型信息。

注释: (Comments:)

  • Comments are difficult to maintain and don’t tell the truth about the code, so try to avoid it. They are almost always out of date.

    注释很难维护,并且无法说明代码的真相,因此请避免使用它。 他们几乎总是过时的。
  • The code is the best documentation.

    该代码是最好的文档。
  • Don’t be redundant.

    不要多余。
  • Avoid unnecessary comments (most of all).

    避免不必要的评论(最重要的是)。
  • Use only as a clarification of code.

    仅用作代码说明。

功能规则: (Functions rules:)

  • They should be short and only do one thing. If your function is doing more than “one thing,” it is a perfect moment to extract to another function.

    他们应该简短,只能做一件事。 如果您的函数所做的不只是“一件事情”,那是提取到另一个函数的绝佳时机。
  • Avoid passing booleans. Why do you need to pass a Boolean? Do you need to do more than one thing within a function?

    避免传递布尔值。 为什么需要传递布尔值? 您是否需要在一个函数中执行多项操作?

  • Keep the number of arguments as low as possible.

    保持参数数量尽可能少。
  • Avoid side effects. Declare the arguments as final(Java) if you can.

    避免副作用。 如果可以,将参数声明为final(Java)。
  • Functions should either answer something or do something, but not both.

    函数应该回答某件事或做某事,但不能两者都做。
  • Prefer Exceptions to return error codes and extract error handling try catch into their function.

    首选使用异常以返回错误代码并提取错误处理,并尝试捕获其功能。
  • Don’t return a null value. What is null? It does not provide any information.

    不要返回空值。 什么是null? 它不提供任何信息。

设计规则: (Design rules:)

  • You should declare local variables as close as you can to their usage.

    您应该声明局部变量,使其尽可能接近其用法。
  • You should declare instance variables at the top of the class.

    您应该在类的顶部声明实例变量。
  • Constants should be declared at the top of the class or in a Constants class by example.

    例如,常量应在类的顶部或常量类中声明。
  • Follow the Law of Demeter: A class should know only its direct dependencies.

    遵循Demeter法则 :类应该只知道其直接依赖关系。

  • Coding is like writing; try to express the purpose of the programmer and the code.

    编码就像写作; 尝试表达程序员的目的和代码。
  • Use dependency injection (DIP).

    使用依赖项注入( DIP )。

  • If you use third-party libraries, wrap them, so if they change, only your wrapper implementation needs to change.

    如果您使用第三方库,请对其进行包装,因此,如果它们发生更改,则只需要更改包装器实现。
  • It is a good idea to separate concepts vertically.

    垂直分离概念是个好主意。
  • Place methods in a top-down direction.

    将方法自上而下放置。

格式: (Formatting:)

  • Avoid too-long files.

    避免文件过长。
  • Good files have a heading, the critical stuff first, and details later.

    好的文件应有标题,关键的内容在前,细节在后。
  • Although we now have big screens and with high resolution, avoid lines get too long (80 or 120 is perfect, in my case 100). You will get used to being more concise, and your code will be more readable.

    尽管我们现在拥有大屏幕且具有高分辨率,但请避免线条过长(以80或120为最佳,以我为100)。 您将习惯于变得更加简洁,并且代码将更具可读性。
  • Be consistent with the rules of your team.

    与团队规则保持一致。

类: (Classes:)

  • Classes should be small.

    班级应该很小。
  • Classes should have only one responsibility and only one reason to change (SRP).

    班级应该只承担一项责任,只有一个改变的理由( SRP )。

  • Keep utility methods and variables private except some cases for doing testing.

    除了某些进行测试的情况外,将实用程序方法和变量保持私有。
  • Use correctly package levels: public, protected, or package.

    正确使用程序包级别:公共,受保护或程序包。

对象和数据结构: (Objects and Data Structures:)

  1. Hide internal structure.

    隐藏内部结构。
  2. If you can, call only your methods of your class, of objects you have created, and avoid call methods reachable through these objects (Law of Demeter).

    如果可以,请仅调用类的方法,已创建的对象的方法,并避免通过这些对象可访问的调用方法(Demeter法)。
  3. Improve the decoupling of objects.

    改善对象的解耦。
  4. Variables should be private and manipulated by getters and setters, but remember, there is no necessity to add getters/setters to each variable to expose them as public.

    变量应该是私有的,并由getter和setter进行操纵,但是请记住,没有必要在每个变量中添加getter / setter以将它们公开。
  5. The base class should know nothing about their derivatives.

    基类对它们的派生词一无所知。
  6. Objects expose behavior and hide data. Conversely, data structures expose data and lacks of (significant) behavior.

    对象公开行为并隐藏数据。 相反,数据结构暴露数据并且缺乏(重要的)行为。

异常处理: (Exception handling:)

  • Throwing errors makes code cleaner than checking for status values throughout the code.

    抛出错误使代码比检查整个代码中的状态值更干净。
  • Provide enough meaning to determine the cause and location of an error.

    提供足够的含义以确定错误的原因和位置。
  • Wrap third-party libraries APIs to remap their exceptions as required.

    包装第三方库API以根据需要重新映射其异常。

并发: (Concurrency:)

  • Concurrency, although it may improve the performance of the program, is difficult, so use it wisely and only when required.

    并发虽然可以提高程序的性能,但是却很困难,因此请明智地使用并仅在需要时使用。
  • Keep concurrency control separate from other code.

    将并发控制与其他代码分开。
  • Know basic concepts and basic programming models like mutual exclusion, starvation, or deadlocks.

    了解基本概念和基本编程模型,例如互斥,饥饿或死锁。
  • Create locked sections small.

    创建较小的锁定部分。

测试: (Tests:)

  • Fast: Unit tests should be fast and being executed in a short time.

    快速:单元测试应快速并在短时间内执行。
  • Independent: Tests should not depend on each other.

    独立性:测试不应相互依赖。
  • Repeatable: Tests should be reproducible in any environment.

    可重复:测试在任何环境下都应具有可重复性。
  • One assert per test.

    每个测试一个断言。
  • If you have the code covered by tests, you will not be afraid to modify it and break it.

    如果您的代码包含在测试中,则无需担心对其进行修改和破坏。
  • TDD: Build your software using tests that guide your development.

    TDD :使用可指导开发的测试来构建软件。

结论 (Conclusion)

Learn the basics and the pillars of programming, and your code will be robust and will adapt to changes more quickly. Also, other programmers will be able to keep it without going crazy.

学习编程的基础知识和基础知识,您的代码将很健壮,并且可以更快地适应更改。 同样,其他程序员也可以保留它而不会发疯。

Thank you very much for reading to me, and thank you for your time!

非常感谢您阅读我的文章,也感谢您的宝贵时间!

翻译自: https://medium.com/swlh/the-must-know-clean-code-principles-1371a14a2e75

代码 干净


http://www.niftyadmin.cn/n/1468977.html

相关文章

mysql开启慢查询日志_mysql开启慢查询日志及验证过程

mysql慢查询1、慢查询作用?它能记录下所有执行超过long_query_time时间的sql语句,帮你找到执行慢的sql,方便我们对这些sql语句进行优化。2、如何开启慢查询?首先我们先查看mysql服务器的慢查询状态是否开启。执行如下指令:我们可…

sparksql mysql_使用SparkSQL操作MySQL - Spark入门教程

抽离Common信息SparkHelper用于获取SparkSession和SparkContext。import org.apache.spark.SparkConf;import org.apache.spark.api.java.JavaSparkContext;import org.apache.spark.sql.SparkSession;/*** Auther: majx2* Date: 2019-8-2 09:41* Description:*/public class S…

web接口开发与测试——你了解多少

目录 前言 Django快速开发之投票系统 编写接口文档 系统接口测试 总结: 前言 最近一直在学习和整理web开发与接口测试的相关资料。接口测试本身毫无任何难度,甚至有很多工具和类库来帮助我们进行接口测试。大多测试人员很难深入了解web接口测试的原…

mysql q4m_MySQL消息存储引擎Q4M试玩

1. 安装二进制包:由于我的是5.1.48,从官网选择对应的包http://q4m.kazuhooku.com/dist/old/下载后解压a. 将support-files/q4m-forward 拷贝到mysql安装目录/bin下b. 将libqueue_engine.so 拷贝到mysql安装目录/lib/mysql/plugin下执行:$cat …

peewee_如何在peewee中使用postgis几何

peeweeIt’s astonishing to me how many people have this problem, and nobody thought to write up a solution to make other people’s searches a little easier. So, here I am, to save you some time with your Peewee and PostGIS adventures.令我惊讶的是&#xff0…

mysql string 连接_MySql 连接字符串

一、MySQL Connector/ODBC 2.50 (MyODBC 2.50)连接方式1、本地数据库连接Driver{MySQL};Serverlocalhost;Option16834;DatabasemyDataBase;2、远程数据连接Driver{MySQL};ServermyServerAddress;Option131072;Stmt;DatabasemyDataBase; UsermyUsername;PasswordmyPassword;3、特…

golang dns服务器_2020年在golang中建立网络服务器的初学者指南

golang dns服务器In this article, I’ll teach you how to create simple web servers with the Go language.在本文中,我将教您如何使用Go语言创建简单的Web服务器。 Golang入门 (Getting Started With Golang) It would be best if you have Go installed on yo…

oracle异构mysql_配置Oracle GoldenGate异构oracle到mysql同步

最近研究了一下oracle Goldengate异构同步的过程,真是几天不用手生,敲命令竟然如此生疏。不过还算顺利,经过一番折腾终于好了。环境描述:192.0.2.101( Oracle ) —>192.0.2.102 (Mysql )版本:操作系统:redhat6.5Ora…