SlideShare ist ein Scribd-Unternehmen logo
1 von 51
Downloaden Sie, um offline zu lesen
Fredrik Wendt
Clean Code
  what, why, how
http://blog.xebia.com/2009/01/clean-code-vs-implementation-patterns/
Disclaimer




 Correlation between
Clean code and success?
How is it even possible
for code to be this bad?
Exhibits
    • Hudson class is 3900+
      LOC
    • Hudson class is untestable
      Singleton
    • Constructor calls a method
      that gets the singleton
      instance and calls a method
      on the instance.
    • Hudson extends Node, but
      Node calls methods defined
      in Hudson
WTF!
So why care – Jenkins is great?
”Smart” vs Professional
• Smart                                • Professional
  ●   Great coding skills                  ●   Readable code
  ●   Writes advanced code                 ●   Maintainable code
  ●   String r; // lowercase url           ●   'Clarity is king'
                                           ●   String lowerCaseUrl
                                               OfCurrentPage;




               http://www.slideshare.net/JandV/clean-code-summary
What is Clean Code?
int d; // elapsed time in days
int d; // elapsed time in days
int elapsedTimeInDays;
int d; // elapsed time in days
int elapsedTimeInDays;
int daysSinceCreation;
int d; // elapsed time in days
int elapsedTimeInDays;
int daysSinceCreation;
int fileAgeInDays;
public List<int[]> getThem() {
    List<int[]> list1 = new ArrayList<int[]>();
    for (int[] x : theList)
      if (x[0] == 4)
        list1.add(x);
    return list1;
}
public List<int[]> getThem() {
    List<int[]> list1 = new ArrayList<int[]>();
    for (int[] cell : gameBoard)
      if (cell[STATUS_VALUE] == FLAGGED)
        list1.add(cell);
    return list1;
}
public List<int[]> getFlaggedCells() {
    List<int[]> flaggedCells = new ArrayList<int[]>();
    for (int[] cell : gameBoard)
      if (cell[STATUS_VALUE] == FLAGGED)
        flaggedCells.add(cell);
    return flaggedCells;
}
public List<Cell> getFlaggedCells() {
    List<Cell> flaggedCells = new ArrayList<Cell>();
    for (Cell cell : gameBoard)
      if (cell.isFlagged())
        flaggedCells.add(cell);
    return flaggedCells;
}
Clean code always

looks like it was written by

    someone who cares
Clean code always

looks like it was wrtten by

    somone who craes
Readable
Maintainable
Changeable
Agile Manifesto


Individuals and interactions
     over processes and tools
Working software
     over comprehensive documentation
Customer collaboration
     over contract negotiation
Responding to change
     over following a plan
Readable?
~ 90% of your time is reading

                  What if you'd
                ddouble write-time
                   and thereby
                  cut read-time
                     by half?
public class Part {
    private String m_dsc; // The textual description
    void setName(String name) {
        m_dsc = name;
    }
}


public class Part {
    private String description;
    void setDescription(String description) {
        this.description = description;
    }
}
Still, why bother?
Code is written for humans!
Why?
Who's the main recipient of the code we write – a computer, an
end user or a programmer?
The primary user of source
code is a programmer, perhaps
   ●   your Customer
   ●   your Colleague
   ●   You
How should I write code?
DRY - Don't Repeat Yourself


YAGNI - You Ain't Gonna Need It
small
Meaningful Names
• Intention-Revealing   • JobQueue
• Pronounceable         • Words from the domain
• Avoid Encoding
                        • Comments
• Classes               • Formatting!
• Methods
Law of Demeter

  More formally, the Law of Demeter
      for functions requires that
      a method M of an object O
   may only invoke the methods of
    the following kinds of objects:

1. O itself
2. M's parameters
3. any objects created/instantiated within M
4. O's direct component objects
5. a variable, accessible by O, in the scope of M
Dog's Legs
public class CareTaker {
    private Dog dog;
    ...
    public void walkTheDog(){
        dog.walk();


        dog.stop(); // tree or grass etc


        dog.doYourThing();
        ...
    }
}
Dog's Legs
public class CareTaker {
    private Dog dog;
    ...
    public void walkTheDog(){
        for (Leg leg : dog.getLegs())
          leg.move();
        for (Leg leg : dog.getLegs())
          leg.stopMoving();
        dog.brain.urinationCenter.releaseUrge();
        ...
    }
}
Don't talk to strangers



