`
亚当爱上java
  • 浏览: 707396 次
  • 性别: 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!

分享到:
评论

相关推荐

    《代码整洁之道》学习笔记

    #### 第一章 整洁代码 **1.3.5 什么是整洁的代码?** - **Bjarne Stroustrup(C++语言发明者)的观点:** - 代码逻辑应当直截了当,使缺陷难以隐藏。 - 尽量减少依赖关系,便于维护。 - 错误处理代码需依据某种...

    高质量代码的设计特点

    高质量代码首先应该具有清晰的结构,这包括合理的模块划分、恰当的函数或方法定义以及整洁的类组织。代码结构清晰可以使其他人更容易理解代码的功能和工作方式,从而减少阅读和调试的时间。 二、良好的命名规范 ...

    代码格式化工具 代码格式化工具 代码格式化工具

    代码格式化是指将混乱或不规范的源代码转换为遵循特定编程规范的整洁格式的过程。这包括缩进、换行、空格、注释位置等多方面的调整。良好的代码格式不仅使代码看起来更专业,而且有助于团队协作,因为大家都能按照...

    01-文本小节-高质量代码的特点.md

    总结来说,高质量的代码应当具备以下特点:首先,它应该符合代码规范,具有良好的逻辑结构和清晰的可读性;其次,代码应保证功能的完整性,能够处理所有预期的功能需求;最后,代码要具有鲁棒性,能够妥善处理异常...

    c++程序 小程序设计 400+行代码

    例如,使用一致的命名规则,添加适当的注释,保持代码整洁等。 通过学习和理解这个400行的C++小程序,不仅可以掌握C++的基本语法,还能了解如何设计和实现一个具有实际功能的应用程序,这对于提升编程能力和项目...

    整洁的架构

    整洁架构的特点之一就是将系统分解为不同的层次或层,每一层都有明确的职责和约束。这些层次通常包括: 1. 实体层(Entities):这是业务规则的中心。实体层包含了业务的核心模型,这些模型可以被多个不同的应用...

    VB 代码格式化插件-Cloud Moon FormatCode 所有源代码

    这款插件的主要特点是提高代码的可读性和一致性,从而提升开发效率。CloudMoonFormatCode.dll是该插件的核心组件,包含了实现代码格式化的全部逻辑和算法。 源代码是软件开发的基础,对于Cloud Moon FormatCode而言...

    cb代码格式化

    "cb代码格式化"是一个专门针对编程代码进行美化和整理的工具,它的主要功能是帮助开发者将杂乱无章的源代码自动调整为统一、整洁的格式,提高代码的可读性和团队协作效率。在软件开发过程中,代码的格式化是一项重要...

    代码格式化工具

    2. **括号对齐**:确保代码中的括号(如大括号、小括号)正确对齐,提升视觉效果。 3. **空格与换行处理**:统一空格数量,避免多余的空格或不必要的换行,提高代码紧凑度。 4. **命名规范检查**:检查变量、函数、...

    Mybatis 代码生成工具

    Strato Mybatis 代码生成工具则是此类工具的一种,它具有高效、易用的特点,为开发者带来了很大的便利。 Mybatis 代码生成工具的核心功能包括: 1. **自动创建实体类**:根据数据库表结构自动生成对应的Java实体类...

    软件著作权代码文档自动生成器

    这款软件的特点在于其小巧的体积和低内存占用,使得它在运行时不会对用户的计算机性能造成太大影响。此外,它的操作界面简洁,用户友好,无需复杂的设置或解压步骤,下载后即可直接使用,极大地方便了开发者。 在...

    VB代码自动缩进工具

    VB代码自动缩进工具是一款专为Visual Basic编程语言设计的实用辅助软件,它极大地提升了VB程序员的代码编写效率和代码可读性。在VB编程中,代码的缩进和排列对于阅读和理解代码逻辑至关重要,尤其在处理大量或复杂的...

    可重用代码管理器

    在编程中,可重用代码是指那些可以被多次调用且无需大幅度修改的代码段,它们通常具有通用性和模块化的特点。通过将这些代码段整理和分类,开发者可以在需要时快速找到并引入,避免了每次从零开始编写相似功能的代码...

    Extjs代码编辑工具

    4. **代码格式化**:提供代码格式化功能,可以按照预设的规则自动调整代码的缩进和换行,保持代码整洁。 5. **调试支持**:集成调试工具,允许设置断点,查看变量值,步进执行,有助于排查和修复问题。 6. **集成...

    *关键 C# 代码生成工具 C# 代码生成工具CodeMissile.Net 代码生成工具

    5. **版本控制兼容**:考虑到现代开发流程中版本控制的重要性,CodeMissile.Net可能会生成整洁的、易于跟踪的代码变更,以确保与Git、SVN等版本控制系统无缝协作。 6. **定制化扩展**:对于有特殊需求的团队,...

    NetBeans权威指南源代码

    源代码将揭示这些功能背后的实现细节,以及如何利用它们来编写更整洁、更易于维护的代码。 7. **Web应用开发**:对于涉及Web应用的章节,源代码会涵盖使用Java EE技术(如Servlets、JSP、JSF)进行开发的方法,以及...

    动软代码生成器2.78 最新+教程

    通过动软代码生成器2.78,开发者可以快速生成包括但不限于控制器、视图、模型、服务接口、DAO等在内的各种组件,极大地减轻了开发负担,尤其在大数据量的表单和复杂业务逻辑的处理上,代码生成器的价值更为突出。...

    程序员表白专用代码

    - 尽管表白代码追求创意,但为了使非程序员也能理解,程序员通常会保持代码的可读性和整洁性,有时甚至采用彩色高亮和美化格式。 9. **情感与技术的结合** - 最后,表白代码体现了技术与情感的完美结合,是程序员...

    JS代码格式化工具

    因此,JS代码格式化工具应运而生,它们能够自动对代码进行缩进、换行、括号对齐等处理,极大地提升了开发效率和代码质量。 JavaScript是一种广泛使用的脚本语言,常用于网页和应用程序的前端开发。然而,由于其灵活...

    自动规范代码格式工具Astyle

    在编程过程中,保持代码整洁和规范是非常重要的,它不仅提高了代码可读性,也有利于团队协作。Astyle(Artistic Style)就是这样一款优秀的自动代码格式化工具,专为C、C++和C++/CLI语言设计。Astyle提供了强大的...

Global site tag (gtag.js) - Google Analytics