SlideShare uma empresa Scribd logo
1 de 45
Clean Code
Knowledge Sharing Session 2020-01-24
An example:
An example:
Idea
Task:
● Find out the amount of labels each labeller created after the 1st of January
2019 that have an area of more than 100m²
Gather
labels from
Database
Filter labels
by date
Filter labels
by area
Aggregate
labels by editor
Print results
Clean code looks like
prose text.
Disclaimer
● I don’t follow all rules in this presentation myself
○ But it’s good to know them
● Business team:
○ Learn: What is “good” code, what is “bad” code?
○ Learn: The every day struggle of a programmer
● Tech team:
○ Learn: How to structure your code
○ Learn: Rules to keep in mind next time you code something!
Rule 1: Naming
● Name things in a way that perfectly describes what they are
○ Be clear
○ Be unambiguous
○ Use one name for one concept
■ Don’t mix words!
■ If you started calling it “Geometry”, stick to it! Don’t call it “Polygon” somewhere else!
○ Avoid abbreviations/acronyms
■ Lbls -> Labels (Its really only 2 letters more!)
■ Cx_hull -> Convex_hull
Rule 1: Naming
a. Be clear
b. Be unambiguous
c. Use one name for one
concept
d. Avoid
abbreviations/acronyms
Rule 1: Naming
Rule 2: Formatting
● Format for readability
○ Empty lines where helpful
○ Avoid too long lines
○ Follow existing style guides (For python: PEP8)
Rule 2: Formatting
○ Empty lines where helpful
○ Avoid too long lines
○ Follow existing style
guides (For python: PEP8)
Rule 2: Formatting
Rule 3: Single level of abstraction
● All things that happen in a code block should use the same level of
abstraction
○ Example:
■ Don’t create a DB connection and filter Polygons by area in the same function!
○ Real world example:
■ Don’t compute the friction of the wheel of a car and define which pedal has to be
pressed to accelerate
● Instead:
○ Split into well-named functions!
Rule 3: Single level of
abstraction
● A database connection is
created
○ WIth all unnecessary details such
as the host and the login data!
● A geometry is reprojected and
its area is calculated
● Labels are grouped by editor
Rule 3: Single level of
abstraction
Rule 4: Single responsibility principle
● Each function should have one clearly defined responsibility
○ Good naming is essential
■ If your name uses the word “and”, you failed.
● Also known as
○ A function should only ever have one reason to change
Rule 4: Single responsibility principle
Rule 4: Single responsibility principle
Rule 5: Code for change
● We live in a terrible world where requirements keep changing
○ Keep this in mind while coding!
● Don’t use “magic numbers”
● Never ever ever ever hard-code credentials!
Rule 6: Group semantic concepts
● And name that group well!
● Avoids having too many variables
○ ⇒ Brain overload
● Don’t be afraid to create classes / named tuples / structs / whatever helps you
group things
● Also helps keeping all information in one point
Rule 6: Group semantic concepts
Rule 6: Group semantic concepts
Rule 7: Avoid mental mapping
● Be explicit!
● Don’t try to “remember” things!
Totally forgot
what this is
Rule 7: Avoid mental mapping
Rule 8: Don’t try to be clever
● Don’t search for the most clever solution
● Search for the simplest solution
● We spend more time reading code than writing code!
○ Make sure it is very easy to understand!
Rule 8: Don’t try to be clever
Weird python
thing that tries to
be smart
Rule 8: Don’t try to be clever
If we don’t know
that editor, this
is his first label!
Rule 9: Comments
● Comments should explain WHY things are happening.
● The code should explain WHAT is happening.
Thanks for repeating that
Annoying comment
repeating the code
If your code block is so
complicated it needs a
caption, why not make it a
function?
Rule 9: Comments
● Comments should explain WHY things are happening.
● The code should explain WHAT is happening.
Oh! That looked like a typo.
Good thing we have that
comment!
Great! Now I know where to
start optimizing
Rules Summary:
1. Name things well
2. Format your files for readability
3. Keep a single level of abstraction
4. Every block should have a single responsibility
5. Code for change
6. Group semantic concepts
7. Avoid mental mapping
8. Don’t try to be clever
9. Comments should explain WHY things are happening!
Let’s look at our code!
[ Switch to IDE here]
Rules Summary:
1. Name things well
2. Format your files for readability
3. Keep a single level of abstraction
4. Every block should have a single responsibility
5. Code for change
6. Group semantic concepts
7. Avoid mental mapping
8. Don’t try to be clever
9. Comments should explain WHY things are happening!
Last Minute “We’re on our way to the meeting”-request
● “Quick! Don’t filter by 100m², but 1000m²!”
○ Easy
● “Quick! Don’t filter by 100m², but 1000m²!”
○ Easy
Last Minute “We’re on our way to the meeting”-request
● “We just noticed that the labels of today are included! We don’t want them!”
○ Uhm, okay one second...
● “Quick! Don’t filter by 100m², but 1000m²!”
○ Easy
● “We just noticed that the labels of today are included! We don’t want them!”
○ Uhm, okay one second...
Last Minute “We’re on our way to the meeting”-request
● “Hey, we found this CSV with labels, can you use that instead of the DB?
Oh, and can you group by modification date instead of editor? And give us a
CSV with the results please. We need to show this to the customer”
○ Ehm… No.
Idea
Task:
● Find out the amount of labels each labeller created after the 1st of January
2019 that have an area of more than 100m²
Gather
labels from
Database
Filter labels
by date
Filter labels
by area
Aggregate
labels by editor
Print results
Idea
New task:
● Take labels from somewhere, group them by some criteria and filter them by
some arbitrary filter and then print the results somewhere
Gather
labels from
somewhere
Filter labels
somehow
Aggregate
labels by some
criteria
Output the
results
somewhere
So...
● We make sure we can define arbitrary data sources
● We want to be able to define arbitrary filters
● We want to be able to specify arbitrary output destinations
Let’s look at our code!
[ Switch to IDE here]
But wait! Now it has gotten super complicated again!
● Solving the “generic” problem always makes the code complicated
● Our implementation is super flexible now, but harder to understand
● Ask yourself: Do we need this flexibility? Will requirements change that often?
KISS =
Keep It Simple, Stupid!
Be pragmatic!
Thanks for listening!

