SlideShare a Scribd company logo
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!

More Related Content

Similar to 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 MAINTAINABLE
Gavin 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() 2017
Ortus Solutions, Corp
 
Software Craftmanship - Cours Polytech
Software Craftmanship - Cours PolytechSoftware Craftmanship - Cours Polytech
Software Craftmanship - Cours Polytech
yannick 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 scbcn17
Julio Martinez
 
Developing Better Software
Developing Better SoftwareDeveloping Better Software
Developing Better Software
Hean Hong Leong
 
Software Engineering Primer
Software Engineering PrimerSoftware Engineering Primer
Software Engineering Primer
Georg 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
 
WTF is TDD
WTF is TDDWTF is TDD
WTF is TDD
Steven Nunez
 
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 Festival
freshdatabos
 
Winning data science competitions
Winning data science competitionsWinning data science competitions
Winning data science competitions
Owen 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 projects
Tommy 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 v2
Federico Razzoli
 
Super lazy side projects - Hamik Mukelyan
Super lazy side projects - Hamik MukelyanSuper lazy side projects - Hamik Mukelyan
Super lazy side projects - Hamik Mukelyan
Drew Malone
 
Take Note of Note Taking
Take Note of Note TakingTake Note of Note Taking
Take Note of Note Taking
Karoline Stoltzenburg
 
Getting started contributing to Apache Spark
Getting started contributing to Apache SparkGetting started contributing to Apache Spark
Getting started contributing to Apache Spark
Holden 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 Theater
Webflow
 
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
Anton Dimitrov
 

Similar to 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
 

More from NeeleEilers

AWS KSS
AWS  KSSAWS  KSS
AWS KSS
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 Dummies
NeeleEilers
 
Machine Learning basics
Machine Learning basicsMachine Learning basics
Machine Learning basics
NeeleEilers
 
Don't Be A Square
Don't Be A SquareDon't Be A Square
Don't Be A Square
NeeleEilers
 
History of Remote Sensing
History of Remote SensingHistory of Remote Sensing
History of Remote Sensing
NeeleEilers
 

More from 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
 

Recently uploaded

GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
Zilliz
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
SOFTTECHHUB
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
Kumud Singh
 
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Vladimir Iglovikov, Ph.D.
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
Claudio Di Ciccio
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
DianaGray10
 

Recently uploaded (20)

GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
 
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
 

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!