`
亚当爱上java
  • 浏览: 697547 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

整洁代码的9大特点

阅读更多

说明:本文为国外网站博文直接转帖,讲的比较好,懒得翻译了,将就看吧!吐舌头

Top 9 qualities of clean code

Clean Code

How often do you express your disbelief when browsing through someone’s code saying out loud “Omg, that’s real spaghetti code…” ? Probably quite often. And how sure are you that no one thought the same when working with your code? In other words, how sure are you that your code is clean? The truth is that you can only be sure if you fully know what clean code means.

 

It is hard to create a precise definition of clean code and probably there are as many definitions as developers. However, some principles that lead to a basic level of clean code apply. I have gathered the 9 most relevant ones and described them in short below.

1. Bad code does too much – Clean code is focused

Each class, method or any other entity should remain undisturbed. It should conform to SRP (Single Responsibility Principle). Shortly speaking, we can say that SRP (according to some well-known definitions) is about making sure that if you can think of the reason for changing a class you should not be able to come up with more than one.

Though, I wouldn’t limit this definition only to the concept of classes. In his latest article Ralf Westphal presents a broader definition of Single Responsibility Principle. According to his definition:

A functional unit on a given level of abstraction should only be responsible for a single aspect of a system’s requirements. An aspect of requirements is a trait or property of requirements, which can change independently of other aspects.

If you would like to read more about the quoted thesis I advise you to dig into his article.

2.  The language you wrote your code with should look like it was made for the problem

It is not the language that makes a program look simple, but the programmer who makes the language appear simple.

(quote from Robert C. Martin)

This means, for instance, that you shouldn’t use workarounds which make code and language usually look awkward. If you claim that something can only be done by means of a workaround, it usually means that you haven’t spent enough time on trying to find a good, clean solution.

3. It should not be redundant

It should comply with the DRY rule (Don’t Repeat Yourself). When the DRY principle has successfully been applied, the modification of any single element of a system doesn’t require a change in any other logically unrelated elements.

4. Reading your code should be pleasant

When  browsing through the code you should feel like reading Harry Potter (yeah I know that’s a slight exaggeration :) ). You should feel that it was made to be read by any developer easily without hours spent on digging into it.

To achieve this you should try to comply with the KISS principle (Keep It Simple, Stupid!) and YAGNI principle (You Ain’t Gonna Need It). The KISS principle states that most systems work best if they are kept simple rather than made complex.

Therefore, simplicity should be a key goal in design, and unnecessary complexity should be avoided. YAGNI is a practice encouraging to purely focus on the simplest things that make your software work.

5. Can be easily extended by any other developer

You don’t write code for yourself , or worse -  for a compiler. You write code for other developers. Don’t be selfish – think about the others. Don’t torture other developers by producing a hardly maintainable and extendable code. Besides, in some months time you could be that “other developer” yourself.

6. It should have minimal dependencies

The more dependencies it has, the harder it is to maintain and change it in the future. You can always help yourself in achieving the goal of having minimal dependencies by using e.g. NDEPEND for checking potential incorrectness in the dependencies of your code.

7. Smaller is better

Code should be minimal. Both classes and methods should be short, preferably just a few lines of code. It should be well divided (also within one class). The better you divide your code the easier it becomes to read it. This principle might positively influence point 4 – it will make it easier for other developers to understand your code.

8. It should have unit and acceptance tests

How can we know that our code complies with the requirements if we don’t write tests? How can we maintain and extend it without the fear that it will stop working? Code without tests is simply not clean. If you would like to get to know more about the pillars of unit testing I advise you to read a very nice article Three Pillars of Unit Tests written by one of my colleagues.

9. It should be expressive

Expressiveness of the code means that it has meaningful names. These names should express their intention. They should not mislead you. They should be distinctive. Expressiveness makes code document itself making the need for documentation less important. If you want to read more about the subject of self-documenting code I recommend you to go through this article.

So what is in fact the definition of clean code?

All in all there is one final quality that summarizes all the above:

Clean code is a code that is written by someone who cares

 quote from Michael Feathers

It is written by someone who has treated it as an art and paid attention to all details.

The subject of clean code is in fact very complex and goes far beyond the knowledge presented in this post. Therefore if you find any other characteristics that you think make code cleaner do not hesitate and share them with us!

分享到:
评论

相关推荐

    全功能的地方视窗综合网站源代码

    深知“面子”在网站中举足轻重位置,本系统由多名资深网页设计师联合设计,所有页面均经过布局策划-页面与UI设计-美术设计三大环节,结构布局清晰、界面大气美观、干净整洁,并注重每个细节的用户体验。让您的网站在...

    浅蓝色学校网站代码_学校网站模版_学校网站

    PageAdmin特点 1、简单易用、灵活扩展:以前开发一个网站只能找网络公司, 做出的网站管理后台功能简单,导致后期维护、修改和扩展困难,甚至只能付费让制作公司维护,PageAdmin强大的功能、易用性、灵活扩展性完美...

    小兵建站橱柜网站模板源码 SoldierStationWebsite5.rar

    1、橱柜网站模板源码代码干净整洁,效果相当的炫酷,相当简洁大气高端,模板简单,全部已数据调用,只需后台修改栏目名称即可 ,适用于家具橱柜公司或个人使用网站手工DIV+css,代码精简,首页排版整洁大方、布局合理...

    TopDown Engine 2.0.unitypackage

    整洁代码、良好实践以及优化是其创作 2D 和 3D 自上而下游戏的强大基础。它速度非常快,可以在台式电脑、手机以及你希望的任何地方使用。它具有多种功能特点,自带玩家反馈,最重要的是游戏感出色,而且还在不断更新...

    TopDown Engine 2.1.unitypackage

    整洁代码、良好实践以及优化是其创作 2D 和 3D 自上而下游戏的强大基础。它速度非常快,可以在台式电脑、手机以及你希望的任何地方使用。它具有多种功能特点,自带玩家反馈,最重要的是游戏感出色,而且还在不断更新...

    Qt网络请求客户端服务器源码 ### (一)、功能特点 1. 支持多个客户端连接并发同时处理, 2. 可设置http请求是长连接

    Qt网络请求客户端服务器源码 ### (一)、功能特点 1. 支持多个客户端连接并发同时处理, ... ... ... 5. 支持8种配色方案(暗黑、灰黑、深绿、浅黄、...9. 代码框架整洁,注释完整,支持任意Qt版本、任意编译器、任意操作系统。

    Topdown Engine2.3.1

    整洁代码、良好实践以及优化是其创作 2D 和 3D 自上而下游戏的强大基础。它速度非常快,可以在台式电脑、手机以及你希望的任何地方使用。它具有多种功能特点,自带玩家反馈,最重要的是游戏感出色,而且还在不断更新...

    响应式无线支付刷卡机POS机网站模板(自适应手机移动端)

    1、该模板代码干净整洁; 2、相当简洁大气高端,模板简单,全部已数据调用,只需后台修改栏目名称即可 3、适用于刷卡机网站模板、POS机网站源码; 4、网站手工DIV+css,代码精简,首页排版整洁大方、布局合理、利于SEO...

    php项目-基于PHP实现的网上留言管理系统的设计(源代码+论文)1.rar

    主要功能特点: 1. 用户注册与登录:用户可以通过注册账号或使用第三方账号(如QQ、微博)快速登录系统。 2. 留言提交与显示:用户可以在留言板中提交自己的意见和建议,其他用户也可以查看和回复这些留言。 3. ...

    Zcurd快速开发平台(通用的后台管理系统).rar

    zcurd的特点 强大的表单配置功能,无需编写代码即可生成表单管理页面,快速搞定简单业务。 简单易用的代码生成,能够把通过表单配置生成的页面生成代码,提搞特定业务开发效率。 完善的权限控制,能够控制到菜单、...

    ObjectiveSql:ObjectiveSQL是Java中基于ActiveRecord模式的ORM框架,该框架鼓励快速开发和整洁,代码最少且约定俗成的配置

    产品特点通过一个注释,您的Class具有SQL编程的全功能易于进行关系( has_one , has_many和belongs_to )查询和分页查询使用Java语法编写SQL表达式( arithmetic , comparison和logical )为什么选择如果您的项目...

    大学自考成考课程培训类网站(带手机端).rar

    1、网站手工DIV+css,代码精简,首页排版整洁大方、布局合理、利于SEO、图文并茂、静态HTML。 2、程序自带XML地图,有利于搜索引擎快速收录和排名。 安装使用: 1、解压后上传到网站根目录 2、输入安装地址:http://...

    TopDown Engine 3.1.1.unitypackage

    整洁代码、良好实践以及优化是其创作 2D 和 3D 自上而下游戏的强大基础。它速度非常快,可以在台式电脑、手机以及你希望的任何地方使用。它具有多种功能特点,自带玩家反馈,最重要的是游戏感出色,而且还在不断更新...

    r4c论坛源代码

    r4cForum 1.4版。 本论坛程序仍然是源码开放程序,请随便下载使用。...本论坛程序的主要特点有:速度快,界面整洁,支持信息互发,支持更多的表情图标。 程序的详细说明请阅读压缩包里面的说明文档。

    电子日记本的 NEO 窗口改进版

    发觉使用 TImage 拼凑,以及 Unit 单元中“一大段”代码对于我这个对源码要求 干净整洁的人来说,总觉得不是个味。于是自己动手改进,采用控件加一个TFORM 继承类来实现。 特点: 1.封装...

    仿1999招聘网大型人才招聘系统 v4.1

    顶尖页面设计、赏心悦目的视觉感受网站的页面设计历来是网站建设重要工程,本系统由资深网页设计师设计,所有页面均经过布局策划-页面与UI设计-美术设计三大环节,结构布局清晰、界面大气美观、干净整洁,并注重每个...

    vscode配置C、C++环境详细教程.pdf

    VS Code的主要特点包括: 界面设计:它采用了极简主义的设计风格,界面清晰、整洁,操作直观。左侧侧边栏包含了丰富的文件浏览器和编辑功能,右侧则为代码编辑区域。此外,VS Code还支持自定义主题,用户可以根据...

    仿1999招聘网大型人才招聘系统 v4.1.zip

    网站的页面设计历来是网站建设重要工程,本系统由资深网页设计师设计,所有页面均经过布局策划-页面与UI设计-美术设计三大环节,结构布局清晰、界面大气美观、干净整洁,并注重每个细节的用户体验。让您的网站在用户...

    iron-redux:无痛类型安全的Redux代码生成器

    iron-redux-使您的redux代码完全类型安全并且非常整洁! 产品特点 iron-redux提供帮助功能,以创建类型安全的redux类型,redux操作和redux状态,而无需依靠打字稿的类型推断能力进行任何额外的类型定义。 尝试使用...

    Python是什么?Python有什么特点?.docx

    Python什么? Python一门有条理的和强的面向对象的程序设计语言,2010年以后随着数据和人工智能的兴起,Python又重新焕发出了耀眼的光芒,Python最初由...源,也即放源代码,意思所有用户都可以看到源代码。 P

Global site tag (gtag.js) - Google Analytics