Defensive design

<< Previous: Programming fundamentals test Next: Testing >>

Defensive design

Defensive design is writing programs to try to avoid problems from accidental or deliberate misuse.



Handling unexpected inputs

A program should be designed so that a user entering an unexpected data type, such as a word where an integer is required, would not crash the program. In Python, this can be achieved with the TRY...EXCEPT statement.



Authentication

This refers to having a system of username and passwords to confirm the identity of the user of a program. It also allows different access rights so users can do different things within a program. Any authentication system should be resistant to brute-force attacks and hacking.



Sanitisation

Sanitisation is the removal of unwanted characters from input data. This is one way we protect against SQL injection.



Validation

Validation is making sure that input data is likely to be valid. There are a number of types of validation:

Maintainability

Most programs are large and worked on by more than one programmer and may well be maintained by different people so there are a number of things that should be done to make the process simpler.

Using sub-programs

This means the program will be easier to understand. Changes will only need to be made to individual sub-programs.

Naming conventions

As well as basics like no spaces in names, starting with a letter or underscore, and using meaningful names, programmers often use naming conventions. Constants are generally written in all capitals. For regular variables either snake case or camel case is usually used. Using all lowercase amd underscores to seperate words is snake_case. Starting with lowercase and using a capital for the first letter of each new word.

Indentation and spacing

As well as basics like no spaces in names, starting with a letter or underscore, and using meaningful names, programmers often use naming conventions. Constants are generally written in all capitals. For regular variables either snake case or camel case is usually used.

Commenting

Commenting means leaving notes in your code to explain how it works. It is increasingly important in large programs where the person who writes the code may not be the one to maintain it.

Why maintainability matters

It is quite likely that code you write will end up being maintained by someone else. This is why there is an old saying in programming - Always code as though the person who maintains your code will be a homicidal axe-wielding sociopath that knows where you live.



Knowledge check


Questions:
Correct:

Question text


<< Previous: Programming fundamentals test Next: Testing >>

© All materials created by and copyright S.Goff