http://www.ccs.neu.edu/research/demeter/demeter-method/LawOfDemeter/paper-boy/
Law of Demeter
public class PaperBoy {
    private Wallet myWallet;
    ...
    public void chargeCustomer(Customer customer) {
        Wallet wallet = customer.getWallet();
        Collection<Bill> bills = wallet.getBills();
        Collection<Bill> payment = extract(5, bills);
        myWallet.add(payment); // or bills?
    }
}
Law of Demeter
public class PaperBoy {
    private Wallet myWallet;
    ...
    public void chargeCustomer(Customer customer) {
        Collection<Bill> payment = customer.getPayment(5);
        myWallet.add(payment);
    }
}
Single Responsibility




       1
        small
Boy Scout Rule




Leave the campground cleaner than you found it.
Hur mäter man Clean Code?
       Eller kodkvalitet?
Can you measure Clean Code?
        Or code quality?
WTFs / minute




The only valid
measurement of
 code quality
Summary


     Source code vs machine code
   Write with the audience in minde
        Smart vs Professional
    Code quality and retrospectives
Code Review (wtf!) vs pair programming
Legacy Code




Code with no tests
What about the code that's
already in my backpack?


• Get the old man on track
  – make the code testable
  (and tested)
• Fix bugs brought out in
  daylight by the tests
• ”Make it right”
What about the code that's
already in my backpack?


• Get the old man on track
  – make the code testable
  (and tested)
• Fix bugs brought out in
  daylight by the tests
• ”Make it right” – refactor!
Code without tests ...
• is legacy code
• is not refactored
• unlikely to be changed by
   someone other than the
   person who wrote it (the
   past few days)
• is heavy load/back pack

Weitere ähnliche Inhalte

Was ist angesagt?

Clean Code II - Dependency Injection at SoCal Code Camp San Diego (07/27/2013)
Clean Code II - Dependency Injection at SoCal Code Camp San Diego (07/27/2013)Clean Code II - Dependency Injection at SoCal Code Camp San Diego (07/27/2013)
Clean Code II - Dependency Injection at SoCal Code Camp San Diego (07/27/2013)Theo Jungeblut
 
Contract First Development with Microsoft Code Contracts and Microsoft Pex at...
Contract First Development with Microsoft Code Contracts and Microsoft Pex at...Contract First Development with Microsoft Code Contracts and Microsoft Pex at...
Contract First Development with Microsoft Code Contracts and Microsoft Pex at...Theo Jungeblut
 
