Posts Tagged ‘Defensive Programming’
Releasing 100% bug free software is nearly impossible (I am talking about some real functionalities), but you can always code your software in a way that when that bug surfaces it handle the situation gracefully.
There are some simple rules to be followed in order to do defensive programming:
Ok this is straight; you have to handle every possible code block as you can. Believe me this will be a life saver.
Validate external input
Think of your user as a most stupid person on earth and guard your data entry screen with all possible (reasonable) validators at the same time not to annoy a smart user.
Test your code
This means you have to test your code for what it meant for (business point of view). Best way is, before you even start coding write down your test cases and once you complete the code test through them. Always test with good amount of data not just with 2-3 records in database.
Maintain a error log
This is very useful in live environment where actual users face the errors and they usually don’t report error in a way you want them to. So whenever your application encounters an error it should log the full error message with location in error-log rather than displaying it to user (error log can be a text file, xml or database), of course user can be displayed a friendly error message.
Be proactive, check the error log regularly and fix the important errors before user report them.
Before you deploy your application to live environment, form a suitable backup policy according to usage of the application. Apply the backup policy, rehearse it (it’s needed; I have seen many backups which never restored when needed after crash just because they were created in a wrong way).
It’s not enough
It will be never enough defensive, so keep thinking new ways which more applies to your context. Get ideas from others on how they are doing to defend their apps.