SlideShare ist ein Scribd-Unternehmen logo
1 von 30
Downloaden Sie, um offline zu lesen
CLEAN CODE

IT REALLY MATTERS
BROUGHT TO YOU
BY
SENIOR SOFTWARE
ENGINEER

DANIEL
KUMMER

NAMICS AG ZÜRICH
WWW.NAMICS.COM
CLEAN CODE
AGENDA

?

WHY

DOES IT EVEN
MATTER?

!

HOW

CAN IT BE
DONE?

*

BESIDE

THE CODE…
DEFINITION
OF CLEAN CODE

WORKS

EFFICIENT

READABLE

EXTENDABLE

CORRECTLY

IT TELLS YOU A STORY

SIMPLE

TO UNDERSTAND

IT DOES IT RIGHT
EASY TO MODIFY

CLEAN

SOMEONE CARED
?

WHY
CARE

	
   ”The only valid code metric is WTFs
per minute” ~ A comic strip	
  
SOFTWARE
ENTROPY

THE BROKEN WINDOW

DON’T LIVE WITH
BROKEN WINDOWS!
IT’S ALL
ABOUT

$$$	
  
THE COST OF
BAD CODE

TIME

PRODUCTIVITY

NERVES

PROJECTS

Mo$va$on	
  No	
  one	
  wants	
  to	
  take	
  it	
  
DETECTING
BAD CODE
DEAD CODE

UNUSED = UNNEEDED

COMMENTS
WHY NOT WHAT

A FEW
SMELLS
DUPLICATES

DON’T REPEAT YOURSELF

CONDITIONAL LONG METHOD / SPECULATIVE
COMPLEXITY PARAMETER LIST GENERALITY
HARD TO TEST

SHORTER = EASIER

SOLVE TODAYS PROBLEM,
NOT TOMORROWS

A more complete list can be found at http://www.codinghorror.com/blog/2006/05/code-smells.html
!

	
  

HOW
TO DO IT

“Any fool can write code that a computer
can understand.
Good programmers write code that
humans can understand.” ~Martin Fowler
2

SIMPLE
RULES

EASY TO FOLLOW	
  
1

WRITE DIRTY CODE
FIRST, THEN CLEAN IT
YOU CAN’T WRITE CLEAN CODE ON THE FIRST GO!
YOUR FIRST ATTEMPT WON’T BE YOUR BEST
SUCCESSIVE REFINEMENT IS KEY
START WITH PSEUDOCODE
2

LEAVE IT CLEANER
THAN YOU FOUND IT

THE “BOYSCOUT” RULE
INCREMENTIALISM – MAKE TINY CHANGES
EVEN IF IT’S JUST REFORMATTING – YOU CARED
YOU ARE TAKING RESPONSIBILITY
THREE WORDS

CARE FOR CODE
CLEAN
TIPPS

HOW TO GO ABOUT IT
NAMING CONVENTIONS
REVEAL INTENTION – CLARITY IS KING
no questions asked about the purpose

LONG NAMES AREN’T BAD

pick names corresponding to the scope

ONE WORD PER CONCEPT / NOT AMBIGUOUS
keep the lexicon consistent

USE ENGLISH – DON’T USE YOUR LANGUAGE
just don’t!
CODING
DRY - DON’T REPEAT YOURSELF
duplication is a root of evil!

DON’T OPTIMIZE PREMATURELY
unless it’s really, really necessary

DON’T SAVE ON CHARACTERS

one liners are not faster, just unreadable

DON’T TRY TO OUTSMART THE COMPILER
really smart people are working on it…
FUNCTIONS
KISS - KEEP IT SIMPLE AND STUPID
equals understandable

DO ONE THING ONLY – AND DO IT WELL
keep the level of abstraction to a minimum

MINIMIZE ARGUMENTS – LESS IS MORE

pursue it, as best as you can (think about testing)
FUNCTIONS
FLAGS ARE BAD

prefer polymorphism

SIDE EFFECTS ARE SCARY
the function should do one thing

EXTRACT ERROR HANDLING

improve readability, don’t obscure logic

RETURN ONCE ONLY – OR FAIL FAST
keep the exit clear
COMMENTING //drunk, fix later
NEVER EXPLAIN WHAT CODE DOES
explain WHY you did it this way

DON’T COMMENT OUT CODE
we have VCS for this!

