English subtitles

← 01-20 Disabling Assertions

01-20 Disabling Assertions

Get Embed Code
1 Language

Showing Revision 2 created 07/16/2014 by Udacity Robot.

  1. One further thin that I want to talk about is
  2. do we disable assertions in production code?
  3. For example, when we run the Python interpreter with the "-O" option,
  4. which causes Python to enable some optimizations,
  5. it disables assertions in order to make our code run faster.
  6. The question we're asking here is is that a good thing?
  7. It turns out that there are arguments we can make in both directions.
  8. Let's first look at reasons why we might want to disable assertions.
  9. One of the main advantages of disabling assertions
  10. would be that it lets our code run faster.
  11. Another advantage--and here we're starting to get into something,
  12. which really depends on what we're trying to do with our system--
  13. is that code running without assertions is less likely to stop running.
  14. The code keeps going even after some sort of condition is found within the code
  15. that would have triggered an assertion violation
  16. if assertions had been enabled.
  17. Here we really have to ask ourselves the question
  18. what is it that we're trying to do with our system?
  19. Is it better to keep going or is it better to stop?
  20. Remember that keeping going after some condition is true
  21. that will lead to an assertion violation may lead to a completely erroneous execution.
  22. On the other hand, possibly, that's better than actually stopping.
  23. Now let's move to disadvantages of disabling assertions in production code.
  24. If our code happens to rely on some side effect performed by an assertion--
  25. that is to say, if whoever wrote the code that we're using has violated
  26. one of the rules for assertions that I gave you--
  27. then turning off assertions is going to break the code.
  28. Not only is it going to break the code, but it's going to break the code
  29. in an extremely confusing way, because lacking assertions,
  30. we're going to have a very hard time detecting the error
  31. except that it's probably going to cause our system to crash in some confusing way
  32. or just to do completely the wrong thing.
  33. This is a real risk of turning off assertions in large systems.
  34. The second reason is even in production code
  35. it may be better to fail early rather than keep going.
  36. Really, it just depends on what our system is doing.
  37. The question is do our users want the system to die
  38. or do they want the system to give them some completely wrong result?