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?

Code - Fu: Defensive Programming

  • 1.
    Code - Fu: DefensiveProgramming With great power comes great responsibility ~ Peter Paker’s Dad’s Brother Ben
  • 2.
    1980 A radiation therapymachine 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 Defensiveprogramming, 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-Fucont... 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 ofCode-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 ● Nevertrust 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 inTeams ● 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
  • 9.
  • 10.
    Check params +Encapsulation // ES5
  • 11.
    Programs need towork ● 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 Defensiveprogrammer you need to have a healthy dosage of paranoia
  • 13.
  • 14.
  • 15.