COMMENT STRANGE OR UNNATURAL CODE
do it even if it’s not your own – explain why

NO REDUNDANT OR MANDATED COMMENTS
example: don’t document getters / setters
FORMATTING DO IT LIKE THE NEWSPAPERS

{

TOP-DOWN PRINCIPLE
MAIN FUNCTIONS ON TOP
FOLLOWED BY CALLED FUNCTIONS

}

VERTICAL DENSITY
WHAT BELONGS TOGETHER
SHOULD BE TOGETHER

NO HORIZONTAL ALIGNMENT
DON’T ALIGN JUST FOR THE LOOKS
OBJECTS AND CLASSES
SRP – SINGLE RESPONSIBILITY PRINCIPLE
one responsibility per object

SMALL

keep away from the “god”-classes

ORGANIZE FOR CHANGE – USE INTERFACES
it’s most likely going to happen

LoD - LAW OF DEMETER (LOOSE COUPLING)
a given object should assume as little as possible
about the structure or properties of anything else
ERROR HANDLING
USE UNCHECKED EXCEPTIONS AND
DOCUMENT THEM IN THE JAVADOC
you decide when to handle them

EXTRACT HANDLING BLOCKS (TRY/CATCH)
don’t disturb the code flow

DON’T RETURN NULL, DON’T PASS NULL
avoid NULL checks and NPEs

CONCENTRATE ON WHAT YOU CAN HANDLE
not what to throw
TEST
YOUR CREATION
SO YOU MAY SLEEP CALM AT NIGHT
THE F.I.R.S.T PRINCIPLE
FAST

tests should run quick – or you won’t run them

INDEPENDENT

no test should depend on another test

REPEATABLE

in any environment, your laptop, the CI server…

SELF-VALIDATING

pass or fail - no manual action required

TIMELY

don’t write your tests too late, it only gets harder
TESTING PYRAMID
BESIDE
CODE

THE SATELLITES OF
A SOFTWARE PROJECT	
  
USE TOOLS

COMBINE YOUR MAGIC HELPERS
CONTINUOUS INTEGRATION
EARLY PROBLEM RECOGNITION

RELEASE MANAGEMENT

SCM / VSC

ISSUE MANAGEMENT

CODE METRICS

DEFINE YOUR RELEASE CYCLES

TRACK DISCUSSIONS AND DESCISSIONS

KEEP A CLEAR HISTORY

HEURISTIC CODE ANALYSIS
HUMAN FACTORS
DON’T BE SHY…

OTHERS HAVE THE SAME PROBLEMS YOU HAVE – EVEN PROS!

REVIEW

LEARN

DISCUSS

SHARE

IMPROVE AND LEARN

GUIDELINES / PROBLEMS / SOLUTIONS

DELIBERATE PRACTICE

THOUGHTS / TOOLS / IDEAS
GOING
DEEPER

more about everything…
READING MATERIAL

SOFTWARE
CRAFTSMANSHIP

CODE OF CONDUCT

CLEAN YOUR CODE
WITH COMMON
SOLUTIONS
CLEAN CODE
DEPENDS ON

YOU

Weitere ähnliche Inhalte

Was ist angesagt?

Refactoring 101
Refactoring 101Refactoring 101
Refactoring 101Adam Culp
 
Refactoring and code smells
Refactoring and code smellsRefactoring and code smells
Refactoring and code smellsPaul Nguyen
 
Agile code quality metrics
Agile code quality metricsAgile code quality metrics
Agile code quality metricsGil Nahmias
 
Working With Legacy Code
Working With Legacy CodeWorking With Legacy Code
Working With Legacy CodeAndrea Polci
 
Clean code: understanding Boundaries and Unit Tests
Clean code: understanding Boundaries and Unit TestsClean code: understanding Boundaries and Unit Tests
Clean code: understanding Boundaries and Unit Testsradin reth
 
The tests are trying to tell you something@VoxxedBucharest.pptx
The tests are trying to tell you something@VoxxedBucharest.pptxThe tests are trying to tell you something@VoxxedBucharest.pptx
The tests are trying to tell you something@VoxxedBucharest.pptxVictor Rentea
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerIgor Crvenov
 
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 Principles
Clean Code PrinciplesClean Code Principles
Clean Code PrinciplesYeurDreamin'
 

Was ist angesagt? (20)

Clean Code
Clean CodeClean Code
Clean Code
 