Mais conteúdo relacionado

Semelhante a Clean Code

AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLEAN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLEGavin Pickin
 
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017Ortus Solutions, Corp
 
Software Craftmanship - Cours Polytech
Software Craftmanship - Cours PolytechSoftware Craftmanship - Cours Polytech
Software Craftmanship - Cours Polytechyannick grenzinger
 
"What we learned from 5 years of building a data science software that actual...
"What we learned from 5 years of building a data science software that actual..."What we learned from 5 years of building a data science software that actual...
"What we learned from 5 years of building a data science software that actual...Dataconomy Media
 
Customer segmentation scbcn17
Customer segmentation scbcn17Customer segmentation scbcn17
Customer segmentation scbcn17Julio Martinez
 
Developing Better Software
Developing Better SoftwareDeveloping Better Software
Developing Better SoftwareHean Hong Leong
 
Software Engineering Primer
Software Engineering PrimerSoftware Engineering Primer
Software Engineering PrimerGeorg Buske
 
Enterprise PHP Architecture through Design Patterns and Modularization (Midwe...
Enterprise PHP Architecture through Design Patterns and Modularization (Midwe...Enterprise PHP Architecture through Design Patterns and Modularization (Midwe...
Enterprise PHP Architecture through Design Patterns and Modularization (Midwe...Aaron Saray
 
How to deliver the right software (Specification by example)
How to deliver the right software (Specification by example)How to deliver the right software (Specification by example)
How to deliver the right software (Specification by example)Asier Barrenetxea
 
Winning Data Science Competitions (Owen Zhang) - 2014 Boston Data Festival
Winning Data Science Competitions (Owen Zhang)  - 2014 Boston Data FestivalWinning Data Science Competitions (Owen Zhang)  - 2014 Boston Data Festival
Winning Data Science Competitions (Owen Zhang) - 2014 Boston Data Festivalfreshdatabos
 
Winning data science competitions
Winning data science competitionsWinning data science competitions
Winning data science competitionsOwen Zhang
 
Taking the plunge: Why you should use new technology on client projects
Taking the plunge: Why you should use new technology on client projectsTaking the plunge: Why you should use new technology on client projects
Taking the plunge: Why you should use new technology on client projectsTommy Ferry
 
Beginner's Guide to Frontend Development: Comparing Angular, React, Ember, an...
Beginner's Guide to Frontend Development: Comparing Angular, React, Ember, an...Beginner's Guide to Frontend Development: Comparing Angular, React, Ember, an...
Beginner's Guide to Frontend Development: Comparing Angular, React, Ember, an...Prasid Pathak
 
How MySQL can boost (or kill) your application v2
How MySQL can boost (or kill) your application v2How MySQL can boost (or kill) your application v2
How MySQL can boost (or kill) your application v2Federico Razzoli
 
Super lazy side projects - Hamik Mukelyan
Super lazy side projects - Hamik MukelyanSuper lazy side projects - Hamik Mukelyan
Super lazy side projects - Hamik MukelyanDrew Malone
 
Getting started contributing to Apache Spark
Getting started contributing to Apache SparkGetting started contributing to Apache Spark
Getting started contributing to Apache SparkHolden Karau
 
No code stewardship - No Code Conf 2019 Demo Theater
No code stewardship - No Code Conf 2019 Demo TheaterNo code stewardship - No Code Conf 2019 Demo Theater
No code stewardship - No Code Conf 2019 Demo TheaterWebflow
 
Ace the Tech Interviews - www.hiredintech.com
Ace the Tech Interviews - www.hiredintech.comAce the Tech Interviews - www.hiredintech.com
Ace the Tech Interviews - www.hiredintech.comAnton Dimitrov
 

Semelhante a Clean Code (20)

AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLEAN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
 
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017
 
Software Craftmanship - Cours Polytech
Software Craftmanship - Cours PolytechSoftware Craftmanship - Cours Polytech
Software Craftmanship - Cours Polytech
 
"What we learned from 5 years of building a data science software that actual...
"What we learned from 5 years of building a data science software that actual..."What we learned from 5 years of building a data science software that actual...
"What we learned from 5 years of building a data science software that actual...
 
Customer segmentation scbcn17
Customer segmentation scbcn17Customer segmentation scbcn17
Customer segmentation scbcn17
 
Developing Better Software
Developing Better SoftwareDeveloping Better Software
Developing Better Software
 
Software Engineering Primer
Software Engineering PrimerSoftware Engineering Primer
Software Engineering Primer
 
Enterprise PHP Architecture through Design Patterns and Modularization (Midwe...
Enterprise PHP Architecture through Design Patterns and Modularization (Midwe...Enterprise PHP Architecture through Design Patterns and Modularization (Midwe...
Enterprise PHP Architecture through Design Patterns and Modularization (Midwe...
 
WTF is TDD
WTF is TDDWTF is TDD
WTF is TDD
 
How to deliver the right software (Specification by example)
How to deliver the right software (Specification by example)How to deliver the right software (Specification by example)
How to deliver the right software (Specification by example)
 
Winning Data Science Competitions (Owen Zhang) - 2014 Boston Data Festival
Winning Data Science Competitions (Owen Zhang)  - 2014 Boston Data FestivalWinning Data Science Competitions (Owen Zhang)  - 2014 Boston Data Festival
Winning Data Science Competitions (Owen Zhang) - 2014 Boston Data Festival
 
Winning data science competitions
Winning data science competitionsWinning data science competitions
Winning data science competitions
 
Taking the plunge: Why you should use new technology on client projects
Taking the plunge: Why you should use new technology on client projectsTaking the plunge: Why you should use new technology on client projects
Taking the plunge: Why you should use new technology on client projects
 
Beginner's Guide to Frontend Development: Comparing Angular, React, Ember, an...
Beginner's Guide to Frontend Development: Comparing Angular, React, Ember, an...Beginner's Guide to Frontend Development: Comparing Angular, React, Ember, an...
Beginner's Guide to Frontend Development: Comparing Angular, React, Ember, an...
 
How MySQL can boost (or kill) your application v2
How MySQL can boost (or kill) your application v2How MySQL can boost (or kill) your application v2
How MySQL can boost (or kill) your application v2
 
Super lazy side projects - Hamik Mukelyan
Super lazy side projects - Hamik MukelyanSuper lazy side projects - Hamik Mukelyan
Super lazy side projects - Hamik Mukelyan
 
Take Note of Note Taking
Take Note of Note TakingTake Note of Note Taking
Take Note of Note Taking
 
Getting started contributing to Apache Spark
Getting started contributing to Apache SparkGetting started contributing to Apache Spark
Getting started contributing to Apache Spark
 
No code stewardship - No Code Conf 2019 Demo Theater
No code stewardship - No Code Conf 2019 Demo TheaterNo code stewardship - No Code Conf 2019 Demo Theater
No code stewardship - No Code Conf 2019 Demo Theater
 
Ace the Tech Interviews - www.hiredintech.com
Ace the Tech Interviews - www.hiredintech.comAce the Tech Interviews - www.hiredintech.com
Ace the Tech Interviews - www.hiredintech.com
 

Mais de NeeleEilers

To infinity,...... and beyond
To infinity,...... and beyond To infinity,...... and beyond
To infinity,...... and beyond NeeleEilers
 
Satellites for Dummies
Satellites for DummiesSatellites for Dummies
Satellites for DummiesNeeleEilers
 
Machine Learning basics
Machine Learning basicsMachine Learning basics
Machine Learning basicsNeeleEilers
 
Don't Be A Square
Don't Be A SquareDon't Be A Square
Don't Be A SquareNeeleEilers
 
History of Remote Sensing
History of Remote SensingHistory of Remote Sensing
History of Remote SensingNeeleEilers
 

Mais de NeeleEilers (6)

AWS KSS
AWS  KSSAWS  KSS
AWS KSS
 
To infinity,...... and beyond
To infinity,...... and beyond To infinity,...... and beyond
To infinity,...... and beyond
 
Satellites for Dummies
Satellites for DummiesSatellites for Dummies
Satellites for Dummies
 
Machine Learning basics
Machine Learning basicsMachine Learning basics
Machine Learning basics
 
Don't Be A Square
Don't Be A SquareDon't Be A Square
Don't Be A Square
 
History of Remote Sensing
History of Remote SensingHistory of Remote Sensing
History of Remote Sensing
 

Último

Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
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
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
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
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
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
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 

Último (20)

Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
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
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
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
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
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
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 

Clean Code

  • 1. Clean Code Knowledge Sharing Session 2020-01-24
  • 4. Idea Task: ● Find out the amount of labels each labeller created after the 1st of January 2019 that have an area of more than 100m² Gather labels from Database Filter labels by date Filter labels by area Aggregate labels by editor Print results
  • 5. Clean code looks like prose text.
  • 6. Disclaimer ● I don’t follow all rules in this presentation myself ○ But it’s good to know them ● Business team: ○ Learn: What is “good” code, what is “bad” code? ○ Learn: The every day struggle of a programmer ● Tech team: ○ Learn: How to structure your code ○ Learn: Rules to keep in mind next time you code something!
  • 7. Rule 1: Naming ● Name things in a way that perfectly describes what they are ○ Be clear ○ Be unambiguous ○ Use one name for one concept ■ Don’t mix words! ■ If you started calling it “Geometry”, stick to it! Don’t call it “Polygon” somewhere else! ○ Avoid abbreviations/acronyms ■ Lbls -> Labels (Its really only 2 letters more!) ■ Cx_hull -> Convex_hull
  • 8. Rule 1: Naming a. Be clear b. Be unambiguous c. Use one name for one concept d. Avoid abbreviations/acronyms
  • 10. Rule 2: Formatting ● Format for readability ○ Empty lines where helpful ○ Avoid too long lines ○ Follow existing style guides (For python: PEP8)
  • 11. Rule 2: Formatting ○ Empty lines where helpful ○ Avoid too long lines ○ Follow existing style guides (For python: PEP8)
  • 13. Rule 3: Single level of abstraction ● All things that happen in a code block should use the same level of abstraction ○ Example: ■ Don’t create a DB connection and filter Polygons by area in the same function! ○ Real world example: ■ Don’t compute the friction of the wheel of a car and define which pedal has to be pressed to accelerate ● Instead: ○ Split into well-named functions!
  • 14. Rule 3: Single level of abstraction ● A database connection is created ○ WIth all unnecessary details such as the host and the login data! ● A geometry is reprojected and its area is calculated ● Labels are grouped by editor
  • 15. Rule 3: Single level of abstraction
  • 16. Rule 4: Single responsibility principle ● Each function should have one clearly defined responsibility ○ Good naming is essential ■ If your name uses the word “and”, you failed. ● Also known as ○ A function should only ever have one reason to change
  • 17. Rule 4: Single responsibility principle
  • 18. Rule 4: Single responsibility principle
  • 19. Rule 5: Code for change ● We live in a terrible world where requirements keep changing ○ Keep this in mind while coding! ● Don’t use “magic numbers” ● Never ever ever ever hard-code credentials!
  • 20.
  • 21.
  • 22. Rule 6: Group semantic concepts ● And name that group well! ● Avoids having too many variables ○ ⇒ Brain overload ● Don’t be afraid to create classes / named tuples / structs / whatever helps you group things ● Also helps keeping all information in one point
  • 23. Rule 6: Group semantic concepts
  • 24. Rule 6: Group semantic concepts
  • 25. Rule 7: Avoid mental mapping ● Be explicit! ● Don’t try to “remember” things! Totally forgot what this is
  • 26. Rule 7: Avoid mental mapping
  • 27. Rule 8: Don’t try to be clever ● Don’t search for the most clever solution ● Search for the simplest solution ● We spend more time reading code than writing code! ○ Make sure it is very easy to understand!
  • 28. Rule 8: Don’t try to be clever Weird python thing that tries to be smart
  • 29. Rule 8: Don’t try to be clever If we don’t know that editor, this is his first label!
  • 30. Rule 9: Comments ● Comments should explain WHY things are happening. ● The code should explain WHAT is happening. Thanks for repeating that Annoying comment repeating the code If your code block is so complicated it needs a caption, why not make it a function?
  • 31. Rule 9: Comments ● Comments should explain WHY things are happening. ● The code should explain WHAT is happening. Oh! That looked like a typo. Good thing we have that comment! Great! Now I know where to start optimizing
  • 32. Rules Summary: 1. Name things well 2. Format your files for readability 3. Keep a single level of abstraction 4. Every block should have a single responsibility 5. Code for change 6. Group semantic concepts 7. Avoid mental mapping 8. Don’t try to be clever 9. Comments should explain WHY things are happening!
  • 33. Let’s look at our code! [ Switch to IDE here]
  • 34. Rules Summary: 1. Name things well 2. Format your files for readability 3. Keep a single level of abstraction 4. Every block should have a single responsibility 5. Code for change 6. Group semantic concepts 7. Avoid mental mapping 8. Don’t try to be clever 9. Comments should explain WHY things are happening!
  • 35. Last Minute “We’re on our way to the meeting”-request ● “Quick! Don’t filter by 100m², but 1000m²!” ○ Easy
  • 36. ● “Quick! Don’t filter by 100m², but 1000m²!” ○ Easy Last Minute “We’re on our way to the meeting”-request ● “We just noticed that the labels of today are included! We don’t want them!” ○ Uhm, okay one second...
  • 37. ● “Quick! Don’t filter by 100m², but 1000m²!” ○ Easy ● “We just noticed that the labels of today are included! We don’t want them!” ○ Uhm, okay one second... Last Minute “We’re on our way to the meeting”-request ● “Hey, we found this CSV with labels, can you use that instead of the DB? Oh, and can you group by modification date instead of editor? And give us a CSV with the results please. We need to show this to the customer” ○ Ehm… No.
  • 38. Idea Task: ● Find out the amount of labels each labeller created after the 1st of January 2019 that have an area of more than 100m² Gather labels from Database Filter labels by date Filter labels by area Aggregate labels by editor Print results
  • 39. Idea New task: ● Take labels from somewhere, group them by some criteria and filter them by some arbitrary filter and then print the results somewhere Gather labels from somewhere Filter labels somehow Aggregate labels by some criteria Output the results somewhere
  • 40. So... ● We make sure we can define arbitrary data sources ● We want to be able to define arbitrary filters ● We want to be able to specify arbitrary output destinations
  • 41. Let’s look at our code! [ Switch to IDE here]
  • 42. But wait! Now it has gotten super complicated again! ● Solving the “generic” problem always makes the code complicated ● Our implementation is super flexible now, but harder to understand ● Ask yourself: Do we need this flexibility? Will requirements change that often?
  • 43. KISS = Keep It Simple, Stupid!