SlideShare a Scribd company logo
Code- Fu:
Defensive Programming
With great power comes great responsibility ~ Peter Paker’s Dad’s Brother Ben
1980
A radiation therapy machine
was directly responsible for
at least 5 patient deaths
1996
A US$1 billion exploded
40 seconds after takeoff
2012
Apple Maps gives us
directions to nowhere
2016
ABSA bank double
charges clients
Why learn another design pattern?
Because: BUGS!
Intro to Code-Fu
Defensive programming, simply
put, is programming with the intent
to anticipate likely failure points.
Defensive programming practices
are often used where high
reliability, safety or security is
needed.
Intro to Code-Fu cont...
Defensive programming aims to improve software and source code, in terms
of:
1. General quality (reduce bugs)
2. Comprehensive code
a. Clean
b. Readable
c. Understandable
d. Reliable
e. Maintainable
f. Testable
g. Robustness
3. Predictable behaviour
Mastering the
ways of Code-Fu
Defensive programming is really
programming for the real world
where if anything can go wrong it
will.
Real world programs
1. Are not in isolation, you can expect user input or external input
1. Are built & maintained by teams and the individuals making the team
can change over time (imagine open source projects)
1. Need to work
External/User input
● Never trust external/user input
● Remember there are also users with malicious intent
What helps
● Protect your code from invalid data (data validation)
● User whitelists not blacklists
● Use database abstraction (e.g. PDO for PHP)
We work in Teams
● Trust your fellow developers but DON’T trust their code
● Defensive Programming is much like Defensive Driving assume that
everyone around us can potentially and possibly make mistakes.
● Teams can consists of individuals working from different planets
What helps
● Enforcing coding standards (naming conversions included)
● Write SOLID code (Encapsulation)
● Write code for the maintainer (The maintainer can be future you)
● Parameter checks at the beginning of a function/method
● Unit tests, Regression testing, Integration tests
Bad programming
// ES5
Check params + Encapsulation
// ES5
Programs need to work
● Imagine paying R200 per month for Software that works 30% of the time
What helps
● Add diagnostic code, logging and tracing (Bugsnag/Sentry)
● Security tests
As a Defensive programmer
you need to have a healthy
dosage of paranoia
Too much Paranoia
Good/Bad?
QUESTIONS?

More Related Content

Similar to Code - Fu: Defensive Programming

PROBLEM SOLVING
PROBLEM SOLVINGPROBLEM SOLVING
PROBLEM SOLVING
shahzadebaujiti
 
Python_Module_1.pdf
Python_Module_1.pdfPython_Module_1.pdf
Python_Module_1.pdf
R.K.College of engg & Tech
 
Stack-Based Buffer Overflows
Stack-Based Buffer OverflowsStack-Based Buffer Overflows
Stack-Based Buffer Overflows
Daniel Tumser
 
Agile Secure Development
Agile Secure DevelopmentAgile Secure Development
Agile Secure Development
Bosnia Agile
 
Stnotes doc 5
Stnotes doc 5Stnotes doc 5
Stnotes doc 5
Alok Jain
 
The pragmatic programmer
The pragmatic programmerThe pragmatic programmer
The pragmatic programmer
LeylimYaln
 
Rooted con 2020 - from the heaven to hell in the CI - CD
Rooted con 2020 - from the heaven to hell in the CI - CDRooted con 2020 - from the heaven to hell in the CI - CD
Rooted con 2020 - from the heaven to hell in the CI - CD
Daniel Garcia (a.k.a cr0hn)
 
Qualidade de Software em zOS usando IBM Debug Tool e RDz
Qualidade de Software em zOS usando IBM Debug Tool e RDzQualidade de Software em zOS usando IBM Debug Tool e RDz
Qualidade de Software em zOS usando IBM Debug Tool e RDz
Paulo Batuta
 
Computer Hardware And Software Types, Functions, Difference between Hardware ...
Computer Hardware And Software Types, Functions, Difference between Hardware ...Computer Hardware And Software Types, Functions, Difference between Hardware ...
Computer Hardware And Software Types, Functions, Difference between Hardware ...
sunil shukla
 
Introduction to system programming
Introduction to system programmingIntroduction to system programming
Introduction to system programming
sonalikharade3
 
