Last active
June 27, 2023 18:19
-
-
Save Jbot29/076266738ce6418d1e85e8d271b821a3 to your computer and use it in GitHub Desktop.
Revisions
-
Jbot29 revised this gist
Jun 27, 2023 . 1 changed file with 26 additions and 17 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,42 +1,51 @@ # Defensive programming ## Intro There are times when working on critical or complex systems where issues manifest but it is difficult to pin down the source. A set of code may write data and succeed with no issues but then another pieces comes along to read and finds that the data is wrong or incompatible. That is not horrific but when there are several actors it can be difficult to test all those scenerios and when things go south figure out which part introduced the issue. Functional tests can help but with complex system it is impossible to create test cases for all scenerios. One way I have found useful for this class of issues is called defensive programming. ## What is it? The basic idea is that at runtime parts of the code are aggressive that things are as expected. This is more than about typing. It is about validating from a logic perspective things are correct. This can be many things from checking that a value is in a certain range, maybe has a certain structure, even validating update operations return the what is expected, and so forth. Once something is a miss it is the job of that code to shut it down. By identifying things and breaking once found it is easier to find the chain that caused it. Often some code introduces a problem but only until it complete breaks things does an issue occur and it is hard to trace which part of the code intiated it. With defensive programming the idea is that your code trusts nothing it gets and nopes out if it gets something it doesn't expect or like. With the hope that you short circuit difficult hard to trace bugs. ### Other benefits Outside of just stopping the train wreck, I find that using this style of programming really makes it easy to understand a piece of code. To me defensive programming are like pratical comments. It also forces you to think and codify your assumptions. ## Practice ### Asserts. In Python this is easy to add just by using the assert statement. assert isinstance(input_param, int) assert input_param in range(0,5) This document has a great list of different ways to use asserts. https://python.plainenglish.io/defensive-programming-in-python-af0266e65dfd ### Other tools Tools like Pydantic can also handle certain classes of defensive programming like data validation. https://docs.pydantic.dev/latest/ -
Jbot29 revised this gist
Jun 13, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,7 +9,7 @@ One way I have found useful for this class of issues is called defensive program The basic idea is that at runtime parts of the code are aggressive that the data passed into it is correct. This is more than about typing. It is about validating from a logic perspective things are correct. By identifying things and breaking once found it is easier to find the chain that caused it. Often some code introduces a problem but only until it complete breaks things does an issue occur and it is hard to trace which part of the code intiated it. The problem with some systems is the change of events. Garbage flows downhill but it is is hard to tell which level introduced the breakage. -
Jbot29 revised this gist
Jun 13, 2023 . 1 changed file with 4 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,8 +1,10 @@ # Defensive programming There are complicated code pieces that all interlock with each other and it is easy for bugs to occur. Often these things are difficult to test for since it is often chains of operations that need to happen and difficult to replicate in a testing environment, but always seem to pop up in production. Functional tests can help but with complex system it is impossible to create test cases for all scenerios. One way I have found useful for this class of issues is called defensive programming. The basic idea is that at runtime parts of the code are aggressive that the data passed into it is correct. This is more than about typing. It is about validating from a logic perspective things are correct. -
Jbot29 created this gist
Jun 13, 2023 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,40 @@ Defensive programming. There are complicated code pieces that all interlock with each other and it is easy for bugs to occur. Often these things are also difficult to test for since it is often chains of operations that need to happen and difficult to replicate in a testing environment, but always seem to pop up in production. One way I have found useful for this class of issues is called defensive programming. The basic idea is that at runtime parts of the code are aggressive that the data passed into it is correct. This is more than about typing. It is about validating from a logic perspective things are correct. By identifying things and breaking there it is easier to find the chain that caused it. Often some code introduces a problem but only until it complete breaks things does an issue occur and it is hard to trace which part of the code intiated it. The problem with some systems is the change of events. Garbage flows downhill but it is is hard to tell which level introduced the breakage. With defensive programming the idea is that your code trusts nothing it gets and nopes out if it gets something it doesn't expect or like. With the hope that you short circuit difficult hard to trace bugs. https://python.plainenglish.io/defensive-programming-in-python-af0266e65dfd Benefits: Like a mix of live test cases, documents the expectations of the code Stopping errors from propagating down the stack. https://docs.pydantic.dev/latest/ The best form of comments forces you to think and codify your assumptions Stack layer Each layer is just compounding earlier problems. Tests are great and we should have more, but the honest truth is they will never replicated the complexity of a real env with people doing crazy shit. Use typing, pydantic and asserts in critical areas of the code.