Clean Code - Design Patterns and Best Practices for Bay.NET SF User Group (01...
Clean Code - Design Patterns and Best Practices for Bay.NET SF User Group (01...Clean Code - Design Patterns and Best Practices for Bay.NET SF User Group (01...
Clean Code - Design Patterns and Best Practices for Bay.NET SF User Group (01...Theo Jungeblut
 
Mental models, complexity and software
Mental models, complexity and softwareMental models, complexity and software
Mental models, complexity and softwareArturo Herrero
 
Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)
Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)
Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)Theo Jungeblut
 
Clean Code Part II - Dependency Injection at SoCal Code Camp
Clean Code Part II - Dependency Injection at SoCal Code CampClean Code Part II - Dependency Injection at SoCal Code Camp
Clean Code Part II - Dependency Injection at SoCal Code CampTheo Jungeblut
 
.NET Coding Standards For The Real World (2012)
.NET Coding Standards For The Real World (2012).NET Coding Standards For The Real World (2012)
.NET Coding Standards For The Real World (2012)David McCarter
 
Clean Code I - Design Patterns and Best Practices at SoCal Code Camp San Dieg...
Clean Code I - Design Patterns and Best Practices at SoCal Code Camp San Dieg...Clean Code I - Design Patterns and Best Practices at SoCal Code Camp San Dieg...
Clean Code I - Design Patterns and Best Practices at SoCal Code Camp San Dieg...Theo Jungeblut
 
Stop wasting-time-by-applying-clean-code-principles
Stop wasting-time-by-applying-clean-code-principlesStop wasting-time-by-applying-clean-code-principles
Stop wasting-time-by-applying-clean-code-principlesEdorian
 
Clean Code Part i - Design Patterns and Best Practices -
Clean Code Part i - Design Patterns and Best Practices -Clean Code Part i - Design Patterns and Best Practices -
Clean Code Part i - Design Patterns and Best Practices -Theo Jungeblut
 
Lego for Software Engineers at Silicon Valley Code Camp 2011 (2010-10-10)
Lego for Software Engineers at Silicon Valley Code Camp 2011 (2010-10-10)Lego for Software Engineers at Silicon Valley Code Camp 2011 (2010-10-10)
Lego for Software Engineers at Silicon Valley Code Camp 2011 (2010-10-10)Theo Jungeblut
 
Clean Code III - Software Craftsmanship
Clean Code III - Software CraftsmanshipClean Code III - Software Craftsmanship
Clean Code III - Software CraftsmanshipTheo Jungeblut
 
Writing High Quality Code in C#
Writing High Quality Code in C#Writing High Quality Code in C#
Writing High Quality Code in C#Svetlin Nakov
 
WordCamp US: Clean Code
WordCamp US: Clean CodeWordCamp US: Clean Code
WordCamp US: Clean Codemtoppa
 
C# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoringC# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoringEyob Lube
 
Tdd is not about testing (C++ version)
Tdd is not about testing (C++ version)Tdd is not about testing (C++ version)
Tdd is not about testing (C++ version)Gianluca Padovani
 
Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Gianluca Padovani
 

Was ist angesagt? (20)

Clean Code II - Dependency Injection at SoCal Code Camp San Diego (07/27/2013)
Clean Code II - Dependency Injection at SoCal Code Camp San Diego (07/27/2013)Clean Code II - Dependency Injection at SoCal Code Camp San Diego (07/27/2013)
Clean Code II - Dependency Injection at SoCal Code Camp San Diego (07/27/2013)
 
Contract First Development with Microsoft Code Contracts and Microsoft Pex at...
Contract First Development with Microsoft Code Contracts and Microsoft Pex at...Contract First Development with Microsoft Code Contracts and Microsoft Pex at...
Contract First Development with Microsoft Code Contracts and Microsoft Pex at...
 
Clean Code - Design Patterns and Best Practices for Bay.NET SF User Group (01...
Clean Code - Design Patterns and Best Practices for Bay.NET SF User Group (01...Clean Code - Design Patterns and Best Practices for Bay.NET SF User Group (01...
Clean Code - Design Patterns and Best Practices for Bay.NET SF User Group (01...
 
Clean code
Clean codeClean code
Clean code
 
Mental models, complexity and software
Mental models, complexity and softwareMental models, complexity and software
Mental models, complexity and software
 
Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)
Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)
Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)
 
Clean Code Part II - Dependency Injection at SoCal Code Camp
Clean Code Part II - Dependency Injection at SoCal Code CampClean Code Part II - Dependency Injection at SoCal Code Camp
Clean Code Part II - Dependency Injection at SoCal Code Camp
 
.NET Coding Standards For The Real World (2012)
.NET Coding Standards For The Real World (2012).NET Coding Standards For The Real World (2012)
.NET Coding Standards For The Real World (2012)
 
Clean Code I - Design Patterns and Best Practices at SoCal Code Camp San Dieg...
Clean Code I - Design Patterns and Best Practices at SoCal Code Camp San Dieg...Clean Code I - Design Patterns and Best Practices at SoCal Code Camp San Dieg...
Clean Code I - Design Patterns and Best Practices at SoCal Code Camp San Dieg...
 
Stop wasting-time-by-applying-clean-code-principles
Stop wasting-time-by-applying-clean-code-principlesStop wasting-time-by-applying-clean-code-principles
Stop wasting-time-by-applying-clean-code-principles
 
Clean Code Part i - Design Patterns and Best Practices -
Clean Code Part i - Design Patterns and Best Practices -Clean Code Part i - Design Patterns and Best Practices -
Clean Code Part i - Design Patterns and Best Practices -
 
Lego for Software Engineers at Silicon Valley Code Camp 2011 (2010-10-10)
Lego for Software Engineers at Silicon Valley Code Camp 2011 (2010-10-10)Lego for Software Engineers at Silicon Valley Code Camp 2011 (2010-10-10)
Lego for Software Engineers at Silicon Valley Code Camp 2011 (2010-10-10)
 
Clean Code III - Software Craftsmanship
Clean Code III - Software CraftsmanshipClean Code III - Software Craftsmanship
Clean Code III - Software Craftsmanship
 
Writing High Quality Code in C#
Writing High Quality Code in C#Writing High Quality Code in C#
Writing High Quality Code in C#
 
WordCamp US: Clean Code
WordCamp US: Clean CodeWordCamp US: Clean Code
WordCamp US: Clean Code
 
C# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoringC# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoring
 
Clean code
Clean codeClean code
Clean code
 
Tdd is not about testing (C++ version)
Tdd is not about testing (C++ version)Tdd is not about testing (C++ version)
Tdd is not about testing (C++ version)
 
Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Tdd is not about testing (OOP)
Tdd is not about testing (OOP)
 
Clean code
Clean codeClean code
Clean code
 

Andere mochten auch

Informationsradiatorer NFI Systemunderhåll 2015-12-01
Informationsradiatorer NFI Systemunderhåll 2015-12-01Informationsradiatorer NFI Systemunderhåll 2015-12-01
Informationsradiatorer NFI Systemunderhåll 2015-12-01Fredrik Wendt
 
Accidentally Manager – A Survival Guide for First-Time Engineering Managers
Accidentally Manager – A Survival Guide for First-Time Engineering ManagersAccidentally Manager – A Survival Guide for First-Time Engineering Managers
Accidentally Manager – A Survival Guide for First-Time Engineering ManagersTheo Jungeblut
 
Clean code and Code Smells
Clean code and Code SmellsClean code and Code Smells
Clean code and Code SmellsMario Sangiorgio
 
Clean Code: Stop wasting my time
Clean Code: Stop wasting my timeClean Code: Stop wasting my time
Clean Code: Stop wasting my timeEdorian
 

Andere mochten auch (8)

Informationsradiatorer NFI Systemunderhåll 2015-12-01
Informationsradiatorer NFI Systemunderhåll 2015-12-01Informationsradiatorer NFI Systemunderhåll 2015-12-01
Informationsradiatorer NFI Systemunderhåll 2015-12-01
 
Clean code
Clean codeClean code
Clean code
 
Accidentally Manager – A Survival Guide for First-Time Engineering Managers
Accidentally Manager – A Survival Guide for First-Time Engineering ManagersAccidentally Manager – A Survival Guide for First-Time Engineering Managers
Accidentally Manager – A Survival Guide for First-Time Engineering Managers
 
Clean code and Code Smells
Clean code and Code SmellsClean code and Code Smells
Clean code and Code Smells
 
Clean Code: Stop wasting my time
Clean Code: Stop wasting my timeClean Code: Stop wasting my time
Clean Code: Stop wasting my time
 
Clean Code
Clean CodeClean Code
Clean Code
 
Clean code
Clean codeClean code
Clean code
 
Clean coding-practices
Clean coding-practicesClean coding-practices
Clean coding-practices
 

Ähnlich wie Clean Code 2

Metrics ekon 14_2_kleiner
Metrics ekon 14_2_kleinerMetrics ekon 14_2_kleiner
Metrics ekon 14_2_kleinerMax Kleiner
 
Javascript done right - Open Web Camp III
Javascript done right - Open Web Camp IIIJavascript done right - Open Web Camp III
Javascript done right - Open Web Camp IIIDirk Ginader
 
Writing clean code in C# and .NET
Writing clean code in C# and .NETWriting clean code in C# and .NET
Writing clean code in C# and .NETDror Helper
 
Addressing Scenario
Addressing ScenarioAddressing Scenario
Addressing ScenarioTara Hardin
 
Your Own Metric System
Your Own Metric SystemYour Own Metric System
Your Own Metric SystemErin Dees
 
CSharp presentation and software developement
CSharp presentation and software developementCSharp presentation and software developement
CSharp presentation and software developementfrwebhelp
 
How to write clean & testable code without losing your mind
How to write clean & testable code without losing your mindHow to write clean & testable code without losing your mind
How to write clean & testable code without losing your mindAndreas Czakaj
 
Being Expressive in Code
Being Expressive in CodeBeing Expressive in Code
Being Expressive in CodeEamonn Boyle
 
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...Sang Don Kim
 
Go 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX GoGo 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX GoRodolfo Carvalho
 
Structured web programming
Structured web programmingStructured web programming
Structured web programmingahfast
 
C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#Hawkman Academy
 
Working with c++ legacy code
Working with c++ legacy codeWorking with c++ legacy code
Working with c++ legacy codeDror Helper
 
PHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing InsanityPHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing InsanityGeorgePeterBanyard
 

Ähnlich wie Clean Code 2 (20)

Clean code
Clean codeClean code
Clean code
 
Metrics ekon 14_2_kleiner
Metrics ekon 14_2_kleinerMetrics ekon 14_2_kleiner
Metrics ekon 14_2_kleiner
 
Javascript done right - Open Web Camp III
Javascript done right - Open Web Camp IIIJavascript done right - Open Web Camp III
Javascript done right - Open Web Camp III
 
Writing clean code in C# and .NET
Writing clean code in C# and .NETWriting clean code in C# and .NET
Writing clean code in C# and .NET
 
Addressing Scenario
Addressing ScenarioAddressing Scenario
Addressing Scenario
 
Your Own Metric System
Your Own Metric SystemYour Own Metric System
Your Own Metric System
 
CSharp presentation and software developement
CSharp presentation and software developementCSharp presentation and software developement
CSharp presentation and software developement
 
Wtf per lineofcode
Wtf per lineofcodeWtf per lineofcode
Wtf per lineofcode
 
How to write clean & testable code without losing your mind
How to write clean & testable code without losing your mindHow to write clean & testable code without losing your mind
How to write clean & testable code without losing your mind
 
Being Expressive in Code
Being Expressive in CodeBeing Expressive in Code
Being Expressive in Code
 
Clean code and code smells
Clean code and code smellsClean code and code smells
Clean code and code smells
 
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
 
Software Engineering
Software EngineeringSoftware Engineering
Software Engineering
 
Go 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX GoGo 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX Go
 
Structured web programming
Structured web programmingStructured web programming
Structured web programming
 
Oops lecture 1
Oops lecture 1Oops lecture 1
Oops lecture 1
 
C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#
 
Working with c++ legacy code
Working with c++ legacy codeWorking with c++ legacy code
Working with c++ legacy code
 
PHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing InsanityPHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing Insanity
 
C++primer
C++primerC++primer
C++primer
 

Mehr von Fredrik Wendt

Continuous Delivery Experience Report - Agile Greece Summit 2016
Continuous Delivery Experience Report - Agile Greece Summit 2016Continuous Delivery Experience Report - Agile Greece Summit 2016
Continuous Delivery Experience Report - Agile Greece Summit 2016Fredrik Wendt
 
Go.cd - the tool that Jenkins ain't
Go.cd - the tool that Jenkins ain'tGo.cd - the tool that Jenkins ain't
Go.cd - the tool that Jenkins ain'tFredrik Wendt
 
Impact of CD, Clean Code, ... on Team Performance
Impact of CD, Clean Code, ... on Team PerformanceImpact of CD, Clean Code, ... on Team Performance
Impact of CD, Clean Code, ... on Team PerformanceFredrik Wendt
 
Arkitektur i agila projekt
Arkitektur i agila projektArkitektur i agila projekt
Arkitektur i agila projektFredrik Wendt
 
Coding dojos på arbetstid
Coding dojos på arbetstidCoding dojos på arbetstid
Coding dojos på arbetstidFredrik Wendt
 
Js Test Driver, JsHamcrest, JsMockito
Js Test Driver, JsHamcrest, JsMockitoJs Test Driver, JsHamcrest, JsMockito
Js Test Driver, JsHamcrest, JsMockitoFredrik Wendt
 
Jdojo@Gbg Introduction
Jdojo@Gbg IntroductionJdojo@Gbg Introduction
Jdojo@Gbg IntroductionFredrik Wendt
 
Presentation of JSConf.eu
Presentation of JSConf.euPresentation of JSConf.eu
Presentation of JSConf.euFredrik Wendt
 
Agile Injection, Varberg
Agile Injection, VarbergAgile Injection, Varberg
Agile Injection, VarbergFredrik Wendt
 
Webboptimering 25 min
Webboptimering 25 minWebboptimering 25 min
Webboptimering 25 minFredrik Wendt
 

Mehr von Fredrik Wendt (12)

Continuous Delivery Experience Report - Agile Greece Summit 2016
Continuous Delivery Experience Report - Agile Greece Summit 2016Continuous Delivery Experience Report - Agile Greece Summit 2016
Continuous Delivery Experience Report - Agile Greece Summit 2016
 
Go.cd - the tool that Jenkins ain't
Go.cd - the tool that Jenkins ain'tGo.cd - the tool that Jenkins ain't
Go.cd - the tool that Jenkins ain't
 
Impact of CD, Clean Code, ... on Team Performance
Impact of CD, Clean Code, ... on Team PerformanceImpact of CD, Clean Code, ... on Team Performance
Impact of CD, Clean Code, ... on Team Performance
 
Arkitektur i agila projekt
Arkitektur i agila projektArkitektur i agila projekt
Arkitektur i agila projekt
 
Coding dojos på arbetstid
Coding dojos på arbetstidCoding dojos på arbetstid
Coding dojos på arbetstid
 
Js Test Driver, JsHamcrest, JsMockito
Js Test Driver, JsHamcrest, JsMockitoJs Test Driver, JsHamcrest, JsMockito
Js Test Driver, JsHamcrest, JsMockito
 
Jdojo@Gbg Introduction
Jdojo@Gbg IntroductionJdojo@Gbg Introduction
Jdojo@Gbg Introduction
 
Presentation of JSConf.eu
Presentation of JSConf.euPresentation of JSConf.eu
Presentation of JSConf.eu
 
Agile Injection, Varberg
Agile Injection, VarbergAgile Injection, Varberg
Agile Injection, Varberg
 
Webboptimering 25 min
Webboptimering 25 minWebboptimering 25 min
Webboptimering 25 min
 
Clean Code
Clean CodeClean Code
Clean Code
 
Using Mockito
Using MockitoUsing Mockito
Using Mockito
 

Kürzlich hochgeladen

Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 

Kürzlich hochgeladen (20)

Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 

Clean Code 2

  • 1.
  • 3. Clean Code what, why, how
  • 6. How is it even possible for code to be this bad?
  • 7. Exhibits • Hudson class is 3900+ LOC • Hudson class is untestable Singleton • Constructor calls a method that gets the singleton instance and calls a method on the instance. • Hudson extends Node, but Node calls methods defined in Hudson
  • 8. WTF! So why care – Jenkins is great?
  • 9.
  • 10. ”Smart” vs Professional • Smart • Professional ● Great coding skills ● Readable code ● Writes advanced code ● Maintainable code ● String r; // lowercase url ● 'Clarity is king' ● String lowerCaseUrl OfCurrentPage; http://www.slideshare.net/JandV/clean-code-summary
  • 11. What is Clean Code?
  • 12. int d; // elapsed time in days
  • 13. int d; // elapsed time in days int elapsedTimeInDays;
  • 14. int d; // elapsed time in days int elapsedTimeInDays; int daysSinceCreation;
  • 15. int d; // elapsed time in days int elapsedTimeInDays; int daysSinceCreation; int fileAgeInDays;
  • 16. public List<int[]> getThem() { List<int[]> list1 = new ArrayList<int[]>(); for (int[] x : theList) if (x[0] == 4) list1.add(x); return list1; }
  • 17. public List<int[]> getThem() { List<int[]> list1 = new ArrayList<int[]>(); for (int[] cell : gameBoard) if (cell[STATUS_VALUE] == FLAGGED) list1.add(cell); return list1; }
  • 18. public List<int[]> getFlaggedCells() { List<int[]> flaggedCells = new ArrayList<int[]>(); for (int[] cell : gameBoard) if (cell[STATUS_VALUE] == FLAGGED) flaggedCells.add(cell); return flaggedCells; }
  • 19. public List<Cell> getFlaggedCells() { List<Cell> flaggedCells = new ArrayList<Cell>(); for (Cell cell : gameBoard) if (cell.isFlagged()) flaggedCells.add(cell); return flaggedCells; }
  • 20. Clean code always looks like it was written by someone who cares
  • 21. Clean code always looks like it was wrtten by somone who craes
  • 23. Agile Manifesto Individuals and interactions over processes and tools Working software over comprehensive documentation Customer collaboration over contract negotiation Responding to change over following a plan
  • 24.
  • 26. ~ 90% of your time is reading What if you'd ddouble write-time and thereby cut read-time by half?
  • 27. public class Part { private String m_dsc; // The textual description void setName(String name) { m_dsc = name; } } public class Part { private String description; void setDescription(String description) { this.description = description; } }
  • 29. Code is written for humans!
  • 30. Why? Who's the main recipient of the code we write – a computer, an end user or a programmer? The primary user of source code is a programmer, perhaps ● your Customer ● your Colleague ● You
  • 31. How should I write code?
  • 32. DRY - Don't Repeat Yourself YAGNI - You Ain't Gonna Need It
  • 33. small
  • 34. Meaningful Names • Intention-Revealing • JobQueue • Pronounceable • Words from the domain • Avoid Encoding • Comments • Classes • Formatting! • Methods
  • 35. Law of Demeter More formally, the Law of Demeter for functions requires that a method M of an object O may only invoke the methods of the following kinds of objects: 1. O itself 2. M's parameters 3. any objects created/instantiated within M 4. O's direct component objects 5. a variable, accessible by O, in the scope of M
  • 36. Dog's Legs public class CareTaker { private Dog dog; ... public void walkTheDog(){ dog.walk(); dog.stop(); // tree or grass etc dog.doYourThing(); ... } }
  • 37. Dog's Legs public class CareTaker { private Dog dog; ... public void walkTheDog(){ for (Leg leg : dog.getLegs()) leg.move(); for (Leg leg : dog.getLegs()) leg.stopMoving(); dog.brain.urinationCenter.releaseUrge(); ... } }
  • 38. Don't talk to strangers http://www.ccs.neu.edu/research/demeter/demeter-method/LawOfDemeter/paper-boy/
  • 39. Law of Demeter public class PaperBoy { private Wallet myWallet; ... public void chargeCustomer(Customer customer) { Wallet wallet = customer.getWallet(); Collection<Bill> bills = wallet.getBills(); Collection<Bill> payment = extract(5, bills); myWallet.add(payment); // or bills? } }
  • 40. Law of Demeter public class PaperBoy { private Wallet myWallet; ... public void chargeCustomer(Customer customer) { Collection<Bill> payment = customer.getPayment(5); myWallet.add(payment); } }
  • 42.
  • 43. Boy Scout Rule Leave the campground cleaner than you found it.
  • 44. Hur mäter man Clean Code? Eller kodkvalitet?
  • 45. Can you measure Clean Code? Or code quality?
  • 46. WTFs / minute The only valid measurement of code quality
  • 47. Summary Source code vs machine code Write with the audience in minde Smart vs Professional Code quality and retrospectives Code Review (wtf!) vs pair programming
  • 49. What about the code that's already in my backpack? • Get the old man on track – make the code testable (and tested) • Fix bugs brought out in daylight by the tests • ”Make it right”
  • 50. What about the code that's already in my backpack? • Get the old man on track – make the code testable (and tested) • Fix bugs brought out in daylight by the tests • ”Make it right” – refactor!
  • 51. Code without tests ... • is legacy code • is not refactored • unlikely to be changed by someone other than the person who wrote it (the past few days) • is heavy load/back pack