Refactoring
RefactoringRefactoring
Refactoring
 
Clean Code
Clean CodeClean Code
Clean Code
 
Clean code
Clean codeClean code
Clean code
 
Refactoring 101
Refactoring 101Refactoring 101
Refactoring 101
 
Refactoring and code smells
Refactoring and code smellsRefactoring and code smells
Refactoring and code smells
 
Writing clean code
Writing clean codeWriting clean code
Writing clean code
 
Clean Code na Prática
Clean Code na PráticaClean Code na Prática
Clean Code na Prática
 
Agile code quality metrics
Agile code quality metricsAgile code quality metrics
Agile code quality metrics
 
Working With Legacy Code
Working With Legacy CodeWorking With Legacy Code
Working With Legacy Code
 
Clean code: understanding Boundaries and Unit Tests
Clean code: understanding Boundaries and Unit TestsClean code: understanding Boundaries and Unit Tests
Clean code: understanding Boundaries and Unit Tests
 
The tests are trying to tell you something@VoxxedBucharest.pptx
The tests are trying to tell you something@VoxxedBucharest.pptxThe tests are trying to tell you something@VoxxedBucharest.pptx
The tests are trying to tell you something@VoxxedBucharest.pptx
 
Clean code
Clean codeClean code
Clean code
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin Fowler
 
Coding standard
Coding standardCoding standard
Coding standard
 
Clean code
Clean code Clean code
Clean code
 
Clean Pragmatic Architecture - Avoiding a Monolith
Clean Pragmatic Architecture - Avoiding a MonolithClean Pragmatic Architecture - Avoiding a Monolith
Clean Pragmatic Architecture - Avoiding a Monolith
 
Clean coding-practices
Clean coding-practicesClean coding-practices
Clean coding-practices
 
Clean Code Principles
Clean Code PrinciplesClean Code Principles
Clean Code Principles
 
Code smell overview
Code smell overviewCode smell overview
Code smell overview
 

Ähnlich wie Clean Code

Graceful Failure with Selenium and Continuous Integration
Graceful Failure with Selenium and Continuous IntegrationGraceful Failure with Selenium and Continuous Integration
Graceful Failure with Selenium and Continuous IntegrationChris B. France
 
Joel Arvidsson, Klarna. What You See is What You Get – a case for visual testing
Joel Arvidsson, Klarna. What You See is What You Get – a case for visual testingJoel Arvidsson, Klarna. What You See is What You Get – a case for visual testing
Joel Arvidsson, Klarna. What You See is What You Get – a case for visual testingIT Arena
 
Mind the Gap - State of the Browser 2015
Mind the Gap - State of the Browser 2015Mind the Gap - State of the Browser 2015
Mind the Gap - State of the Browser 2015Christian Heilmann
 
Red teaming the CCDC
Red teaming the CCDCRed teaming the CCDC
Red teaming the CCDCscriptjunkie
 
Culture And Aesthetic Revisited
Culture And Aesthetic RevisitedCulture And Aesthetic Revisited
Culture And Aesthetic RevisitedAdam Keys
 
Cinci ug-january2011-anti-patterns
Cinci ug-january2011-anti-patternsCinci ug-january2011-anti-patterns
Cinci ug-january2011-anti-patternsSteven Smith
 
Zero to tested
Zero to testedZero to tested
Zero to testedMagenTys
 
Back to basics simple, elegant, beautiful code
Back to basics   simple, elegant, beautiful codeBack to basics   simple, elegant, beautiful code
Back to basics simple, elegant, beautiful codeAndrew Harcourt
 
Streams on top of scala - #lambdaCon
Streams on top of scala - #lambdaConStreams on top of scala - #lambdaCon
Streams on top of scala - #lambdaConQuentin Adam
 
Immutability - open source summit paris 2015
Immutability - open source summit paris 2015Immutability - open source summit paris 2015
Immutability - open source summit paris 2015Quentin Adam
 
You shouldneverdo
You shouldneverdoYou shouldneverdo
You shouldneverdodaniil3
 
Software Development Essential Skills
Software Development Essential SkillsSoftware Development Essential Skills
Software Development Essential SkillsJohn Choi
 
Chaos Engineering Without Observability ... Is Just Chaos
Chaos Engineering Without Observability ... Is Just ChaosChaos Engineering Without Observability ... Is Just Chaos
Chaos Engineering Without Observability ... Is Just ChaosCharity Majors
 
