Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.
Clean Code
// by Ingo Schwarz
What‘s the content?
// try to find a simple definition
What is Clean Code?
// try to convince them, so you don‘t have to f...
What is Clean Code?
What is Clean Code?
Bjarne Stroustrup
“I like my code to be
elegant and efficient“
“Clean code does
one thing well“
What is Clean Code?
Grady Booch
“Clean code is
Simple and direct“
“Clean code reads like
well-written prose“
What is Clean Code?
Dave Thomas
“Clean code can be read“
“Clean code should
be literate“
What is Clean Code?
Michael Feathers
“Clean Code always looks
like it was written by someone
who cares“
What is Clean Code?
Ward Cunningham
“You know you are working
on clean code, when each routine
you read turns out to be
th...
What is Clean Code?
How to measure good code?
What is Clean Code?
Why do I need Clean Code?
Why do I need Clean Code?
…because you are an author
// YES, YOU ARE!!!
“An author is someone
who practices writing as a p...
Why do I need Clean Code?
…because you don‘t want to be a verb
// NO, YOU DON‘T!!!
“Oh man, this code has been Jimmy’d“
by...
Why do I need Clean Code?
…because you are lazy
// YES, YOU ARE!!!
// so am I
// PROOF: no quotation at this slide
How to produce Clean Code?
How to produce Clean Code?
Robert C. Martin
(aka Uncle Bob)
“The Boy Scout Rule“
How to produce Clean Code?
// General
How to produce Clean Code?
// Naming
“Names are sound and smoke”
Johannes Wolfgang von Goethe – Faust I
How to produce Clean Code?
Take care about names!
// we name everything:
// variables, functions, arguments, classes, pack...
How to produce Clean Code?
public class DtaRcrd102 {
private Date genymdhms;
private Date modymdhms;
private static final ...
How to produce Clean Code?
public class Customer {
private static final String RECORD_ID = “102“;
private Date generationT...
How to produce Clean Code?
private String m_dsc;
// Naming
Avoid Encodings (member prefixes)
private PhoneNumber phoneStri...
How to produce Clean Code?
private String description;
// Naming
Avoid Encodings (member prefixes)
private PhoneNumber pho...
How to produce Clean Code?
Add Meaningful Context
fistName, lastName, street, city, state, zipcode
// a better solution
ad...
How to produce Clean Code?
Small!
Rules of Functions:
1. should be small
2. should be smaller than that
// < 150 character...
How to produce Clean Code?
Do One Thing
Functions should do ONE thing.
They should do it WELL.
They should do it ONLY.
// ...
How to produce Clean Code?
One Level of Abstraction
// high level of abstraction
getHtml();
// intermediate level of abstr...
How to produce Clean Code?
Switch Statements
class Employee {
int getSalary() {
switch( getType() ) {
case EmployeeType.EN...
How to produce Clean Code?
Switch Statements
interface Employee {
int getSalary();
}
class Salesman implements Employee {
...
How to produce Clean Code?
// niladic
getHtml();
// monadic
execute( boolean executionType );
// dyadic
assertEquals(Strin...
How to produce Clean Code?
Improve Monadic Functions with Flag Argument
// ???
execute( boolean executionType );
// !!!
ex...
How to produce Clean Code?
Improve Dyadic Functions with two similar Argument Types
// ???
assertEquals(String expected, S...
How to produce Clean Code?
Improve Triadic Functions with three similar Argument Types
// ???
drawCircle(double x, double ...
How to produce Clean Code?
Don‘t comment bad code, rewrite it!
// Comments
How to produce Clean Code?
Noise
/** Default Consturctor */
public AnnaulDateRule() { … }
/** The day of the Month. */
pri...
How to produce Clean Code?
Scary Noise
/** The name. */
private String name;
/** The version. */
private String version;
/...
How to produce Clean Code?
Don‘t use comments if you can use a Function/Variable
// does the moduel from the global list <...
How to produce Clean Code?
The Law of Demeter
// Bad
String outputDir = ctxt.getOptions()
.getScratchDir()
.getAbsolutePat...
How to produce Clean Code?
Prefer Exceptions to Returning Error Codes
if( deletePage( page ) == E_OK ) {
if( registry.dele...
How to produce Clean Code?
Prefer Exceptions to Returning Error Codes
try {
deletePage(page);
registry.deleteReference(pag...
How to produce Clean Code?
Extract Try/Catch Blocks
public void delete(Page page) {
try {
deletePageAndAllReferences( page...
How to produce Clean Code?
Don‘t return Null
List<Employee> employees = getEmployees();
If( employees != null ) {
for( Emp...
How to produce Clean Code?
Don‘t return Null
List<Employee> employees = getEmployees(); 
for( Employee employee : employee...
How to produce Clean Code?
Small!
Rules of Classes:
1. should be small
2. should be smaller than that
// Single Responsibi...
How to produce Clean Code?
// More Information
Have a look at:
Clean Code
Clean Code
Prochain SlideShare
Chargement dans…5
×

Clean Code

1 520 vues

Publié le

Presentation about the book "Clean Code - A Handbook of Agile Software Craftmanship" by Robert C. Martin

Publié dans : Logiciels
  • Login to see the comments

Clean Code

  1. 1. Clean Code // by Ingo Schwarz
  2. 2. What‘s the content? // try to find a simple definition What is Clean Code? // try to convince them, so you don‘t have to force them Why do I need Clean Code? // show some simple rules How to produce Clean Code?
  3. 3. What is Clean Code?
  4. 4. What is Clean Code? Bjarne Stroustrup “I like my code to be elegant and efficient“ “Clean code does one thing well“
  5. 5. What is Clean Code? Grady Booch “Clean code is Simple and direct“ “Clean code reads like well-written prose“
  6. 6. What is Clean Code? Dave Thomas “Clean code can be read“ “Clean code should be literate“
  7. 7. What is Clean Code? Michael Feathers “Clean Code always looks like it was written by someone who cares“
  8. 8. What is Clean Code? Ward Cunningham “You know you are working on clean code, when each routine you read turns out to be the pretty much what you expected“
  9. 9. What is Clean Code? How to measure good code?
  10. 10. What is Clean Code?
  11. 11. Why do I need Clean Code?
  12. 12. Why do I need Clean Code? …because you are an author // YES, YOU ARE!!! “An author is someone who practices writing as a profession“ by somebody
  13. 13. Why do I need Clean Code? …because you don‘t want to be a verb // NO, YOU DON‘T!!! “Oh man, this code has been Jimmy’d“ by somebody else
  14. 14. Why do I need Clean Code? …because you are lazy // YES, YOU ARE!!! // so am I // PROOF: no quotation at this slide
  15. 15. How to produce Clean Code?
  16. 16. How to produce Clean Code?
  17. 17. Robert C. Martin (aka Uncle Bob) “The Boy Scout Rule“ How to produce Clean Code? // General
  18. 18. How to produce Clean Code? // Naming “Names are sound and smoke” Johannes Wolfgang von Goethe – Faust I
  19. 19. How to produce Clean Code? Take care about names! // we name everything: // variables, functions, arguments, classes, packages // Naming
  20. 20. How to produce Clean Code? public class DtaRcrd102 { private Date genymdhms; private Date modymdhms; private static final String pszqint = “102“; } // Naming Use Pronounceable Names
  21. 21. How to produce Clean Code? public class Customer { private static final String RECORD_ID = “102“; private Date generationTimestamp; private Date modificationTimestamp; } // Naming Use Pronounceable Names
  22. 22. How to produce Clean Code? private String m_dsc; // Naming Avoid Encodings (member prefixes) private PhoneNumber phoneString; Avoid Encodings (hungarian notation)
  23. 23. How to produce Clean Code? private String description; // Naming Avoid Encodings (member prefixes) private PhoneNumber phone; Avoid Encodings (hungarian notation)
  24. 24. How to produce Clean Code? Add Meaningful Context fistName, lastName, street, city, state, zipcode // a better solution addressFirstName, addressLastName, addressState // a better solution Adress myAddress = new Address(); myAddress.getFirstName(); // Naming
  25. 25. How to produce Clean Code? Small! Rules of Functions: 1. should be small 2. should be smaller than that // < 150 characters per line // < 20 lines // Functions
  26. 26. How to produce Clean Code? Do One Thing Functions should do ONE thing. They should do it WELL. They should do it ONLY. // Functions
  27. 27. How to produce Clean Code? One Level of Abstraction // high level of abstraction getHtml(); // intermediate level of abstraction String pagePathName = PathParser.getName( pagePath ); // remarkable low level htmlBuilder.append(“n”); // Functions
  28. 28. How to produce Clean Code? Switch Statements class Employee { int getSalary() { switch( getType() ) { case EmployeeType.ENGINEER: return _monthlySalary; case EmployeeType.SALESMAN: return _monthlySalary + _commission; case EmployeeType.MANAGER: return _monthlySalary + _bonus; default: throw new InvalidEmployeeException(); } } } // Functions
  29. 29. How to produce Clean Code? Switch Statements interface Employee { int getSalary(); } class Salesman implements Employee { int getSalary() { return getMonthlySalary() + getCommision(); } } class Manager implements Employee { … } // Functions
  30. 30. How to produce Clean Code? // niladic getHtml(); // monadic execute( boolean executionType ); // dyadic assertEquals(String expected, String actual); // triadic drawCircle(double x, double y, double radius); // Function Arguments
  31. 31. How to produce Clean Code? Improve Monadic Functions with Flag Argument // ??? execute( boolean executionType ); // !!! executeInSuite(); executeStandAlone(); // Function Arguments
  32. 32. How to produce Clean Code? Improve Dyadic Functions with two similar Argument Types // ??? assertEquals(String expected, String actual); assertEquals(String actual, String expected); // !!! assertExpectedEqualsActual(expected, actual); // Function Arguments
  33. 33. How to produce Clean Code? Improve Triadic Functions with three similar Argument Types // ??? drawCircle(double x, double y, double radius); drawCircle(double radius, double x, double y); // … // !!! drawCircle(Point center, double radius); // Function Arguments
  34. 34. How to produce Clean Code? Don‘t comment bad code, rewrite it! // Comments
  35. 35. How to produce Clean Code? Noise /** Default Consturctor */ public AnnaulDateRule() { … } /** The day of the Month. */ private int dayOfMonth(); /** Returns the day of the Month. @return the day of the Month. */ public int getDayOfMonth() { return dayOfMonth; } // Comments
  36. 36. How to produce Clean Code? Scary Noise /** The name. */ private String name; /** The version. */ private String version; /** The licenseName. */ private String licenseName; /** The version. */ private String info; // Comments
  37. 37. How to produce Clean Code? Don‘t use comments if you can use a Function/Variable // does the moduel from the global list <mod> depend on the // subsystem we are part of? if(smodule.getDependSubsystems() .contains(subSysMod.getSubSystem())) { … } // improved ArrayList moduleDependencies = module.getDependentSubsystems(); String ourSubsystem = subSystemModule.getSubSystem(); if( moduleDependencies.contains( ourSubsystem ) ) { … } // Comments
  38. 38. How to produce Clean Code? The Law of Demeter // Bad String outputDir = ctxt.getOptions() .getScratchDir() .getAbsolutePath(); // Good String outputDir = ctxt.getScratchDirPath(); // Data Access
  39. 39. How to produce Clean Code? Prefer Exceptions to Returning Error 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”); } // Error Handling
  40. 40. How to produce Clean Code? Prefer Exceptions to Returning Error Codes try { deletePage(page); registry.deleteReference(page.name); configKeys.deleteKey(page.name.makeKey()); } catch( Exception e ) { logger.log(e.getMessage()); } // Error Handling
  41. 41. How to produce Clean Code? Extract Try/Catch Blocks 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()); } // Error Handling
  42. 42. How to produce Clean Code? Don‘t return Null List<Employee> employees = getEmployees(); If( employees != null ) { for( Employee employee : employees ) { totalSalary += employee.getSalary(); } } // Error Handling
  43. 43. How to produce Clean Code? Don‘t return Null List<Employee> employees = getEmployees();  for( Employee employee : employees ) { totalSalary += employee.getSalary(); } // Error Handling
  44. 44. How to produce Clean Code? Small! Rules of Classes: 1. should be small 2. should be smaller than that // Single Responsibility Principle (SRP) // a class should have one, and only one, readon to change // Classes
  45. 45. How to produce Clean Code? // More Information Have a look at:

×