Defect Tracking Software Project Presentation
Defect Tracking Software Project PresentationDefect Tracking Software Project Presentation
Defect Tracking Software Project Presentation
Shiv Prakash
 
grade 10 2023.pptx
grade 10 2023.pptxgrade 10 2023.pptx
grade 10 2023.pptx
RaymartHerera
 
Compilers programmingembedded
Compilers programmingembeddedCompilers programmingembedded
Compilers programmingembedded
Manish Pandey
 
Testingfor Sw Security
Testingfor Sw SecurityTestingfor Sw Security
Testingfor Sw Security
ankitmehta21
 
Year Zero
Year ZeroYear Zero
Year Zero
leifdreizler
 
10 Things You Probably Should Have Learned With Your Computer Science Degree....
10 Things You Probably Should Have Learned With Your Computer Science Degree....10 Things You Probably Should Have Learned With Your Computer Science Degree....
10 Things You Probably Should Have Learned With Your Computer Science Degree....
New Relic
 
Best practices in coding for beginners
Best practices in coding for beginnersBest practices in coding for beginners
Best practices in coding for beginners
Miguel González-Fierro
 
Computer system
Computer systemComputer system
Computer system
RajThakuri
 
Security overview 2
Security overview 2Security overview 2
Security overview 2
CMR WORLD TECH
 
IRJET - V-IDE: Voice Controlled IDE using Natural Language Processing and...
IRJET -  	  V-IDE: Voice Controlled IDE using Natural Language Processing and...IRJET -  	  V-IDE: Voice Controlled IDE using Natural Language Processing and...
IRJET - V-IDE: Voice Controlled IDE using Natural Language Processing and...
IRJET Journal
 

Similar to Code - Fu: Defensive Programming (20)

PROBLEM SOLVING
PROBLEM SOLVINGPROBLEM SOLVING
PROBLEM SOLVING
 
Python_Module_1.pdf
Python_Module_1.pdfPython_Module_1.pdf
Python_Module_1.pdf
 
Stack-Based Buffer Overflows
Stack-Based Buffer OverflowsStack-Based Buffer Overflows
Stack-Based Buffer Overflows
 
Agile Secure Development
Agile Secure DevelopmentAgile Secure Development
Agile Secure Development
 
Stnotes doc 5
Stnotes doc 5Stnotes doc 5
Stnotes doc 5
 
The pragmatic programmer
The pragmatic programmerThe pragmatic programmer
The pragmatic programmer
 
Rooted con 2020 - from the heaven to hell in the CI - CD
Rooted con 2020 - from the heaven to hell in the CI - CDRooted con 2020 - from the heaven to hell in the CI - CD
Rooted con 2020 - from the heaven to hell in the CI - CD
 
Qualidade de Software em zOS usando IBM Debug Tool e RDz
Qualidade de Software em zOS usando IBM Debug Tool e RDzQualidade de Software em zOS usando IBM Debug Tool e RDz
Qualidade de Software em zOS usando IBM Debug Tool e RDz
 
Computer Hardware And Software Types, Functions, Difference between Hardware ...
Computer Hardware And Software Types, Functions, Difference between Hardware ...Computer Hardware And Software Types, Functions, Difference between Hardware ...
Computer Hardware And Software Types, Functions, Difference between Hardware ...
 
Introduction to system programming
Introduction to system programmingIntroduction to system programming
Introduction to system programming
 
Defect Tracking Software Project Presentation
Defect Tracking Software Project PresentationDefect Tracking Software Project Presentation
Defect Tracking Software Project Presentation
 
grade 10 2023.pptx
grade 10 2023.pptxgrade 10 2023.pptx
grade 10 2023.pptx
 
Compilers programmingembedded
Compilers programmingembeddedCompilers programmingembedded
Compilers programmingembedded
 
Testingfor Sw Security
Testingfor Sw SecurityTestingfor Sw Security
Testingfor Sw Security
 
Year Zero
Year ZeroYear Zero
Year Zero
 
10 Things You Probably Should Have Learned With Your Computer Science Degree....
10 Things You Probably Should Have Learned With Your Computer Science Degree....10 Things You Probably Should Have Learned With Your Computer Science Degree....
10 Things You Probably Should Have Learned With Your Computer Science Degree....
 
Best practices in coding for beginners
Best practices in coding for beginnersBest practices in coding for beginners
Best practices in coding for beginners
 
Computer system
Computer systemComputer system
Computer system
 