Automated Software Testing
Automated Software TestingAutomated Software Testing
Automated Software TestingRay Nicholus
 
Scottish Ruby Conference 2014
Scottish Ruby Conference  2014Scottish Ruby Conference  2014
Scottish Ruby Conference 2014michaelag1971
 

Ähnlich wie Clean Code (20)

Graceful Failure with Selenium and Continuous Integration
Graceful Failure with Selenium and Continuous IntegrationGraceful Failure with Selenium and Continuous Integration
Graceful Failure with Selenium and Continuous Integration
 
Tdd vs SQL
Tdd vs SQLTdd vs SQL
Tdd vs SQL
 
Joel Arvidsson, Klarna. What You See is What You Get – a case for visual testing
Joel Arvidsson, Klarna. What You See is What You Get – a case for visual testingJoel Arvidsson, Klarna. What You See is What You Get – a case for visual testing
Joel Arvidsson, Klarna. What You See is What You Get – a case for visual testing
 
Mind the Gap - State of the Browser 2015
Mind the Gap - State of the Browser 2015Mind the Gap - State of the Browser 2015
Mind the Gap - State of the Browser 2015
 
TxJS 2011
TxJS 2011TxJS 2011
TxJS 2011
 
Red teaming the CCDC
Red teaming the CCDCRed teaming the CCDC
Red teaming the CCDC
 
Culture And Aesthetic Revisited
Culture And Aesthetic RevisitedCulture And Aesthetic Revisited
Culture And Aesthetic Revisited
 
Good programming
Good programmingGood programming
Good programming
 
Cinci ug-january2011-anti-patterns
Cinci ug-january2011-anti-patternsCinci ug-january2011-anti-patterns
Cinci ug-january2011-anti-patterns
 
Zero to tested
Zero to testedZero to tested
Zero to tested
 
Back to basics simple, elegant, beautiful code
Back to basics   simple, elegant, beautiful codeBack to basics   simple, elegant, beautiful code
Back to basics simple, elegant, beautiful code
 
Clean Code
Clean CodeClean Code
Clean Code
 
Streams on top of scala - #lambdaCon
Streams on top of scala - #lambdaConStreams on top of scala - #lambdaCon
Streams on top of scala - #lambdaCon
 
Immutability - open source summit paris 2015
Immutability - open source summit paris 2015Immutability - open source summit paris 2015
Immutability - open source summit paris 2015
 
You shouldneverdo
You shouldneverdoYou shouldneverdo
You shouldneverdo
 
Minimalism
MinimalismMinimalism
Minimalism
 
Software Development Essential Skills
Software Development Essential SkillsSoftware Development Essential Skills
Software Development Essential Skills
 
Chaos Engineering Without Observability ... Is Just Chaos
Chaos Engineering Without Observability ... Is Just ChaosChaos Engineering Without Observability ... Is Just Chaos
Chaos Engineering Without Observability ... Is Just Chaos
 
Automated Software Testing
Automated Software TestingAutomated Software Testing
Automated Software Testing
 
Scottish Ruby Conference 2014
Scottish Ruby Conference  2014Scottish Ruby Conference  2014
Scottish Ruby Conference 2014
 

Mehr von Daniel Kummer

DIY IoT - The InfoDome
DIY IoT - The InfoDomeDIY IoT - The InfoDome
DIY IoT - The InfoDomeDaniel Kummer
 
Bluetooth LE - Dodging the Bullets
Bluetooth LE - Dodging the BulletsBluetooth LE - Dodging the Bullets
Bluetooth LE - Dodging the BulletsDaniel Kummer
 
Changing internet - where we come from where we go
Changing internet - where we come from where we goChanging internet - where we come from where we go
Changing internet - where we come from where we goDaniel Kummer
 
Git - Get Ready To Use It
Git - Get Ready To Use ItGit - Get Ready To Use It
Git - Get Ready To Use ItDaniel Kummer
 
Magnolia 4.5 Migration - Storytelling
Magnolia 4.5 Migration - StorytellingMagnolia 4.5 Migration - Storytelling
Magnolia 4.5 Migration - StorytellingDaniel Kummer
 
Code Kata Workshop - Improve your skills through deliberate practice
Code Kata Workshop - Improve your skills through deliberate practiceCode Kata Workshop - Improve your skills through deliberate practice
Code Kata Workshop - Improve your skills through deliberate practiceDaniel Kummer
 
