Clean Code




Clean Code
A Handbook of Agile Software Craftsmanship

Robert C. Martin



Cette page résume les concepts clés du livre.

Je suis d'accord avec 95% des conseils données par Robert C. Martin.

Voici un exemple ou je suis en désaccord avec l'auteur.



Chapitre 1 : Introduction




The Total Cost of Owning a Mess.

Une histoire vraie:

"But then the release cycles began to stretch. Bugs were not repaired from one release to the next [..] The company went out of business a short time after that."

"They had rushed the product to market and had made a huge mess in the code. As they added more and more features, the code got worse and worse until they simply could not manage it any longer. It was the bad code that brought the company down."

Chapitre 2 : Meaningful Names

R1: Use Intention-Revealing Names

BadBetter
int d;int remaingTimeInDays;

getObjectID() : the ID must be unique.

R2: Avoid Disinformation

accountList : must be a List<>

R3: Make Meaningful Distinctions


BadBetter
copyChars(char[] a1, char[] a2)copyChars(char[] src, char[] dest)


R4: Use Pronounceable Names


BadBetter
class DtaRcrd102 {class Customer {

R5: Use Searchable Names


R6: Avoid Encoding



BadBetter
interface IShapeFactoryinterface ShapeFactory

R6: Avoid Mental Mapping


Chapitre 3 : Functions

R1: Functions should be small
R2: Functions should be smaller than that

R3: Do One Thing

FUNCTIONS SHOULD DO ONE THING. THEY SHOULD DO IT WELL. THEY SHOULD DO IT ONLY.

R4: Function Arguments

R5 : No Flag (boolean) Arguments

R6 : No Output Arguments

R7 : Prefer Exceptions to Returning Error Code

R8 : Structued Programming

Chapitre 4 : Comments

"Don't comment bad code - rewrite it" - Brian W. Kernighan and P.J. Plaugher

Good Comments:
Bad Comments:
Chapitre 5 : Formmatting

R1: Limit Vertical Distance
R1: Limit Horizontal Distance


BadBetter
class Widget
{
    void render()
    {
        ...
class Widget {
    void render()  {
        ...




Chapitre 6 : Data Structures

R1: The Law of Demeter ("Talk to friends, not to strangers")

R2 : Hide Internal Structure

Chapitre 7 : Error Handling

R1 : Prefer Exceptions to Returning Error Code
R2 : Write your Try-Catch-Finally Statement First
R3 : Define Exception Classes in Terms of Caller's Needs
R4 : Don't Return Null
R5 : Don't Pass Null

Chapitre 8 : Boundaries

Chapitre 9 : Unit Tests

Chapitre 10 : Classes

R1: Classes should be small
R2: The Single Responsibility Principle
R3: Organizing for Change
R4 : Isolating from Change

Chapitre 11 : Systems

Chapitre 12 : Emergence

Chapitre 13 : Concurrency

Chapitre 14 : Successive Refinements



















Dernière mise à jour: 05 novembre 2011.