Security overview 2
Security overview 2Security overview 2
Security overview 2
 
IRJET - V-IDE: Voice Controlled IDE using Natural Language Processing and...
IRJET -  	  V-IDE: Voice Controlled IDE using Natural Language Processing and...IRJET -  	  V-IDE: Voice Controlled IDE using Natural Language Processing and...
IRJET - V-IDE: Voice Controlled IDE using Natural Language Processing and...
 

Recently uploaded

GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
Green Software Development
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
brainerhub1
 
SMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API ServiceSMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API Service
Yara Milbes
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
Sven Peters
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
Octavian Nadolu
 
Revolutionizing Visual Effects Mastering AI Face Swaps.pdf
Revolutionizing Visual Effects Mastering AI Face Swaps.pdfRevolutionizing Visual Effects Mastering AI Face Swaps.pdf
Revolutionizing Visual Effects Mastering AI Face Swaps.pdf
Undress Baby
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Julian Hyde
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
Peter Muessig
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
Aftab Hussain
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
Aftab Hussain
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
Rakesh Kumar R
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
Rakesh Kumar R
 
socradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdfsocradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdf
SOCRadar
 
What is Augmented Reality Image Tracking
What is Augmented Reality Image TrackingWhat is Augmented Reality Image Tracking
What is Augmented Reality Image Tracking
pavan998932
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
Remote DBA Services
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Łukasz Chruściel
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
Quickdice ERP
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
Grant Fritchey
 

Recently uploaded (20)

GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
 
SMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API ServiceSMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API Service
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
 
Revolutionizing Visual Effects Mastering AI Face Swaps.pdf
Revolutionizing Visual Effects Mastering AI Face Swaps.pdfRevolutionizing Visual Effects Mastering AI Face Swaps.pdf
Revolutionizing Visual Effects Mastering AI Face Swaps.pdf
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
 
socradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdfsocradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdf
 
What is Augmented Reality Image Tracking
What is Augmented Reality Image TrackingWhat is Augmented Reality Image Tracking
What is Augmented Reality Image Tracking
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
 

Code - Fu: Defensive Programming

  • 1. Code- Fu: Defensive Programming With great power comes great responsibility ~ Peter Paker’s Dad’s Brother Ben
  • 2. 1980 A radiation therapy machine was directly responsible for at least 5 patient deaths 1996 A US$1 billion exploded 40 seconds after takeoff 2012 Apple Maps gives us directions to nowhere 2016 ABSA bank double charges clients Why learn another design pattern? Because: BUGS!
  • 3. Intro to Code-Fu Defensive programming, simply put, is programming with the intent to anticipate likely failure points. Defensive programming practices are often used where high reliability, safety or security is needed.
  • 4. Intro to Code-Fu cont... Defensive programming aims to improve software and source code, in terms of: 1. General quality (reduce bugs) 2. Comprehensive code a. Clean b. Readable c. Understandable d. Reliable e. Maintainable f. Testable g. Robustness 3. Predictable behaviour
  • 5. Mastering the ways of Code-Fu Defensive programming is really programming for the real world where if anything can go wrong it will.
  • 6. Real world programs 1. Are not in isolation, you can expect user input or external input 1. Are built & maintained by teams and the individuals making the team can change over time (imagine open source projects) 1. Need to work
  • 7. External/User input ● Never trust external/user input ● Remember there are also users with malicious intent What helps ● Protect your code from invalid data (data validation) ● User whitelists not blacklists ● Use database abstraction (e.g. PDO for PHP)
  • 8. We work in Teams ● Trust your fellow developers but DON’T trust their code ● Defensive Programming is much like Defensive Driving assume that everyone around us can potentially and possibly make mistakes. ● Teams can consists of individuals working from different planets What helps ● Enforcing coding standards (naming conversions included) ● Write SOLID code (Encapsulation) ● Write code for the maintainer (The maintainer can be future you) ● Parameter checks at the beginning of a function/method ● Unit tests, Regression testing, Integration tests
  • 10. Check params + Encapsulation // ES5
  • 11. Programs need to work ● Imagine paying R200 per month for Software that works 30% of the time What helps ● Add diagnostic code, logging and tracing (Bugsnag/Sentry) ● Security tests
  • 12. As a Defensive programmer you need to have a healthy dosage of paranoia