BDD testing with cucumber
BDD testing with cucumberBDD testing with cucumber
BDD testing with cucumberDaniel Kummer
 
Git get-the-job-done
Git get-the-job-doneGit get-the-job-done
Git get-the-job-doneDaniel Kummer
 

Mehr von Daniel Kummer (12)

Git code reviews
Git code reviewsGit code reviews
Git code reviews
 
DIY IoT - The InfoDome
DIY IoT - The InfoDomeDIY IoT - The InfoDome
DIY IoT - The InfoDome
 
Bluetooth LE - Dodging the Bullets
Bluetooth LE - Dodging the BulletsBluetooth LE - Dodging the Bullets
Bluetooth LE - Dodging the Bullets
 
HTTP
HTTPHTTP
HTTP
 
Changing internet - where we come from where we go
Changing internet - where we come from where we goChanging internet - where we come from where we go
Changing internet - where we come from where we go
 
Git - Get Ready To Use It
Git - Get Ready To Use ItGit - Get Ready To Use It
Git - Get Ready To Use It
 
Magnolia 4.5 Migration - Storytelling
Magnolia 4.5 Migration - StorytellingMagnolia 4.5 Migration - Storytelling
Magnolia 4.5 Migration - Storytelling
 
Code Kata Workshop - Improve your skills through deliberate practice
Code Kata Workshop - Improve your skills through deliberate practiceCode Kata Workshop - Improve your skills through deliberate practice
Code Kata Workshop - Improve your skills through deliberate practice
 
BDD testing with cucumber
BDD testing with cucumberBDD testing with cucumber
BDD testing with cucumber
 
Git get-the-job-done
Git get-the-job-doneGit get-the-job-done
Git get-the-job-done
 
Git
GitGit
Git
 
Git! Why? How?
Git! Why? How?Git! Why? How?
Git! Why? How?
 

Kürzlich hochgeladen

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...apidays
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
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
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 

Kürzlich hochgeladen (20)

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
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
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 

