SlideShare ist ein Scribd-Unternehmen logo
1 von 45
Downloaden Sie, um offline zu lesen
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!
Clean Code
Clean Code
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!

Weitere ähnliche Inhalte

Ähnlich wie 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
 

Ähnlich wie 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
 

Mehr von 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
 

Mehr von 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
 

Kürzlich hochgeladen

How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?IES VE
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1DianaGray10
 
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...Aggregage
 
VoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXVoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXTarek Kalaji
 
Building AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxBuilding AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxUdaiappa Ramachandran
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URLRuncy Oommen
 
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesAI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesMd Hossain Ali
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaborationbruanjhuli
 
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfJamie (Taka) Wang
 
9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding TeamAdam Moalla
 
Bird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemBird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemAsko Soukka
 
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration WorkflowsIgniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration WorkflowsSafe Software
 
UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7DianaGray10
 
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IES VE
 
Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Commit University
 
Introduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxIntroduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxMatsuo Lab
 
Computer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsComputer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsSeth Reyes
 
OpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureOpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureEric D. Schabell
 

Kürzlich hochgeladen (20)

How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
 
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
 
VoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXVoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBX
 
Building AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxBuilding AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptx
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URL
 
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesAI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
 
201610817 - edge part1
201610817 - edge part1201610817 - edge part1
201610817 - edge part1
 
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
 
9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team
 
Bird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemBird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystem
 
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration WorkflowsIgniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
 
20230104 - machine vision
20230104 - machine vision20230104 - machine vision
20230104 - machine vision
 
UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7
 
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
 
Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)
 
Introduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxIntroduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptx
 
Computer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsComputer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and Hazards
 
OpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureOpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability Adventure
 

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!
  • 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!