SlideShare une entreprise Scribd logo
1  sur  60
Télécharger pour lire hors ligne
CLEAN CODE
R O B E R T C . M A R T I N
A H A N D B O O K O F A G I L E S O F T W A R E C R A F T S M A N S H I P
❖ CLEAN CODE
❖ MEANINGFUL NAMES
❖ FUNCTIONS
❖ COMMENTS
❖ FORMATTING
❖ OBJECTS AND DATA
STRUCTURES
❖ ERROR HANDLING
❖ UNIT TESTS
❖ CLASSES
❖ EMERGENCE
INDEX
Q u i e n
s oy
a l o a i s a
CLEAN CODE
1 There are two parts to learning craftsmanship: knowledge and work.
C R A F T S M A N S H I P
“You are reading this book for two reasons.
First, you are a programmer. Second, you
want to be a better programmer. We need
better programmers.”
TO BE BETTER
WANT
R O B E RT C . M A RT I N
It requires much time and effort, but we think it's worth.
L E A R N & P R A C T I C E
L
CODE QUALITY
MEASUREMENT
WTFs/minute
The main source of Bad Code are we, the programmers. We are not professionals.
WHAT IS CLEAN CODE?
GOOD/BAD CODE
E L E G A N T
- I like my code to be elegante and efficient
- Clean code does one thing well
BJARNE STROUSTRUP
S I M P L E , D I R E C T, P R O S E
- Clean code is simple and direct
- Clean code reads like well-wriHen prose
GRADY BOOCH
L I T E R AT E
- Clean code can be read
- Clean code shuold be literate
DAVE THOMAS
C A R E
- Clean code always looks like it was wriHen by someone who cares
MICHAEL FEATHERS
S M A L L , E X P R E S S I V E , S I M P L E
- Reduced duplicaJon, high expressiveness, and early building of simple abstracJons
RON JEFFRIES
W H AT Y O U E X P E C T E D
- You know you are working on clean code when each rouJne you reads turns out to be preHy
much what you expected
WARD CUNNINGHAM
THE BOY SCOUT RULE
"Always leave the campground cleaner than you found it."
If you find a mess on the ground, you clean it up regardless of who
might have made the mess.
ROBERT C. MARTIN
U N C L E B O B .
T
THE
BROKEN
Consider a building with a few broken windows.
If the windows are not repaired, the tendency
is for vandals to break a few more windows
WINDOW THEORY
Time, Productivity, Stress and Proyects.
All it’s about the Money $$$
THE COST OF THE BAD CODE
MONEY
WHY ?
BUT
“Any fool can write code that a computer can understand.
Good programmers write code that humans can understand.”
HOW DO WE DO IT ?
,
M A RT I N F OW L E R
2
In the code, the names are everywhere. Variables, Functions, Arguments, Classes and Packages,
Directories and Files, Jars, Wars and Ears.
M E A N I N G F U L N A M E S
CLEAN CODE
NAMES
INTENTION-REVEALING
• getLinearSearchPosition: -- indicates how the method works
• getSearchPosition: -- is better
• getPosition: -- is even better
USE
NAMING:
DISINFORMATION
int a = l;
if (O == l)
a=O1;
else
l=01;
AVOID
NAMING:
MEANINGFUL
MAKE
NAMING:
DISTINCTIONS
public static void copyChars(Char a1[], Char a2[]) {
for (int i = 0; i < a1.length; i++) {
a2[i] = a1[i];
}
}
PRONOUNCEABLE NAMES
NAMING:
class DtaCrc {
private Date genymdhms;
…
class Customer {
private Date generationTimestamp;
…
SEARCHEABLE
int realTaskWeeks =
(realDays / WORK_DAYS_PER_WEEK)
HUNGARIAN NOTATION
PhoneNumber phoneString;
// name not changed when type changed!
MEMBER
PREFIXES
public class Part {
private String m_dsc;
void setName(String name) {
m_dsc = name;
}
}
for (a = 0; a < 10; a++)
for (b = 0; b < 10; b++)
…
for (i = 0; i < 10; i++)
for (j = 0; j < 10; j++)
…
AVOID MENTAL MAPPING
NAMING:
METHOD NAMES
postpayment, deletePage, save, …
// methods should have verb or verb phrase names
String name = employee.getName();
customer.setName(“Alvaro”);
if (paycheck.isPosted())…
Complex complex = Complex.fromRealNumber(23.0);
// is generally better than
Complex complex = new Complex(23.0);
CLASS NAMES
Manager, Processor, Data, Info
// a class name should not be a verb
Customer, WikiPage, Account,
AddressParser
PICK ONE WORD
PER CONCEPT
fetch, retrieve, get //As equivalen methods
controller, manager, driver //Confusing
DOMAIN NAMES
USE
NAMING:
AccountVisitor, JobQueue
// people who read you code will be programmers
// Don't Add Gratuitous Context
class Address {
private String nameAddress;
…
CLEAN CODE
3
The first rule of functions is that they should be small.
The second rule of functions is that they should be smaller than that.
F U N C T I O N S
ONE THING
DO
Functions should do one thing. They should do it
well. They should do it only.
// < 150 CARACTERES PER LINE
// < 20 LINES
FUNCTIONS:
// high level of abstraction
getHTML();
// intermediate level of abstraction
String pagePathName = PathParse.render(pagePath);
// remarkably low level
message.append(“n”);
ONE LEVEL OF ABSTRACTION
THE STEPDOWN
RULE
// Reading Code from Top to Bottom
// we want the code to read like a
// top-down narrative
SWITCH STATEMENTS
class Employee…
int payAmount() {
switch (getType()) {
case EmployeeType.ENGENIEER:
return salary;
case EmployeeType.MANAGER:
return salary + bonus;
…
USE DESCRIPTIVE
NAMES
// Don’t be afraid to make a name long.
// A long descriptive name is better than a
// short enigmatic one.
// Experiment with names to find the
// ones that clarify the design of your
// module
FUNCTIONS: class EmployeeType…
abstract int payAmount() {
class Engenieer extends EmployeeType…
int payAmount() {
return salary;
}
class Manager extends EmployeeType…
int payAmount() {
return salar + bonus;
}
// if a función is going to transform its input argument
// the transformation shuold appear as the return value
InputStream openFile = fileOpen(“My File”);
COMMON MONADIC FORM DYADIC/TRIAD FUNCTIONS
writeField(name);
// is easier to understand than
writeField(outputStream, name);
// perfectly reasonable
Point point = new Point(0, 0);
// problematic
assertEquals(expected, actual);
assertEquals(message, expected, actual);
FUNCTIONS:
ARGUMENTS
FLAG
ARGUMENTS
render(true);
renderForSuite();
renderForSingleTest();
ARGUMENT
OBJECTS
Circle makeCircle(double x, double y,
double radius);
Circle makeCircle(Point center,
double radius);
write(name);
writeField(name);
assertEquals(expected, actual);
assertExpectedEqualsActual(expected, actual);
VERBS AND KEYWORDS COMMAND QUERY SEPARATION
Functions should:
- do something (change the state of an object) — a command
- return something (return some information about that object) — a query
but not do both, as doing both often leads to confusion.
The solution is to separate the command from the query, by creating two
functions from the one that did two things.
FUNCTIONS:
HAVE NO SIDE
EFFECTS
public boolean checkPassword(User user, String password) {
String codedPhrase = user.getPhraseEncodedByPassword();
if (cryptographer.decrypt(codedPhrase, password)) {
Session.initialize();
return true;
}
return false;
}
OUTPUT
ARGUMENTS
// In general output arguments should
// be avoided.
// If your function must change the state
// of something, have it change the state
// of its owning object.
DRY
DON'T REPEAT YOURSELF
D
DRY
FUNCTIONS:
Duplicaction may be the root of all evil in software
K I S S
KISS
KEEP IT SIMPLE, STUPID
CLEAN CODE
4
• Comments Do Not Make Up for Bad Code
• Don't comment bad code, rewrite it!
• Explain Yourself in Code
C O M M E N T S
/************************************
* Copyright (C) 2010-2011 {name} <{email}>
*
* This file is part of {project}.
*
* {project} can not be copied and/or distributed
* without the express permission of {name}
*************************************/
LEGAL COMMENTS EXPLANATION OF INTENT
// This is our best attempt to get a race condition
// by creating large number of threads.
for (int i = 0; i < 25000; i++) {
Thread….
COMMENTS:
INFORMATIVE
COMMENTS
// return an instance of the Response being tested
Protected abstract Responder responderInstance();
// Format matched kk:mm:ss EEE, MMM dd, yyyy
Pattern tomeMatcher = Pattern.compile(
“d*:d*:d* w*, w* d*, d*”);
CLARIFICATION
assertTrue(a.compareTo(b == -1)); // a < b
assertTrue(b.compareTo(a == 1)); // b > a
GOOD
public static SimpleDateFormat makeStandardHttpDateFormat() {
// SimpleDateFormat is not thread safe,
// so we need to create each instance independently
SimpleDateFormat df = new SimpleDateFormat(‘dd MM yyyy’);
df.setTimeZone(TimeZone.getTimeZone(“GMT”));
return df;
}
WAITING OF CONSEQUENCES AMPLIFICATION
String listItemContent = match.group(3).trim();
// the trim is real important. It removes the starting
// spaces that could cause the item to be recognized
// as another list.
…
COMMENTS:
TODO
COMMENTS
// TODO We need to test this class
// and controll the Exceptions
JAVADOCS IN
PUBLIC APIS
Only in publics APIs!
GOOD
try {
String propertiesPath = propertiesLocation + “” + PROPERTIES;
FileInputStream properties = new FileInputStream(propertiesPath);
loadedProperties.load(properties);
} catch () {
// No properties files means all default are loaded
}
MUMBLING MISLEADING COMMENTS
// The Processor delay for this component
protected int backgroundProcessorDelay = -1;
// The lifecycle event support for this component
protected LifecycleSupport lifecycle = new …
// The container event list for this Container
protected ArrayList eventList = new ArrayList();
COMMENTS:
REDUNDANT
COMMENTS
// Utility method that returns when this.closed is true.
// Throws an exception if the timeout is reached.
public synchronized void waitForClose(
final long timeoutMillis) throws Exception {
….
MANDATED
COMMENTS
/**
* @param title The title of the CD
* @param author The author of the CD
* @param tracks The tracks of the CD
**/
public void addCD(String title,
String author, int tracks) {
…
BAD
/**********************************************
* commit ca82a6dff817ec66f44342007202690a93763949
* Author: Scott Chacon <schacon@gee-mail.com>
* Date: Mon Mar 17 21:52:11 2008 -0700
*
* changed the version number
JOURNAL COMMENTS
SCARY NOISE
/** The name */
private String name;
/** The version */
private String version;
COMMENTS:
NOISE
COMMENTS
/**
* Default constructor
**/
protected AnnualDateRule() {}
// The day of the month
private int dayOfMonth;
DON’T USE A
COMMENT
// Don’t Use a Comment When You Can
// use a Function or a Variable
BAD
// Private Methods /////////////////////////////////
POSITION MAKERS ATTRIBUTIONS AND BYLINES
/**Added by Rick **/
COMMENTS:
CLOSING BRACE
COMMENTS
while (message.isPresent()) {
…
} // while
COMMENTED-OUT
CODE
String name = person.getName();
// String name = “text”;
// loadedProperties.load(properties);
BAD
/**
* <p>Here are my notes and favourite from chapter 4,
* “Comments”, of one of my all-time favourite books:
* <em>
* <a href="..." target="_blank">Clean Code: A Handbook of
Agile Software Craftsmanship</a>
* </em>, by Robert C. Martin.</p>
HTML COMMENTS TOO MUCH INFORMATION
/**
* SIP is based on an HTTP-like request/response transaction model.
* Each transaction consists of a request that invokes a particular
* method, or function, on the server and at least one response. In
* this example, the transaction begins with Alice's softphone sending
* an INVITE request addressed to Bob's SIP URI. INVITE is an example
* of a SIP method that specifies the action that the requestor (Alice)
* wants the server (Bob) to take. The INVITE request contains a
* of header fields. Header fields are named attributes that provide
* additional information about a message. The ones present in an
* INVITE include a unique identifier for the call, the destination
COMMENTS:
NONLOCAL
INFORMATION
/**
* Port on with fitnesse would run.
* Default to <b>8082</b>.
*/
public void setFitnessePort(int port)
INOBVIOUS
CONNECTION
/**
* start with an array that is big enough to hold
* all the pixel (plus filtre bytes), and an extra
* 200 bytes for header info
*/
this.pngBytes = new byte[(
(this.width + 1) * this.height * 3)
+ 200];
BAD
SHORT FUNCTIONS DON’T
NEED MUCH DESCRIPTION
FUNCTION
HEADERS
SHORT
5 T H E P U R P O S E O F F O R M AT T I N G I S CO M M U N I C AT I O N
CLEAN CODE
FORMATTING
First of all, let's be clear. Code formatting is important.
VERTICAL OPENNESS BETWEEN CONCEPTS
// Variables
// should be declared as close to their usage as possible
// instance variables
// should be declared at the top of the class
// dependent functions
// if one function calls another, they should be vertically
// close, and the caller should be above the called
// conceptual affinity
// certain bits of code want to be near other bits
VERTICAL DISTANCE
FORMATTING:
VERTICAL
DENSITY
// Vertical density implies close association
/**
* the class name of the reportar listener
*/
private String m_className;
/**
* the properties of the reporter listener
*/
private m_properties = new ArrayList();
THE NEWSPAPER
METAPHOR
// high-level —> details
VERTICAL
// each blank line is a visual cue
// that identifies a new and separate concept
// another concept…
HORIZONTAL OPENNESS AND DENSITY
public class FitNesseServer implements SocketServer {
private FitNesseContext context;
public FitNesseServer(FitNesseContext context) {
this.context = context; }
public void serve(Socket s) { serve(s, 10000); }
public void serve(Socket s, long requestTimeout) {
try { FitNesseExpediter sender = new
FitNesseExpediter(s, context);
sender.setRequestParsingTimeLimit(requestTimeout);
sender.start(); }
catch(Exception e) { e.printStackTrace(); } }
}
BREAKING INDENTATION
FORMATTING:
HORIZONTAL
ALIGNMENT
public FitNesseExpediter(Socket s,
FitNesseContext context)
throws Exception {
this.context = context;
socket = s;
input = s.getInputStream();
output = s.getOutputStream();
requestParsingTimeLimit = 10000;
}
TEAM RULES
// Every Programmer has his own
// favourite formatting rules.
// But if he works in a team
// then the team rules
HORIZONTAL
public void PersistLogicResultToTheDataAccessObject() {
Query query = new Query();
Result result = new Result();
when(businessLogic.handle(query)).thenReturn(result);
Response response = controller.doSomethingWith(query);
assertThat(response.status(), is(OK));
verify(dataAccessObject.persist(result));
}
CLEAN CODE
6
O B J E C T S
A N D
D ATA S T R U C T U R E S
DATA ABSTRACTION
// objects hide their data behind abstractions and
// expose functions that operate on that data
// data structure expose their data and
// have no meaningful functions
DATA/OBJECT ANTI-SYMMETRY
DATA STRUCTURE:
OBJECTS
public interface Vehicle {
double getFuelTankCapacityInGallons();
double getGallonsOfGasoline();
}
public interface Vehicle {
double getPercentFuelRemaining();
}
TRAIN WRECKS
Options opts = ctxt.getOptions();
File scratchDir = opts.getScratchDir();
final String outputDir = scratchDir.getAbsolutePath();
THE LAW OF
DEMETER
// a module should not know about the
// innards of the objects it manipulates.
final String outputDir = ctxt.getOptions()
.getScratchDir()
.getAbsolutePath();
7
Things can go wrong, and when they do, we as programmers
are responsible for making sure that our code does what it needs to do.
E R R O R H A N D L I N G
CLEAN CODE
ERROR
HANDLING
E X C E P T I O N S
USE EXCEPTIONS RATHER THAN RETURN CODES
if (deletePage(page) == E_OK) {
if (registry.deleteReference(page.name) == E_OK) {
if (configKeys.deleteKey(page.name.makeKey()) == E_OK){
logger.log("page deleted");
} else {
logger.log("configKey not deleted");
}
} else {
logger.log("deleteReference from registry failed");
}
} else {
logger.log("delete failed");
return E_ERROR;
}
try {
deletePage(page);
registry.deleteReference(page.name);
configKeys.deleteKey(page.name.makeKey());
} catch (Exception e) {
logger.log(e.getMessage());
}
TRY/CATCH
BLOCKS
EXTRACT
public void delete(Page page) {
try {
deletePageAndAllReferences(page);
}
catch (Exception e) {
logError(e);
}
}
private void deletePageAndAllReferences(Page page)
throws Exception {
deletePage(page);
registry.deleteReference(page.name);
configKeys.deleteKey(page.name.makeKey());
}
private void logError(Exception e) {
logger.log(e.getMessage());
}
ERROR HANDLING IS ONE THING
List<Employee> employees = getEmployees();
if (employees != null) {
for(Employee e : employees) {
totalPay += e.getPay();
}
}
DON'T RETURN NULL
ERROR:
HANDLING
// Functions should do one thing.
// Error handing is one thing.
List<Employee> employees = getEmployees();
for(Employee e : employees) {
totalPay += e.getPay();
}
public List<Employee> getEmployees() {
if ( .. there are no employees .. )
return Collections.emptyList();
}
DEFINE THE
NORMAL FLOW
try {
MealExpenses expenses = expenseReportDAO
.getMeals(employee.getID());
m_total += expenses.getTotal();
} catch(MealExpensesNotFound e) {
m_total += getMealPerDiem();
}
DON'T PASS
NULL
calculator.xProjection(null, new Point(12, 13));
8 O N C E I G OT A S U I T E O F T E S T S TO PA S S , I WO U L D M A K E S U R E T H AT
T H O S E T E S T S W E R E CO N V E N I E N T TO R U N F O R A N YO N E E L S E W H O
N E E D E D TO WO R K W I T H T H E CO D E .CLEAN CODE
UNIT TESTS
producJon code unJl you have wriHen
a failing unit test.
YOU MAY NOT WRITE…
more of a unit test than is sufficient to
fail, and not compiling is failing.
more producJon code than is sufficient
to pass the currently
YOU MAY NOT WRITE… YOU MAY NOT WRITE…
THE THREE LAWS OF TDD:
KEEPING TEST CLEAN
// the best rule is that you should
// minimize the number of asserts per concept and
// test just one concept per test function
SINGLE CONCEPT PER TESTCLEAN TESTS
// what makes a clean test? three things
// readability, readability, and readability
// test code is just as important as production code
ONE ASSERT
PER TEST
// tests come to a single conclusión
// that is quick and easy to understand
UNIT TESTS:
F I R S T
FAST
Tests should be fast. They should run
quickly
INDEPENDENT
Tests should not depend on each
other. One test should not set up the
condiJons
REPEATABLE
Tests should be repeatable in any
environment.
SELF-VALIDATING
The tests should have a boolean
output. Either they pass or fail.
TIMELY
The tests need to be wriHen in a
Jmely fashion.
9
C L A S S E S
CLEAN CODE
CLASS ORGANIZATION
// a class or module should have one, and only one,
// reason to change and only one responsibility
// SRP is one of the more important concept in OO design
THE SINGLE RESPONSIBILITY PRINCIPLE
SHOULD BE
SMALL!
// the first rule is that they should be small
// the second rule is that they should be smaller
// than that
// public static constants
// private static variables
// private instance variables
// public functions
// private utilities called by a public function right after
COHESION
// maintaining cohesion results in
// many small classes
CLASS:
CLEAN CODE
10
E M E R G E N C E !
SIMPLE DESIGN
1RUNS ALL THE TESTS
4MINIMAL CLASSES
AND METHODS
3EXPRESSIVE
2NO DUPLICATION
KENT BECK’S FOUR RULES OF
E N D
Clean code
Clean code

Contenu connexe

Tendances

Clean Code I - Best Practices
Clean Code I - Best PracticesClean Code I - Best Practices
Clean Code I - Best PracticesTheo Jungeblut
 
Clean code presentation
Clean code presentationClean code presentation
Clean code presentationBhavin Gandhi
 
Clean Code: Chapter 3 Function
Clean Code: Chapter 3 FunctionClean Code: Chapter 3 Function
Clean Code: Chapter 3 FunctionKent Huang
 
Clean Code summary
Clean Code summaryClean Code summary
Clean Code summaryJan de Vries
 
7 rules of simple and maintainable code
7 rules of simple and maintainable code7 rules of simple and maintainable code
7 rules of simple and maintainable codeGeshan Manandhar
 
Coding Standards & Best Practices for iOS/C#
Coding Standards & Best Practices for iOS/C#Coding Standards & Best Practices for iOS/C#
Coding Standards & Best Practices for iOS/C#Asim Rais Siddiqui
 
Coding Best Practices
Coding Best PracticesCoding Best Practices
Coding Best Practicesmh_azad
 
Clean Pragmatic Architecture - Avoiding a Monolith
Clean Pragmatic Architecture - Avoiding a MonolithClean Pragmatic Architecture - Avoiding a Monolith
Clean Pragmatic Architecture - Avoiding a MonolithVictor Rentea
 
Clean Code III - Software Craftsmanship
Clean Code III - Software CraftsmanshipClean Code III - Software Craftsmanship
Clean Code III - Software CraftsmanshipTheo Jungeblut
 
Refactoring and code smells
Refactoring and code smellsRefactoring and code smells
Refactoring and code smellsPaul Nguyen
 
Kotlin scope functions
Kotlin scope functionsKotlin scope functions
Kotlin scope functionsWaheed Nazir
 

Tendances (20)

Clean code
Clean codeClean code
Clean code
 
Clean Code
Clean CodeClean Code
Clean Code
 
Clean Code I - Best Practices
Clean Code I - Best PracticesClean Code I - Best Practices
Clean Code I - Best Practices
 
Clean coding-practices
Clean coding-practicesClean coding-practices
Clean coding-practices
 
Clean code
Clean code Clean code
Clean code
 
Clean code
Clean codeClean code
Clean code
 
Clean code
Clean codeClean code
Clean code
 
Clean Code
Clean CodeClean Code
Clean Code
 
Clean code presentation
Clean code presentationClean code presentation
Clean code presentation
 
Clean Code: Chapter 3 Function
Clean Code: Chapter 3 FunctionClean Code: Chapter 3 Function
Clean Code: Chapter 3 Function
 
Clean Code summary
Clean Code summaryClean Code summary
Clean Code summary
 
Writing clean code
Writing clean codeWriting clean code
Writing clean code
 
Clean Code
Clean CodeClean Code
Clean Code
 
7 rules of simple and maintainable code
7 rules of simple and maintainable code7 rules of simple and maintainable code
7 rules of simple and maintainable code
 
Coding Standards & Best Practices for iOS/C#
Coding Standards & Best Practices for iOS/C#Coding Standards & Best Practices for iOS/C#
Coding Standards & Best Practices for iOS/C#
 
Coding Best Practices
Coding Best PracticesCoding Best Practices
Coding Best Practices
 
Clean Pragmatic Architecture - Avoiding a Monolith
Clean Pragmatic Architecture - Avoiding a MonolithClean Pragmatic Architecture - Avoiding a Monolith
Clean Pragmatic Architecture - Avoiding a Monolith
 
Clean Code III - Software Craftsmanship
Clean Code III - Software CraftsmanshipClean Code III - Software Craftsmanship
Clean Code III - Software Craftsmanship
 
Refactoring and code smells
Refactoring and code smellsRefactoring and code smells
Refactoring and code smells
 
Kotlin scope functions
Kotlin scope functionsKotlin scope functions
Kotlin scope functions
 

Similaire à Clean code

Clean code _v2003
 Clean code _v2003 Clean code _v2003
Clean code _v2003R696
 
Flow or Type - how to React to that?
Flow or Type - how to React to that?Flow or Type - how to React to that?
Flow or Type - how to React to that?Krešimir Antolić
 
Save time by applying clean code principles
Save time by applying clean code principlesSave time by applying clean code principles
Save time by applying clean code principlesEdorian
 
Naming Standards, Clean Code
Naming Standards, Clean CodeNaming Standards, Clean Code
Naming Standards, Clean CodeCleanestCode
 
Douglas Crockford Presentation Goodparts
Douglas Crockford Presentation GoodpartsDouglas Crockford Presentation Goodparts
Douglas Crockford Presentation GoodpartsAjax Experience 2009
 
Writing code that writes code - Nguyen Luong
Writing code that writes code - Nguyen LuongWriting code that writes code - Nguyen Luong
Writing code that writes code - Nguyen LuongVu Huy
 
TechkTalk #12 Grokking: Writing code that writes code – Nguyen Luong
TechkTalk #12 Grokking: Writing code that writes code – Nguyen LuongTechkTalk #12 Grokking: Writing code that writes code – Nguyen Luong
TechkTalk #12 Grokking: Writing code that writes code – Nguyen LuongGrokking VN
 
CLEAN CODING AND DEVOPS Final.pptx
CLEAN CODING AND DEVOPS Final.pptxCLEAN CODING AND DEVOPS Final.pptx
CLEAN CODING AND DEVOPS Final.pptxJEEVANANTHAMG6
 
Clean code & design patterns
Clean code & design patternsClean code & design patterns
Clean code & design patternsPascal Larocque
 
Reduce, Reuse, Refactor
Reduce, Reuse, RefactorReduce, Reuse, Refactor
Reduce, Reuse, Refactorcklosowski
 
Lessons from a coding veteran - Web Directions @Media
Lessons from a coding veteran - Web Directions @MediaLessons from a coding veteran - Web Directions @Media
Lessons from a coding veteran - Web Directions @MediaTom Croucher
 
Code Smells and How to avoid it
Code Smells and How to avoid itCode Smells and How to avoid it
Code Smells and How to avoid itSyed Shah
 
Hidden Gems of Performance Tuning: Hierarchical Profiler and DML Trigger Opti...
Hidden Gems of Performance Tuning: Hierarchical Profiler and DML Trigger Opti...Hidden Gems of Performance Tuning: Hierarchical Profiler and DML Trigger Opti...
Hidden Gems of Performance Tuning: Hierarchical Profiler and DML Trigger Opti...Michael Rosenblum
 

Similaire à Clean code (20)

Clean code and code smells
Clean code and code smellsClean code and code smells
Clean code and code smells
 
Clean code _v2003
 Clean code _v2003 Clean code _v2003
Clean code _v2003
 
Flow or Type - how to React to that?
Flow or Type - how to React to that?Flow or Type - how to React to that?
Flow or Type - how to React to that?
 
Save time by applying clean code principles
Save time by applying clean code principlesSave time by applying clean code principles
Save time by applying clean code principles
 
What's in a name
What's in a nameWhat's in a name
What's in a name
 
Naming Standards, Clean Code
Naming Standards, Clean CodeNaming Standards, Clean Code
Naming Standards, Clean Code
 
Clean Code 2
Clean Code 2Clean Code 2
Clean Code 2
 
Douglas Crockford Presentation Goodparts
Douglas Crockford Presentation GoodpartsDouglas Crockford Presentation Goodparts
Douglas Crockford Presentation Goodparts
 
Legacy is Good
Legacy is GoodLegacy is Good
Legacy is Good
 
Writing code that writes code - Nguyen Luong
Writing code that writes code - Nguyen LuongWriting code that writes code - Nguyen Luong
Writing code that writes code - Nguyen Luong
 
TechkTalk #12 Grokking: Writing code that writes code – Nguyen Luong
TechkTalk #12 Grokking: Writing code that writes code – Nguyen LuongTechkTalk #12 Grokking: Writing code that writes code – Nguyen Luong
TechkTalk #12 Grokking: Writing code that writes code – Nguyen Luong
 
Wtf per lineofcode
Wtf per lineofcodeWtf per lineofcode
Wtf per lineofcode
 
CLEAN CODING AND DEVOPS Final.pptx
CLEAN CODING AND DEVOPS Final.pptxCLEAN CODING AND DEVOPS Final.pptx
CLEAN CODING AND DEVOPS Final.pptx
 
Goodparts
GoodpartsGoodparts
Goodparts
 
Clean code & design patterns
Clean code & design patternsClean code & design patterns
Clean code & design patterns
 
Reduce, Reuse, Refactor
Reduce, Reuse, RefactorReduce, Reuse, Refactor
Reduce, Reuse, Refactor
 
Lessons from a coding veteran - Web Directions @Media
Lessons from a coding veteran - Web Directions @MediaLessons from a coding veteran - Web Directions @Media
Lessons from a coding veteran - Web Directions @Media
 
Code Smells and How to avoid it
Code Smells and How to avoid itCode Smells and How to avoid it
Code Smells and How to avoid it
 
C++primer
C++primerC++primer
C++primer
 
Hidden Gems of Performance Tuning: Hierarchical Profiler and DML Trigger Opti...
Hidden Gems of Performance Tuning: Hierarchical Profiler and DML Trigger Opti...Hidden Gems of Performance Tuning: Hierarchical Profiler and DML Trigger Opti...
Hidden Gems of Performance Tuning: Hierarchical Profiler and DML Trigger Opti...
 

Plus de Alvaro García Loaisa (14)

Clean architectures Extended
Clean architectures ExtendedClean architectures Extended
Clean architectures Extended
 
Architectural kata
Architectural kataArchitectural kata
Architectural kata
 
Clean architecture
Clean architectureClean architecture
Clean architecture
 
The billion dollar mistake
The billion dollar mistakeThe billion dollar mistake
The billion dollar mistake
 
Scrum
ScrumScrum
Scrum
 
Agile ese mundillo...
Agile ese mundillo...Agile ese mundillo...
Agile ese mundillo...
 
AWS para torpes - Introducción a AWS
AWS para torpes - Introducción a AWSAWS para torpes - Introducción a AWS
AWS para torpes - Introducción a AWS
 
AWS para Torpes - Introducción a AWS
AWS para Torpes - Introducción a AWSAWS para Torpes - Introducción a AWS
AWS para Torpes - Introducción a AWS
 
Monta tu Centro Multimedia con Raspberry Pi
Monta tu Centro Multimedia con Raspberry PiMonta tu Centro Multimedia con Raspberry Pi
Monta tu Centro Multimedia con Raspberry Pi
 
Monta tu Centro Multimedia con Raspberry Pi
Monta tu Centro Multimedia con Raspberry PiMonta tu Centro Multimedia con Raspberry Pi
Monta tu Centro Multimedia con Raspberry Pi
 
Taller arduino I - CyLicon Valley
Taller arduino I - CyLicon ValleyTaller arduino I - CyLicon Valley
Taller arduino I - CyLicon Valley
 
Servidores de mapas en alta disponibilidad CyLiconValley
Servidores de mapas en alta disponibilidad CyLiconValleyServidores de mapas en alta disponibilidad CyLiconValley
Servidores de mapas en alta disponibilidad CyLiconValley
 
Metodologías Ágiles
Metodologías ÁgilesMetodologías Ágiles
Metodologías Ágiles
 
Presentacion motivacion
Presentacion motivacionPresentacion motivacion
Presentacion motivacion
 

Dernier

HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 

Dernier (20)

HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 

Clean code

  • 1. CLEAN CODE R O B E R T C . M A R T I N A H A N D B O O K O F A G I L E S O F T W A R E C R A F T S M A N S H I P
  • 2. ❖ CLEAN CODE ❖ MEANINGFUL NAMES ❖ FUNCTIONS ❖ COMMENTS ❖ FORMATTING ❖ OBJECTS AND DATA STRUCTURES ❖ ERROR HANDLING ❖ UNIT TESTS ❖ CLASSES ❖ EMERGENCE INDEX
  • 3. Q u i e n s oy a l o a i s a
  • 4. CLEAN CODE 1 There are two parts to learning craftsmanship: knowledge and work. C R A F T S M A N S H I P
  • 5. “You are reading this book for two reasons. First, you are a programmer. Second, you want to be a better programmer. We need better programmers.” TO BE BETTER WANT R O B E RT C . M A RT I N
  • 6. It requires much time and effort, but we think it's worth. L E A R N & P R A C T I C E L
  • 8. The main source of Bad Code are we, the programmers. We are not professionals. WHAT IS CLEAN CODE? GOOD/BAD CODE
  • 9. E L E G A N T - I like my code to be elegante and efficient - Clean code does one thing well BJARNE STROUSTRUP
  • 10. S I M P L E , D I R E C T, P R O S E - Clean code is simple and direct - Clean code reads like well-wriHen prose GRADY BOOCH
  • 11. L I T E R AT E - Clean code can be read - Clean code shuold be literate DAVE THOMAS
  • 12. C A R E - Clean code always looks like it was wriHen by someone who cares MICHAEL FEATHERS
  • 13. S M A L L , E X P R E S S I V E , S I M P L E - Reduced duplicaJon, high expressiveness, and early building of simple abstracJons RON JEFFRIES
  • 14. W H AT Y O U E X P E C T E D - You know you are working on clean code when each rouJne you reads turns out to be preHy much what you expected WARD CUNNINGHAM
  • 15. THE BOY SCOUT RULE "Always leave the campground cleaner than you found it." If you find a mess on the ground, you clean it up regardless of who might have made the mess. ROBERT C. MARTIN U N C L E B O B .
  • 16. T THE BROKEN Consider a building with a few broken windows. If the windows are not repaired, the tendency is for vandals to break a few more windows WINDOW THEORY
  • 17. Time, Productivity, Stress and Proyects. All it’s about the Money $$$ THE COST OF THE BAD CODE MONEY WHY ?
  • 18. BUT “Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” HOW DO WE DO IT ? , M A RT I N F OW L E R
  • 19. 2 In the code, the names are everywhere. Variables, Functions, Arguments, Classes and Packages, Directories and Files, Jars, Wars and Ears. M E A N I N G F U L N A M E S CLEAN CODE
  • 20. NAMES INTENTION-REVEALING • getLinearSearchPosition: -- indicates how the method works • getSearchPosition: -- is better • getPosition: -- is even better USE NAMING:
  • 21. DISINFORMATION int a = l; if (O == l) a=O1; else l=01; AVOID NAMING:
  • 22. MEANINGFUL MAKE NAMING: DISTINCTIONS public static void copyChars(Char a1[], Char a2[]) { for (int i = 0; i < a1.length; i++) { a2[i] = a1[i]; } }
  • 23. PRONOUNCEABLE NAMES NAMING: class DtaCrc { private Date genymdhms; … class Customer { private Date generationTimestamp; … SEARCHEABLE int realTaskWeeks = (realDays / WORK_DAYS_PER_WEEK) HUNGARIAN NOTATION PhoneNumber phoneString; // name not changed when type changed! MEMBER PREFIXES public class Part { private String m_dsc; void setName(String name) { m_dsc = name; } }
  • 24. for (a = 0; a < 10; a++) for (b = 0; b < 10; b++) … for (i = 0; i < 10; i++) for (j = 0; j < 10; j++) … AVOID MENTAL MAPPING NAMING: METHOD NAMES postpayment, deletePage, save, … // methods should have verb or verb phrase names String name = employee.getName(); customer.setName(“Alvaro”); if (paycheck.isPosted())… Complex complex = Complex.fromRealNumber(23.0); // is generally better than Complex complex = new Complex(23.0); CLASS NAMES Manager, Processor, Data, Info // a class name should not be a verb Customer, WikiPage, Account, AddressParser PICK ONE WORD PER CONCEPT fetch, retrieve, get //As equivalen methods controller, manager, driver //Confusing
  • 25. DOMAIN NAMES USE NAMING: AccountVisitor, JobQueue // people who read you code will be programmers // Don't Add Gratuitous Context class Address { private String nameAddress; …
  • 26. CLEAN CODE 3 The first rule of functions is that they should be small. The second rule of functions is that they should be smaller than that. F U N C T I O N S
  • 27. ONE THING DO Functions should do one thing. They should do it well. They should do it only. // < 150 CARACTERES PER LINE // < 20 LINES FUNCTIONS:
  • 28. // high level of abstraction getHTML(); // intermediate level of abstraction String pagePathName = PathParse.render(pagePath); // remarkably low level message.append(“n”); ONE LEVEL OF ABSTRACTION THE STEPDOWN RULE // Reading Code from Top to Bottom // we want the code to read like a // top-down narrative SWITCH STATEMENTS class Employee… int payAmount() { switch (getType()) { case EmployeeType.ENGENIEER: return salary; case EmployeeType.MANAGER: return salary + bonus; … USE DESCRIPTIVE NAMES // Don’t be afraid to make a name long. // A long descriptive name is better than a // short enigmatic one. // Experiment with names to find the // ones that clarify the design of your // module FUNCTIONS: class EmployeeType… abstract int payAmount() { class Engenieer extends EmployeeType… int payAmount() { return salary; } class Manager extends EmployeeType… int payAmount() { return salar + bonus; }
  • 29. // if a función is going to transform its input argument // the transformation shuold appear as the return value InputStream openFile = fileOpen(“My File”); COMMON MONADIC FORM DYADIC/TRIAD FUNCTIONS writeField(name); // is easier to understand than writeField(outputStream, name); // perfectly reasonable Point point = new Point(0, 0); // problematic assertEquals(expected, actual); assertEquals(message, expected, actual); FUNCTIONS: ARGUMENTS FLAG ARGUMENTS render(true); renderForSuite(); renderForSingleTest(); ARGUMENT OBJECTS Circle makeCircle(double x, double y, double radius); Circle makeCircle(Point center, double radius);
  • 30. write(name); writeField(name); assertEquals(expected, actual); assertExpectedEqualsActual(expected, actual); VERBS AND KEYWORDS COMMAND QUERY SEPARATION Functions should: - do something (change the state of an object) — a command - return something (return some information about that object) — a query but not do both, as doing both often leads to confusion. The solution is to separate the command from the query, by creating two functions from the one that did two things. FUNCTIONS: HAVE NO SIDE EFFECTS public boolean checkPassword(User user, String password) { String codedPhrase = user.getPhraseEncodedByPassword(); if (cryptographer.decrypt(codedPhrase, password)) { Session.initialize(); return true; } return false; } OUTPUT ARGUMENTS // In general output arguments should // be avoided. // If your function must change the state // of something, have it change the state // of its owning object.
  • 31. DRY DON'T REPEAT YOURSELF D DRY FUNCTIONS: Duplicaction may be the root of all evil in software
  • 32. K I S S KISS KEEP IT SIMPLE, STUPID
  • 33. CLEAN CODE 4 • Comments Do Not Make Up for Bad Code • Don't comment bad code, rewrite it! • Explain Yourself in Code C O M M E N T S
  • 34. /************************************ * Copyright (C) 2010-2011 {name} <{email}> * * This file is part of {project}. * * {project} can not be copied and/or distributed * without the express permission of {name} *************************************/ LEGAL COMMENTS EXPLANATION OF INTENT // This is our best attempt to get a race condition // by creating large number of threads. for (int i = 0; i < 25000; i++) { Thread…. COMMENTS: INFORMATIVE COMMENTS // return an instance of the Response being tested Protected abstract Responder responderInstance(); // Format matched kk:mm:ss EEE, MMM dd, yyyy Pattern tomeMatcher = Pattern.compile( “d*:d*:d* w*, w* d*, d*”); CLARIFICATION assertTrue(a.compareTo(b == -1)); // a < b assertTrue(b.compareTo(a == 1)); // b > a GOOD
  • 35. public static SimpleDateFormat makeStandardHttpDateFormat() { // SimpleDateFormat is not thread safe, // so we need to create each instance independently SimpleDateFormat df = new SimpleDateFormat(‘dd MM yyyy’); df.setTimeZone(TimeZone.getTimeZone(“GMT”)); return df; } WAITING OF CONSEQUENCES AMPLIFICATION String listItemContent = match.group(3).trim(); // the trim is real important. It removes the starting // spaces that could cause the item to be recognized // as another list. … COMMENTS: TODO COMMENTS // TODO We need to test this class // and controll the Exceptions JAVADOCS IN PUBLIC APIS Only in publics APIs! GOOD
  • 36. try { String propertiesPath = propertiesLocation + “” + PROPERTIES; FileInputStream properties = new FileInputStream(propertiesPath); loadedProperties.load(properties); } catch () { // No properties files means all default are loaded } MUMBLING MISLEADING COMMENTS // The Processor delay for this component protected int backgroundProcessorDelay = -1; // The lifecycle event support for this component protected LifecycleSupport lifecycle = new … // The container event list for this Container protected ArrayList eventList = new ArrayList(); COMMENTS: REDUNDANT COMMENTS // Utility method that returns when this.closed is true. // Throws an exception if the timeout is reached. public synchronized void waitForClose( final long timeoutMillis) throws Exception { …. MANDATED COMMENTS /** * @param title The title of the CD * @param author The author of the CD * @param tracks The tracks of the CD **/ public void addCD(String title, String author, int tracks) { … BAD
  • 37. /********************************************** * commit ca82a6dff817ec66f44342007202690a93763949 * Author: Scott Chacon <schacon@gee-mail.com> * Date: Mon Mar 17 21:52:11 2008 -0700 * * changed the version number JOURNAL COMMENTS SCARY NOISE /** The name */ private String name; /** The version */ private String version; COMMENTS: NOISE COMMENTS /** * Default constructor **/ protected AnnualDateRule() {} // The day of the month private int dayOfMonth; DON’T USE A COMMENT // Don’t Use a Comment When You Can // use a Function or a Variable BAD
  • 38. // Private Methods ///////////////////////////////// POSITION MAKERS ATTRIBUTIONS AND BYLINES /**Added by Rick **/ COMMENTS: CLOSING BRACE COMMENTS while (message.isPresent()) { … } // while COMMENTED-OUT CODE String name = person.getName(); // String name = “text”; // loadedProperties.load(properties); BAD
  • 39. /** * <p>Here are my notes and favourite from chapter 4, * “Comments”, of one of my all-time favourite books: * <em> * <a href="..." target="_blank">Clean Code: A Handbook of Agile Software Craftsmanship</a> * </em>, by Robert C. Martin.</p> HTML COMMENTS TOO MUCH INFORMATION /** * SIP is based on an HTTP-like request/response transaction model. * Each transaction consists of a request that invokes a particular * method, or function, on the server and at least one response. In * this example, the transaction begins with Alice's softphone sending * an INVITE request addressed to Bob's SIP URI. INVITE is an example * of a SIP method that specifies the action that the requestor (Alice) * wants the server (Bob) to take. The INVITE request contains a * of header fields. Header fields are named attributes that provide * additional information about a message. The ones present in an * INVITE include a unique identifier for the call, the destination COMMENTS: NONLOCAL INFORMATION /** * Port on with fitnesse would run. * Default to <b>8082</b>. */ public void setFitnessePort(int port) INOBVIOUS CONNECTION /** * start with an array that is big enough to hold * all the pixel (plus filtre bytes), and an extra * 200 bytes for header info */ this.pngBytes = new byte[( (this.width + 1) * this.height * 3) + 200]; BAD
  • 40. SHORT FUNCTIONS DON’T NEED MUCH DESCRIPTION FUNCTION HEADERS SHORT
  • 41. 5 T H E P U R P O S E O F F O R M AT T I N G I S CO M M U N I C AT I O N CLEAN CODE FORMATTING First of all, let's be clear. Code formatting is important.
  • 42. VERTICAL OPENNESS BETWEEN CONCEPTS // Variables // should be declared as close to their usage as possible // instance variables // should be declared at the top of the class // dependent functions // if one function calls another, they should be vertically // close, and the caller should be above the called // conceptual affinity // certain bits of code want to be near other bits VERTICAL DISTANCE FORMATTING: VERTICAL DENSITY // Vertical density implies close association /** * the class name of the reportar listener */ private String m_className; /** * the properties of the reporter listener */ private m_properties = new ArrayList(); THE NEWSPAPER METAPHOR // high-level —> details VERTICAL // each blank line is a visual cue // that identifies a new and separate concept // another concept…
  • 43. HORIZONTAL OPENNESS AND DENSITY public class FitNesseServer implements SocketServer { private FitNesseContext context; public FitNesseServer(FitNesseContext context) { this.context = context; } public void serve(Socket s) { serve(s, 10000); } public void serve(Socket s, long requestTimeout) { try { FitNesseExpediter sender = new FitNesseExpediter(s, context); sender.setRequestParsingTimeLimit(requestTimeout); sender.start(); } catch(Exception e) { e.printStackTrace(); } } } BREAKING INDENTATION FORMATTING: HORIZONTAL ALIGNMENT public FitNesseExpediter(Socket s, FitNesseContext context) throws Exception { this.context = context; socket = s; input = s.getInputStream(); output = s.getOutputStream(); requestParsingTimeLimit = 10000; } TEAM RULES // Every Programmer has his own // favourite formatting rules. // But if he works in a team // then the team rules HORIZONTAL public void PersistLogicResultToTheDataAccessObject() { Query query = new Query(); Result result = new Result(); when(businessLogic.handle(query)).thenReturn(result); Response response = controller.doSomethingWith(query); assertThat(response.status(), is(OK)); verify(dataAccessObject.persist(result)); }
  • 44. CLEAN CODE 6 O B J E C T S A N D D ATA S T R U C T U R E S
  • 45. DATA ABSTRACTION // objects hide their data behind abstractions and // expose functions that operate on that data // data structure expose their data and // have no meaningful functions DATA/OBJECT ANTI-SYMMETRY DATA STRUCTURE: OBJECTS public interface Vehicle { double getFuelTankCapacityInGallons(); double getGallonsOfGasoline(); } public interface Vehicle { double getPercentFuelRemaining(); } TRAIN WRECKS Options opts = ctxt.getOptions(); File scratchDir = opts.getScratchDir(); final String outputDir = scratchDir.getAbsolutePath(); THE LAW OF DEMETER // a module should not know about the // innards of the objects it manipulates. final String outputDir = ctxt.getOptions() .getScratchDir() .getAbsolutePath();
  • 46. 7 Things can go wrong, and when they do, we as programmers are responsible for making sure that our code does what it needs to do. E R R O R H A N D L I N G CLEAN CODE
  • 47. ERROR HANDLING E X C E P T I O N S USE EXCEPTIONS RATHER THAN RETURN CODES if (deletePage(page) == E_OK) { if (registry.deleteReference(page.name) == E_OK) { if (configKeys.deleteKey(page.name.makeKey()) == E_OK){ logger.log("page deleted"); } else { logger.log("configKey not deleted"); } } else { logger.log("deleteReference from registry failed"); } } else { logger.log("delete failed"); return E_ERROR; } try { deletePage(page); registry.deleteReference(page.name); configKeys.deleteKey(page.name.makeKey()); } catch (Exception e) { logger.log(e.getMessage()); }
  • 48. TRY/CATCH BLOCKS EXTRACT public void delete(Page page) { try { deletePageAndAllReferences(page); } catch (Exception e) { logError(e); } } private void deletePageAndAllReferences(Page page) throws Exception { deletePage(page); registry.deleteReference(page.name); configKeys.deleteKey(page.name.makeKey()); } private void logError(Exception e) { logger.log(e.getMessage()); }
  • 49. ERROR HANDLING IS ONE THING List<Employee> employees = getEmployees(); if (employees != null) { for(Employee e : employees) { totalPay += e.getPay(); } } DON'T RETURN NULL ERROR: HANDLING // Functions should do one thing. // Error handing is one thing. List<Employee> employees = getEmployees(); for(Employee e : employees) { totalPay += e.getPay(); } public List<Employee> getEmployees() { if ( .. there are no employees .. ) return Collections.emptyList(); } DEFINE THE NORMAL FLOW try { MealExpenses expenses = expenseReportDAO .getMeals(employee.getID()); m_total += expenses.getTotal(); } catch(MealExpensesNotFound e) { m_total += getMealPerDiem(); } DON'T PASS NULL calculator.xProjection(null, new Point(12, 13));
  • 50. 8 O N C E I G OT A S U I T E O F T E S T S TO PA S S , I WO U L D M A K E S U R E T H AT T H O S E T E S T S W E R E CO N V E N I E N T TO R U N F O R A N YO N E E L S E W H O N E E D E D TO WO R K W I T H T H E CO D E .CLEAN CODE UNIT TESTS
  • 51. producJon code unJl you have wriHen a failing unit test. YOU MAY NOT WRITE… more of a unit test than is sufficient to fail, and not compiling is failing. more producJon code than is sufficient to pass the currently YOU MAY NOT WRITE… YOU MAY NOT WRITE… THE THREE LAWS OF TDD:
  • 52. KEEPING TEST CLEAN // the best rule is that you should // minimize the number of asserts per concept and // test just one concept per test function SINGLE CONCEPT PER TESTCLEAN TESTS // what makes a clean test? three things // readability, readability, and readability // test code is just as important as production code ONE ASSERT PER TEST // tests come to a single conclusión // that is quick and easy to understand UNIT TESTS:
  • 53. F I R S T FAST Tests should be fast. They should run quickly INDEPENDENT Tests should not depend on each other. One test should not set up the condiJons REPEATABLE Tests should be repeatable in any environment. SELF-VALIDATING The tests should have a boolean output. Either they pass or fail. TIMELY The tests need to be wriHen in a Jmely fashion.
  • 54. 9 C L A S S E S CLEAN CODE
  • 55. CLASS ORGANIZATION // a class or module should have one, and only one, // reason to change and only one responsibility // SRP is one of the more important concept in OO design THE SINGLE RESPONSIBILITY PRINCIPLE SHOULD BE SMALL! // the first rule is that they should be small // the second rule is that they should be smaller // than that // public static constants // private static variables // private instance variables // public functions // private utilities called by a public function right after COHESION // maintaining cohesion results in // many small classes CLASS:
  • 56. CLEAN CODE 10 E M E R G E N C E !
  • 57. SIMPLE DESIGN 1RUNS ALL THE TESTS 4MINIMAL CLASSES AND METHODS 3EXPRESSIVE 2NO DUPLICATION KENT BECK’S FOUR RULES OF
  • 58. E N D