Clean Code

  • 1. CLEAN CODE IT REALLY MATTERS BROUGHT TO YOU BY SENIOR SOFTWARE ENGINEER DANIEL KUMMER NAMICS AG ZÜRICH WWW.NAMICS.COM
  • 2. CLEAN CODE AGENDA ? WHY DOES IT EVEN MATTER? ! HOW CAN IT BE DONE? * BESIDE THE CODE…
  • 3. DEFINITION OF CLEAN CODE WORKS EFFICIENT READABLE EXTENDABLE CORRECTLY IT TELLS YOU A STORY SIMPLE TO UNDERSTAND IT DOES IT RIGHT EASY TO MODIFY CLEAN SOMEONE CARED
  • 4. ? WHY CARE   ”The only valid code metric is WTFs per minute” ~ A comic strip  
  • 5. SOFTWARE ENTROPY THE BROKEN WINDOW DON’T LIVE WITH BROKEN WINDOWS!
  • 6. IT’S ALL ABOUT $$$   THE COST OF BAD CODE TIME PRODUCTIVITY NERVES PROJECTS Mo$va$on  No  one  wants  to  take  it  
  • 7. DETECTING BAD CODE DEAD CODE UNUSED = UNNEEDED COMMENTS WHY NOT WHAT A FEW SMELLS DUPLICATES DON’T REPEAT YOURSELF CONDITIONAL LONG METHOD / SPECULATIVE COMPLEXITY PARAMETER LIST GENERALITY HARD TO TEST SHORTER = EASIER SOLVE TODAYS PROBLEM, NOT TOMORROWS A more complete list can be found at http://www.codinghorror.com/blog/2006/05/code-smells.html
  • 8. !   HOW TO DO IT “Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” ~Martin Fowler
  • 10. 1 WRITE DIRTY CODE FIRST, THEN CLEAN IT YOU CAN’T WRITE CLEAN CODE ON THE FIRST GO! YOUR FIRST ATTEMPT WON’T BE YOUR BEST SUCCESSIVE REFINEMENT IS KEY START WITH PSEUDOCODE
  • 11. 2 LEAVE IT CLEANER THAN YOU FOUND IT THE “BOYSCOUT” RULE INCREMENTIALISM – MAKE TINY CHANGES EVEN IF IT’S JUST REFORMATTING – YOU CARED YOU ARE TAKING RESPONSIBILITY
  • 14. NAMING CONVENTIONS REVEAL INTENTION – CLARITY IS KING no questions asked about the purpose LONG NAMES AREN’T BAD pick names corresponding to the scope ONE WORD PER CONCEPT / NOT AMBIGUOUS keep the lexicon consistent USE ENGLISH – DON’T USE YOUR LANGUAGE just don’t!
  • 15. CODING DRY - DON’T REPEAT YOURSELF duplication is a root of evil! DON’T OPTIMIZE PREMATURELY unless it’s really, really necessary DON’T SAVE ON CHARACTERS one liners are not faster, just unreadable DON’T TRY TO OUTSMART THE COMPILER really smart people are working on it…
  • 16. FUNCTIONS KISS - KEEP IT SIMPLE AND STUPID equals understandable DO ONE THING ONLY – AND DO IT WELL keep the level of abstraction to a minimum MINIMIZE ARGUMENTS – LESS IS MORE pursue it, as best as you can (think about testing)
  • 17. FUNCTIONS FLAGS ARE BAD prefer polymorphism SIDE EFFECTS ARE SCARY the function should do one thing EXTRACT ERROR HANDLING improve readability, don’t obscure logic RETURN ONCE ONLY – OR FAIL FAST keep the exit clear
  • 18. COMMENTING //drunk, fix later NEVER EXPLAIN WHAT CODE DOES explain WHY you did it this way DON’T COMMENT OUT CODE we have VCS for this! COMMENT STRANGE OR UNNATURAL CODE do it even if it’s not your own – explain why NO REDUNDANT OR MANDATED COMMENTS example: don’t document getters / setters
  • 19. FORMATTING DO IT LIKE THE NEWSPAPERS { TOP-DOWN PRINCIPLE MAIN FUNCTIONS ON TOP FOLLOWED BY CALLED FUNCTIONS } VERTICAL DENSITY WHAT BELONGS TOGETHER SHOULD BE TOGETHER NO HORIZONTAL ALIGNMENT DON’T ALIGN JUST FOR THE LOOKS
  • 20. OBJECTS AND CLASSES SRP – SINGLE RESPONSIBILITY PRINCIPLE one responsibility per object SMALL keep away from the “god”-classes ORGANIZE FOR CHANGE – USE INTERFACES it’s most likely going to happen LoD - LAW OF DEMETER (LOOSE COUPLING) a given object should assume as little as possible about the structure or properties of anything else
  • 21. ERROR HANDLING USE UNCHECKED EXCEPTIONS AND DOCUMENT THEM IN THE JAVADOC you decide when to handle them EXTRACT HANDLING BLOCKS (TRY/CATCH) don’t disturb the code flow DON’T RETURN NULL, DON’T PASS NULL avoid NULL checks and NPEs CONCENTRATE ON WHAT YOU CAN HANDLE not what to throw
  • 22. TEST YOUR CREATION SO YOU MAY SLEEP CALM AT NIGHT
  • 23. THE F.I.R.S.T PRINCIPLE FAST tests should run quick – or you won’t run them INDEPENDENT no test should depend on another test REPEATABLE in any environment, your laptop, the CI server… SELF-VALIDATING pass or fail - no manual action required TIMELY don’t write your tests too late, it only gets harder
  • 25. BESIDE CODE THE SATELLITES OF A SOFTWARE PROJECT  
  • 26. USE TOOLS COMBINE YOUR MAGIC HELPERS CONTINUOUS INTEGRATION EARLY PROBLEM RECOGNITION RELEASE MANAGEMENT SCM / VSC ISSUE MANAGEMENT CODE METRICS DEFINE YOUR RELEASE CYCLES TRACK DISCUSSIONS AND DESCISSIONS KEEP A CLEAR HISTORY HEURISTIC CODE ANALYSIS
  • 27. HUMAN FACTORS DON’T BE SHY… OTHERS HAVE THE SAME PROBLEMS YOU HAVE – EVEN PROS! REVIEW LEARN DISCUSS SHARE IMPROVE AND LEARN GUIDELINES / PROBLEMS / SOLUTIONS DELIBERATE PRACTICE THOUGHTS / TOOLS / IDEAS
  • 29. READING MATERIAL SOFTWARE CRAFTSMANSHIP CODE OF CONDUCT CLEAN YOUR CODE WITH COMMON SOLUTIONS