Python Clean Code简洁代码的9原则

本文介绍了编写简洁代码的九大原则,包括单一职责原则、避免使用变通方法、去除冗余、保持简单、易于扩展、减少依赖库、代码精简、进行单元测试以及提高代码的表现力。

简单介绍:

说明: 很难创造一个对简洁代码的精准定义,也许它的定义和程序员的数量一样多.然而,有些原则是可以应用到简洁代码的基础层面.我收集了9个最相关的原则,并将在下面简短地介绍他们.

 

九大原则:

1. 差的代码会做太多的事情,简洁代码则非常专一

说明: 每个类,方法或是其它实体应该保持(SRP)单一职责原则,也就是说在一个给定的抽象层,一个功能单元仅仅应当为单方面系统需求(一个可以独立于其它方面而改变的需求的一个特性)负责.

2. 你代码的语言应当看起来像是为问题而设计

说明: 不应该使用会使代码和语言看起来拙劣的变通方法,如果你说一件事只能以一种变通的方法完成,这通常意味着你没有花费足够的事件去寻找一个好的简洁的解决办法.

3. 不要有冗余

说明: 代码符合DRY(不自我重复),当DRY原则被成功应后,对系统中的任何单个元素的修改都不需要在其它的逻辑上不相关的元素做修改.

4. 你的代码读起来应当很舒服

说明: 尝试去遵循KISS(保持简单笨蛋)原则,大多数系统,如果它们能保持简单而不是复杂,那么它们就能更好的工作.

5. 易于被其它开发者扩展

说明: 永远相信代码不是给自己写的,而是给其它开发者写的,也许几个月后你就是"其它开发者"中的一员.

6. 有最少的依赖库

说明: 依赖越多未来修改或维护就更难, 可使用pigar来检测你代码中潜在的依赖库错误,从而减少依赖库目标.

7. 越小越好

说明: 代码应当很小,所有的类或方法都因应短小精悍,最好只有几行代码.

8. 应该有单元化的验收测试

说明: 单元测试代码会帮我们更快判断代码是否符合要求,维护或扩展原有代码.

9. 应富有表现力

说明: 代码表现力是让代码本身变成文档,从而使文档不再那么重要.

 

登录乐搏学院官网http://www.learnbo.com/

或关注我们的官方微博微信,还有更多惊喜哦~

 

本文出自 “满满李 - 运维开发之路” 博客,请务必保留此出处http://xmdevops.blog.51cto.com/11144840/1856057

转载于:https://my.oschina.net/learnbo/blog/864723

Apress 2019出版 Python is one of the most popular languages today. Relatively new fields such as data science, AI, robotics, and data analytics, along with traditional professions such as web development and scientific research, are embracing Python. It’s increasingly important for programmers writing code in a dynamic language like Python to make sure that the code is highquality and error-free. As a Python developer, you want to make sure that the software you are building makes your users happy without going over budget or never releasing. Python is a simple language, yet it’s difficult to write great code because there aren’t many resources that teach how to write better Python code. Currently lacking in the Python world are code consistency, patterns, and an understanding of good Pythonic code among developers. For every Python programmer, great Pythonic code has a different meaning. The reason for this could be that Python is being used in so many areas that it’s difficult to reach consensus among developers about specific patterns. In addition, Python doesn’t have any books about clean code like Java and Ruby do. There have been attempts to write those kinds of books to bring clarity to good Python practices, but those attempts have been few and far between, and quickly frankly, they haven’t been high-quality. The main goal of this book is to provide tips to Python developers of various levels so they can write better Python software and programs. This book gives you various techniques irrespective of the field you use Python in. This book covers all levels of Python, from basic to advanced, and shows you how to make your code more Pythonic. Remember, writing software is not only science but art, and this book will teach you how to become a better Python programmer.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值