English subtitles

← Beware the Time Bomb - Software Debugging

Get Embed Code
4 Languages

Showing Revision 3 created 05/25/2016 by Udacity Robot.

  1. What you see here is an instance of a problem that's commonly known as a time bomb.
  2. A time bomb is infection in the code that's just waiting to explode into your face as a failure.
  3. Here's something that's definitely wrong and it's in there and it's sleeping.
  4. It can be there for million of cycles.
  5. Only when it's being accessed or processed that's when the time bomb explodes
  6. and this is hard to debug because you have to figure out
  7. where the time bomb originally was planted and set -- that's where assertions save the day.
  8. An assertion prohibits time bomb by checking whether the data is sane
  9. at the very moment it is being stored.
  10. When we are creating a timedobject, we could for instance use assertions
  11. to make sure that the arguments actually are all within specified ranges.
  12. For instance, we could say the hour must be between 0 and 23.
  13. At the same time, we give a hint to the user that time uses 24-hour format.
  14. We also want to make sure that the minutes and the seconds are within the proper ranges
  15. between 0 and 59, respectively.
  16. This special syntax you see over here is a speciality of Python
  17. which allows to coerce multiple comparisons into one.
  18. Actually now that I think of it, 59 is not correct here.
  19. There's years in which there's leap seconds and if there's a leap second,
  20. then there can actually be 61 seconds in a minute.
  21. So, in order to be perfectly correct, this needs to be 60 up here.
  22. This is still a very simplistic time class, it just assumes, say local time for instance.
  23. We don't care about time zones or calendars or daylight saving times or anything at this point.
  24. The real time class is way more complicated than this one.
  25. Now, let's see whether these assertions help us in avoiding time bombs.
  26. We still have the string passed in here.
  27. Let's see whether our assertion catches this. Again, press and run.
  28. And see, as we put in 2 minutes after midnight as a string, the assertion fails
  29. because the string is not between the values of 0 and 23.
  30. What happens if it pass negative values in here say hours is valid, minutes is negative,
  31. seconds is valid, press on run, and again we get a failing assertion--
  32. now, we get the failing assertion that the minutes is not within the right range,
  33. which is exactly what we wanted to catch.
  34. With these assertions up here, any attempt to set any time object
  35. to an illegal state will immediately be detected.